Skip to content

Commit

Permalink
Expand, improve, and prune tests
Browse files Browse the repository at this point in the history
Refactored some tests for more accurate testing.  Added some tests.
Removed some unnecessary code in tests.
  • Loading branch information
mybrainishuge committed Aug 25, 2016
1 parent 2a551e8 commit 3a85a24
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 76 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
"extends": "airbnb",
"installedESLint": true
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
13 changes: 1 addition & 12 deletions SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,14 @@
<script src="lib/chai.js"></script>
<script src="lib/mocha.js"></script>
<script src="lib/sinon.js"></script>
<script src="lib/sinon-chai.js"></script>
<!-- <script src="lib/sinon-chai.js"></script> -->
<script src="lib/testSupport.js"></script>

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

<!-- Test files -->
<script>
var clock;

before(function() {
clock = sinon.useFakeTimers();
});

after(function() {
clock.restore();
});
</script>
<script src="spec/part1.js"></script>
<script src="spec/part2.js"></script>
<!-- End test files -->
Expand Down
18 changes: 0 additions & 18 deletions lib/testSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,16 @@
// Disabling native methods is dangerous, we should spy on them instead
before(function() {
sinon.spy(Array.prototype,'map');
sinon.spy(Array.prototype,'indexOf');
sinon.spy(Array.prototype,'forEach');
sinon.spy(Array.prototype,'filter');
sinon.spy(Array.prototype,'reduce');
sinon.spy(Array.prototype,'every');
sinon.spy(Array.prototype,'some');
sinon.spy(Array.prototype,'sort');
});

afterEach(function() {
Array.prototype.map.reset();
Array.prototype.indexOf.reset();
Array.prototype.forEach.reset();
Array.prototype.filter.reset();
Array.prototype.reduce.reset();
Array.prototype.every.reset();
Array.prototype.some.reset();
Array.prototype.sort.reset();
});

after(function() {
Array.prototype.map.restore();
Array.prototype.indexOf.restore();
Array.prototype.forEach.restore();
Array.prototype.filter.restore();
Array.prototype.reduce.restore();
Array.prototype.every.restore();
Array.prototype.some.restore();
Array.prototype.sort.restore();
});

Expand Down
79 changes: 56 additions & 23 deletions spec/part1.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* jshint esversion: 6 */

(function() {
'use strict';

Expand Down Expand Up @@ -256,6 +258,16 @@


describe('7. Compute Exponent', function() {
var originalExponent = exponent;
exponent = sinon.spy(exponent);

afterEach(function() {
exponent.reset();
});

after(function() {
exponent = originalExponent;
});

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

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

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

it('should use recursion by calling self', function () {
var originalExponent = exponent;
exponent = sinon.spy(exponent);
exponent(3,4);
expect(exponent.callCount).to.be.above(1);
exponent = originalExponent;
});

// remove the 'x' to enable test
xit('optimize for even numbers', function() {
exponent.reset();
exponent(3,4);
expect(exponent.callCount).to.equal(4);

exponent.reset();
exponent(12,5);
expect(exponent.callCount).to.equal(5);

exponent.reset();
exponent(19,7);
expect(exponent.callCount).to.equal(6);
});

// remove the 'x' to enable test
xit('should accept negative integer for base', function() {
expect(exponent(-3,4)).to.equal(-81);
expect(exponent(-12,5)).to.equal(-248832);
expect(exponent(-7,2)).to.equal(-49);
expect(exponent(-7,4)).to.equal(-2401);
});

});
Expand Down Expand Up @@ -690,21 +716,28 @@


describe('20. Recursive Map', function() {
var timesTwo, input;

var timesTwo = function(n) { return n * 2; };
var input3 = [1,2,3,4,5];
beforeEach(function() {
timesTwo = function(n) { return n * 2; };
input = [1,2,3,4,5];
});
// var timesTwo = function(n) { return n * 2; };
// var input3 = [1,2,3,4,5];

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

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

it('should return new array without mutating the input array', function() {
var input = [1,2,3,4,5];
var result = rMap(input, function(num) { /* poop */ });
// var input = [1,2,3,4,5];
var result = rMap(input, num => num);
expect(input).to.eql([1,2,3,4,5]);
expect(result).to.not.equal(input);
});
Expand Down Expand Up @@ -1170,12 +1203,12 @@

});

function checkForNativeMethods(runFunction) {
it('should not use the native version of map', function() {
// These spies are set up in testSupport.js
runFunction();
expect(Array.prototype.map.called).to.equal(false);
});
}
// function checkForNativeMethods(runFunction) {
// it('should not use the native version of map', function() {
// // These spies are set up in testSupport.js
// runFunction();
// expect(Array.prototype.map.called).to.equal(false);
// });
// }

}());
44 changes: 23 additions & 21 deletions spec/part2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* jshint esversion: 6 */

(function() {
'use strict';

Expand Down Expand Up @@ -88,7 +90,7 @@
it('should use recursion by calling self', function () {
var originalBinarySearch = binarySearch;
binarySearch = sinon.spy(binarySearch);
binarySearch(primes,19);
binarySearch(primes, 19);
expect(binarySearch.callCount).to.be.above(1);
binarySearch = originalBinarySearch;
});
Expand All @@ -98,7 +100,11 @@


describe('38. Merge Sort', function() {
var numbers = [8,2,20,1,15];
var numbers;

beforeEach(function() {
numbers = [8,2,20,1,15];
});

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

it('should sort an array of numbers in order of least to greatest', function() {
var sortedNumbers = mergeSort(numbers);
expect(sortedNumbers).to.eql([1,2,8,15,20]);
expect(mergeSort([])).to.eql([]);
expect(mergeSort([0])).to.eql([0]);
expect(mergeSort([1,0])).to.eql([0,1]);
expect(mergeSort([0,1,2,3])).to.eql([0,1,2,3]);
expect(mergeSort([5,4,3,2,1])).to.eql([1,2,3,4,5]);
expect(mergeSort([10,1,8,5,0])).to.eql([0,1,5,8,10]);
expect(mergeSort([8,2,20,1,15])).to.eql([1,2,8,15,20]);
});

it('should be able to handle negative numbers', function() {
var numbers = [8,-2,20,1,-15];
var sortedNumbers = mergeSort(numbers);
expect(sortedNumbers).to.eql([-15,-2,1,8,20]);
expect(mergeSort([-1])).to.eql([-1]);
expect(mergeSort([0,-1])).to.eql([-1,0]);
expect(mergeSort([0,1,-2,-3])).to.eql([-3,-2,0,1]);
expect(mergeSort([8,-2,20,1,-15])).to.eql([-15,-2,1,8,20]);
expect(mergeSort([0,-1,-2,-3,-4,-5,-10])).to.eql([-10,-5,-4,-3,-2,-1,0]);
});

it("should not use the native Array sort method", function() {
expect(mergeSort.toString()).to.not.contain('sort');
// Spying on Array.prototype.sort in testSupport.js
mergeSort(numbers);
expect(Array.prototype.sort.called).to.equal(false);
});

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

});

// function checkForNativeMethods(runUnderbarFunction) {
// it('should not use the native version of sort', function() {
// // These spies are set up in testSupport.js
// runUnderbarFunction();
// expect(Array.prototype.indexOf.called).to.equal(false);
// expect(Array.prototype.filter.called).to.equal(false);
// expect(Array.prototype.sort.called).to.equal(false);
// });
// }
// checkForNativeMethods(function() {
// mergeSort(numbers);
// });

}());
9 changes: 7 additions & 2 deletions src/recursion.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* jshint esversion: 6 */

// Solve all of the following prompts using recursion.

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

// 16. Write a function that accepts a string and creates an array where each letter
// occupies an index of the array.
var createArray = function(str){
var createArray = function(str) {
};

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

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


// *** EXTRA CREDIT ***

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

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

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

0 comments on commit 3a85a24

Please sign in to comment.