forked from hejny/spacetrim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceTrim.test.ts
134 lines (105 loc) · 3.34 KB
/
spaceTrim.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
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
import { spaceTrim } from './spaceTrim';
// tslint:disable:no-trailing-whitespace
describe('how space trim works', () => {
it('will not crash for empty string', () => {
expect(spaceTrim('')).toBe('');
expect(spaceTrim(' ')).toBe('');
expect(spaceTrim(' \n \n ')).toBe('');
expect(spaceTrim(' \n \n\n \n\n \n ')).toBe('');
});
it('will trim one sentence', () => {
expect(spaceTrim(` foo `)).toBe('foo');
expect(spaceTrim(` foo bar `)).toBe('foo bar');
expect(spaceTrim(` foo bar `)).toBe('foo bar');
expect(spaceTrim(`\n\n\n foo `)).toBe('foo');
expect(
spaceTrim(`\n\n\r foo
`),
).toBe('foo');
expect(
spaceTrim(`\t foo
`),
).toBe('foo');
});
it('will space trim', () => {
expect(
spaceTrim(`
Hell1
Space
Trim
`),
).toBe(['Hell1', 'Space', 'Trim'].join('\n'));
});
it('will space trim with 2 zig-zag lines', () => {
expect(
spaceTrim(`
Hell2
Space
`),
).toBe([' Hell2', 'Space'].join('\n'));
});
it('will space trim with 3 zig-zag lines', () => {
expect(
spaceTrim(`
Hell3
Space${' '}
Trim${' '}
`),
).toBe([' Hell3', 'Space ', ' Trim '].join('\n'));
});
it('will space trim with 5 zig-zag lines', () => {
expect(
spaceTrim(`
Hell5
Space
Trim
Moooore${' '}
Words
`),
).toBe(
[
' Hell5',
'Space',
' Trim',
' Moooore ',
' Words',
].join('\n'),
);
});
it('will preserve white lines inside', () => {
expect(
spaceTrim(`
Hell9
Space
${' '}
${' '}
Trim
`),
).toBe(['Hell9', 'Space', '', ' ', ' ', 'Trim'].join('\n'));
});
it('will fail on invalid usage', () => {
// Note: This is usefull for usage in javascript
for (const invalidValue of [undefined, null, 1, true, false, [], {}]) {
expect(() => spaceTrim(invalidValue as any)).toThrowError(
/expected string/,
);
expect(() => spaceTrim(invalidValue as any)).toThrowError(
/expected string/,
);
expect(() =>
spaceTrim((block) => ` ${block(invalidValue as any)} `),
).toThrowError(/expected string/);
expect(
async () =>
await spaceTrim(
async (block) =>
` ${await Promise.resolve(
block(invalidValue as any),
)} `,
),
).rejects.toThrowError(/expected string/);
}
});
// TODO it('can space trim with tabs', () => {
// TODO it('can space trim with \n and \r and both ', () => {
});