-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
420 lines (363 loc) · 13.8 KB
/
index.test.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// @ts-check
const AlphanumericEncoder = require('./index')
let encoder = new AlphanumericEncoder({
allowLowerCaseDictionary: false,
dictionary: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
})
const numberToEncodedLetters = [
[1, 'A'],
[4, 'D'],
[25, 'Y'],
[26, 'Z'],
[27, 'AA'],
[52, 'AZ'],
[53, 'BA'],
[78, 'BZ'],
[705, 'AAC'],
[16384, 'XFD']
]
const numberWithDictionaryToEncodedLetters = [
['ABC', 4, 'AA'],
['123ABC', 4, 'A'],
['123ABC', 2, '2'],
['123ABC', 200, 'B32'],
['0123456789ABCDEF', 1, '0'],
['0123456789ABCDEF', 16, 'F'],
['0123456789ABCDEF', 17, '00'],
['0123456789ABCDEF', 272, 'FF'],
['EDCBA', 4, 'B'],
['EDCBA', 7, 'ED'],
['EDCBA', 26, 'AE'],
['EDCBA', 27, 'AD'],
['EDCBA', 31, 'EEE']
]
const numberWithLowerCaseDictionaryToEncodedLetters = [
['ABCDabcd', 4, 'D'],
['ABCDabcd', 6, 'b'],
['ABCDabcd', 9, 'AA'],
['ABCDabcd', 15, 'Ac'],
['ABCDabcd', 2984, 'abDd']
]
const expectedValidDictionaryValues = ['ABCD', 'ABcd', 'aBcD', 'ABC123']
const expectedInvalidDictionaryValuesWithRepeatingSymbols = ['ABCDA', 'ABCDa', 'ABCD1231']
const expectedInvalidDictionaryValuesWithImproperSymbols = ['ABC!', 'ABC@', 'ABC?', '&#$%(*)']
function setupNewEncoderForTesting() {
beforeEach(() => {
encoder = new AlphanumericEncoder()
})
afterEach(() => {
encoder = null
})
}
describe('Allow Lower Case Dictionaries', () => {
setupNewEncoderForTesting()
test('Default should not allow lower case dictionaries', () => {
expect(encoder.allowLowerCaseDictionary).toBeFalsy()
})
test.each([true, 1, [123], { value: 1 }])(
'allowLowerCaseDictionary with truthy value %p',
(truthyTestValue) => {
// @ts-ignore
encoder.allowLowerCaseDictionary = truthyTestValue
expect(encoder.allowLowerCaseDictionary).toBeTruthy()
}
)
test.each([true, 1, [123], { value: 1 }])(
'allowLowerCaseDictionary with truthy value in setup: new AlphanumericEncoder({ allowLowerCaseDictionary: %p })',
(truthyTestValue) => {
const setupEncoder = new AlphanumericEncoder({
// @ts-ignore
allowLowerCaseDictionary: truthyTestValue
})
expect(setupEncoder.allowLowerCaseDictionary).toBeTruthy()
}
)
test.each([false, 0, null, undefined])(
'allowLowerCaseDictionary with falsy value %p',
(truthyTestValue) => {
// @ts-ignore
encoder.allowLowerCaseDictionary = truthyTestValue
expect(encoder.allowLowerCaseDictionary).toBeFalsy()
}
)
test.each([false, 0, null, undefined])(
'allowLowerCaseDictionary with falsy value in setup: new AlphanumericEncoder({ allowLowerCaseDictionary: %p })',
(truthyTestValue) => {
const setupEncoder = new AlphanumericEncoder({
// @ts-ignore
allowLowerCaseDictionary: truthyTestValue
})
expect(setupEncoder.allowLowerCaseDictionary).toBeFalsy()
}
)
test('Allow lower case dictionaries by using a config object', () => {
encoder = new AlphanumericEncoder({ allowLowerCaseDictionary: true })
expect(encoder.allowLowerCaseDictionary).toBeTruthy()
})
})
describe('Dictionary Validation', () => {
setupNewEncoderForTesting()
test('Default dictionary should be capital alphabet in order', () => {
expect(encoder.dictionary).toBe('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
})
test('Resetting the dictionary after changing it should be capital alphabet again', () => {
encoder.dictionary = 'ABCD'
encoder.resetDefaultDictionary()
expect(encoder.dictionary).toBe('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
})
test('Dictionary cannot be an empty string', () => {
expect(() => {
encoder.dictionary = ''
}).toThrow(/empty string/)
})
test('Dictionary cannot be null', () => {
expect(() => {
encoder.dictionary = null
}).toThrow(/null/)
})
test('Dictionary cannot be undefined', () => {
expect(() => {
encoder.dictionary = undefined
}).toThrow(/undefined/)
})
test.each([true, false])('Dictionary cannot be boolean %p', (input) => {
expect(() => {
// @ts-ignore
encoder.dictionary = input
}).toThrow(/boolean/)
})
test('Dictionary cannot be NaN', () => {
expect(() => {
// @ts-ignore
encoder.dictionary = NaN
}).toThrow(/NaN/)
})
test.each([true, false, null, undefined, '', NaN, { dictionary: 'ABC' }, ['ABC'], 'ABCDA'])(
'Cannot pass bad dictionary arguments in constructor: new AlphanumericEncoder({dictionary: %p}',
(dictionaryInput) => {
expect(() => {
// eslint-disable-next-line no-unused-vars
const setupEncoder = new AlphanumericEncoder({
// @ts-ignore
dictionary: dictionaryInput
})
}).toThrow()
}
)
describe('Valid Dictionaries (no lower case)', () => {
setupNewEncoderForTesting()
test.each(expectedValidDictionaryValues)(
'Expect %p to be a valid dictionary',
(validDictionaryString) => {
encoder.dictionary = validDictionaryString
expect(encoder.dictionary).toBe(validDictionaryString.toUpperCase())
}
)
test('Dictionaries should be converted to uppercase', () => {
encoder.dictionary = 'abcd'
expect(encoder.dictionary).toBe('ABCD')
})
test('Allow setting dictionaries by using a config object', () => {
encoder = new AlphanumericEncoder({ dictionary: 'abcd' })
expect(encoder.dictionary).toBe('ABCD')
})
})
describe('Valid Dictionaries (allow lower case)', () => {
setupNewEncoderForTesting()
test.each(expectedValidDictionaryValues)(
'Expect %p to be a valid dictionary',
(validDictionaryString) => {
encoder.allowLowerCaseDictionary = true
encoder.dictionary = validDictionaryString
expect(encoder.dictionary).toBe(validDictionaryString)
}
)
const complexDictionary = 'ABCD123abcd'
test(`Expect ${complexDictionary} to be a valid dictionary`, () => {
encoder.allowLowerCaseDictionary = true
encoder.dictionary = complexDictionary
expect(encoder.dictionary).toBe('ABCD123abcd')
})
test('Allow setting lower case dictionaries by using a config object', () => {
encoder = new AlphanumericEncoder({
allowLowerCaseDictionary: true,
dictionary: 'abcd'
})
expect(encoder.dictionary).toBe('abcd')
})
test('Class should be configurable using externally defined object', () => {
const configOptions = { allowLowerCaseDictionary: true, dictionary: 'abcd' }
encoder = new AlphanumericEncoder(configOptions)
expect(encoder.dictionary).toBe('abcd')
})
})
describe('Invalid Dictionaries', () => {
setupNewEncoderForTesting()
test.each(expectedInvalidDictionaryValuesWithRepeatingSymbols)(
'Expect %p to be a be invalid dictionary and throw error due to repeating symbol',
(invalidDictionaryString) => {
expect(() => {
encoder.dictionary = invalidDictionaryString
}).toThrow(/repeating symbol/)
}
)
test.each(expectedInvalidDictionaryValuesWithImproperSymbols)(
'Expect %p to be a be invalid dictionary due to non-alphanumeric symbol',
(invalidDictionaryString) => {
expect(() => {
encoder.dictionary = invalidDictionaryString
}).toThrow(/must be alphanumeric/)
}
)
test('Passing invalid dictionary in options should throw error', () => {
expect(() => {
encoder = new AlphanumericEncoder({ dictionary: 'ABC@#$' })
}).toThrow(/must be alphanumeric/)
})
})
})
describe('Test Encoding', () => {
setupNewEncoderForTesting()
test('Expect 0 to return undefined', () => {
expect(encoder.encode(0)).toBeUndefined()
})
test('Expect negative numbers to return undefined', () => {
expect(encoder.encode(-1)).toBeUndefined()
})
test('Expect encoding numbers greater than Number.MAX_SAFE_INTEGER throws an error', () => {
expect(() => {
encoder.encode(Number.MAX_SAFE_INTEGER + 1)
}).toThrow(/maximum safe integer/)
})
test('Expect non-integers to return value of floor: e.g. 1.5 = "B", 3.1 = "C", 4.9 = "D"', () => {
expect(encoder.encode(2.5)).toBe('B')
expect(encoder.encode(3.1)).toBe('C')
expect(encoder.encode(4.9)).toBe('D')
})
describe('Encode with Default Dictionary', () => {
test.each(numberToEncodedLetters)(
'Under default dictionary, expect %p to encode to %p',
(number, letter) => {
// @ts-ignore
expect(encoder.encode(number)).toBe(letter)
}
)
})
describe('Encode with Custom Upper Case Dictionary', () => {
test.each(numberWithDictionaryToEncodedLetters)(
'Under dictionary %p, expect %p to encode to %p',
(dictionary, number, letter) => {
// @ts-ignore
encoder.dictionary = dictionary
// @ts-ignore
expect(encoder.encode(number)).toBe(letter)
}
)
})
describe('Encode with Custom Lower Case Dictionary', () => {
test.each(numberWithLowerCaseDictionaryToEncodedLetters)(
'Under dictionary %p, expect %p to encode to %p',
(dictionary, number, letter) => {
encoder.allowLowerCaseDictionary = true
// @ts-ignore
encoder.dictionary = dictionary
// @ts-ignore
expect(encoder.encode(number)).toBe(letter)
}
)
})
})
describe('Test Decoding', () => {
setupNewEncoderForTesting()
test.each(['', undefined, null])('Trying to decode %p should return "undefined"', (letter) => {
expect(encoder.decode(letter)).toBeUndefined()
})
test('Expect decoding strings to integers greater than Number.MAX_SAFE_INTEGER throws an error', () => {
expect(() => {
encoder.decode('BKTXHSOGHKKF') // BKTXHSOGHKKE is what max safe integer encodes to under default dictionary
}).toThrow(/maximum safe integer/)
})
describe('Invalid Decode Values Using Default Dictionary', () => {
const encodedWithNonexistentCharacter = [0, 2, -15, -1.7, 22.6, '16', 'D1', 'A#', '&']
test.each(encodedWithNonexistentCharacter)(
'Under default dictionary, trying to decode %p will return "undefined" due to character not existing in dictionary',
(letter) => {
// @ts-ignore
expect(encoder.decode(letter)).toBeUndefined()
}
)
})
describe('Decode with Default Dictionary', () => {
test.each(numberToEncodedLetters)(
'Under default dictionary, expect %p to be decoded from %p',
(number, letter) => {
// @ts-ignore
expect(encoder.decode(letter)).toBe(number)
}
)
})
describe('Decode with Custom Upper Case Dictionary', () => {
test.each(numberWithDictionaryToEncodedLetters)(
'Under dictionary %p, expect %p to be decoded from %p',
(dictionary, number, letter) => {
// @ts-ignore
encoder.dictionary = dictionary
// @ts-ignore
expect(encoder.decode(letter)).toBe(number)
}
)
})
describe('Decode with Custom Lower Case Dictionary', () => {
test.each(numberWithLowerCaseDictionaryToEncodedLetters)(
'Under dictionary %p, expect %p to be decoded from %p',
(dictionary, number, letter) => {
encoder.allowLowerCaseDictionary = true
// @ts-ignore
encoder.dictionary = dictionary
// @ts-ignore
expect(encoder.decode(letter)).toBe(number)
}
)
})
})
describe('Test Deconstruction', () => {
setupNewEncoderForTesting()
test.each(['', undefined, null])(
'Trying to deconstruct %p should return "undefined"',
(badArgument) => {
expect(encoder.deconstruct(badArgument)).toBeUndefined()
}
)
test('Expect dictionaries containing numbers to throw an error', () => {
expect(() => {
encoder.dictionary = 'ABC123'
encoder.deconstruct('C3')
}).toThrow(/dictionary contains numbers/)
})
const deconstructionTestValues = [
['A', [1]],
['A1', [1, 1]],
['C7', [3, 7]],
['7C', [7, 3]],
['AE18', [31, 18]],
['18AE', [18, 31]],
['1', [1]],
[1, [1]],
[733, [733]],
[[733], [733]],
[['7C'], [7, 3]],
['7C82AA', [7, 3, 82, 27]],
['C3ABC123EFGH456', [3, 3, 731, 123, 92126, 456]],
['A1aB2B', [1, 1, undefined, 2, 2]],
['7AC!23A1%', [7, undefined, 23, 1, 1, undefined]],
['&', [undefined]]
]
test.each(deconstructionTestValues)(
'Under default dictionary, deconstructing %p should return array %p',
// @ts-ignored
(deconstructArgument, resultArray) => {
// @ts-ignore
expect(encoder.deconstruct(deconstructArgument)).toEqual(resultArray)
}
)
})