-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enumeration.spec.js
96 lines (86 loc) · 3.4 KB
/
Enumeration.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* global describe, it */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Enumeration = require('../../../main')
const { ClassNotExtendableError } = require('@ballistagroup/error-support')
const EnumClassNotInstantiableError = require('../../../main').errors.EnumClassNotInstantiableError
describe('unit tests of Enumeration', function () {
it('should create a new enum', () => {
const Boolean = Enumeration.new({ name: 'BooleanValue', values: ['TRUE', 'FALSE'] })
const OneTwo = Enumeration.new({
name: 'OneTwo',
values: {
ONE: {
one () {
return 1
},
get eleven () {
return this.one() + 10
}
},
TWO: {
two () {
return 2
},
get twelve () {
return this.two() + 10
}
}
}
}, {
myName: function () {
return `my name is ${this.name}`
},
get myNameIsThisDotMyNameAsProp () {
return `${this.myName()} as prop`
},
get myNameIsThisDotNameAsProp () {
return `my name is ${this.name} as prop`
}
})
expect(Boolean.TRUE).to.be.ok()
expect(Boolean.FALSE).to.be.ok()
expect(Boolean.BOGUS).not.to.be.ok()
expect(Boolean.isInstance(Boolean.TRUE)).to.be.true()
expect(Boolean.isClass(Boolean)).to.be.true()
expect(OneTwo.isInstance(OneTwo.ONE)).to.be.true()
expect(OneTwo.isClass(OneTwo)).to.be.true()
expect(OneTwo.isInstance(Boolean.TRUE)).to.be.false()
expect(OneTwo.isClass(Boolean)).to.be.false()
expect(OneTwo.ONE.myName()).to.equal('my name is ONE')
expect(OneTwo.ONE.one()).to.equal(1)
expect(OneTwo.ONE.eleven).to.equal(11)
expect(OneTwo.ONE.two).not.to.be.ok()
expect(OneTwo.ONE.twelve).not.to.be.ok()
expect(OneTwo.TWO.myName()).to.equal('my name is TWO')
expect(OneTwo.TWO.two()).to.equal(2)
expect(OneTwo.TWO.twelve).to.equal(12)
expect(OneTwo.TWO.one).not.to.be.ok()
expect(OneTwo.TWO.eleven).not.to.be.ok()
expect(Boolean.of(Boolean.TRUE)).to.equal(Boolean.TRUE)
expect(Boolean.of('TRUE')).to.equal(Boolean.TRUE)
expect(Boolean.of(0)).to.equal(Boolean.TRUE)
expect(() => Boolean.of('BOGUS')).to.throw(Boolean.$ERROR$)
.that.has.property('code').that.equals('E_UNKNOWN_BOOLEAN_VALUE_ENUM_VALUE')
expect(OneTwo.ONE.myNameIsThisDotMyNameAsProp).to.equal('my name is ONE as prop')
expect(OneTwo.ONE.myNameIsThisDotNameAsProp).to.equal('my name is ONE as prop')
expect(OneTwo.TWO.myNameIsThisDotMyNameAsProp).to.equal('my name is TWO as prop')
expect(OneTwo.TWO.myNameIsThisDotNameAsProp).to.equal('my name is TWO as prop')
expect(Enumeration.isEnumerationClass(Boolean)).to.be.true()
expect(Enumeration.isEnumerationClass(OneTwo)).to.be.true()
expect(Enumeration.isEnumerationClass(OneTwo.ONE)).to.be.false()
expect(Enumeration.isEnumerationInstance(OneTwo.ONE)).to.be.true()
})
it('should not allow enum classes to be instantiable', function () {
const It = Enumeration.new({ name: 'It', values: ['THING'] })
expect(() => new It()).to.throw(EnumClassNotInstantiableError)
})
it('should not allow extensions of enum classes', function () {
const Super = Enumeration.new({ name: 'Super', values: ['SUPER'] })
class Sub extends Super {
}
expect(() => new Sub()).to.throw(ClassNotExtendableError)
})
})