-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
122 lines (104 loc) · 3.28 KB
/
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
const assert = require('assert');
const alphanumerize = require('./index.js');
// TODO test for very big sequences, like bigint size
const base8 = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'ag', 'ah',
'ba',
];
base8.options = { alphabet: 'abcdefgh' };
const base10 = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'ag', 'ah', 'ai', 'aj',
'ba',
];
base10.options = { alphabet: 'abcdefghij' };
const base26 = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'ag', 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as', 'at', 'au', 'av', 'aw', 'ax', 'ay', 'az',
'ba',
];
// eslint-disable-next-line no-console
const log = (...arg) => console.log(...arg);
let failed;
function test(name, fn) {
let error;
try {
fn();
} catch (e) {
error = e;
}
log(`${name}...${error ? '❌' : '✅'}`);
if (error) {
log(error);
failed = true;
}
}
test('base 8 english alphabet', () => {
base8.forEach((alphanumeral, i) => {
const num = i + 1;
assert.strictEqual(alphanumerize(num, base8.options), alphanumeral);
});
});
test('base 10 english alphabet', () => {
base10.forEach((alphanumeral, i) => {
const num = i + 1;
assert.strictEqual(alphanumerize(num, base10.options), alphanumeral);
});
});
test('base 26 english alphabet', () => {
base26.forEach((alphanumeral, i) => {
const num = i + 1;
assert.strictEqual(alphanumerize(num, base26.options), alphanumeral);
});
});
test('defaults to the english alphabet', () => {
base26.forEach((alphanumeral, i) => {
const num = i + 1;
assert.strictEqual(alphanumerize(num), alphanumeral);
});
});
test('the number zero', () => {
assert.strictEqual(alphanumerize(0), '');
});
test('not a numeral sequence', () => {
let undef;
[true, false, 'abc', Symbol(0), null, undef, () => null]
.forEach(emptyCase => {
assert.strictEqual(alphanumerize(emptyCase), '',
`failed on input ${JSON.stringify(emptyCase)}`);
});
});
test('negative numbers', () => {
base26.forEach((alphanumeral, i) => {
const num = -1 * (i + 1);
assert.strictEqual(alphanumerize(num), `-${alphanumeral}`);
});
});
test('exposes the current alphabet', () => {
const a = alphanumerize({ alphabet: 'abc' });
assert.strictEqual(a.alphabet, 'abc');
assert.strictEqual(alphanumerize({ alphabet: 'xyz' }).alphabet, 'xyz');
assert.strictEqual(a.alphabet, 'abc');
assert.strictEqual(alphanumerize.alphabet, 'abcdefghijklmnopqrstuvwxyz');
});
test('can return a function with preset alphabet', () => {
const instance = alphanumerize(base8.options);
assert.strictEqual(instance.alphabet, base8.options.alphabet);
base8.forEach((alphanumeral, i) => {
const num = i + 1;
assert.strictEqual(instance(num), alphanumeral);
});
});
test('cannot modify preset alphabet', () => {
// module object
alphanumerize.alphabet = '123';
assert.strictEqual(alphanumerize.alphabet, 'abcdefghijklmnopqrstuvwxyz');
// instance object
const instance = alphanumerize({ alphabet: 'abc' });
instance.alphabet = 'xyz';
assert.strictEqual(instance.alphabet, 'abc');
});
if (failed) {
process.exit(1);
}