Skip to content

Commit 3a85a24

Browse files
committed
Expand, improve, and prune tests
Refactored some tests for more accurate testing. Added some tests. Removed some unnecessary code in tests.
1 parent 2a551e8 commit 3a85a24

File tree

7 files changed

+92
-76
lines changed

7 files changed

+92
-76
lines changed

.eslintrc.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
"extends": "airbnb",
3+
"installedESLint": true
4+
};

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

SpecRunner.html

+1-12
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,14 @@
88
<script src="lib/chai.js"></script>
99
<script src="lib/mocha.js"></script>
1010
<script src="lib/sinon.js"></script>
11-
<script src="lib/sinon-chai.js"></script>
11+
<!-- <script src="lib/sinon-chai.js"></script> -->
1212
<script src="lib/testSupport.js"></script>
1313

1414
<!-- Source files -->
1515
<script src="src/recursion.js"></script>
1616
<!-- End source files -->
1717

1818
<!-- Test files -->
19-
<script>
20-
var clock;
21-
22-
before(function() {
23-
clock = sinon.useFakeTimers();
24-
});
25-
26-
after(function() {
27-
clock.restore();
28-
});
29-
</script>
3019
<script src="spec/part1.js"></script>
3120
<script src="spec/part2.js"></script>
3221
<!-- End test files -->

lib/testSupport.js

-18
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,16 @@
1111
// Disabling native methods is dangerous, we should spy on them instead
1212
before(function() {
1313
sinon.spy(Array.prototype,'map');
14-
sinon.spy(Array.prototype,'indexOf');
15-
sinon.spy(Array.prototype,'forEach');
16-
sinon.spy(Array.prototype,'filter');
17-
sinon.spy(Array.prototype,'reduce');
18-
sinon.spy(Array.prototype,'every');
19-
sinon.spy(Array.prototype,'some');
2014
sinon.spy(Array.prototype,'sort');
2115
});
2216

2317
afterEach(function() {
2418
Array.prototype.map.reset();
25-
Array.prototype.indexOf.reset();
26-
Array.prototype.forEach.reset();
27-
Array.prototype.filter.reset();
28-
Array.prototype.reduce.reset();
29-
Array.prototype.every.reset();
30-
Array.prototype.some.reset();
3119
Array.prototype.sort.reset();
3220
});
3321

3422
after(function() {
3523
Array.prototype.map.restore();
36-
Array.prototype.indexOf.restore();
37-
Array.prototype.forEach.restore();
38-
Array.prototype.filter.restore();
39-
Array.prototype.reduce.restore();
40-
Array.prototype.every.restore();
41-
Array.prototype.some.restore();
4224
Array.prototype.sort.restore();
4325
});
4426

spec/part1.js

+56-23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* jshint esversion: 6 */
2+
13
(function() {
24
'use strict';
35

@@ -256,6 +258,16 @@
256258

257259

258260
describe('7. Compute Exponent', function() {
261+
var originalExponent = exponent;
262+
exponent = sinon.spy(exponent);
263+
264+
afterEach(function() {
265+
exponent.reset();
266+
});
267+
268+
after(function() {
269+
exponent = originalExponent;
270+
});
259271

260272
it('should return a number', function() {
261273
expect(typeof(exponent(4,3))).to.eql('number');
@@ -281,12 +293,6 @@
281293
expect(exponent(2300,1)).to.equal(2300);
282294
});
283295

284-
// it('BONUS: should accept negative integer for base', function() {
285-
// expect(exponent(-3,4)).to.equal(-81);
286-
// expect(exponent(-12,5)).to.equal(-248832);
287-
// expect(exponent(-7,2)).to.equal(-49);
288-
// expect(exponent(-7,4)).to.equal(-2401);
289-
// });
290296

291297
it('should accept negative integer for exponent', function() {
292298
expect(exponent(4,-2)).to.equal(0.0625);
@@ -295,11 +301,31 @@
295301
});
296302

297303
it('should use recursion by calling self', function () {
298-
var originalExponent = exponent;
299-
exponent = sinon.spy(exponent);
300304
exponent(3,4);
301305
expect(exponent.callCount).to.be.above(1);
302-
exponent = originalExponent;
306+
});
307+
308+
// remove the 'x' to enable test
309+
xit('optimize for even numbers', function() {
310+
exponent.reset();
311+
exponent(3,4);
312+
expect(exponent.callCount).to.equal(4);
313+
314+
exponent.reset();
315+
exponent(12,5);
316+
expect(exponent.callCount).to.equal(5);
317+
318+
exponent.reset();
319+
exponent(19,7);
320+
expect(exponent.callCount).to.equal(6);
321+
});
322+
323+
// remove the 'x' to enable test
324+
xit('should accept negative integer for base', function() {
325+
expect(exponent(-3,4)).to.equal(-81);
326+
expect(exponent(-12,5)).to.equal(-248832);
327+
expect(exponent(-7,2)).to.equal(-49);
328+
expect(exponent(-7,4)).to.equal(-2401);
303329
});
304330

305331
});
@@ -690,21 +716,28 @@
690716

691717

692718
describe('20. Recursive Map', function() {
719+
var timesTwo, input;
693720

694-
var timesTwo = function(n) { return n * 2; };
695-
var input3 = [1,2,3,4,5];
721+
beforeEach(function() {
722+
timesTwo = function(n) { return n * 2; };
723+
input = [1,2,3,4,5];
724+
});
725+
// var timesTwo = function(n) { return n * 2; };
726+
// var input3 = [1,2,3,4,5];
696727

697728
it('should return an array', function() {
698-
expect(Array.isArray(rMap([1,2,3], timesTwo))).to.equal(true);
729+
expect(Array.isArray(rMap(input, timesTwo))).to.equal(true);
699730
});
700731

701-
checkForNativeMethods(function() {
702-
rMap([1,2,3,4], timesTwo);
732+
it('should not use the native version of map', function() {
733+
// Spying on Array.prototype.map in testSupport.js
734+
rMap(input, timesTwo);
735+
expect(Array.prototype.map.called).to.equal(false);
703736
});
704737

705738
it('should return new array without mutating the input array', function() {
706-
var input = [1,2,3,4,5];
707-
var result = rMap(input, function(num) { /* poop */ });
739+
// var input = [1,2,3,4,5];
740+
var result = rMap(input, num => num);
708741
expect(input).to.eql([1,2,3,4,5]);
709742
expect(result).to.not.equal(input);
710743
});
@@ -1170,12 +1203,12 @@
11701203

11711204
});
11721205

1173-
function checkForNativeMethods(runFunction) {
1174-
it('should not use the native version of map', function() {
1175-
// These spies are set up in testSupport.js
1176-
runFunction();
1177-
expect(Array.prototype.map.called).to.equal(false);
1178-
});
1179-
}
1206+
// function checkForNativeMethods(runFunction) {
1207+
// it('should not use the native version of map', function() {
1208+
// // These spies are set up in testSupport.js
1209+
// runFunction();
1210+
// expect(Array.prototype.map.called).to.equal(false);
1211+
// });
1212+
// }
11801213

11811214
}());

spec/part2.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* jshint esversion: 6 */
2+
13
(function() {
24
'use strict';
35

@@ -88,7 +90,7 @@
8890
it('should use recursion by calling self', function () {
8991
var originalBinarySearch = binarySearch;
9092
binarySearch = sinon.spy(binarySearch);
91-
binarySearch(primes,19);
93+
binarySearch(primes, 19);
9294
expect(binarySearch.callCount).to.be.above(1);
9395
binarySearch = originalBinarySearch;
9496
});
@@ -98,7 +100,11 @@
98100

99101

100102
describe('38. Merge Sort', function() {
101-
var numbers = [8,2,20,1,15];
103+
var numbers;
104+
105+
beforeEach(function() {
106+
numbers = [8,2,20,1,15];
107+
});
102108

103109
it('should return an array', function() {
104110
var sortedNumbers = mergeSort(numbers);
@@ -111,18 +117,27 @@
111117
});
112118

113119
it('should sort an array of numbers in order of least to greatest', function() {
114-
var sortedNumbers = mergeSort(numbers);
115-
expect(sortedNumbers).to.eql([1,2,8,15,20]);
120+
expect(mergeSort([])).to.eql([]);
121+
expect(mergeSort([0])).to.eql([0]);
122+
expect(mergeSort([1,0])).to.eql([0,1]);
123+
expect(mergeSort([0,1,2,3])).to.eql([0,1,2,3]);
124+
expect(mergeSort([5,4,3,2,1])).to.eql([1,2,3,4,5]);
125+
expect(mergeSort([10,1,8,5,0])).to.eql([0,1,5,8,10]);
126+
expect(mergeSort([8,2,20,1,15])).to.eql([1,2,8,15,20]);
116127
});
117128

118129
it('should be able to handle negative numbers', function() {
119-
var numbers = [8,-2,20,1,-15];
120-
var sortedNumbers = mergeSort(numbers);
121-
expect(sortedNumbers).to.eql([-15,-2,1,8,20]);
130+
expect(mergeSort([-1])).to.eql([-1]);
131+
expect(mergeSort([0,-1])).to.eql([-1,0]);
132+
expect(mergeSort([0,1,-2,-3])).to.eql([-3,-2,0,1]);
133+
expect(mergeSort([8,-2,20,1,-15])).to.eql([-15,-2,1,8,20]);
134+
expect(mergeSort([0,-1,-2,-3,-4,-5,-10])).to.eql([-10,-5,-4,-3,-2,-1,0]);
122135
});
123136

124137
it("should not use the native Array sort method", function() {
125-
expect(mergeSort.toString()).to.not.contain('sort');
138+
// Spying on Array.prototype.sort in testSupport.js
139+
mergeSort(numbers);
140+
expect(Array.prototype.sort.called).to.equal(false);
126141
});
127142

128143
it('should use recursion by calling self', function () {
@@ -137,17 +152,4 @@
137152

138153
});
139154

140-
// function checkForNativeMethods(runUnderbarFunction) {
141-
// it('should not use the native version of sort', function() {
142-
// // These spies are set up in testSupport.js
143-
// runUnderbarFunction();
144-
// expect(Array.prototype.indexOf.called).to.equal(false);
145-
// expect(Array.prototype.filter.called).to.equal(false);
146-
// expect(Array.prototype.sort.called).to.equal(false);
147-
// });
148-
// }
149-
// checkForNativeMethods(function() {
150-
// mergeSort(numbers);
151-
// });
152-
153155
}());

src/recursion.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* jshint esversion: 6 */
2+
13
// Solve all of the following prompts using recursion.
24

35
// 1. Calculate the factorial of a number. The factorial of a non-negative integer n,
@@ -91,7 +93,7 @@ var compareStr = function(str1, str2) {
9193

9294
// 16. Write a function that accepts a string and creates an array where each letter
9395
// occupies an index of the array.
94-
var createArray = function(str){
96+
var createArray = function(str) {
9597
};
9698

9799
// 17. Reverse the order of an array
@@ -153,7 +155,7 @@ var nthFibo = function(n) {
153155
// 26. Given an array of words, return a new array containing each word capitalized.
154156
// var words = ['i', 'am', 'learning', 'recursion'];
155157
// capitalizedWords(words); // ['I', 'AM', 'LEARNING', 'RECURSION']
156-
var capitalizeWords = function(input) {
158+
var capitalizeWords = function(array) {
157159
};
158160

159161
// 27. Given an array of strings, capitalize the first letter of each index.
@@ -216,6 +218,7 @@ var alternateSign = function(array) {
216218
var numToText = function(str) {
217219
};
218220

221+
219222
// *** EXTRA CREDIT ***
220223

221224
// 36. Return the number of times a tag occurs in the DOM.
@@ -225,12 +228,14 @@ var tagCount = function(tag, node) {
225228
// 37. Write a function for binary search.
226229
// Sample array: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
227230
// console.log(binarySearch(5)) will return '5'
231+
// https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-search
228232

229233
var binarySearch = function(array, target, min, max) {
230234
};
231235

232236
// 38. Write a merge sort function.
233237
// Sample array: [34,7,23,32,5,62]
234238
// Sample output: [5,7,23,32,34,62]
239+
// https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/divide-and-conquer-algorithms
235240
var mergeSort = function(array) {
236241
};

0 commit comments

Comments
 (0)