From c0dc7d1ed2cdb15117da2dcd6fb2606a77cddd4c Mon Sep 17 00:00:00 2001 From: Jason O'Gray Date: Wed, 10 Jul 2019 13:09:45 -0400 Subject: [PATCH] Can be used on coderpad --- generic-javascript/tests.js | 94 +++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 generic-javascript/tests.js diff --git a/generic-javascript/tests.js b/generic-javascript/tests.js new file mode 100644 index 0000000..c28a0ab --- /dev/null +++ b/generic-javascript/tests.js @@ -0,0 +1,94 @@ +const _ = require('lodash'); +const Mocha = require('mocha'); +const assert = require('assert'); +const mocha = new Mocha(); + +// Bit of a hack, sorry! +mocha.suite.emit('pre-require', this, 'solution', mocha); + +// your code can go here + + +// everything below are tests +const fruit = [ + { + word: 'apple', + count: 5 + }, + { + word: 'banana', + count: 2 + }, + { + word: 'orange', + count: 1 + }, + { + word: 'cherry', + count: 9 + }, + { + word: 'plum', + count: 3 + } +]; + + +const sortedFruit = [ + { + word: 'orange', + count: 1 + }, + { + word: 'banana', + count: 2 + }, + { + word: 'plum', + count: 3 + }, + { + word: 'apple', + count: 5 + }, + { + word: 'cherry', + count: 9 + } +]; + + +describe('Test suite', () => { + it('should always work', () => { + assert(true) + }); + + it('should sort fruit', () => { + sorted = sortFruit(fruit); + assert.deepStrictEqual(sorted, sortedFruit); + }); + + it('greaterThenAverage', () => { + assert.deepStrictEqual([3], greaterThenAverage([1, 2, 3])); + assert.deepStrictEqual([8, 5, 11], greaterThenAverage([-1, 8, 0, 4, 5, 2, 11])); + }); + + it('should transpose', () => { + const transposed = transpose({'item1': 1, 'item2': 'two', 5: 'fiver'}); + assert.deepStrictEqual(transposed.fiver, 5); + assert.deepStrictEqual(transposed.two, 'item2'); + assert.deepStrictEqual(transposed['1'], 'item1'); + }); + + it('should detect a palindrome', () => { + const f = isPalindrome; + assert(f('Doc, note: I dissent. A fast never prevents a fatness. I diet on cod')); + assert(!f('I am not one for sure')) + assert(f('Able was I ere I saw Elba')) + assert(f('Never odd or even')) + assert(f('aabbccddeefeeddccbbaa')) + assert(!f('abbccddeeffeddccbbaa')) + }); +}); + +mocha.run();