-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.test.ts
85 lines (63 loc) · 2.83 KB
/
index.test.ts
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
import zalgo, { summon } from '../src';
const msg = 'Hello, World!';
const isString = str => expect(typeof str).toBe('string');
test('returns a string', () => {
isString(zalgo('Hello'));
});
test('returns empty string on bad input', () => {
const z: any = zalgo; // Cast so we can use incorrectly without TS yelling
expect(z()).toBe('');
expect(z(null)).toBe('');
expect(z(undefined)).toBe('');
expect(z({})).toBe('');
});
test('keeps original message intact in the output', () => {
const result = zalgo(msg);
// Check that our original message is still somewhere in the output
let searchIndex = 0;
result.split('').forEach(char => char === msg[searchIndex] ? searchIndex++ : null);
expect(searchIndex).toBe(msg.length);
});
test('seeding produces consistent ouputs', () => {
const options = { seed: 'This is my seed' };
const zalgo1 = summon(options);
const zalgo2 = summon(options);
for (let i = 0; i < 10; i++) {
expect(zalgo1(msg)).toBe(zalgo2(msg));
}
});
test('providing custom character set only outputs provided characters', () => {
const input = 'aaaaa';
const options = { characters: ['b'.charCodeAt(0)] };
// Output should have 5 'a's and some number of 'b's and nothing more.
const result = zalgo(input, options);
const aCount = result.split('').filter(char => char === 'a').length;
const bCount = result.split('').filter(char => char === 'b').length;
expect(aCount).toBe(input.length); // Expect 5 'a's
expect(result.length).toBe(aCount + bCount); // Expect only 'a's and 'b's
});
test('providing custom numeric intensity', () => {
isString(zalgo(msg, { intensity: 0.6 }));
isString(zalgo(msg, { intensity: 0 }));
isString(zalgo(msg, { intensity: 1 }));
// Zero intensity shouldn't affect input
expect(zalgo(msg, { intensity: 0}).length).toBe(msg.length);
expect(zalgo(msg, { intensity: 0.6}).length).toBeGreaterThan(msg.length);
expect(zalgo(msg, { intensity: 1 }).length).toBeGreaterThan(msg.length);
});
test('providing custom functional intensity', () => {
isString(zalgo(msg, { intensity: () => .6 }));
isString(zalgo(msg, { intensity: () => 0 }));
isString(zalgo(msg, { intensity: () => 1 }));
// Zero intensity shouldn't affect input
expect(zalgo(msg, { intensity: () => 0 }).length).toBe(msg.length);
expect(zalgo(msg, { intensity: () => 0.6 }).length).toBeGreaterThan(msg.length);
expect(zalgo(msg, { intensity: () => 1 }).length).toBeGreaterThan(msg.length);
// Ensure per-character intensity assigns different intensities
const customMsg = 'ab';
const intensity = (str: string, i: number) => i;
const result = zalgo(customMsg, { intensity });
expect(result[0]).toBe('a');
expect(result[1]).toBe('b');
expect(result.length).toBeGreaterThan(customMsg.length);
});