-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathtest_index.rb
289 lines (257 loc) · 6.5 KB
/
test_index.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
# frozen_string_literal: true
require 'test_helper'
require 'review/compiler'
require 'review/book'
require 'review/book/index'
require 'review/topbuilder'
require 'review/i18n'
class IndexTest < Test::Unit::TestCase
include ReVIEW
def setup
@builder = TOPBuilder.new
@config = ReVIEW::Configure.create(config: { 'secnolevel' => 2, 'language' => 'ja' })
@book = Book::Base.new(config: @config)
@log_io = StringIO.new
ReVIEW.logger = ReVIEW::Logger.new(@log_io)
@compiler = ReVIEW::Compiler.new(@builder)
@chapter = Book::Chapter.new(@book, 1, '-', nil, StringIO.new)
location = Location.new(nil, nil)
@builder.bind(@compiler, @chapter, location)
I18n.setup(@config['language'])
end
def test_footnote_index
compile_block("@<fn>{foo}\n//footnote[foo][bar]\n")
fn = @chapter.footnote_index
items = fn.to_a
item = items[0]
assert_equal 'foo', item.id
assert_equal 'bar', item.content
compile_block("//footnote[foo][bar]\n")
assert_match(/ID foo is not referred/, @log_io.string)
end
def test_footnote_index_with_escape
compile_block("@<fn>{foo}\n" + '//footnote[foo][bar[\]buz]' + "\n")
fn = @chapter.footnote_index
items = fn.to_a
item = items[0]
assert_equal 'foo', item.id
assert_equal 'bar[]buz', item.content
end
def test_footnote_index_with_escape2
compile_block("@<fn>{foo}\n" + '//footnote[foo][bar\\a\\$buz]' + "\n")
fn = @chapter.footnote_index
items = fn.to_a
item = items[0]
assert_equal 'foo', item.id
assert_equal 'bar\\a\\$buz', item.content
end
def test_footnote_index_key?
compile_block("@<fn>{foo}\n" + '//footnote[foo][bar]' + "\n")
fn = @chapter.footnote_index
assert_equal true, fn.key?('foo')
## for compatibility
# rubocop:disable Style/PreferredHashMethods
assert_equal true, fn.has_key?('foo')
# rubocop:enable Style/PreferredHashMethods
end
def test_endnote_index
compile_block("@<endnote>{foo}\n//endnote[foo][bar]\n//printendnotes\n")
endnote = @chapter.endnote_index
items = endnote.to_a
item = items[0]
assert_equal 'foo', item.id
assert_equal 'bar', item.content
assert_equal true, endnote.key?('foo')
# rubocop:disable Style/PreferredHashMethods
assert_equal true, endnote.has_key?('foo')
# rubocop:enable Style/PreferredHashMethods
e = assert_raises(ReVIEW::ApplicationError) do
compile_block("@<endnote>{foo}\n//endnote[foo][bar]\n")
end
assert_equal ':3: //endnote is found but //printendnotes is not found.', e.message
compile_block("//endnote[foo][bar]\n//printendnotes\n")
assert_match(/ID foo is not referred/, @log_io.string)
end
def test_headline_index
src = <<-EOB
= chap1
== sec1-1
== sec1-2
=== sec1-2-1
===[column] column1
==== inside_column
===[/column]
===[column] column2
=== sec1-2-2
== sec1-3
==== sec1-3-0-1
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [2, 2], index['sec1-2|sec1-2-2'].number
assert_equal '1.2.2', index.number('sec1-2|sec1-2-2')
end
def test_headline_index2
src = <<-EOB
= chap1
== sec1-1
== sec1-2
=== sec1-2-1
===[column] column1
== sec1-3
=== sec1-3-1
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [3, 1], index['sec1-3|sec1-3-1'].number
assert_equal '1.3.1', index.number('sec1-3|sec1-3-1')
end
def test_headline_index3
src = <<-EOB
= chap1
== sec1-1
== sec1-2
=== sec1-2-1
===[column] column1
=== sec1-2-2
== sec1-3
=== sec1-3-1
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [2, 2], index['sec1-2|sec1-2-2'].number
assert_equal '1.2.2', index.number('sec1-2|sec1-2-2')
assert_equal [3, 1], index['sec1-3|sec1-3-1'].number
assert_equal '1.3.1', index.number('sec1-3|sec1-3-1')
end
def test_headline_index4
src = <<-EOB
= chap1
====[column] c1
== sec1-1
== sec1-2
=== sec1-2-1
=== sec1-2-2
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [2, 2], index['sec1-2|sec1-2-2'].number
assert_equal '1.2.2', index.number('sec1-2|sec1-2-2')
end
def test_headline_index5
src = <<-EOB
= chap1
====[column] c1
== sec1-1
== sec1-2
=== sec1-2-1
=== sec1-2-2
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [2, 2], index['sec1-2-2'].number
assert_equal '1.2.2', index.number('sec1-2-2')
end
def test_headline_index6
src = <<-EOB
= chap1
== sec1
=== target
== sec2
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [1, 1], index['target'].number
assert_equal '1.1.1', index.number('target')
end
def test_headline_index7
src = <<-EOB
= chap1
== sec1
=== target
^-- dummy target
== sec2
=== target
^-- real target but it cannot be detected, because there is another one.
EOB
compile_block(src)
index = @chapter.headline_index
assert_raise ReVIEW::KeyError do
assert_equal [1, 1], index['target'].number
end
end
def test_headline_index8
src = <<-EOB
= chap1
== sec1
=== sec1-1
==== sec1-1-1
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal '1.1.1', index.number('sec1-1')
end
def test_headline_index9
src = <<-EOB
= chap1
== sec1
=== sec1-1
===[column] column1
===[/column]
==== sec1-1-1
=== sec1-2
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [1, 1, 1], index['sec1-1-1'].number
end
def test_headline_index10
src = <<-EOB
= chap1
== sec1
=== sec1-1
====[column] column1
==== sec1-1-1
=== sec1-2
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [1, 1, 1], index['sec1-1-1'].number
end
def test_headline_index11
src = <<-EOB
= chap1
==[nodisp] sec01
==[notoc] sec02
== sec1
===[nodisp] sec1-0
=== sec1-1
==[nonum] sec03
== sec04
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal nil, index['sec01'].number
assert_equal nil, index['sec02'].number
assert_equal [1], index['sec1'].number
assert_equal nil, index['sec1-0'].number
assert_equal [1, 1], index['sec1-1'].number
assert_equal nil, index['sec03'].number
assert_equal [2], index['sec04'].number
end
def test_headline_index12
src = <<-EOB
= chap1
== A
=== A2
==[nonum] B
=== B2
EOB
compile_block(src)
index = @chapter.headline_index
assert_equal [1], index['A'].number
assert_equal [1, 1], index['A2'].number
assert_equal nil, index['B'].number
assert_equal [1, 2], index['B2'].number
end
end