forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute_list_test.rb
280 lines (246 loc) · 9.47 KB
/
attribute_list_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
# frozen_string_literal: true
require_relative 'test_helper'
context 'AttributeList' do
test 'collect unnamed attribute' do
attributes = {}
line = 'quote'
expected = { 1 => 'quote' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect unnamed attribute double-quoted' do
attributes = {}
line = '"quote"'
expected = { 1 => 'quote' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect empty unnamed attribute double-quoted' do
attributes = {}
line = '""'
expected = { 1 => '' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect unnamed attribute double-quoted containing escaped quote' do
attributes = {}
line = '"ba\"zaar"'
expected = { 1 => 'ba"zaar' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect unnamed attribute single-quoted' do
attributes = {}
line = '\'quote\''
expected = { 1 => 'quote' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect empty unnamed attribute single-quoted' do
attributes = {}
line = '\'\''
expected = { 1 => '' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect isolated single quote positional attribute' do
attributes = {}
line = '\''
expected = { 1 => '\'' }
doc = empty_document
def doc.apply_subs *args
raise 'apply_subs should not be called'
end
(Asciidoctor::AttributeList.new line, doc).parse_into attributes
assert_equal expected, attributes
end
test 'collect isolated single quote attribute value' do
attributes = {}
line = 'name=\''
expected = { 'name' => '\'' }
doc = empty_document
def doc.apply_subs *args
raise 'apply_subs should not be called'
end
(Asciidoctor::AttributeList.new line, doc).parse_into attributes
assert_equal expected, attributes
end
test 'collect attribute value as is if it has only leading single quote' do
attributes = {}
line = 'name=\'{val}'
expected = { 'name' => '\'{val}' }
doc = empty_document attributes: { 'val' => 'val' }
def doc.apply_subs *args
raise 'apply_subs should not be called'
end
(Asciidoctor::AttributeList.new line, doc).parse_into attributes
assert_equal expected, attributes
end
test 'collect unnamed attribute single-quoted containing escaped quote' do
attributes = {}
line = '\'ba\\\'zaar\''
expected = { 1 => 'ba\'zaar' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect unnamed attribute with dangling delimiter' do
attributes = {}
line = 'quote , '
expected = { 1 => 'quote', 2 => nil }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect unnamed attribute in second position after empty attribute' do
attributes = {}
line = ', John Smith'
expected = { 1 => nil, 2 => 'John Smith' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect unnamed attributes' do
attributes = {}
line = 'first, second one, third'
expected = { 1 => 'first', 2 => 'second one', 3 => 'third' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect blank unnamed attributes' do
attributes = {}
line = 'first,,third,'
expected = { 1 => 'first', 2 => nil, 3 => 'third', 4 => nil }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect unnamed attribute enclosed in equal signs' do
attributes = {}
line = '=foo='
expected = { 1 => '=foo=' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect named attribute' do
attributes = {}
line = 'foo=bar'
expected = { 'foo' => 'bar' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect named attribute double-quoted' do
attributes = {}
line = 'foo="bar"'
expected = { 'foo' => 'bar' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect named attribute with double-quoted empty value' do
attributes = {}
line = 'height=100,caption="",link="images/octocat.png"'
expected = { 'height' => '100', 'caption' => '', 'link' => 'images/octocat.png' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect named attribute single-quoted' do
attributes = {}
line = 'foo=\'bar\''
expected = { 'foo' => 'bar' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect named attribute with single-quoted empty value' do
attributes = {}
line = %(height=100,caption='',link='images/octocat.png')
expected = { 'height' => '100', 'caption' => '', 'link' => 'images/octocat.png' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect single named attribute with empty value' do
attributes = {}
line = 'foo='
expected = { 'foo' => '' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect single named attribute with empty value when followed by other attributes' do
attributes = {}
line = 'foo=,bar=baz'
expected = { 'foo' => '', 'bar' => 'baz' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect named attributes unquoted' do
attributes = {}
line = 'first=value, second=two, third=3'
expected = { 'first' => 'value', 'second' => 'two', 'third' => '3' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect named attributes quoted' do
attributes = {}
line = %(first='value', second="value two", third=three)
expected = { 'first' => 'value', 'second' => 'value two', 'third' => 'three' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect named attributes quoted containing non-semantic spaces' do
attributes = {}
line = %( first = 'value', second ="value two" , third= three )
expected = { 'first' => 'value', 'second' => 'value two', 'third' => 'three' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect mixed named and unnamed attributes' do
attributes = {}
line = %(first, second="value two", third=three, Sherlock Holmes)
expected = { 1 => 'first', 'second' => 'value two', 'third' => 'three', 4 => 'Sherlock Holmes' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect mixed empty named and blank unnamed attributes' do
attributes = {}
line = 'first,,third=,,fifth=five'
expected = { 1 => 'first', 2 => nil, 'third' => '', 4 => nil, 'fifth' => 'five' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect options attribute' do
attributes = {}
line = %(quote, options='opt1,,opt2 , opt3')
expected = { 1 => 'quote', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => '' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect opts attribute as options' do
attributes = {}
line = %(quote, opts='opt1,,opt2 , opt3')
expected = { 1 => 'quote', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => '' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'should ignore options attribute if empty' do
attributes = {}
line = %(quote, opts=)
expected = { 1 => 'quote' }
(Asciidoctor::AttributeList.new line).parse_into attributes
assert_equal expected, attributes
end
test 'collect and rekey unnamed attributes' do
attributes = {}
line = 'first, second one, third, fourth'
expected = { 1 => 'first', 2 => 'second one', 3 => 'third', 4 => 'fourth', 'a' => 'first', 'b' => 'second one', 'c' => 'third' }
(Asciidoctor::AttributeList.new line).parse_into attributes, %w(a b c)
assert_equal expected, attributes
end
test 'should not assign nil to attribute mapped to missing positional attribute' do
attributes = {}
line = 'alt text,,100'
expected = { 1 => 'alt text', 2 => nil, 3 => '100', 'alt' => 'alt text', 'height' => '100' }
(Asciidoctor::AttributeList.new line).parse_into attributes, %w(alt width height)
assert_equal expected, attributes
end
test 'rekey positional attributes' do
attributes = { 1 => 'source', 2 => 'java' }
expected = { 1 => 'source', 2 => 'java', 'style' => 'source', 'language' => 'java' }
Asciidoctor::AttributeList.rekey attributes, %w(style language linenums)
assert_equal expected, attributes
end
end