forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_test.rb
307 lines (250 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
305
306
307
# encoding: UTF-8
unless defined? ASCIIDOCTOR_PROJECT_DIR
$: << File.dirname(__FILE__); $:.uniq!
require 'test_helper'
end
context "Text" do
test "proper encoding to handle utf8 characters in document using html backend" do
output = example_document(:encoding).render
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, :header_footer => false).render
assert_xpath '//p', output, 4
assert_xpath '//a', output, 1
end
test "proper encoding to handle utf8 characters in document using docbook45 backend" do
output = example_document(:encoding, :attributes => {'backend' => 'docbook45', 'xmlns' => ''}).render
assert_xpath '//xmlns:simpara', output, 4
assert_xpath '//xmlns:ulink', output, 1
end
test "proper encoding to handle utf8 characters in embedded document using docbook45 backend" do
output = example_document(:encoding, :header_footer => false, :attributes => {'backend' => 'docbook45'}).render
assert_xpath '//simpara', output, 4
assert_xpath '//ulink', 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.concat(File.readlines(sample_doc_path(:encoding)))
doc = empty_document
reader = Asciidoctor::PreprocessorReader.new doc, input
block = Asciidoctor::Parser.next_block(reader, doc)
assert_xpath '//pre', block.render.gsub(/^\s*\n/, ''), 1
end
test 'proper encoding to handle utf8 characters from included file' do
input = <<-EOS
include::fixtures/encoding.asciidoc[tags=romé]
EOS
doc = empty_safe_document :base_dir => File.expand_path(File.dirname(__FILE__))
reader = Asciidoctor::PreprocessorReader.new doc, input
block = Asciidoctor::Parser.next_block(reader, doc)
output = block.render
assert_css '.paragraph', output, 1
end
test 'escaped text markup' do
assert_match(/All your <em>inline<\/em> markup belongs to <strong>us<\/strong>!/,
render_string('All your <em>inline</em> markup belongs to <strong>us</strong>!'))
end
test "line breaks" do
assert_xpath "//br", render_string("Well this is +\njust fine and dandy, isn't it?"), 1
end
test 'single- and double-quoted text' do
rendered = render_embedded_string(%q(``Where?,'' she said, flipping through her copy of `The New Yorker.'), :attributes => {'compat-mode' => ''})
assert_match(/“Where\?,”/, rendered)
assert_match(/‘The New Yorker.’/, rendered)
rendered = render_embedded_string(%q("`Where?,`" she said, flipping through her copy of '`The New Yorker.`'))
assert_match(/“Where\?,”/, rendered)
assert_match(/‘The New Yorker.’/, rendered)
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.”',
render_embedded_string(%q(``Our business is constantly changing'' or ``We need faster time to market.''), :doctype => :inline, :attributes => {'compat-mode' => ''})
assert_equal '“Our business is constantly changing” or “We need faster time to market.”',
render_embedded_string(%q("`Our business is constantly changing`" or "`We need faster time to market.`"), :doctype => :inline)
end
test 'horizontal rule' do
input = <<-EOS
This line is separated by a horizontal rule...
'''
...from this line.
EOS
output = render_embedded_string 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 = render_embedded_string 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 = render_embedded_string 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 = render_embedded_string input
assert_xpath '//hr', output, 0
end
end
end
test "emphasized text using underscore characters" do
assert_xpath "//em", render_string("An _emphatic_ no")
end
test 'emphasized text with single quote using apostrophe characters' do
rsquo = [8217].pack 'U*'
assert_xpath %(//em[text()="Johnny#{rsquo}s"]), render_string(%q(It's 'Johnny's' phone), :attributes => {'compat-mode' => ''})
assert_xpath %(//p[text()="It#{rsquo}s 'Johnny#{rsquo}s' phone"]), render_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"]), render_string(%q(It's 'Johnny\\'s' phone), :attributes => {'compat-mode' => ''})
assert_xpath %(//p[text()="It's 'Johnny's' phone"]), render_string(%q(It\\'s 'Johnny\\'s' phone))
end
test "escaped single quote is restored as single quote" do
assert_xpath "//p[contains(text(), \"Let's do it!\")]", render_string("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"]), render_embedded_string(%(A \\'single quoted string' example), :attributes => {'compat-mode' => ''})
assert_xpath %(//p[text()="'single quoted string'"]), render_embedded_string(%(\\'single quoted string'), :attributes => {'compat-mode' => ''})
assert_xpath %(//p[text()="A \\'single quoted string' example"]), render_embedded_string(%(A \\'single quoted string' example))
assert_xpath %(//p[text()="\\'single quoted string'"]), render_embedded_string(%(\\'single quoted string'))
end
test "emphasized text at end of line" do
assert_xpath "//em", render_string("This library is _awesome_")
end
test "emphasized text at beginning of line" do
assert_xpath "//em", render_string("_drop_ it")
end
test "emphasized text across line" do
assert_xpath "//em", render_string("_check it_")
end
test "unquoted text" do
refute_match(/#/, render_string("An #unquoted# word"))
end
test 'backticks and straight quotes in text' do
backslash = '\\'
assert_equal %q(run <code>foo</code> <em>dog</em>), render_embedded_string(%q(run `foo` 'dog'), :doctype => :inline, :attributes => {'compat-mode' => ''})
assert_equal %q(run <code>foo</code> 'dog'), render_embedded_string(%q(run `foo` 'dog'), :doctype => :inline)
assert_equal %q(run `foo` 'dog'), render_embedded_string(%(run #{backslash}`foo` 'dog'), :doctype => :inline)
assert_equal %q(run ‘foo` 'dog’), render_embedded_string(%q(run '`foo` 'dog`'), :doctype => :inline)
assert_equal %q(run '`foo` 'dog`'), render_embedded_string(%(run #{backslash}'`foo` 'dog#{backslash}`'), :doctype => :inline)
end
test 'plus characters inside single plus passthrough' do
assert_xpath '//p[text()="+"]', render_embedded_string('+++')
assert_xpath '//p[text()="+="]', render_embedded_string('++=+')
end
test 'plus passthrough escapes entity reference' do
assert_match(/&#44;/, render_embedded_string('+,+'))
assert_match(/one&#44;two/, render_embedded_string('one++,++two'))
end
context "basic styling" do
setup do
@rendered = render_string("A *BOLD* word. An _italic_ word. A `mono` word. ^superscript!^ and some ~subscript~.")
end
test "strong" do
assert_xpath "//strong", @rendered, 1
end
test "italic" do
assert_xpath "//em", @rendered, 1
end
test "monospaced" do
assert_xpath "//code", @rendered, 1
end
test "superscript" do
assert_xpath "//sup", @rendered, 1
end
test "subscript" do
assert_xpath "//sub", @rendered, 1
end
test "passthrough" do
assert_xpath "//code", render_string("This is +passed through+."), 0
assert_xpath "//code", render_string("This is +passed through and monospaced+.", :attributes => {'compat-mode' => ''}), 1
end
test "nested styles" do
rendered = render_string("Winning *big _time_* in the +city *boyeeee*+.", :attributes => {'compat-mode' => ''})
assert_xpath "//strong/em", rendered
assert_xpath "//code/strong", rendered
rendered = render_string("Winning *big _time_* in the `city *boyeeee*`.")
assert_xpath "//strong/em", rendered
assert_xpath "//code/strong", rendered
end
test "unconstrained quotes" do
rendered_chars = render_string("**B**__I__++M++", :attributes => {'compat-mode' => ''})
assert_xpath "//strong", rendered_chars
assert_xpath "//em", rendered_chars
assert_xpath "//code", rendered_chars
rendered_chars = render_string("**B**__I__``M``")
assert_xpath "//strong", rendered_chars
assert_xpath "//em", rendered_chars
assert_xpath "//code", rendered_chars
end
end
test 'should format Asian characters as words' do
assert_xpath '//strong', (render_embedded_string 'bold *要* bold')
assert_xpath '//strong', (render_embedded_string 'bold *素* bold')
assert_xpath '//strong', (render_embedded_string 'bold *要素* bold')
end
end