Skip to content

Commit

Permalink
fix: make it possible to count recursion calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhama committed Feb 8, 2020
1 parent 3b6a8ef commit 04176c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/recursive-depth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = function calculateDepth(arr) {
// write your code here
};
module.exports = class MyClass {
calculateDepth(arr) {
// magic
}
}
12 changes: 7 additions & 5 deletions test/recursive-depth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ const sinon = require('sinon');

Object.freeze(assert);

const calculateDepth = require('../src/recursive-depth.js');
const MyClass = require('../src/recursive-depth.js');
const instance = new MyClass();
const calculateDepth = instance.calculateDepth.bind(instance);

const createFlatArr = (length) => Array.from({length}, () => Math.floor(Math.random() * length));


describe('Recursive depth', () => {
//Presence requirement
describe('variable presence', () => {
it('function calculateDepth exists', () => {
expect(calculateDepth).to.exist;
expect(instance.calculateDepth).to.exist;
});
});

Expand All @@ -36,9 +37,10 @@ describe('Recursive depth', () => {
assert.equal(calculateDepth([1, [8, [[]]], [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]], []]]], []]]]]]]]], []]]], []]]]]]]]]], 2, 3, [8, [[[[[[[[[[[[[[]]]]]]]]]]]]]]], [8, [[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]], 4, 5, ['6575',['adas', ['dfg', [0]]]]]), 31);
});
it('works recursively?', () => {
const spy1 = sinon.spy(calculateDepth);
assert.equal(spy1([1, 2, 3, 4, 5, [1, []]]), 3);
const spy1 = sinon.spy(instance, 'calculateDepth');
assert.equal(calculateDepth([1, 2, 3, 4, 5, [1, []]]), 3);
expect(spy1.callCount).to.be.greaterThan(1);
spy1.restore();
});
});
});

0 comments on commit 04176c3

Please sign in to comment.