diff --git a/README.md b/README.md index 5e67af7..1c4133e 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,3 @@ -# Test Framework - -An empty project with Tape to demonstrate proper unit test writing. Part of a recent Intro to -JavaScript class I taught. - -## Running - -Follow the usual procedure for setting up NPM: - -```sh -$ npm install -```` - -And then run the unit tests via NPM scripts: - -```sh -$ npm test -# or npm run test -``` - -## Files - -Any file ending in `spec.js` anywhere within the source folder will be included in the running -tests. For an example, check out `src/spec.js`, which tests `src/index.js`. +# Hello +Questions and thoughs are in the comments. \ No newline at end of file diff --git a/src/index.js b/src/index.js index 5237ff4..ef07b46 100644 --- a/src/index.js +++ b/src/index.js @@ -1,26 +1,28 @@ -export function double ( x ) { - return x * 2; -} - -export function doubleXTimes ( x, num ) { - let result = x; - for ( let i = 1; i <= num; i++ ) { - // result = result * 2; - result = double( result ); - } - - return result; +const Animal = function() {}; +Animal.prototype = function() {}; +Animal.prototype.speak = function() { + return 'generic sound'; } -export function doubleEach ( arr ) { - let result = arr.map( double ); +const Reptile = function() {}; +Reptile.prototype = Object.create(Animal.prototype); +Reptile.SOUND = 'Slither slither'; +Reptile.prototype.speak = function(sound = Reptile.SOUND) { + return Reptile.SOUND; +}; - // const result = arr.map( x => double( x ) ); - // const result = arr.map( function ( x ) { - // return double( x ); - // }); +const Primate = function() {}; +Primate.prototype = Object.create(Animal.prototype); +Primate.SOUND = 'Ug ug'; +Primate.prototype.speak = function(sound = Primate.SOUND) { + return Primate.SOUND; +}; - return result; -} +const Human = function() {}; +Human.prototype = Object.create(Primate.prototype); +Human.prototype.speak = function(sound) { + return sound; +}; +export { Animal, Reptile, Primate, Human }; diff --git a/src/spec.js b/src/spec.js index 3e22930..eef9f81 100644 --- a/src/spec.js +++ b/src/spec.js @@ -1,30 +1,76 @@ import test from 'tape'; -import { double, doubleXTimes, doubleEach } from './index'; +import { Animal, Reptile, Primate, Human } from './index'; -test( 'double fn', function ( test ) { - const actual = double( 5 ); - const expected = 10; - test.equal( actual, expected, 'should double the value' ); + +test('Create a constructor function and a prototype for an Animal', function(test) { + + const animal = new Animal(); + + test.ok( animal instanceof Animal, 'should create an instance of Animal' ); test.end(); }); -test( 'doubleXTimes', function ( test ) { - const actual = doubleXTimes( 5, 3 ); - const expected = 40; - test.equal( actual, expected, 'should double 5 three times' ); + +test('Create a function on Animal ‘s prototype called speak that returns the animal’s sound.', function(test) { + + const animal = new Animal(); + const expected = 'generic sound'; + const actual = animal.speak(); + + test.equal( actual, expected, 'should make a generic sound when it speaks' ); test.end(); }); -test( 'doubleEach', function ( test ) { - const actual = doubleEach([ 0, 1, 2 ]); - const expected = [ 0, 2, 4 ]; - test.deepEqual( actual, expected, 'should double each in the array' ); +test('Create two new constructors with prototypes that inherit from Animal: Reptile and Primate. Overwrite the speak method for each.', function(test) { + + let actual, expected; + + const reptile = new Reptile(); + + test.ok( reptile instanceof Reptile, 'should be an instance of Reptile' ); + test.ok( reptile instanceof Animal, 'should be an instance of Animal' ); + + expected = Reptile.SOUND; + + actual = reptile.speak(); + console.log(`* Reptile says, "${actual}"`); + test.equal( actual, expected, 'should make a reptile sound when it speaks' ); + + const primate = new Primate(); + + test.ok( primate instanceof Primate, 'should be an instance of Primate' ); + test.ok( primate instanceof Animal, 'should be an instance of Animal' ); + + expected = Primate.SOUND; + actual = primate.speak(); + console.log(`* Primate, says, "${actual}"`); + test.equal( actual, expected, 'should make a primate sound when it speaks' ); test.end(); }); + + +test('Create one more that inherits from Primate called Human.', function(test) { + + let actual, expected; + + const human = new Human(); + + test.ok( human instanceof Human, 'should be an instance of Human' ); + test.ok( human instanceof Primate, 'should be an instance of Primate' ); + test.ok( human instanceof Animal, 'should be an instance of Animal' ); + + const message = 'Hello'; + expected = message; + actual = human.speak( message ); + console.log(`* Human says, "${actual}"`); + test.equal( actual, expected, 'should speak what it is asked to speak' ); + + test.end(); +});