forked from kmuto/review
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_htmlbuilder.rb
More file actions
560 lines (462 loc) · 15.8 KB
/
Copy pathtest_htmlbuilder.rb
File metadata and controls
560 lines (462 loc) · 15.8 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# encoding: utf-8
require 'test_helper'
require 'review/compiler'
require 'review/book'
require 'review/htmlbuilder'
class HTMLBuidlerTest < Test::Unit::TestCase
include ReVIEW
def setup
@builder = HTMLBuilder.new()
@param = {
"secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder
"inencoding" => "UTF-8",
"outencoding" => "UTF-8",
"subdirmode" => nil,
"stylesheet" => nil, # for HTMLBuilder
}
ReVIEW.book.param = @param
@compiler = ReVIEW::Compiler.new(@builder)
@chapter = Book::Chapter.new(nil, 1, '-', nil, StringIO.new)
location = Location.new(nil, nil)
@builder.bind(@compiler, @chapter, location)
end
def test_headline_level1
@builder.headline(1,"test","this is test.")
assert_equal %Q|<h1 id="test"><a id="h1" />第1章 this is test.</h1>\n|, @builder.raw_result
end
def test_headline_level1_without_secno
@param["secnolevel"] = 0
@builder.headline(1,"test","this is test.")
assert_equal %Q|<h1 id="test"><a id="h1" />this is test.</h1>\n|, @builder.raw_result
end
def test_headline_level1_with_inlinetag
@builder.headline(1,"test","this @<b>{is} test.<&\">")
assert_equal %Q|<h1 id="test"><a id="h1" />第1章 this <b>is</b> test.<&"></h1>\n|, @builder.raw_result
end
def test_headline_level2
@builder.headline(2,"test","this is test.")
assert_equal %Q|\n<h2 id="test"><a id="h1-1" />1.1 this is test.</h2>\n|, @builder.raw_result
end
def test_headline_level3
@builder.headline(3,"test","this is test.")
assert_equal %Q|\n<h3 id="test"><a id="h1-0-1" />this is test.</h3>\n|, @builder.raw_result
end
def test_headline_level3_with_secno
@param["secnolevel"] = 3
@builder.headline(3,"test","this is test.")
assert_equal %Q|\n<h3 id="test"><a id="h1-0-1" />1.0.1 this is test.</h3>\n|, @builder.raw_result
end
def test_label
@builder.label("label_test")
assert_equal %Q|<a id="label_test" />\n|, @builder.raw_result
end
def test_href
ret = @builder.compile_href("http://github.com", "GitHub")
assert_equal %Q|<a href="http://github.com" class="link">GitHub</a>|, ret
end
def test_href_without_label
ret = @builder.compile_href("http://github.com",nil)
assert_equal %Q|<a href="http://github.com" class="link">http://github.com</a>|, ret
end
def test_inline_href
ret = @builder.inline_href("http://github.com, Git\\,Hub")
assert_equal %Q|<a href="http://github.com" class="link">Git,Hub</a>|, ret
end
def test_inline_href_without_label
ret = @builder.inline_href("http://github.com")
assert_equal %Q|<a href="http://github.com" class="link">http://github.com</a>|, ret
end
def test_inline_raw
ret = @builder.inline_raw("@<tt>{inline}")
assert_equal %Q|@<tt>{inline}|, ret
end
def test_inline_in_table
ret = @builder.table(["<b>1</b>\t<i>2</i>", "------------", "<b>3</b>\t<i>4</i><>&"])
assert_equal %Q|<div class="table">\n<table>\n<tr><th><b>1</b></th><th><i>2</i></th></tr>\n<tr><td><b>3</b></td><td><i>4</i><>&</td></tr>\n</table>\n</div>\n|, @builder.raw_result
end
def test_inline_br
ret = @builder.inline_br("")
assert_equal %Q|<br />|, ret
end
def test_inline_i
ret = @builder.compile_inline("test @<i>{inline test} test2")
assert_equal %Q|test <i>inline test</i> test2|, ret
end
def test_inline_i_and_escape
ret = @builder.compile_inline("test @<i>{inline<&;\\ test} test2")
assert_equal %Q|test <i>inline<&;\\ test</i> test2|, ret
end
def test_inline_b
ret = @builder.compile_inline("test @<b>{inline test} test2")
assert_equal %Q|test <b>inline test</b> test2|, ret
end
def test_inline_b_and_escape
ret = @builder.compile_inline("test @<b>{inline<&;\\ test} test2")
assert_equal %Q|test <b>inline<&;\\ test</b> test2|, ret
end
def test_inline_tt
ret = @builder.compile_inline("test @<tt>{inline test} test2")
assert_equal %Q|test <tt>inline test</tt> test2|, ret
end
def test_inline_tti
ret = @builder.compile_inline("test @<tti>{inline test} test2")
assert_equal %Q|test <tt><i>inline test</i></tt> test2|, ret
end
def test_inline_ttb
ret = @builder.compile_inline("test @<ttb>{inline test} test2")
assert_equal %Q|test <tt><b>inline test</b></tt> test2|, ret
end
def test_inline_hd_chap
def @chapter.headline_index
items = [Book::HeadlineIndex::Item.new("chap1|test", [1, 1], "te_st")]
Book::HeadlineIndex.new(items, self)
end
ret = @builder.compile_inline("test @<hd>{chap1|test} test2")
assert_equal %Q|test 「1.1.1 te_st」 test2|, ret
end
def test_inline_uchar
ret = @builder.compile_inline("test @<uchar>{2460} test2")
assert_equal %Q|test ① test2|, ret
end
def test_inline_ruby
ret = @builder.compile_inline("@<ruby>{粗雑,クルード}と思われているなら@<ruby>{繊細,テクニカル}にやり、繊細と思われているなら粗雑にやる。")
assert_equal "<ruby><rb>粗雑</rb><rp>(</rp><rt>クルード</rt><rp>)</rp></ruby>と思われているなら<ruby><rb>繊細</rb><rp>(</rp><rt>テクニカル</rt><rp>)</rp></ruby>にやり、繊細と思われているなら粗雑にやる。", ret
end
def test_inline_ref
ret = @builder.compile_inline("@<ref>{外部参照<>&}")
assert_equal %Q|<a target='外部参照<>&'>「●● 外部参照<>&」</a>|, ret
end
def test_quote
lines = ["foo", "bar", "","buz"]
@builder.quote(lines)
assert_equal %Q|<blockquote><p>foobar</p>\n<p>buz</p></blockquote>\n|, @builder.raw_result
end
def test_memo
@builder.memo(["test1", "", "test<i>2</i>"], "this is @<b>{test}<&>_")
assert_equal %Q|<div class="memo">\n<p class="caption">this is <b>test</b><&>_</p>\n<p>test1</p>\n<p>test<i>2</i></p>\n</div>\n|, @builder.raw_result
end
def test_noindent
@builder.noindent
@builder.paragraph(["foo", "bar"])
@builder.paragraph(["foo2", "bar2"])
assert_equal %Q|<p class="noindent">foobar</p>\n<p>foo2bar2</p>\n|, @builder.raw_result
end
def test_flushright
@builder.flushright(["foo", "bar", "", "buz"])
assert_equal %Q|<p class="flushright">foobar</p>\n<p class="flushright">buz</p>\n|, @builder.raw_result
end
def test_centering
@builder.centering(["foo", "bar", "", "buz"])
assert_equal %Q|<p class="center">foobar</p>\n<p class="center">buz</p>\n|, @builder.raw_result
end
def test_image
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]}
item
end
@builder.image_image("sampleimg","sample photo",nil)
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.png" alt="sample photo" />\n<p class="caption">\n図1.1: sample photo\n</p>\n</div>\n|, @builder.raw_result
end
def test_image_eps
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.eps"]}
item
end
@builder.image_image("sampleimg","sample photo",nil)
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.eps.png" alt="sample photo" />\n<p class="caption">\n図1.1: sample photo\n</p>\n</div>\n|, @builder.raw_result
end
def test_image_with_metric
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]}
item
end
@builder.image_image("sampleimg","sample photo","scale=1.2")
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.png" alt="sample photo" width="120%" />\n<p class="caption">\n図1.1: sample photo\n</p>\n</div>\n|, @builder.raw_result
end
def test_image_with_metric2
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]}
item
end
@builder.image_image("sampleimg","sample photo","scale=1.2,html::class=sample,latex::ignore=params")
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.png" alt="sample photo" width="120%" class="sample" />\n<p class="caption">\n図1.1: sample photo\n</p>\n</div>\n|, @builder.raw_result
end
def test_indepimage
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]}
item
end
@builder.indepimage("sampleimg","sample photo",nil)
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.png" alt="sample photo" />\n<p class="caption">\n図: sample photo\n</p>\n</div>\n|, @builder.raw_result
end
def test_indepimage_without_caption
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]}
item
end
@builder.indepimage("sampleimg",nil,nil)
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.png" alt="" />\n</div>\n|, @builder.raw_result
end
def test_indepimage_with_metric
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]}
item
end
@builder.indepimage("sampleimg","sample photo","scale=1.2")
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.png" alt="sample photo" width="120%" />\n<p class="caption">\n図: sample photo\n</p>\n</div>\n|, @builder.raw_result
end
def test_indepimage_with_metric2
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]}
item
end
@builder.indepimage("sampleimg","sample photo","scale=1.2, html::class=\"sample\",latex::ignore=params")
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.png" alt="sample photo" width="120%" class="sample" />\n<p class="caption">\n図: sample photo\n</p>\n</div>\n|, @builder.raw_result
end
def test_indepimage_without_caption_but_with_metric
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1)
item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]}
item
end
@builder.indepimage("sampleimg",nil,"scale=1.2")
assert_equal %Q|<div class="image">\n<img src="images/chap1-sampleimg.png" alt="" width="120%" />\n</div>\n|, @builder.raw_result
end
def test_emlist
@builder.emlist(["lineA","lineB"])
assert_equal %Q|<div class="emlist-code">\n<pre class="emlist">lineA\nlineB\n</pre>\n</div>\n|, @builder.raw_result
end
def test_emlist_caption
@builder.emlist(["lineA","lineB"],"cap1")
assert_equal %Q|<div class="emlist-code">\n<p class="caption">cap1</p>\n<pre class="emlist">lineA\nlineB\n</pre>\n</div>\n|, @builder.raw_result
end
def test_emlist_with_tab
@builder.emlist(["\tlineA","\t\tlineB","\tlineC"])
assert_equal %Q|<div class="emlist-code">\n<pre class="emlist"> lineA\n lineB\n lineC\n</pre>\n</div>\n|, @builder.raw_result
end
def test_emlist_with_4tab
@builder.instance_eval{@tabwidth=4}
@builder.emlist(["\tlineA","\t\tlineB","\tlineC"])
assert_equal %Q|<div class="emlist-code">\n<pre class="emlist"> lineA\n lineB\n lineC\n</pre>\n</div>\n|, @builder.raw_result
end
def test_cmd
@builder.cmd(["lineA","lineB"])
assert_equal %Q|<div class="cmd-code">\n<pre class="cmd">lineA\nlineB\n</pre>\n</div>\n|, @builder.raw_result
end
def test_cmd_caption
@builder.cmd(["lineA","lineB"], "cap1")
assert_equal %Q|<div class="cmd-code">\n<p class="caption">cap1</p>\n<pre class="cmd">lineA\nlineB\n</pre>\n</div>\n|, @builder.raw_result
end
def test_bib
def @chapter.bibpaper(id)
Book::BibpaperIndex::Item.new("samplebib",1,"sample bib")
end
assert_equal %Q|<a href="./bib.html#bib-samplebib">[1]</a>|, @builder.inline_bib("samplebib")
end
def test_bibpaper
def @chapter.bibpaper(id)
Book::BibpaperIndex::Item.new("samplebib",1,"sample bib")
end
@builder.bibpaper(["a", "b"], "samplebib", "sample bib @<b>{bold}")
assert_equal %Q|<div class=\"bibpaper\">\n<a id=\"bib-samplebib\">[1] sample bib <b>bold</b></a>\n<p>a\nb\n</p>\n</div>\n|, @builder.raw_result
end
def column_helper(review)
chap_singleton = class << @chapter; self; end
chap_singleton.send(:define_method, :content) { review }
@compiler.compile(@chapter).match(/<body>\n(.+)<\/body>/m)[1]
end
def test_column_1
review =<<-EOS
===[column] prev column
inside prev column
===[column] test
inside column
===[/column]
EOS
expect =<<-EOS
<div class="column">
<h3><a id="h1-0-1" />prev column</h3>
<p>inside prev column</p>
</div>
<div class="column">
<h3><a id="h1-0-2" />test</h3>
<p>inside column</p>
</div>
EOS
assert_equal expect, column_helper(review)
end
def test_column_2
review =<<-EOS
===[column] test
inside column
=== next level
EOS
expect =<<-EOS
<div class="column">
<h3><a id="h1-0-1" />test</h3>
<p>inside column</p>
</div>
<h3><a id="h1-0-2" />next level</h3>
EOS
assert_equal expect, column_helper(review)
end
def test_column_3
review =<<-EOS
===[column] test
inside column
===[/column_dummy]
EOS
assert_raise(ReVIEW::CompileError) do
column_helper(review)
end
end
def test_ul
src =<<-EOS
* AAA
* BBB
EOS
expect = "<ul>\n<li>AAA</li>\n<li>BBB</li>\n</ul>\n"
ul_helper(src, expect)
end
def test_ul_cont
src =<<-EOS
* AAA
-AA
* BBB
-BB
EOS
expect = "<ul>\n<li>AAA-AA</li>\n<li>BBB-BB</li>\n</ul>\n"
ul_helper(src, expect)
end
def test_ul_nest1
src =<<-EOS
* AAA
** AA
EOS
expect =<<-EOS
<ul>
<li>AAA<ul>
<li>AA</li>
</ul>
</li>
</ul>
EOS
ul_helper(src, expect)
end
def test_ul_nest2
src =<<-EOS
* AAA
** AA
* BBB
** BB
EOS
expect =<<-EOS
<ul>
<li>AAA<ul>
<li>AA</li>
</ul>
</li>
<li>BBB<ul>
<li>BB</li>
</ul>
</li>
</ul>
EOS
ul_helper(src, expect)
end
def test_ul_nest3
src =<<-EOS
** AAA
* AA
* BBB
** BB
EOS
expect =<<-EOS
<ul>
<li><ul>
<li>AAA</li>
</ul>
</li>
<li>AA</li>
<li>BBB<ul>
<li>BB</li>
</ul>
</li>
</ul>
EOS
ul_helper(src, expect)
end
def test_inline_raw0
assert_equal "normal", @builder.inline_raw("normal")
end
def test_inline_raw1
assert_equal "body", @builder.inline_raw("|html|body")
end
def test_inline_raw2
assert_equal "body", @builder.inline_raw("|html, latex|body")
end
def test_inline_raw3
assert_equal "", @builder.inline_raw("|idgxml, latex|body")
end
def test_inline_raw4
assert_equal "|html body", @builder.inline_raw("|html body")
end
def test_inline_raw5
assert_equal "nor\nmal", @builder.inline_raw("|html|nor\\nmal")
end
def test_block_raw0
@builder.raw("<>!\"\\n& ")
expect =<<-EOS
<>!"
&
EOS
assert_equal expect.chomp, @builder.raw_result
end
def test_block_raw1
@builder.raw("|html|<>!\"\\n& ")
expect =<<-EOS
<>!"
&
EOS
assert_equal expect.chomp, @builder.raw_result
end
def test_block_raw2
@builder.raw("|html, latex|<>!\"\\n& ")
expect =<<-EOS
<>!\"
&
EOS
assert_equal expect.chomp, @builder.raw_result
end
def test_block_raw3
@builder.raw("|latex, idgxml|<>!\"\\n& ")
expect =<<-EOS
EOS
assert_equal expect.chomp, @builder.raw_result
end
def test_block_raw4
@builder.raw("|html <>!\"\\n& ")
expect =<<-EOS
|html <>!\"
&
EOS
assert_equal expect.chomp, @builder.raw_result
end
def test_inline_fn
fn = Book::FootnoteIndex.parse(['//footnote[foo][bar\\a\\$buz]'])
@chapter.instance_eval{@footnote_index=fn}
@builder.footnote("foo",'bar\\a\\$buz')
expect =<<-'EOS'
<div class="footnote"><p class="footnote">[<a id="fn-foo">*1</a>] bar\a\$buz</p></div>
EOS
assert_equal expect, @builder.raw_result
end
end