This repository was archived by the owner on Nov 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.test.js
More file actions
235 lines (191 loc) · 9.26 KB
/
enum.test.js
File metadata and controls
235 lines (191 loc) · 9.26 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const Enum = require('./enum');
let exceptions = require('node-exceptions');
let InvalidArgumentException = exceptions.InvalidArgumentException;
describe('Creating basic enums', () => {
test('Сreating new enum', () => {
let MyEnum = Enum.create({'mercury': 1, 'earth': 3, 'Saturn': 5});
expect(MyEnum.MERCURY.value).toBe(1);
expect(MyEnum.EARTH.value).toBe(3);
expect(MyEnum.SATURN.value).toBe(5);
expect(MyEnum.MERCURY.name).toBe('mercury');
expect(MyEnum.EARTH.name).toBe('earth');
expect(MyEnum.SATURN.name).toBe('Saturn');
});
test('Сreating new enum with extras', () => {
let xExtra = {'foo': 'bar'};
let MyEnum = Enum.create({'x': 1, 'y': 2, 'z': 3}, [xExtra, 10, 'hello']);
expect(MyEnum.X.value).toBe(1);
expect(MyEnum.Y.value).toBe(2);
expect(MyEnum.Z.value).toBe(3);
expect(MyEnum.X.name).toBe('x');
expect(MyEnum.Y.name).toBe('y');
expect(MyEnum.Z.name).toBe('z');
expect(MyEnum.X.extra).toBe(xExtra);
expect(MyEnum.Y.extra).toBe(10);
expect(MyEnum.Z.extra).toBe('hello');
});
test('Creating with incorrect extra argument', () => {
expect(() => Enum.create({'x': 1}))
.not.toThrow();
expect(() => Enum.create({'x': 1}, null))
.not.toThrow();
expect(() => Enum.create({'x': 1}, ['hello']))
.not.toThrow();
expect(() => Enum.create({'x': 1}, 'hello'))
.toThrow(InvalidArgumentException);
expect(() => Enum.create({'x': 1}, 'hello'))
.toThrow('Extra params should be an array or null');
expect(() => Enum.create({'x': 1, 'y': 2}))
.not.toThrow();
expect(() => Enum.create({'x': 1, 'y': 2}, null))
.not.toThrow();
expect(() => Enum.create({'x': 1, 'y': 2}, ['hello', 'world']))
.not.toThrow();
expect(() => Enum.create({'x': 1, 'y': 2}, ['hello']))
.toThrow(InvalidArgumentException);
expect(() => Enum.create({'x': 1, 'y': 2}, ['hello']))
.toThrow('Extra params should be an array of the same length as enum has or null');
expect(() => Enum.create({'x': 1, 'y': 2}, ['hello', 'world', '!']))
.toThrow(InvalidArgumentException);
expect(() => Enum.create({'x': 1, 'y': 2}, ['hello', 'world', '!']))
.toThrow('Extra params should be an array of the same length as enum has or null');
expect(() => Enum.create({'x': 1, 'y': 2}, 'hello', 'world'))
.toThrow(InvalidArgumentException);
expect(() => Enum.create({'x': 1, 'y': 2}, 'hello', 'world'))
.toThrow('Extra params should be an array or null');
});
});
describe('Enums behavior', () => {
test('enum has only keys for given names', () => {
let MyEnum = Enum.create({'x': 1, 'y': 2, 'z': 3});
expect(Object.keys(MyEnum)).toEqual(expect.arrayContaining(['X', 'Y', 'Z']));
expect(Object.keys(MyEnum)).not.toContain('valueToEnumMap');
expect(Object.keys(MyEnum)).not.toContain('nameToEnumMap');
});
test('enums don\'t share items', () => {
let extra1 = {};
let extra2 = {};
let MyEnum1 = Enum.create({'x': 1, 'y': 2, 'z': 3}, [extra1, null, null]);
let MyEnum2 = Enum.create({'x': 1, 'y': 2, 'z': 3}, [extra2, null, null]);
expect(MyEnum1).not.toBe(MyEnum2);
expect(MyEnum1.X).not.toBe(MyEnum2.X);
expect(MyEnum1.X.id).toBe(MyEnum2.X.id);
expect(MyEnum1.X.name).toBe(MyEnum2.X.name);
expect(MyEnum1.X.extra).not.toBe(MyEnum2.X.extra);
expect(MyEnum1.X).toBe(MyEnum1.X);
});
test('enums have expected inheritance', () => {
let MyEnumA = Enum.create({'x': 1, 'y': 2, 'z': 3});
let MyEnumB = Enum.create({'x': 1, 'y': 2, 'z': 3});
// MyEnumA and MyEnumB are just frozen holders of enums, they're not Enums themselves
expect(MyEnumA).not.toBeInstanceOf(Enum);
expect(MyEnumB).not.toBeInstanceOf(Enum);
// But all fields in MyEnumA and MyEnumB are Enum instances
// You can also do
// x = EnumName.KEY;
// x instanceof EnumName
// to ensure that var is a item of EnumName. Or
// x instanceof Enum
// to ensure x is an enum
Object.keys(MyEnumA).map((key) => {
expect(MyEnumA[key]).toBeInstanceOf(MyEnumA);
expect(MyEnumA[key]).toBeInstanceOf(Enum);
expect(MyEnumA[key]).not.toBeInstanceOf(MyEnumB);
});
Object.keys(MyEnumB).map((key) => {
expect(MyEnumB[key]).toBeInstanceOf(MyEnumB);
expect(MyEnumB[key]).toBeInstanceOf(Enum);
expect(MyEnumB[key]).not.toBeInstanceOf(MyEnumA);
});
});
test('enum is not changeable', () => {
let Bar = Enum.create({'A': 1, 'B': 2, 'C': 3});
Bar.D = new Enum('D', 4);
expect(Bar.D).toBeUndefined();
Bar.B = new Enum('X', 10, 'test');
expect(Bar.B.value).toBe(2);
expect(Bar.B.name).toBe('B');
expect(Bar.B.extra).toBeNull();
Bar.A.name = 'AAA';
expect(Bar.A.name).toBe('A');
Bar.A.value = -1;
expect(Bar.A.value).toBe(1);
});
});
describe('Enum.valueOf', () => {
test('Lower and upper case of name', () => {
let Foo = Enum.create({'UPPERCASE': 1, 'lowercase': 2});
expect(Foo.UPPERCASE).toBe(Foo.valueOf('uppercase'));
expect(Foo.UPPERCASE).toBe(Foo.valueOf('UPPERCASE'));
expect(Foo.UPPERCASE).toBe(Foo.valueOf(1));
expect(Foo.LOWERCASE).toBe(Foo.valueOf('lowercase'));
expect(Foo.LOWERCASE).toBe(Foo.valueOf('LOWERCASE'));
expect(Foo.LOWERCASE).toBe(Foo.valueOf(2));
});
test('Working with string values', () => {
let Bar = Enum.create({'X': 1, 'Y': 'X', 'A': 'b', 'D': 'E'});
expect(Bar.Y).toBe(Bar.valueOf('Y'));
expect(Bar.Y).toBe(Bar.valueOf('y'));
expect(Bar.X).toBe(Bar.valueOf('x'));
expect(Bar.X).toBe(Bar.valueOf('X'));
expect(Bar.A).toBe(Bar.valueOf('a'));
expect(Bar.A).toBe(Bar.valueOf('A'));
expect(Bar.A).toBe(Bar.valueOf('b'));
expect(() => Bar.valueOf('B')).toThrow(InvalidArgumentException);
expect(() => Bar.valueOf('B')).toThrow('No enum with specified name');
expect(Bar.D).toBe(Bar.valueOf('E'));
expect(() => Bar.valueOf('e')).toThrow(InvalidArgumentException);
expect(() => Bar.valueOf('e')).toThrow('No enum with specified name');
});
});
// valid property name - is a name which is available as CreatedEnum.<propertyName>
// non-valid one - is a name which is only available as CreatedEnum[propertyName]
describe('Enums with non-valid property names', () => {
test('Creating enums with non valid property names', () => {
let MyEnum = Enum.create({'X x': 1, '123': 2, '$x': 3, '$123': 4, 'X/Y': 5, 'X-Z': 6});
expect(MyEnum.X_X.name).toBe('X x');
expect(MyEnum.___.name).toBe('123');
expect(MyEnum.$X.name).toBe('$x');
expect(MyEnum.$123.name).toBe('$123');
expect(MyEnum.X_Y.name).toBe('X/Y');
expect(MyEnum.X_Z.name).toBe('X-Z');
});
test('Creating enums with non valid property names which cause property duplicates', () => {
expect(() => Enum.create({'X x': 1, 'X/X': 2})).toThrow(InvalidArgumentException);
expect(() => Enum.create({'X x': 1, 'X/X': 2}))
.toThrow('Some names turn to be the same after making them valid JS IdentifierName');
});
test('Enum.valueOf works fine with invalid property names', () => {
let MyEnum = Enum.create({'X x': 1});
expect(MyEnum.X_X).toBe(MyEnum.valueOf(1));
expect(MyEnum.X_X).toBe(MyEnum.valueOf('x x'));
expect(MyEnum.X_X).toBe(MyEnum.valueOf('X X'));
expect(() => MyEnum.valueOf('X_X')).toThrow(InvalidArgumentException);
expect(() => MyEnum.valueOf('X_X')).toThrow('No enum with specified name');
});
});
describe('Creating classes based on Enum', () => {
test('Child class with additional getter', () => {
class DescriptionEnum extends Enum {
constructor(...args) {
super(...args);
if (!this.extra || !this.extra.description) {
throw new InvalidArgumentException('Description is a required property of an extra');
}
}
get description() {
return this.extra.description;
}
}
expect(() => DescriptionEnum.create({'Add': 1, 'Remove': 2})).toThrow(InvalidArgumentException);
expect(() => DescriptionEnum.create({'Add': 1, 'Remove': 2})).toThrow('Description is a required property of an extra');
let MyDescriptionEnum = DescriptionEnum.create({'Add': 1}, [{description: 'Adds something'}]);
expect(MyDescriptionEnum).not.toBeInstanceOf(MyDescriptionEnum);
expect(MyDescriptionEnum).not.toBeInstanceOf(DescriptionEnum);
expect(MyDescriptionEnum).not.toBeInstanceOf(Enum);
expect(MyDescriptionEnum.ADD).toBeInstanceOf(MyDescriptionEnum);
expect(MyDescriptionEnum.ADD).toBeInstanceOf(DescriptionEnum);
expect(MyDescriptionEnum.ADD).toBeInstanceOf(Enum);
expect(MyDescriptionEnum.ADD.description).toBe('Adds something');
});
});