-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathwrapSpannedStringByWord.test.ts
More file actions
142 lines (125 loc) · 4.63 KB
/
wrapSpannedStringByWord.test.ts
File metadata and controls
142 lines (125 loc) · 4.63 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
import { T } from './formattedString';
import { wrapSpannedStringByWord } from './wrapSpannedStringByWord';
function wrapString(string: string, width: number): string[] {
return Array.from(
wrapSpannedStringByWord(T().appendString(string), width)
).map((formattedString) => formattedString.getString());
}
test('single line', () => {
expect(wrapString('one two three', 100)).toEqual(['one two three']);
});
test('one word per line', () => {
expect(wrapString('one two three', 5)).toEqual(['one ', 'two ', 'three']);
});
test('multiple words per line', () => {
expect(wrapString('one two three four five six', 10)).toEqual([
'one two ',
'three four',
' five six',
]);
});
test('leading and trailing spaces', () => {
expect(wrapString(' one two ', 3)).toEqual([' ', 'one', ' ', 'two', ' ']);
});
test('multiple spaces', () => {
expect(wrapString('one two', 4)).toEqual(['one ', ' ', 'two']);
});
test('break long words', () => {
expect(wrapString('one longword two', 3)).toEqual([
'one',
' lo',
'ngw',
'ord',
' ',
'two',
]);
});
test('should not add or remove characters', () => {
for (const s of ['\none\n\ntwo three four\n']) {
expect(wrapString(s, 3).join('')).toEqual(s);
}
});
test('snapshot tests', () => {
function wrapAndJoin(text: string, width: number): string {
return wrapString(text, width).join('\n');
}
expect(
wrapAndJoin(
`export function defaultTheme(chalkInstance: Chalk): Theme {
return {
COMMIT_SHA_COLOR: chalkInstance.green,
COMMIT_AUTHOR_COLOR: chalkInstance.blueBright,
COMMIT_DATE_COLOR: chalkInstance.white,
FILE_NAME_COLOR: chalkInstance.yellowBright,
FILE_NAME_BORDER_COLOR: chalkInstance.yellow,
HUNK_HEADER_COLOR: chalkInstance.white.dim,
DELETED_LINE_COLOR: chalkInstance.redBright,
INSERTED_LINE_COLOR: chalkInstance.greenBright,
UNMODIFIED_LINE_COLOR: chalkInstance.white,
MISSING_LINE_COLOR: chalkInstance.white,
};
}`,
80
)
).toMatchInlineSnapshot(`
"export function defaultTheme(chalkInstance: Chalk): Theme {
return {
COMMIT_SHA_COLOR: chalkInstance.green,
COMMIT_AUTHOR_COLOR:
chalkInstance.blueBright,
COMMIT_DATE_COLOR: chalkInstance.white,
FILE_NAME_COLOR: chalkInstance.yellowBright,
FILE_NAME_BORDER_COLOR:
chalkInstance.yellow,
HUNK_HEADER_COLOR: chalkInstance.white.dim,
DELETED_LINE_COLOR: chalkInstance.redBright,
INSERTED_LINE_COLOR:
chalkInstance.greenBright,
UNMODIFIED_LINE_COLOR: chalkInstance.white,
MISSING_LINE_COLOR: chalkInstance.white,
};
}"
`);
expect(
wrapAndJoin(
`“TO THE RED-HEADED LEAGUE: On account of the bequest of the late Ezekiah Hopkins, of Lebanon, Pennsylvania, U.S.A., there is now another vacancy open which entitles a member of the League to a salary of £ 4 a week for purely nominal services. All red-headed men who are sound in body and mind and above the age of twenty-one years, are eligible. Apply in person on Monday, at eleven o’clock, to Duncan Ross, at the offices of the League, 7 Pope’s Court, Fleet Street.”`,
40
)
).toMatchInlineSnapshot(`
"“TO THE RED-HEADED LEAGUE: On account of
the bequest of the late Ezekiah
Hopkins, of Lebanon, Pennsylvania,
U.S.A., there is now another vacancy
open which entitles a member of the
League to a salary of £ 4 a week for
purely nominal services. All red-headed
men who are sound in body and mind and
above the age of twenty-one years, are
eligible. Apply in person on Monday, at
eleven o’clock, to Duncan Ross, at the
offices of the League, 7 Pope’s Court,
Fleet Street.”"
`);
});
test('wide characters', () => {
expect(wrapString('のオリジナルのソース', 5).join('\n'))
.toMatchInlineSnapshot(`
"のオ
リジ
ナル
のソ
ース
"
`);
});
test('off-by-one', () => {
expect(
wrapString(
' "url": "https://homebrew.bintray.com/bottles/go-1.14.2_1.catalina.bottle.tar.gz",',
72
)
).toEqual([
' "url": "https://homebrew.bintray.com/bottles/go-1.14.2_1.c',
'atalina.bottle.tar.gz",',
]);
});