forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.rb
1715 lines (1473 loc) · 59 KB
/
api_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
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require_relative 'test_helper'
context 'API' do
context 'Load' do
test 'should load input file' do
sample_input_path = fixture_path('sample.adoc')
doc = File.open(sample_input_path, Asciidoctor::FILE_READ_MODE) {|file| Asciidoctor.load file, safe: Asciidoctor::SafeMode::SAFE }
assert_equal 'Document Title', doc.doctitle
assert_equal File.expand_path(sample_input_path), doc.attr('docfile')
assert_equal File.expand_path(File.dirname(sample_input_path)), doc.attr('docdir')
assert_equal '.adoc', doc.attr('docfilesuffix')
end
test 'should load input file from filename' do
sample_input_path = fixture_path('sample.adoc')
doc = Asciidoctor.load_file(sample_input_path, safe: Asciidoctor::SafeMode::SAFE)
assert_equal 'Document Title', doc.doctitle
assert_equal File.expand_path(sample_input_path), doc.attr('docfile')
assert_equal File.expand_path(File.dirname(sample_input_path)), doc.attr('docdir')
assert_equal '.adoc', doc.attr('docfilesuffix')
end
test 'should load input file from pathname' do
sample_input_path = Pathname fixture_path 'sample.adoc'
doc = Asciidoctor.load_file sample_input_path, safe: :safe
assert_equal 'Document Title', doc.doctitle
assert_equal sample_input_path.expand_path.to_s, (doc.attr 'docfile')
assert_equal sample_input_path.expand_path.dirname.to_s, (doc.attr 'docdir')
assert_equal '.adoc', (doc.attr 'docfilesuffix')
end
test 'should load input file with alternate file extension' do
sample_input_path = fixture_path 'sample-alt-extension.asciidoc'
doc = Asciidoctor.load_file sample_input_path, safe: :safe
assert_equal 'Document Title', doc.doctitle
assert_equal File.expand_path(sample_input_path), doc.attr('docfile')
assert_equal File.expand_path(File.dirname(sample_input_path)), doc.attr('docdir')
assert_equal '.asciidoc', doc.attr('docfilesuffix')
end
test 'should coerce encoding of file to UTF-8' do
old_external = Encoding.default_external
old_internal = Encoding.default_internal
old_verbose = $VERBOSE
begin
$VERBOSE = nil # disable warnings since we have to modify constants
input_path = fixture_path 'encoding.adoc'
Encoding.default_external = Encoding.default_internal = Encoding::IBM437
output = Asciidoctor.convert_file input_path, to_file: false, safe: :safe
assert_equal Encoding::UTF_8, output.encoding
assert_include 'Romé', output
ensure
Encoding.default_external = old_external
Encoding.default_internal = old_internal
$VERBOSE = old_verbose
end
end
test 'should not load file with unrecognized encoding' do
begin
tmp_input = Tempfile.new %w(test- .adoc), encoding: Encoding::IBM437
# NOTE using a character whose code differs between UTF-8 and IBM437
tmp_input.write %(ƒ\n)
tmp_input.close
exception = assert_raises ArgumentError do
Asciidoctor.load_file tmp_input.path, safe: :safe
end
expected_message = 'Failed to load AsciiDoc document - source is either binary or contains invalid Unicode data'
assert_include expected_message, exception.message
ensure
tmp_input.close!
end
end
test 'should not load invalid file' do
sample_input_path = fixture_path('hello-asciidoctor.pdf')
exception = assert_raises ArgumentError do
Asciidoctor.load_file(sample_input_path, safe: Asciidoctor::SafeMode::SAFE)
end
expected_message = 'Failed to load AsciiDoc document - source is either binary or contains invalid Unicode data'
assert_include expected_message, exception.message
# verify we have the correct backtrace (should be at least in the first 5 lines)
assert_match(/reader\.rb.*prepare_lines/, exception.backtrace[0..4].join(?\n))
end
test 'should convert filename that contains non-ASCII characters independent of default encodings' do
old_external = Encoding.default_external
old_internal = Encoding.default_internal
old_verbose = $VERBOSE
begin
$VERBOSE = nil # disable warnings since we have to modify constants
tmp_input = Tempfile.new %w(test-UTF8- .adoc)
tmp_input.write %(UTF8\n)
tmp_input.close
Encoding.default_external = Encoding.default_internal = Encoding::IBM437
tmp_output = tmp_input.path.sub '.adoc', '.html'
Asciidoctor.convert_file tmp_input.path, safe: :safe, attributes: 'linkcss !copycss'
assert File.exist? tmp_output
output = File.binread tmp_output
refute_empty output
# force encoding to UTF-8 and we should see that the string is in fact UTF-8 encoded
output = String.new output, encoding: Encoding::UTF_8
assert_equal Encoding::UTF_8, output.encoding
assert_include 'UTF8', output
ensure
tmp_input.close!
FileUtils.rm_f tmp_output
Encoding.default_external = old_external
Encoding.default_internal = old_internal
$VERBOSE = old_verbose
end
end
test 'should load input IO' do
input = StringIO.new <<~'EOS'
Document Title
==============
preamble
EOS
doc = Asciidoctor.load(input, safe: Asciidoctor::SafeMode::SAFE)
assert_equal 'Document Title', doc.doctitle
refute doc.attr?('docfile')
assert_equal doc.base_dir, doc.attr('docdir')
end
test 'should load input string' do
input = <<~'EOS'
Document Title
==============
preamble
EOS
doc = Asciidoctor.load(input, safe: Asciidoctor::SafeMode::SAFE)
assert_equal 'Document Title', doc.doctitle
refute doc.attr?('docfile')
assert_equal doc.base_dir, doc.attr('docdir')
end
test 'should load input string array' do
input = <<~'EOS'
Document Title
==============
preamble
EOS
doc = Asciidoctor.load(input.lines, safe: Asciidoctor::SafeMode::SAFE)
assert_equal 'Document Title', doc.doctitle
refute doc.attr?('docfile')
assert_equal doc.base_dir, doc.attr('docdir')
end
test 'should load nil input' do
doc = Asciidoctor.load nil, safe: :safe
refute_nil doc
assert_empty doc.blocks
end
test 'should accept attributes as array' do
# NOTE there's a tab character before idseparator
doc = Asciidoctor.load('text', attributes: %w(toc sectnums source-highlighter=coderay idprefix idseparator=-))
assert_kind_of Hash, doc.attributes
assert doc.attr?('toc')
assert_equal '', doc.attr('toc')
assert doc.attr?('sectnums')
assert_equal '', doc.attr('sectnums')
assert doc.attr?('source-highlighter')
assert_equal 'coderay', doc.attr('source-highlighter')
assert doc.attr?('idprefix')
assert_equal '', doc.attr('idprefix')
assert doc.attr?('idseparator')
assert_equal '-', doc.attr('idseparator')
end
test 'should accept attributes as empty array' do
doc = Asciidoctor.load('text', attributes: [])
assert_kind_of Hash, doc.attributes
end
test 'should accept attributes as string' do
doc = Asciidoctor.load 'text', attributes: %(toc sectnums\nsource-highlighter=coderay\nidprefix\nidseparator=-)
assert_kind_of Hash, doc.attributes
assert doc.attr?('toc')
assert_equal '', doc.attr('toc')
assert doc.attr?('sectnums')
assert_equal '', doc.attr('sectnums')
assert doc.attr?('source-highlighter')
assert_equal 'coderay', doc.attr('source-highlighter')
assert doc.attr?('idprefix')
assert_equal '', doc.attr('idprefix')
assert doc.attr?('idseparator')
assert_equal '-', doc.attr('idseparator')
end
test 'should accept values containing spaces in attributes string' do
doc = Asciidoctor.load('text', attributes: %(idprefix idseparator=- note-caption=Note\\ to\\\tself toc))
assert_kind_of Hash, doc.attributes
assert doc.attr?('idprefix')
assert_equal '', doc.attr('idprefix')
assert doc.attr?('idseparator')
assert_equal '-', doc.attr('idseparator')
assert doc.attr?('note-caption')
assert_equal "Note to\tself", doc.attr('note-caption')
end
test 'should accept attributes as empty string' do
doc = Asciidoctor.load('text', attributes: '')
assert_kind_of Hash, doc.attributes
end
test 'should accept attributes as nil' do
doc = Asciidoctor.load('text', attributes: nil)
assert_kind_of Hash, doc.attributes
end
test 'should accept attributes if hash like' do
class Hashish
def initialize
@table = { 'toc' => '' }
end
def keys
@table.keys
end
def [](key)
@table[key]
end
end
doc = Asciidoctor.load('text', attributes: Hashish.new)
assert_kind_of Hash, doc.attributes
assert doc.attributes.has_key?('toc')
end
test 'should not expand value of docdir attribute if specified via API' do
docdir = 'virtual/directory'
doc = document_from_string '', safe: :safe, attributes: { 'docdir' => docdir }
assert_equal docdir, (doc.attr 'docdir')
assert_equal docdir, doc.base_dir
end
test 'converts block to output format when convert is called' do
doc = Asciidoctor.load 'paragraph text'
expected = <<~'EOS'.chop
<div class="paragraph">
<p>paragraph text</p>
</div>
EOS
assert_equal 1, doc.blocks.length
assert_equal :paragraph, doc.blocks[0].context
assert_equal expected, doc.blocks[0].convert
end
test 'render method on node is aliased to convert method' do
input = <<~'EOS'
paragraph text
* list item
EOS
doc = Asciidoctor.load input
assert_equal 2, doc.blocks.length
([doc] + doc.blocks).each do |block|
assert_equal block.method(:convert), block.method(:render)
end
inline = Asciidoctor::Inline.new doc.blocks[0], :image, nil, type: 'image', target: 'tiger.png'
assert_equal inline.method(:convert), inline.method(:render)
end
test 'should output timestamps by default' do
doc = document_from_string 'text', backend: :html5, attributes: nil
result = doc.convert
assert doc.attr?('docdate')
refute doc.attr? 'reproducible'
assert_xpath '//div[@id="footer-text" and contains(string(.//text()), "Last updated")]', result, 1
end
test 'should not output timestamps if reproducible attribute is set in HTML 5' do
doc = document_from_string 'text', backend: :html5, attributes: { 'reproducible' => '' }
result = doc.convert
assert doc.attr?('docdate')
assert doc.attr?('reproducible')
assert_xpath '//div[@id="footer-text" and contains(string(.//text()), "Last updated")]', result, 0
end
test 'should not output timestamps if reproducible attribute is set in DocBook' do
doc = document_from_string 'text', backend: :docbook, attributes: { 'reproducible' => '' }
result = doc.convert
assert doc.attr?('docdate')
assert doc.attr?('reproducible')
assert_xpath '/article/info/date', result, 0
end
test 'should not modify options argument' do
options = { safe: Asciidoctor::SafeMode::SAFE }
options.freeze
sample_input_path = fixture_path('sample.adoc')
begin
Asciidoctor.load_file sample_input_path, options
rescue
flunk %(options argument should not be modified)
end
end
test 'should not modify attributes Hash argument' do
attributes = {}
attributes.freeze
options = {
safe: Asciidoctor::SafeMode::SAFE,
attributes: attributes,
}
sample_input_path = fixture_path('sample.adoc')
begin
Asciidoctor.load_file sample_input_path, options
rescue
flunk %(attributes argument should not be modified)
end
end
test 'should be able to restore header attributes after call to convert' do
input = <<~'EOS'
= Document Title
:foo: bar
content
:foo: baz
content
EOS
doc = Asciidoctor.load input
assert_equal 'bar', (doc.attr 'foo')
doc.convert
assert_equal 'baz', (doc.attr 'foo')
doc.restore_attributes
assert_equal 'bar', (doc.attr 'foo')
end
test 'should track file and line information with blocks if sourcemap option is set' do
doc = Asciidoctor.load_file fixture_path('sample.adoc'), sourcemap: true
refute_nil doc.source_location
assert_equal 'sample.adoc', doc.file
assert_equal 1, doc.lineno
section_1 = doc.sections[0]
assert_equal 'Section A', section_1.title
refute_nil section_1.source_location
assert_equal 'sample.adoc', section_1.file
assert_equal 10, section_1.lineno
section_2 = doc.sections[1]
assert_equal 'Section B', section_2.title
refute_nil section_2.source_location
assert_equal 'sample.adoc', section_2.file
assert_equal 18, section_2.lineno
table_block = section_2.blocks[1]
assert_equal :table, table_block.context
refute_nil table_block.source_location
assert_equal 'sample.adoc', table_block.file
assert_equal 22, table_block.lineno
first_cell = table_block.rows.body[0][0]
refute_nil first_cell.source_location
assert_equal 'sample.adoc', first_cell.file
assert_equal 23, first_cell.lineno
second_cell = table_block.rows.body[0][1]
refute_nil second_cell.source_location
assert_equal 'sample.adoc', second_cell.file
assert_equal 23, second_cell.lineno
last_cell = table_block.rows.body[-1][-1]
refute_nil last_cell.source_location
assert_equal 'sample.adoc', last_cell.file
assert_equal 24, last_cell.lineno
last_block = section_2.blocks[-1]
assert_equal :ulist, last_block.context
refute_nil last_block.source_location
assert_equal 'sample.adoc', last_block.file
assert_equal 28, last_block.lineno
list_items = last_block.blocks
refute_nil list_items[0].source_location
assert_equal 'sample.adoc', list_items[0].file
assert_equal 28, list_items[0].lineno
refute_nil list_items[1].source_location
assert_equal 'sample.adoc', list_items[1].file
assert_equal 29, list_items[1].lineno
refute_nil list_items[2].source_location
assert_equal 'sample.adoc', list_items[2].file
assert_equal 30, list_items[2].lineno
doc = Asciidoctor.load_file fixture_path('master.adoc'), sourcemap: true, safe: :safe
section_1 = doc.sections[0]
assert_equal 'Chapter A', section_1.title
refute_nil section_1.source_location
assert_equal fixture_path('chapter-a.adoc'), section_1.file
assert_equal 1, section_1.lineno
end
test 'should track file and line information on list items if sourcemap option is set' do
doc = Asciidoctor.load_file fixture_path('lists.adoc'), sourcemap: true
first_section = doc.blocks[1]
unordered_basic_list = first_section.blocks[0]
assert_equal 11, unordered_basic_list.lineno
unordered_basic_list_items = unordered_basic_list.find_by context: :list_item
assert_equal 11, unordered_basic_list_items[0].lineno
assert_equal 12, unordered_basic_list_items[1].lineno
assert_equal 13, unordered_basic_list_items[2].lineno
unordered_max_nesting = first_section.blocks[1]
assert_equal 16, unordered_max_nesting.lineno
unordered_max_nesting_items = unordered_max_nesting.find_by context: :list_item
assert_equal 16, unordered_max_nesting_items[0].lineno
assert_equal 17, unordered_max_nesting_items[1].lineno
assert_equal 18, unordered_max_nesting_items[2].lineno
assert_equal 19, unordered_max_nesting_items[3].lineno
assert_equal 20, unordered_max_nesting_items[4].lineno
assert_equal 21, unordered_max_nesting_items[5].lineno
checklist = first_section.blocks[2]
assert_equal 24, checklist.lineno
checklist_list_items = checklist.find_by context: :list_item
assert_equal 24, checklist_list_items[0].lineno
assert_equal 25, checklist_list_items[1].lineno
assert_equal 26, checklist_list_items[2].lineno
assert_equal 27, checklist_list_items[3].lineno
ordered_basic = first_section.blocks[3]
assert_equal 30, ordered_basic.lineno
ordered_basic_list_items = ordered_basic.find_by context: :list_item
assert_equal 30, ordered_basic_list_items[0].lineno
assert_equal 31, ordered_basic_list_items[1].lineno
assert_equal 32, ordered_basic_list_items[2].lineno
ordered_nested = first_section.blocks[4]
assert_equal 35, ordered_nested.lineno
ordered_nested_list_items = ordered_nested.find_by context: :list_item
assert_equal 35, ordered_nested_list_items[0].lineno
assert_equal 36, ordered_nested_list_items[1].lineno
assert_equal 37, ordered_nested_list_items[2].lineno
assert_equal 38, ordered_nested_list_items[3].lineno
assert_equal 39, ordered_nested_list_items[4].lineno
ordered_max_nesting = first_section.blocks[5]
assert_equal 42, ordered_max_nesting.lineno
ordered_max_nesting_items = ordered_max_nesting.find_by context: :list_item
assert_equal 42, ordered_max_nesting_items[0].lineno
assert_equal 43, ordered_max_nesting_items[1].lineno
assert_equal 44, ordered_max_nesting_items[2].lineno
assert_equal 45, ordered_max_nesting_items[3].lineno
assert_equal 46, ordered_max_nesting_items[4].lineno
assert_equal 47, ordered_max_nesting_items[5].lineno
labeled_singleline = first_section.blocks[6]
assert_equal 50, labeled_singleline.lineno
labeled_singleline_items = labeled_singleline.find_by context: :list_item
assert_equal 50, labeled_singleline_items[0].lineno
assert_equal 50, labeled_singleline_items[1].lineno
assert_equal 51, labeled_singleline_items[2].lineno
assert_equal 51, labeled_singleline_items[3].lineno
labeled_multiline = first_section.blocks[7]
assert_equal 54, labeled_multiline.lineno
labeled_multiline_items = labeled_multiline.find_by context: :list_item
assert_equal 54, labeled_multiline_items[0].lineno
assert_equal 55, labeled_multiline_items[1].lineno
assert_equal 56, labeled_multiline_items[2].lineno
assert_equal 57, labeled_multiline_items[3].lineno
qanda = first_section.blocks[8]
assert_equal 61, qanda.lineno
qanda_items = qanda.find_by context: :list_item
assert_equal 61, qanda_items[0].lineno
assert_equal 62, qanda_items[1].lineno
assert_equal 63, qanda_items[2].lineno
assert_equal 63, qanda_items[3].lineno
mixed = first_section.blocks[9]
assert_equal 66, mixed.lineno
mixed_items = mixed.find_by(context: :list_item) {|block| block.text? }
assert_equal 66, mixed_items[0].lineno
assert_equal 67, mixed_items[1].lineno
assert_equal 68, mixed_items[2].lineno
assert_equal 69, mixed_items[3].lineno
assert_equal 70, mixed_items[4].lineno
assert_equal 71, mixed_items[5].lineno
assert_equal 72, mixed_items[6].lineno
assert_equal 73, mixed_items[7].lineno
assert_equal 74, mixed_items[8].lineno
assert_equal 75, mixed_items[9].lineno
assert_equal 77, mixed_items[10].lineno
assert_equal 78, mixed_items[11].lineno
assert_equal 79, mixed_items[12].lineno
assert_equal 80, mixed_items[13].lineno
assert_equal 81, mixed_items[14].lineno
assert_equal 82, mixed_items[15].lineno
assert_equal 83, mixed_items[16].lineno
unordered_complex_list = first_section.blocks[10]
assert_equal 86, unordered_complex_list.lineno
unordered_complex_items = unordered_complex_list.find_by context: :list_item
assert_equal 86, unordered_complex_items[0].lineno
assert_equal 87, unordered_complex_items[1].lineno
assert_equal 88, unordered_complex_items[2].lineno
assert_equal 92, unordered_complex_items[3].lineno
assert_equal 96, unordered_complex_items[4].lineno
end
# NOTE this does not work for a list continuation that attached to a grandparent
test 'should assign correct source location to blocks that follow a detached list continuation' do
input = <<~'EOS'
* parent
** child
+
paragraph attached to parent
****
sidebar outside list
****
EOS
doc = document_from_string input, sourcemap: true
assert_equal [5, 8], (doc.find_by context: :paragraph).map(&:lineno)
end
test 'should assign correct source location if section occurs on last line of input' do
input = <<~'EOS'
= Document Title
== Section A
content
== Section B
EOS
doc = document_from_string input, sourcemap: true
assert_equal [1, 3, 7], (doc.find_by context: :section).map(&:lineno)
end
test 'should allow sourcemap option on document to be modified before document is parsed' do
doc = Asciidoctor.load_file fixture_path('sample.adoc'), parse: false
doc.sourcemap = true
refute doc.parsed?
doc = doc.parse
assert doc.parsed?
section_1 = doc.sections[0]
assert_equal 'Section A', section_1.title
refute_nil section_1.source_location
assert_equal 'sample.adoc', section_1.file
assert_equal 10, section_1.lineno
end
test 'find_by should return Array of blocks anywhere in document tree that match criteria' do
input = <<~'EOS'
= Document Title
preamble
== Section A
paragraph
--
Exhibit A::
+
[#tiger.animal]
image::tiger.png[Tiger]
--
image::shoe.png[Shoe]
== Section B
paragraph
EOS
doc = Asciidoctor.load input
result = doc.find_by context: :image
assert_equal 2, result.size
assert_equal :image, result[0].context
assert_equal 'tiger.png', result[0].attr('target')
assert_equal :image, result[1].context
assert_equal 'shoe.png', result[1].attr('target')
end
test 'find_by should return an empty Array if no matches are found' do
input = 'paragraph'
doc = Asciidoctor.load input
result = doc.find_by context: :section
refute_nil result
assert_equal 0, result.size
end
test 'find_by should discover blocks inside AsciiDoc table cells if traverse_documents selector option is true' do
input = <<~'EOS'
paragraph in parent document (before)
[%footer,cols=2*]
|===
a|
paragraph in nested document (body)
|normal table cell
a|
paragraph in nested document (foot)
|normal table cell
|===
paragraph in parent document (after)
EOS
doc = Asciidoctor.load input
result = doc.find_by context: :paragraph
assert_equal 2, result.size
result = doc.find_by context: :paragraph, traverse_documents: true
assert_equal 4, result.size
end
test 'find_by should return inner document of AsciiDoc table cell if traverse_documents selector option is true' do
input = <<~'EOS'
|===
a|paragraph in nested document
|===
EOS
doc = Asciidoctor.load input
inner_doc = doc.blocks[0].rows.body[0][0].inner_document
result = doc.find_by traverse_documents: true
assert_include inner_doc, result
result = doc.find_by context: :inner_document, traverse_documents: true
assert_equal 1, result.size
assert_equal inner_doc, result[0]
end
test 'find_by should match table cells' do
input = <<~'EOS'
|===
|a |b |c
|1
one
a|NOTE: 2, as it goes.
l|
3
you
me
|===
EOS
doc = document_from_string input
table = doc.blocks[0]
first_head_cell = table.rows.head[0][0]
first_body_cell = table.rows.body[0][0]
result = doc.find_by
assert_include first_head_cell, result
assert_include first_body_cell, result
assert_equal 'a', first_head_cell.source
assert_equal ['a'], first_head_cell.lines
assert_equal %(1\none), first_body_cell.source
assert_equal ['1', 'one'], first_body_cell.lines
result = doc.find_by context: :table_cell, style: :asciidoc
assert_equal 1, result.size
assert_kind_of Asciidoctor::Table::Cell, result[0]
assert_equal :asciidoc, result[0].style
assert_equal 'NOTE: 2, as it goes.', result[0].source
end
test 'find_by should return Array of blocks that match style criteria' do
input = <<~'EOS'
[square]
* one
* two
* three
---
* apples
* bananas
* pears
EOS
doc = Asciidoctor.load input
result = doc.find_by context: :ulist, style: 'square'
assert_equal 1, result.size
assert_equal :ulist, result[0].context
end
test 'find_by should return Array of blocks that match role criteria' do
input = <<~'EOS'
[#tiger.animal]
image::tiger.png[Tiger]
image::shoe.png[Shoe]
EOS
doc = Asciidoctor.load input
result = doc.find_by context: :image, role: 'animal'
assert_equal 1, result.size
assert_equal :image, result[0].context
assert_equal 'tiger.png', result[0].attr('target')
end
test 'find_by should return the document title section if context selector is :section' do
input = <<~'EOS'
= Document Title
preamble
== Section One
content
EOS
doc = Asciidoctor.load input
result = doc.find_by context: :section
refute_nil result
assert_equal 2, result.size
assert_equal :section, result[0].context
assert_equal 'Document Title', result[0].title
end
test 'find_by should only return results for which the block argument yields true' do
input = <<~'EOS'
== Section
content
=== Subsection
content
EOS
doc = Asciidoctor.load input
result = doc.find_by(context: :section) {|sect| sect.level == 1 }
refute_nil result
assert_equal 1, result.size
assert_equal :section, result[0].context
assert_equal 'Section', result[0].title
end
test 'find_by should reject node and its children if block returns :reject' do
input = <<~'EOS'
paragraph 1
====
paragraph 2
term::
+
paragraph 3
====
paragraph 4
EOS
doc = Asciidoctor.load input
result = doc.find_by do |candidate|
ctx = candidate.context
if ctx == :example
:reject
elsif ctx == :paragraph
true
end
end
refute_nil result
assert_equal 2, result.size
assert_equal :paragraph, result[0].context
assert_equal :paragraph, result[1].context
end
test 'find_by should reject node matched by ID selector if block returns :reject' do
input = <<~'EOS'
[.rolename]
paragraph 1
[.rolename#idname]
paragraph 2
EOS
doc = Asciidoctor.load input
result = doc.find_by id: 'idname', role: 'rolename'
refute_nil result
assert_equal 1, result.size
assert_equal doc.blocks[1], result[0]
result = doc.find_by(id: 'idname', role: 'rolename') { :reject }
refute_nil result
assert_equal 0, result.size
end
test 'find_by should accept node matched by ID selector if block returns :prune' do
input = <<~'EOS'
[.rolename]
paragraph 1
[.rolename#idname]
====
paragraph 2
====
EOS
doc = Asciidoctor.load input
result = doc.find_by id: 'idname', role: 'rolename'
refute_nil result
assert_equal 1, result.size
assert_equal doc.blocks[1], result[0]
result = doc.find_by(id: 'idname', role: 'rolename') { :prune }
refute_nil result
assert_equal 1, result.size
assert_equal doc.blocks[1], result[0]
end
test 'find_by should accept node but reject its children if block returns :prune' do
input = <<~'EOS'
====
paragraph 2
term::
+
paragraph 3
====
EOS
doc = Asciidoctor.load input
result = doc.find_by do |candidate|
if candidate.context == :example
:prune
end
end
refute_nil result
assert_equal 1, result.size
assert_equal :example, result[0].context
end
test 'find_by should stop looking for blocks when StopIteration is raised' do
input = <<~'EOS'
paragraph 1
====
paragraph 2
****
paragraph 3
****
====
paragraph 4
* item
+
paragraph 5
EOS
doc = Asciidoctor.load input
stop_at_next = false
result = doc.find_by do |candidate|
raise StopIteration if stop_at_next
if candidate.context == :paragraph
candidate.parent.context == :sidebar ? (stop_at_next = true) : true
end
end
refute_nil result
assert_equal 3, result.size
assert_equal 'paragraph 1', result[0].content
assert_equal 'paragraph 2', result[1].content
assert_equal 'paragraph 3', result[2].content
end
test 'find_by should stop looking for blocks when filter block returns :stop directive' do
input = <<~'EOS'
paragraph 1
====
paragraph 2
****
paragraph 3
****
====
paragraph 4
* item
+
paragraph 5
EOS
doc = Asciidoctor.load input
stop_at_next = false
result = doc.find_by do |candidate|
next :stop if stop_at_next
if candidate.context == :paragraph
candidate.parent.context == :sidebar ? (stop_at_next = true) : true
end
end
refute_nil result
assert_equal 3, result.size
assert_equal 'paragraph 1', result[0].content
assert_equal 'paragraph 2', result[1].content
assert_equal 'paragraph 3', result[2].content
end
test 'find_by should only return one result when matching by id' do
input = <<~'EOS'
== Section
content
[#subsection]
=== Subsection
content
EOS
doc = Asciidoctor.load input
result = doc.find_by(context: :section, id: 'subsection')
refute_nil result
assert_equal 1, result.size
assert_equal :section, result[0].context
assert_equal 'Subsection', result[0].title
end
test 'find_by should stop seeking once match is found' do
input = <<~'EOS'
== Section
content
[#subsection]
=== Subsection
[#last]
content
EOS
doc = Asciidoctor.load input
visited_last = false
result = doc.find_by(id: 'subsection') do |candidate|
visited_last = true if candidate.id == 'last'
true
end
refute_nil result
assert_equal 1, result.size
refute visited_last
end
test 'find_by should return an empty Array if the id criteria matches but the block argument yields false' do
input = <<~'EOS'
== Section
content
[#subsection]
=== Subsection
content
EOS
doc = Asciidoctor.load input
result = doc.find_by(context: :section, id: 'subsection') {|sect| false }
refute_nil result
assert_equal 0, result.size
end
test 'find_by should not crash if dlist entry does not have description' do
input = 'term without description::'
doc = Asciidoctor.load input
result = doc.find_by
refute_nil result
assert_equal 3, result.size
assert_kind_of Asciidoctor::Document, result[0]
assert_kind_of Asciidoctor::List, result[1]
assert_kind_of Asciidoctor::ListItem, result[2]
end
test 'dlist item should always have two entries for terms and desc' do
[
'term w/o desc::',
%(term::\nalias::),
%(primary:: 1\nsecondary:: 2),
].each do |input|
dlist = (Asciidoctor.load input).blocks[0]
dlist.items.each do |item|
assert_equal 2, item.size
assert_kind_of ::Array, item[0]
assert_kind_of Asciidoctor::ListItem, item[1] if item[1]
end
end
end
test 'timings are recorded for each step when load and convert are called separately' do
sample_input_path = fixture_path 'asciidoc_index.txt'
(Asciidoctor.load_file sample_input_path, timings: (timings = Asciidoctor::Timings.new)).convert
refute_equal '0.00000', '%05.5f' % timings.read_parse.to_f
refute_equal '0.00000', '%05.5f' % timings.convert.to_f
refute_equal timings.read_parse, timings.total
end
test 'can disable syntax highlighter by setting value to nil in :syntax_highlighters option' do