Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new bug in dist build script, improve test coverage and accuracy #2

Merged
merged 1 commit into from
Jun 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
105 changes: 51 additions & 54 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}),
Expand All @@ -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();
});

});
Expand All @@ -93,71 +98,67 @@ 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) {
keys.push(key);
}
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');
Expand All @@ -172,26 +173,22 @@ 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]);

const dmapEnum = Enum(mapEnum.map, mapEnum.keys[1]);
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();
});

});
Expand Down