forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_test.rb
304 lines (247 loc) · 10.5 KB
/
text_test.rb
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# frozen_string_literal: true
require_relative 'test_helper'
context 'Text' do
test 'proper encoding to handle utf8 characters in document using html backend' do
output = example_document(:encoding).convert
assert_xpath '//p', output, 4
assert_xpath '//a', output, 1
end
test 'proper encoding to handle utf8 characters in embedded document using html backend' do
output = example_document(:encoding, standalone: false).convert
assert_xpath '//p', output, 4
assert_xpath '//a', output, 1
end
test 'proper encoding to handle utf8 characters in document using docbook backend' do
output = example_document(:encoding, attributes: { 'backend' => 'docbook', 'xmlns' => '' }).convert
assert_xpath '//xmlns:simpara', output, 4
assert_xpath '//xmlns:link', output, 1
end
test 'proper encoding to handle utf8 characters in embedded document using docbook backend' do
output = example_document(:encoding, standalone: false, attributes: { 'backend' => 'docbook' }).convert
assert_xpath '//simpara', output, 4
assert_xpath '//link', output, 1
end
# NOTE this test ensures we have the encoding line on block templates too
test 'proper encoding to handle utf8 characters in arbitrary block' do
input = []
input << %([verse]\n)
input += (File.readlines (sample_doc_path :encoding), mode: Asciidoctor::FILE_READ_MODE)
doc = empty_document
reader = Asciidoctor::PreprocessorReader.new doc, input, nil, normalize: true
block = Asciidoctor::Parser.next_block reader, doc
assert_xpath '//pre', block.convert.gsub(/^\s*\n/, ''), 1
end
test 'proper encoding to handle utf8 characters from included file' do
input = 'include::fixtures/encoding.adoc[tags=romé]'
doc = empty_safe_document base_dir: testdir
reader = Asciidoctor::PreprocessorReader.new doc, input, nil, normalize: true
block = Asciidoctor::Parser.next_block reader, doc
output = block.convert
assert_css '.paragraph', output, 1
end
test 'escaped text markup' do
assert_match(/All your <em>inline<\/em> markup belongs to <strong>us<\/strong>!/,
convert_string('All your <em>inline</em> markup belongs to <strong>us</strong>!'))
end
test 'line breaks' do
assert_xpath '//br', convert_string("Well this is +\njust fine and dandy, isn't it?"), 1
end
test 'single- and double-quoted text' do
output = convert_string_to_embedded %q(``Where?,'' she said, flipping through her copy of `The New Yorker.'), attributes: { 'compat-mode' => '' }
assert_match(/“Where\?,”/, output)
assert_match(/‘The New Yorker.’/, output)
output = convert_string_to_embedded %q("`Where?,`" she said, flipping through her copy of '`The New Yorker.`')
assert_match(/“Where\?,”/, output)
assert_match(/‘The New Yorker.’/, output)
end
test 'multiple double-quoted text on a single line' do
assert_equal '“Our business is constantly changing” or “We need faster time to market.”',
convert_inline_string(%q(``Our business is constantly changing'' or ``We need faster time to market.''), attributes: { 'compat-mode' => '' })
assert_equal '“Our business is constantly changing” or “We need faster time to market.”',
convert_inline_string('"`Our business is constantly changing`" or "`We need faster time to market.`"')
end
test 'horizontal rule' do
input = <<~'EOS'
This line is separated by a horizontal rule...
'''
...from this line.
EOS
output = convert_string_to_embedded input
assert_xpath '//hr', output, 1
assert_xpath '/*[@class="paragraph"]', output, 2
assert_xpath '(/*[@class="paragraph"])[1]/following-sibling::hr', output, 1
assert_xpath '/hr/following-sibling::*[@class="paragraph"]', output, 1
end
test 'markdown horizontal rules' do
variants = [
'---',
'- - -',
'***',
'* * *',
'___',
'_ _ _',
]
offsets = [
'',
' ',
' ',
' ',
]
variants.each do |variant|
offsets.each do |offset|
input = <<~EOS
This line is separated by a horizontal rule...
#{offset}#{variant}
...from this line.
EOS
output = convert_string_to_embedded input
assert_xpath '//hr', output, 1
assert_xpath '/*[@class="paragraph"]', output, 2
assert_xpath '(/*[@class="paragraph"])[1]/following-sibling::hr', output, 1
assert_xpath '/hr/following-sibling::*[@class="paragraph"]', output, 1
end
end
end
test 'markdown horizontal rules negative case' do
bad_variants = [
'- - - -',
'* * * *',
'_ _ _ _',
]
good_offsets = [
'',
' ',
' ',
' ',
]
bad_variants.each do |variant|
good_offsets.each do |offset|
input = <<~EOS
This line is separated by something that is not a horizontal rule...
#{offset}#{variant}
...from this line.
EOS
output = convert_string_to_embedded input
assert_xpath '//hr', output, 0
end
end
good_variants = [
'- - -',
'* * *',
'_ _ _',
]
bad_offsets = [
?\t,
' ',
]
good_variants.each do |variant|
bad_offsets.each do |offset|
input = <<~EOS
This line is separated by something that is not a horizontal rule...
#{offset}#{variant}
...from this line.
EOS
output = convert_string_to_embedded input
assert_xpath '//hr', output, 0
end
end
end
test 'emphasized text using underscore characters' do
assert_xpath '//em', convert_string('An _emphatic_ no')
end
test 'emphasized text with single quote using apostrophe characters' do
rsquo = decode_char 8217
assert_xpath %(//em[text()="Johnny#{rsquo}s"]), convert_string(%q(It's 'Johnny's' phone), attributes: { 'compat-mode' => '' })
assert_xpath %(//p[text()="It#{rsquo}s 'Johnny#{rsquo}s' phone"]), convert_string(%q(It's 'Johnny's' phone))
end
test 'emphasized text with escaped single quote using apostrophe characters' do
assert_xpath %(//em[text()="Johnny's"]), convert_string(%q(It's 'Johnny\\'s' phone), attributes: { 'compat-mode' => '' })
assert_xpath %(//p[text()="It's 'Johnny's' phone"]), convert_string(%q(It\\'s 'Johnny\\'s' phone))
end
test 'escaped single quote is restored as single quote' do
assert_xpath %q(//p[contains(text(), "Let's do it!")]), convert_string(%q(Let\\'s do it!))
end
test 'unescape escaped single quote emphasis in compat mode only' do
assert_xpath %(//p[text()="A 'single quoted string' example"]), convert_string_to_embedded(%(A \\'single quoted string' example), attributes: { 'compat-mode' => '' })
assert_xpath %(//p[text()="'single quoted string'"]), convert_string_to_embedded(%(\\'single quoted string'), attributes: { 'compat-mode' => '' })
assert_xpath %(//p[text()="A \\'single quoted string' example"]), convert_string_to_embedded(%(A \\'single quoted string' example))
assert_xpath %(//p[text()="\\'single quoted string'"]), convert_string_to_embedded(%(\\'single quoted string'))
end
test 'emphasized text at end of line' do
assert_xpath '//em', convert_string('This library is _awesome_')
end
test 'emphasized text at beginning of line' do
assert_xpath '//em', convert_string('_drop_ it')
end
test 'emphasized text across line' do
assert_xpath '//em', convert_string('_check it_')
end
test 'unquoted text' do
refute_include '#', convert_string('An #unquoted# word')
end
test 'backticks and straight quotes in text' do
backslash = '\\'
assert_equal %q(run <code>foo</code> <em>dog</em>), convert_inline_string(%q(run `foo` 'dog'), attributes: { 'compat-mode' => '' })
assert_equal %q(run <code>foo</code> 'dog'), convert_inline_string(%q(run `foo` 'dog'))
assert_equal %q(run `foo` 'dog'), convert_inline_string(%(run #{backslash}`foo` 'dog'))
assert_equal %q(run ‘foo` 'dog’), convert_inline_string(%q(run '`foo` 'dog`'))
assert_equal %q(run '`foo` 'dog`'), convert_inline_string(%(run #{backslash}'`foo` 'dog#{backslash}`'))
end
test 'plus characters inside single plus passthrough' do
assert_xpath '//p[text()="+"]', convert_string_to_embedded('+++')
assert_xpath '//p[text()="+="]', convert_string_to_embedded('++=+')
end
test 'plus passthrough escapes entity reference' do
assert_match(/&#44;/, convert_string_to_embedded('+,+'))
assert_match(/one&#44;two/, convert_string_to_embedded('one++,++two'))
end
context 'basic styling' do
setup do
@output = convert_string 'A *BOLD* word. An _italic_ word. A `mono` word. ^superscript!^ and some ~subscript~.'
end
test 'strong' do
assert_xpath '//strong', @output, 1
end
test 'italic' do
assert_xpath '//em', @output, 1
end
test 'monospaced' do
assert_xpath '//code', @output, 1
end
test 'superscript' do
assert_xpath '//sup', @output, 1
end
test 'subscript' do
assert_xpath '//sub', @output, 1
end
test 'passthrough' do
assert_xpath '//code', convert_string('This is +passed through+.'), 0
assert_xpath '//code', convert_string('This is +passed through and monospaced+.', attributes: { 'compat-mode' => '' }), 1
end
test 'nested styles' do
output = convert_string 'Winning *big _time_* in the +city *boyeeee*+.', attributes: { 'compat-mode' => '' }
assert_xpath '//strong/em', output
assert_xpath '//code/strong', output
output = convert_string 'Winning *big _time_* in the `city *boyeeee*`.'
assert_xpath '//strong/em', output
assert_xpath '//code/strong', output
end
test 'unconstrained quotes' do
output = convert_string '**B**__I__++M++[role]++M++', attributes: { 'compat-mode' => '' }
assert_xpath '//strong', output, 1
assert_xpath '//em', output, 1
assert_xpath '//code[not(@class)]', output, 1
assert_xpath '//code[@class="role"]', output, 1
output = convert_string '**B**__I__``M``[role]``M``'
assert_xpath '//strong', output, 1
assert_xpath '//em', output, 1
assert_xpath '//code[not(@class)]', output, 1
assert_xpath '//code[@class="role"]', output, 1
end
end
test 'should format Asian characters as words' do
assert_xpath '//strong', (convert_string_to_embedded 'bold *要* bold')
assert_xpath '//strong', (convert_string_to_embedded 'bold *素* bold')
assert_xpath '//strong', (convert_string_to_embedded 'bold *要素* bold')
end
end