From c04063837ce7f17e57f5744769e4300587502968 Mon Sep 17 00:00:00 2001 From: Evan King Date: Sun, 19 Jun 2016 16:26:57 -0300 Subject: [PATCH] Fix new bug in dist build script, improve test coverage and accuracy Note: immutability tests failing on Firefox and IE 11. --- build.js | 2 +- package.json | 2 +- test/index.js | 105 ++++++++++++++++++++++++-------------------------- 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/build.js b/build.js index 3bc6e1f..0a992c0 100644 --- a/build.js +++ b/build.js @@ -55,7 +55,7 @@ browserify() 'chai': 'chai', '../index': 'PrimitiveEnum', })) - .require('./test/test.js', {entry: true}) + .require('./test/index.js', {entry: true}) .bundle() .on("error", err => console.log("Error: " + err.message)) .pipe(fs.createWriteStream('dist/test.js')); diff --git a/package.json b/package.json index 24c503d..7c69247 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "posttravis": "istanbul check-coverage --statements $npm_package_config_min_coverage --branches $npm_package_config_min_coverage --lines $npm_package_config_min_coverage" }, "config": { - "min_coverage": 60 + "min_coverage": 80 }, "repository": { "type": "git", diff --git a/test/index.js b/test/index.js index ebd8e3c..16a19b2 100644 --- a/test/index.js +++ b/test/index.js @@ -6,7 +6,7 @@ const describe('PrimitiveEnumBuilder', function() { - it('accepts flexible configuration', function(done) { + it('accepts flexible configuration', function() { const e1 = Enum(['a', 'c', 'b'], {transform: Enum.bitwise, defaultKey: 'b'}), e2 = Enum(['a', 'c', 'b'], {transform: Enum.bitwise}), @@ -23,62 +23,67 @@ describe('PrimitiveEnumBuilder', function() { expect(e1.defaultKey).eql(e5.defaultKey); expect(e2.defaultKey).eql(e3.defaultKey); expect(e1.defaultKey).not.eql(e2.defaultKey); + }); + + it('rejects invalid configuration', function() { + const + eArr = ['a', 'b'], + eObj = {a: 1, b: 2}, + invalidTransform = {transform: 'blah'}; + + expect(Enum.bind(null, eArr, true)).throw(); + expect(Enum.bind(null, eArr, false)).throw(); + expect(Enum.bind(null, eArr, null)).throw(); + expect(Enum.bind(null, eArr, invalidTransform)).throw(); + expect(Enum.bind(null, eObj, invalidTransform)).throw(); - done(); + expect(Enum.bind(null, eObj, 'c')).throw(); + expect(Enum.bind(null, eObj, {defaultKey: 'c'})).throw(); }); - it('rejects duplicate keys or values', function(done) { + it('rejects duplicate keys or values', function() { expect(() => Enum(['a', 'a'])).throw(); expect(() => Enum({a: 1, b: 1})).throw(); - done(); }); - it('rejects maps with lookup conflict', function(done) { + it('rejects maps with lookup conflict', function() { expect(Enum.bind(null, {a: 'x', b: 'x'})).throw(); expect(Enum.bind(null, {a: 'b', b: 'c'})).throw(); expect(Enum.bind(null, ['a', 'a', 'b'])).throw(); - done(); }); - it('rejects enum values duplicating unrelated keys', function(done) { + it('rejects enum values duplicating unrelated keys', function() { expect(() => Enum({a: 'b', b: 'c'})).throw(); expect(() => Enum({a: 'a', b: 'b'})).not.throw(); - done(); }); - it('works with built-in array transforms', function(done) { + it('works with built-in array transforms', function() { expect(Enum(['a', 'b', 'c', 'd', 'e', 'f'], Enum.bitwise).values) .eql([1, 2, 4, 8, 16, 32]); expect(Enum(['a', 'b', 'c', 'd', 'e', 'f'], Enum.sequential).values) .eql([1, 2, 3, 4, 5, 6]); - - done(); }); - it('works with built-in object transforms', function(done) { + it('works with built-in object transforms', function() { expect(Enum(['FIRST_KEY', 'SECOND_KEY'], Enum.idString).values) .eql(['first-key', 'second-key']); - - done(); }); - it('supports additional transforms', function(done) { + it('supports additional transforms', function() { const transEnum = Enum(['hello', 'world'], key => key.split('').reverse().join('')); expect(transEnum.values).eql(['olleh', 'dlrow']); const errEnum = Enum(['error', 'really error'], (key, idx) => 10000 + idx); expect(errEnum.values).eql([10000, 10001]); - done(); }); - it('configures default transforms', function(done) { + it('configures default transforms', function() { Enum.defaultObjectTransform = (propvalue, propname) => propname.toUpperCase(); expect(Enum({a: 1, b: 2}).map).eql({a: 'A', b: 'B'}); Enum.defaultArrayTransform = (value, idx) => value.toUpperCase(); expect(Enum(['a', 'b']).map).eql({a: 'A', b: 'B'}); - done(); }); }); @@ -93,54 +98,52 @@ describe('PrimitiveEnum instances', function() { bitEnum = Enum(seqEnum.keys, Enum.bitwise), mapEnum = Enum({a: 'x', c: 'z', b: 'y'}); - they('are identifiable as enums', function(done) { + they('are identifiable as enums', function() { expect(mapEnum.name).eql('PrimitiveEnum'); expect(mapEnum instanceof Enum).true; - done(); }); - they('are immutable', function(done) { - expect(() => mapEnum.c.d = 'q').throw; - expect(() => mapEnum.key.c = 'q').throw; - expect(() => mapEnum.value.c = 'q').throw; - expect(() => mapEnum.defaultKey = 'q').throw; - expect(() => mapEnum.defaultValue = 'q').throw; + they('are immutable', function() { + expect(function() { mapEnum.c.d = 'q'; }).throw(); + expect(function() { mapEnum.key = 'q'; }).throw(); + expect(function() { mapEnum.value = 'q'; }).throw(); + expect(function() { mapEnum.defaultKey = 'q'; }).throw(); + expect(function() { mapEnum.defaultValue = 'q'; }).throw(); - expect(() => mapEnum.c = 'q').throw; - expect(() => mapEnum.map.c = 'q').throw; - expect(() => mapEnum.keys.c = 'q').throw; - expect(() => mapEnum.values.c = 'q').throw; - expect(() => mapEnum.reverseMap.c = 'q').throw; + expect(function() { mapEnum.c = 'q'; }).throw(); + expect(function() { mapEnum.map.c = 'q'; }).throw(); + expect(function() { mapEnum.keys.c = 'q'; }).throw(); + expect(function() { mapEnum.values.c = 'q'; }).throw(); + expect(function() { mapEnum.reverseMap.c = 'q'; }).throw(); - expect(() => mapEnum.d = 'q').throw; - expect(() => mapEnum.map.d = 'q').throw; - expect(() => mapEnum.keys.d = 'q').throw; - expect(() => mapEnum.values.d = 'q').throw; - expect(() => mapEnum.reverseMap.d = 'q').throw; - - done(); + expect(function() { mapEnum.d = 'q'; }).throw(); + expect(function() { mapEnum.map.d = 'q'; }).throw(); + expect(function() { mapEnum.keys.d = 'q'; }).throw(); + expect(function() { mapEnum.values.d = 'q'; }).throw(); + expect(function() { mapEnum.reverseMap.d = 'q'; }).throw(); }); - they('are serializable', function(done) { + they('are serializable', function() { const json = JSON.stringify(mapEnum), copy = Enum.fromJSON(json); expect(copy.map).eql(mapEnum.map); expect(copy.defaultValue).eql(mapEnum.defaultValue); - done(); + expect(Enum.fromJSON.bind(null, "{notAnEnum: true}")).throw(); + expect(Enum.fromJSON.bind(null, {notAnEnum: true})).throw(); + expect(Enum.fromJSON.bind(null, "not even json")).throw(); }); - they('are string-comparable', function(done) { + they('are string-comparable', function() { expect(''+mapEnum == Enum({a: 'x', c: 'z', b: 'y'})).true; expect(''+mapEnum == Enum({a: 'x', c: 'z', b: 'aa'})).false; expect(''+mapEnum == Enum({a: 'x', aa: 'z', b: 'y'})).false; expect(''+mapEnum == Enum({a: 'x', c: 'z', b: 'y'}, 'b')).false; expect(''+bitEnum == seqEnum).false; - done(); }); - they('enumerate and iterate over keys only', function(done) { + they('enumerate and iterate over keys only', function() { expect(Object.keys(mapEnum)).eql(mapEnum.keys); const keys = []; for(var key in mapEnum) { @@ -148,16 +151,14 @@ describe('PrimitiveEnum instances', function() { } expect(keys).eql(mapEnum.keys); expect(mapEnum.count).eql(keys.length); - done(); }); - they('provide keys and values, in original order', function(done) { + they('provide keys and values, in original order', function() { expect(mapEnum.keys).eql(['a', 'c', 'b']); expect(mapEnum.values).eql(['x', 'z', 'y']); - done(); }); - they('perform lookups and reverse lookups', function(done) { + they('perform lookups and reverse lookups', function() { expect(seqEnum(4)).eql('d'); expect(seqEnum('e')).eql(5); expect(bitEnum(4)).eql('c'); @@ -172,16 +173,14 @@ describe('PrimitiveEnum instances', function() { expect(mapEnum.value('a')).eql('x'); expect(seqEnum['4']).eql('d'); expect(seqEnum.d).eql(4); - done(); }); - they('build from arrays using sequential or bitwise integer values', function(done) { + they('build from arrays using sequential or bitwise integer values', function() { expect(bitEnum.values).eql([1, 2, 4, 8, 16, 32]); expect(seqEnum.values).eql([1, 2, 3, 4, 5, 6]); - done(); }); - they('have default keys and values', function(done) { + they('have default keys and values', function() { expect(mapEnum.defaultKey).eql(mapEnum.keys[0]); expect(mapEnum.defaultValue).eql(mapEnum.values[0]); @@ -189,9 +188,7 @@ describe('PrimitiveEnum instances', function() { expect(dmapEnum.defaultKey).eql(dmapEnum.keys[1]); expect(dmapEnum.defaultValue).eql(dmapEnum.values[1]); - expect(Enum.bind(null, mapEnum.map, 'q')).throw; - - done(); + expect(Enum.bind(null, mapEnum.map, 'q')).throw(); }); });