-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathtest_markdownbuilder.rb
395 lines (327 loc) · 8.5 KB
/
test_markdownbuilder.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# frozen_string_literal: true
require 'test_helper'
require 'review/compiler'
require 'review/book'
require 'review/markdownbuilder'
require 'review/i18n'
class MARKDOWNBuilderTest < Test::Unit::TestCase
include ReVIEW
def setup
@builder = MARKDOWNBuilder.new
@config = ReVIEW::Configure.values
@book = Book::Base.new('.')
@book.config = @config
@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_quote
actual = compile_block("//quote{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
> foobar
>
> buz
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//quote{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
> foo bar
>
> buz
EOS
assert_equal expected, actual
end
def test_memo
actual = compile_block("//memo[this is @<b>{test}<&>_]{\ntest1\n\ntest@<i>{2}\n//}\n")
expected = <<-EOS
<div class="memo">
<p class="caption">this is **test**<&>_</p>
test1
test*2*
</div>
EOS
assert_equal expected, actual
end
def test_noindent
actual = compile_block("//noindent\nfoo\nbar\n\nfoo2\nbar2\n")
expected = <<-EOS
<p class="noindent">foobar</p>
foo2bar2
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//noindent\nfoo\nbar\n\nfoo2\nbar2\n")
expected = <<-EOS
<p class="noindent">foo bar</p>
foo2 bar2
EOS
assert_equal expected, actual
end
def test_inline_em
assert_equal 'test*foo*abc', compile_inline('test@<em>{foo}abc')
end
def test_inline_strong
assert_equal 'test**foo**abc', compile_inline('test@<strong>{foo}abc')
end
def test_ul
src = <<-EOS
* AAA
* BBB
EOS
expected = "\n* AAA\n* BBB\n\n"
actual = compile_block(src)
assert_equal expected, actual
end
def test_inline_comment
actual = compile_inline('test @<comment>{コメント} test2')
assert_equal 'test test2', actual
end
def test_inline_comment_for_draft
@config['draft'] = true
actual = compile_inline('test @<comment>{コメント} test2')
assert_equal %Q(test <span class="red">コメント</span> test2), actual
end
def test_endnote
e = assert_raises(ReVIEW::ApplicationError) { compile_block("//endnote[foo][bar]\n\n@<endnote>{foo}\n") }
assert_equal ':4: //endnote is found but //printendnotes is not found.', e.message
actual = compile_block("@<endnote>{foo}\n//endnote[foo][bar]\n//printendnotes\n")
expected = <<-EOS
<sup>(1)</sup>
(1) bar
EOS
assert_equal expected, actual
end
def test_inline_hd_chap
def @chapter.headline_index
item = Book::Index::Item.new('chap1|test', [1, 1], 'te_st')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end
@config['secnolevel'] = 2
actual = compile_inline('test @<hd>{chap1|test} test2')
assert_equal 'test <a href="#h1-1-1">「te_st」</a> test2', actual
actual = compile_inline('test @<hd>{test} test2')
assert_equal 'test <a href="#h1-1-1">「te_st」</a> test2', actual
@config['secnolevel'] = 3
actual = compile_inline('test @<hd>{chap1|test} test2')
assert_equal 'test <a href="#h1-1-1">「1.1.1 te_st」</a> test2', actual
@config['chapterlink'] = nil
@config['secnolevel'] = 2
actual = compile_inline('test @<hd>{chap1|test} test2')
assert_equal 'test 「te_st」 test2', actual
@config['secnolevel'] = 3
actual = compile_inline('test @<hd>{chap1|test} test2')
assert_equal 'test 「1.1.1 te_st」 test2', actual
end
def test_inline_sec
def @chapter.headline_index
item = Book::Index::Item.new('chap1|test', [1, 1], 'te_st<>')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end
@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test <a href="#h1-1-1">「1.1.1 te_st<>」</a>', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st<>', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test 1.1.1', actual
@config['secnolevel'] = 2
actual = compile_inline('test @<secref>{test}')
assert_equal 'test <a href="#h1-1-1">「te_st<>」</a>', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st<>', actual
assert_raises(ReVIEW::ApplicationError) { compile_block('test @<sec>{test}') }
@config['chapterlink'] = nil
@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test 「1.1.1 te_st<>」', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st<>', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test 1.1.1', actual
end
def test_ul_nest1
src = <<-EOS
* AAA
** AA
*** A
EOS
expected = "\n* AAA\n * AA\n * A\n\n"
actual = compile_block(src)
assert_equal expected, actual
end
def test_cmd
actual = compile_block("//cmd{\nlineA\nlineB\n//}\n")
expected = <<-EOS
```shell-session
lineA
lineB
```
EOS
assert_equal expected, actual
end
def test_dlist
actual = compile_block(" : foo\n foo.\n bar.\n")
expected = <<-EOS
<dl>
<dt>foo</dt>
<dd>foo.bar.</dd>
</dl>
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block(" : foo\n foo.\n bar.\n")
expected = <<-EOS
<dl>
<dt>foo</dt>
<dd>foo. bar.</dd>
</dl>
EOS
assert_equal expected, actual
end
def test_dlist_with_bracket
actual = compile_block(" : foo[bar]\n foo.\n bar.\n")
expected = <<-EOS
<dl>
<dt>foo[bar]</dt>
<dd>foo.bar.</dd>
</dl>
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block(" : foo[bar]\n foo.\n bar.\n")
expected = <<-EOS
<dl>
<dt>foo[bar]</dt>
<dd>foo. bar.</dd>
</dl>
EOS
assert_equal expected, actual
end
def test_dlist_with_comment
source = " : title\n body\n\#@ comment\n\#@ comment\n: title2\n body2\n"
actual = compile_block(source)
expected = <<-EOS
<dl>
<dt>title</dt>
<dd>body</dd>
<dt>title2</dt>
<dd>body2</dd>
</dl>
EOS
assert_equal expected, actual
end
def test_comment
actual = compile_block('//comment[コメント]')
assert_equal '', actual
end
def test_comment_for_draft
@config['draft'] = true
actual = compile_block('//comment[コメント]')
assert_equal %Q(<div class="red">コメント</div>\n), actual
end
def test_list
actual = compile_block(<<-EOS)
//list[name][caption]{
AAA
BBB
//}
EOS
assert_equal <<-EOS, actual
リスト1.1 caption
```
AAA
BBB
```
EOS
end
def test_list_lang
actual = compile_block(<<-EOS)
//list[name][caption][ruby]{
AAA
BBB
//}
EOS
assert_equal <<-EOS, actual
リスト1.1 caption
```ruby
AAA
BBB
```
EOS
end
def test_listnum
def @chapter.list(_id)
Book::Index::Item.new('test', 1)
end
actual = compile_block("//listnum[test][this is @<b>{test}<&>_]{\nfoo\nbar\n\tbuz\n//}\n")
expected = <<-EOS
リスト1.1 this is **test**<&>_
```
1: foo
2: bar
3: buz
```
EOS
assert_equal expected, actual
end
def test_emlist_lang
actual = compile_block(<<-EOS)
//emlist[caption][ruby]{
AAA
BBB
//}
EOS
assert_equal <<-EOS, actual
caption
```ruby
AAA
BBB
```
EOS
end
def test_table
actual = compile_block("//table{\ntestA\ttestB\n------------\ncontentA\tcontentB\n//}\n")
expected = <<-EOS
|testA|testB|
|:--|:--|
|contentA|contentB|
EOS
assert_equal expected, actual
actual = compile_block("//table[foo][FOO]{\ntestA\ttestB\n------------\ncontentA\tcontentB\n//}\n")
expected = <<-EOS
表1.1: FOO
|testA|testB|
|:--|:--|
|contentA|contentB|
EOS
assert_equal expected, actual
end
def test_ruby
actual = compile_block('@<ruby>{謳,うた}い文句')
assert_equal "<ruby>謳<rp>(</rp><rt>うた</rt><rp>)</rp></ruby>い文句\n\n", actual
end
def test_graph_mermaid
def @chapter.image(_id)
item = Book::Index::Item.new('id', 1, 'id')
item.instance_eval { @path = './images/markdown/id.svg' }
item
end
begin
require 'playwrightrunner'
rescue LoadError
return true
end
actual = compile_block("//graph[id][mermaid][foo]{\ngraph LR; B --> C\n//}")
expected = <<-EOS

EOS
assert_equal expected, actual
end
end