-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtext.test.gs
184 lines (161 loc) · 3.76 KB
/
text.test.gs
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
function testAllText() {
testSuite('text.js', [
testGetStyle,
testCodeFromCRUrl,
testCapitalizeIfAppropriate,
testIsCapitalized,
testCapitalize,
testLabelNumberHandler,
testRefNumberHandler,
])
}
function testCodeFromCRUrl() {
It('ref url returns 3-string code',
codeFromUrl('#fig_hello'),
'fig'
)
It('lab url returns 5 string code',
codeFromUrl('#figur_hello'),
'figur'
)
It('returns null for other url',
codeFromUrl('https://google.com'),
null
)
It('returns null for malformed CRUrl',
codeFromUrl('#figu_hello'),
null
)
}
function testCapitalizeIfAppropriate() {
It('returns capitalized when reference text is already capitalized',
capitalizeIfAppropriate('aa bb Figure', 5, 'Figure'),
'Figure'
)
It('returns capitalized when text to replace is already capitalized',
capitalizeIfAppropriate('aa bb Figur', 5, 'figure'),
'Figure'
)
It('returns uncapitalized when text to replace is not capitalized',
capitalizeIfAppropriate('aa bb figur', 5, 'figure'),
'Figure'
)
}
function testCapitalize() {
It('returns empty for empty',
capitalize(''),
''
)
It('capitalizes non-empty string',
capitalize('hello'),
'Hello'
)
It('does not modify already capitalized',
capitalize('Hello'),
'Hello'
)
}
function testIsCapitalized() {
It('returns false for empty string',
isCapitalized(''),
false
)
It('returns true if is capitalized',
isCapitalized('Hello'),
true
)
It('returns false if not capitalize',
isCapitalized('hello'),
false
)
}
function testLabelNumberHandler() {
const recordedNumbers = {
'#fig_first': 1,
}
const countByLabelType = {
'figur': 1,
'table': 2,
}
let sut = handleLabNumber(recordedNumbers)(countByLabelType)
let got = sut('#figur_somename')
It('increments the existing entry in the label map',
countByLabelType['figur'],
2
)
It('records the url in the recorded numbers',
recordedNumbers,
{
'#fig_first': 1,
'#fig_somename': 2,
}
)
It('returns the correct number',
got,
2
)
got = sut('#figur_first')
It('returns duplicate error',
sut('#figur_first').message,
'duplicate'
)
}
function testRefNumberHandler() {
let recordedNumbers = {
'#fig_first': 1,
'#fig_second': 2,
}
let sut = handleRefNumber(recordedNumbers)
let got = sut('#fig_second')
It('returns correct number for url',
got,
2
)
It('returns error for missing url',
sut('#fig_missing').message,
'missref'
)
}
function testGetStyle() {
let settings = getDefaultSettings()
let props = getProps('lab')(settings)
let got = getStyle(props.figur)
It('returns correct style object for default',
got,
{ BOLD: false, ITALIC: false, UNDERLINE: false, FOREGROUND_COLOR: null }
)
}
function testGetCRUrls() {
let mockText = {
getText: () => 'lorem ipsum',
getTextAttributeIndices: () => [6, 10],
getLinkUrl: idx => [6, 9].includes(idx) ? '#figur_test' : null
}
const isCR = isCRUrl(5)
It('returns start and url',
getCRUrls(isCR)(mockText),
[{ start: 6, end: 9, url: '#figur_test'}]
)
mockText.getLinkUrl = idx => [6, 9].includes(idx) ? '#figur_test' : 'https://google.com'
It("doesn't get broken by surrounding links",
getCRUrls(isCR)(mockText),
[{ start: 6, end: 9, url: '#figur_test'}]
)
}
function testUpdateText() {
const results = []
let got = updateText([1, 2, 3])(cr => {results.push(cr)})
It('calls mock handler for each cr in reverse order',
results,
[3, 2, 1]
)
It('returns undefined if no error',
got,
undefined
)
got = updateText([1, 2, 3])(cr => new Error('hello'))
It('returns error if there is one',
typeof got.message,
'string'
)
}