forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sections_test.rb
3906 lines (2902 loc) · 106 KB
/
sections_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 'Sections' do
context 'Ids' do
test 'synthetic id is generated by default' do
sec = block_from_string '== Section One'
assert_equal '_section_one', sec.id
end
test 'duplicate synthetic id is automatically enumerated' do
doc = document_from_string <<~'EOS'
== Section One
== Section One
EOS
assert_equal 2, doc.blocks.size
assert_equal '_section_one', doc.blocks[0].id
assert_equal '_section_one_2', doc.blocks[1].id
end
test 'synthetic id removes non-word characters' do
sec = block_from_string '== We’re back!'
assert_equal '_were_back', sec.id
end
test 'synthetic id removes repeating separators' do
sec = block_from_string '== Section $ One'
assert_equal '_section_one', sec.id
end
test 'synthetic id removes entities' do
sec = block_from_string '== Ben & Jerry & Company¹ "Ice Cream Brothers" あ'
assert_equal '_ben_jerry_company_ice_cream_brothers', sec.id
end
test 'synthetic id removes adjacent entities with mixed case' do
sec = block_from_string '== a ®&© b'
assert_equal '_a_b', sec.id
end
test 'synthetic id removes XML tags' do
sec = block_from_string '== Use the `run` command to make it icon:gear[]'
assert_equal '_use_the_run_command_to_make_it_gear', sec.id
end
test 'synthetic id collapses repeating spaces' do
sec = block_from_string '== Go Far'
assert_equal '_go_far', sec.id
end
test 'synthetic id replaces hyphens with separator' do
sec = block_from_string '== State-of-the-art design'
assert_equal '_state_of_the_art_design', sec.id
end
test 'synthetic id replaces dots with separator' do
sec = block_from_string '== Section 1.1.1'
assert_equal '_section_1_1_1', sec.id
end
test 'synthetic id prefix can be customized' do
sec = block_from_string ":idprefix: id_\n\n== Section One"
assert_equal 'id_section_one', sec.id
end
test 'synthetic id prefix can be set to blank' do
sec = block_from_string ":idprefix:\n\n== Section One"
assert_equal 'section_one', sec.id
end
test 'synthetic id prefix is stripped from beginning of id if set to blank' do
sec = block_from_string ":idprefix:\n\n== & ! More"
assert_equal 'more', sec.id
end
test 'synthetic id separator can be customized' do
sec = block_from_string ":idseparator: -\n\n== Section One"
assert_equal '_section-one', sec.id
end
test 'synthetic id separator can be hyphen and hyphens are preserved' do
sec = block_from_string ":idseparator: -\n\n== State-of-the-art design"
assert_equal '_state-of-the-art-design', sec.id
end
test 'synthetic id separator can be dot and dots are preserved' do
sec = block_from_string ":idseparator: .\n\n== Version 5.0.1"
assert_equal '_version.5.0.1', sec.id
end
test 'synthetic id separator can only be one character' do
input = <<~'EOS'
:idseparator: -=-
== This Section Is All You Need
EOS
sec = block_from_string input
assert_equal '_this-section-is-all-you-need', sec.id
end
test 'synthetic id separator can be set to blank' do
sec = block_from_string ":idseparator:\n\n== Section One"
assert_equal '_sectionone', sec.id
end
test 'synthetic id separator can be set to blank when idprefix is blank' do
sec = block_from_string ":idprefix:\n:idseparator:\n\n== Section One"
assert_equal 'sectionone', sec.id
end
test 'synthetic id separator is removed from beginning of id when idprefix is blank' do
sec = block_from_string ":idprefix:\n:idseparator: _\n\n== +Section One"
assert_equal 'section_one', sec.id
end
test 'synthetic ids can be disabled' do
sec = block_from_string ":sectids!:\n\n== Section One\n"
assert_nil sec.id
end
test 'explicit id in anchor above section title overrides synthetic id' do
sec = block_from_string "[[one]]\n== Section One"
assert_equal 'one', sec.id
end
test 'explicit id in block attributes above section title overrides synthetic id' do
sec = block_from_string "[id=one]\n== Section One"
assert_equal 'one', sec.id
end
test 'empty id in block attributes above section title prevents assignment of synthetic id' do
sect = block_from_string %([id=]\n== Section One)
assert_nil sect.id
end
test 'explicit id set using shorthand in style above section title overrides synthetic id' do
sec = block_from_string "[#one]\n== Section One"
assert_equal 'one', sec.id
end
test 'should use explicit id from last block attribute line above section title that defines an explicit id' do
input = <<~'EOS'
[#un]
[#one]
== Section One
EOS
sec = block_from_string input
assert_equal 'one', sec.id
end
test 'explicit id can be defined using an embedded anchor' do
sec = block_from_string '== Section One [[one]] =='
assert_equal 'one', sec.id
assert_equal 'Section One', sec.title
end
test 'explicit id can be defined using an embedded anchor when using setext section titles' do
input = <<~'EOS'
Section Title [[refid,reftext]]
-------------------------------
EOS
sec = block_from_string input
assert_equal 'Section Title', sec.title
assert_equal 'refid', sec.id
assert_equal 'reftext', (sec.attr 'reftext')
end
test 'explicit id can be defined using an embedded anchor with reftext' do
sec = block_from_string '== Section One [[one,Section Uno]] =='
assert_equal 'one', sec.id
assert_equal 'Section One', sec.title
assert_equal 'Section Uno', (sec.attr 'reftext')
end
test 'id and reftext in embedded anchor cannot be quoted' do
sec = block_from_string %(== Section One [["one","Section Uno"]] ==)
refute_equal 'one', sec.id
assert_equal 'Section One [["one","Section Uno"]]', sec.title
assert_nil sec.attr 'reftext'
end
test 'reftext in embedded anchor may contain comma' do
sec = block_from_string %(== Section One [[one, Section,Uno]] ==)
assert_equal 'one', sec.id
assert_equal 'Section One', sec.title
assert_equal 'Section,Uno', (sec.attr 'reftext')
end
test 'should unescape but not process inline anchor' do
sec = block_from_string %(== Section One \\[[one]] ==)
refute_equal 'one', sec.id
assert_equal 'Section One [[one]]', sec.title
end
test 'should not process inline anchor in section title if section has explicit ID' do
sec = block_from_string %([#sect-one]\n== Section One [[one]])
assert_equal 'sect-one', sec.id
assert_equal 'Section One <a id="one"></a>', sec.title
end
test 'should apply substititons to title with attribute references when registering section with auto-generated ID' do
input = <<~'EOS'
= Document Title
:foo: bar
See <<_section_baz>>.
:foo: baz
== Section {foo}
That's all, folks!
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['_section_baz']
refute_nil ref
output = doc.convert standalone: false
assert_xpath '//a[@href="#_section_baz"][text()="Section baz"]', output, 1
assert_xpath '//h2[@id="_section_baz"][text()="Section baz"]', output, 1
end
test 'should apply substititons to title with attribute references when registering section with explicit ID' do
input = <<~'EOS'
= Document Title
:foo: bar
See <<explicit>>.
:foo: baz
[#explicit]
== Section {foo}
That's all, folks!
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['explicit']
refute_nil ref
output = doc.convert standalone: false
assert_xpath '//a[@href="#explicit"][text()="Section baz"]', output, 1
assert_xpath '//h2[@id="explicit"][text()="Section baz"]', output, 1
end
test 'should not apply substititons to title with attribute references when parsing section with empty ID' do
input = <<~'EOS'
= Document Title
:attribute-missing: warn
[id=]
== Section {missing}
That's all, folks!
EOS
using_memory_logger do |logger|
doc = document_from_string input
assert_empty doc.catalog[:refs]
assert_empty logger.messages
end
end
test 'title substitutions are applied before generating id' do
sec = block_from_string "== Section{sp}One\n"
assert_equal '_section_one', sec.id
end
test 'synthetic ids are unique' do
input = <<~'EOS'
== Some section
text
== Some section
text
EOS
doc = document_from_string input
assert_equal '_some_section', doc.blocks[0].id
assert_equal '_some_section_2', doc.blocks[1].id
end
# NOTE test cannot be run in parallel with other tests
test 'can set start index of synthetic ids' do
old_unique_id_start_index = Asciidoctor::Compliance.unique_id_start_index
begin
input = <<~'EOS'
== Some section
text
== Some section
text
EOS
Asciidoctor::Compliance.unique_id_start_index = 1
doc = document_from_string input
assert_equal '_some_section', doc.blocks[0].id
assert_equal '_some_section_1', doc.blocks[1].id
ensure
Asciidoctor::Compliance.unique_id_start_index = old_unique_id_start_index
end
end
test 'should use specified id and reftext when registering section reference' do
input = <<~'EOS'
[[install,Install Procedure]]
== Install
content
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['install']
refute_nil ref
assert_equal 'Install Procedure', ref.reftext
assert_equal 'install', (doc.resolve_id 'Install Procedure')
end
test 'should use specified reftext when registering section reference' do
input = <<~'EOS'
[reftext="Install Procedure"]
== Install
content
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['_install']
refute_nil ref
assert_equal 'Install Procedure', ref.reftext
assert_equal '_install', (doc.resolve_id 'Install Procedure')
end
test 'should resolve attribute reference in title using attribute defined at location of section title' do
input = <<~'EOS'
:platform-id: linux
:platform-name: Linux
[#install-{platform-id}]
== Install on {platform-name}
content
:platform-id: win32
:platform-name: Windows
[#install-{platform-id}]
== Install on {platform-name}
content
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['install-win32']
refute_nil ref
assert_equal 'Install on Windows', ref.title
assert_equal 'install-win32', (doc.resolve_id 'Install on Windows')
end
test 'should substitute attributes when registering reftext for section' do
input = <<~'EOS'
:platform-name: n/a
== Overview
:platform-name: Linux
[[install,install on {platform-name}]]
== Install
content
EOS
doc = document_from_string input
ref = doc.catalog[:refs]['install']
refute_nil ref
assert_equal 'install on Linux', ref.reftext
assert_equal 'install', (doc.resolve_id 'install on Linux')
end
test 'duplicate section id should not overwrite existing section id entry in references table' do
input = <<~'EOS'
[#install]
== First Install
content
[#install]
== Second Install
content
EOS
using_memory_logger do |logger|
doc = document_from_string input
ref = doc.catalog[:refs]['install']
refute_nil ref
assert_nil ref.reftext
assert_equal 'First Install', ref.title
assert_equal 'install', (doc.resolve_id 'First Install')
assert_message logger, :WARN, '<stdin>: line 7: id assigned to section already in use: install', Hash
end
end
test 'should warn if explicit section ID matches auto-generated section ID' do
input = <<~'EOS'
== Do Not Repeat Yourself
content
[#_do_not_repeat_yourself]
== Do Not Repeat Yourself
content
EOS
using_memory_logger do |logger|
doc = document_from_string input
ref = doc.catalog[:refs]['_do_not_repeat_yourself']
refute_nil ref
assert_nil ref.reftext
assert_equal 'Do Not Repeat Yourself', ref.title
assert_equal '_do_not_repeat_yourself', (doc.resolve_id 'Do Not Repeat Yourself')
assert_message logger, :WARN, '<stdin>: line 6: id assigned to section already in use: _do_not_repeat_yourself', Hash
assert_equal 2, (doc.convert.scan 'id="_do_not_repeat_yourself"').size
end
end
test 'duplicate block id should not overwrite existing section id entry in references table' do
input = <<~'EOS'
[#install]
== First Install
content
[#install]
content
EOS
using_memory_logger do |logger|
doc = document_from_string input
ref = doc.catalog[:refs]['install']
refute_nil ref
assert_nil ref.reftext
assert_equal 'First Install', ref.title
assert_equal 'install', (doc.resolve_id 'First Install')
assert_message logger, :WARN, '<stdin>: line 7: id assigned to block already in use: install', Hash
end
end
end
context 'Levels' do
context 'Document Title (Level 0)' do
test 'document title with multiline syntax' do
title = 'My Title'
chars = '=' * title.length
assert_xpath '//h1[not(@id)][text() = "My Title"]', convert_string(title + "\n" + chars)
assert_xpath '//h1[not(@id)][text() = "My Title"]', convert_string(title + "\n" + chars + "\n")
end
test 'document title with multiline syntax, give a char' do
title = 'My Title'
chars = '=' * (title.length + 1)
assert_xpath '//h1[not(@id)][text() = "My Title"]', convert_string(title + "\n" + chars)
assert_xpath '//h1[not(@id)][text() = "My Title"]', convert_string(title + "\n" + chars + "\n")
end
test 'document title with multiline syntax, take a char' do
title = 'My Title'
chars = '=' * (title.length - 1)
assert_xpath '//h1[not(@id)][text() = "My Title"]', convert_string(title + "\n" + chars)
assert_xpath '//h1[not(@id)][text() = "My Title"]', convert_string(title + "\n" + chars + "\n")
end
test 'document title with multiline syntax and unicode characters' do
input = <<~'EOS'
AsciiDoc Writer’s Guide
=======================
Author Name
preamble
EOS
result = convert_string input
assert_xpath '//h1', result, 1
assert_xpath '//h1[text()="AsciiDoc Writer’s Guide"]', result, 1
end
test 'not enough chars for a multiline document title' do
title = 'My Title'
chars = '=' * (title.length - 2)
using_memory_logger do |logger|
output = convert_string title + "\n" + chars
assert_xpath '//h1', output, 0
refute_empty logger
logger.clear
output = convert_string title + "\n" + chars + "\n"
assert_xpath '//h1', output, 0
refute_empty logger
end
end
test 'too many chars for a multiline document title' do
title = 'My Title'
chars = '=' * (title.length + 2)
using_memory_logger do |logger|
output = convert_string title + "\n" + chars
assert_xpath '//h1', output, 0
refute_empty logger
logger.clear
output = convert_string title + "\n" + chars + "\n"
assert_xpath '//h1', output, 0
refute_empty logger
end
end
test 'document title with multiline syntax cannot begin with a dot' do
title = '.My Title'
chars = '=' * title.length
using_memory_logger do |logger|
output = convert_string title + "\n" + chars
assert_xpath '//h1', output, 0
refute_empty logger
end
end
test 'document title with atx syntax' do
assert_xpath '//h1[not(@id)][text() = "My Title"]', convert_string('= My Title')
end
test 'document title with symmetric syntax' do
assert_xpath '//h1[not(@id)][text() = "My Title"]', convert_string('= My Title =')
end
test 'document title created from leveloffset shift defined in document' do
assert_xpath '//h1[not(@id)][text() = "Document Title"]', convert_string(%(:leveloffset: -1\n== Document Title))
end
test 'document title created from leveloffset shift defined in API' do
assert_xpath '//h1[not(@id)][text() = "Document Title"]', convert_string('== Document Title', attributes: { 'leveloffset' => '-1@' })
end
test 'should assign id on document title to body' do
input = <<~'EOS'
[[idname]]
= Document Title
content
EOS
output = convert_string input
assert_css 'body#idname', output, 1
end
test 'should assign id defined using shorthand syntax on document title to body' do
input = <<~'EOS'
[#idname]
= Document Title
content
EOS
output = convert_string input
assert_css 'body#idname', output, 1
end
test 'should use ID defined in block attributes instead of ID defined inline' do
input = <<~'EOS'
[#idname-block]
= Document Title [[idname-inline]]
content
EOS
output = convert_string input
assert_css 'body#idname-block', output, 1
end
test 'block id above document title sets id on document' do
input = <<~'EOS'
[[reference]]
= Reference Manual
:css-signature: refguide
preamble
EOS
doc = document_from_string input
assert_equal 'reference', doc.id
assert_equal 'refguide', doc.attr('css-signature')
output = doc.convert
assert_css 'body#reference', output, 1
end
test 'should register document in catalog if id is set' do
input = <<~'EOS'
[[manual,Manual]]
= Reference Manual
preamble
EOS
doc = document_from_string input
assert_equal 'manual', doc.id
assert_equal 'Manual', doc.attributes['reftext']
assert_equal doc, doc.catalog[:refs]['manual']
end
test 'should compute xreftext to document title' do
input = <<~'EOS'
[#manual]
= Reference Manual
:xrefstyle: full
This is the <<manual>>.
EOS
output = convert_string input
assert_xpath '//a[text()="Reference Manual"]', output, 1
end
test 'should not interpret level-0 section as document title if it has a style' do
input = <<~'EOS'
[glossary]
= Document Title
content
EOS
using_memory_logger do |logger|
doc = document_from_string input
assert_message logger, :ERROR, '<stdin>: line 2: level 0 sections can only be used when doctype is book', Hash
refute doc.header?
assert_nil doc.attributes['title']
assert_equal 'glossary', doc.blocks[0].attributes['style']
end
end
test 'should discard role and options shorthand attributes defined on document title' do
input = <<~'EOS'
[#idname.rolename%optionname]
= Document Title
content
EOS
doc = document_from_string input
assert_empty doc.blocks[0].attributes
output = doc.convert
assert_css '#idname', output, 1
assert_css 'body#idname', output, 1
assert_css '.rolename', output, 1
assert_css 'body.rolename', output, 1
end
end
context 'Level 1' do
test 'with multiline syntax' do
assert_xpath '//h2[@id="_my_section"][text() = "My Section"]', convert_string(%(My Section\n-----------))
end
test 'should not recognize underline containing a mix of characters as setext section title' do
input = <<~'EOS'
My Section
----^^----
EOS
result = convert_string_to_embedded input
assert_xpath '//h2[@id="_my_section"][text() = "My Section"]', result, 0
assert_includes result, '----^^----'
end
test 'should not recognize section title that does not contain alphanumeric character' do
input = <<~'EOS'
!@#$
----
EOS
using_memory_logger do |logger|
result = convert_string_to_embedded input
assert_css 'h2', result, 0
assert_message logger, :WARN, '<stdin>: line 2: unterminated listing block', Hash
end
end
test 'should not recognize section title that consists of only underscores' do
input = <<~'EOS'
____
----
EOS
using_memory_logger do |logger|
result = convert_string_to_embedded input
assert_css 'h2', result, 0
assert_messages logger, [
[:WARN, '<stdin>: line 1: unterminated quote block', Hash],
[:WARN, '<stdin>: line 2: unterminated listing block', Hash],
]
end
end
test 'should preprocess second line of setext section title' do
input = <<~'EOS'
Section Title
ifdef::asciidoctor[]
-------------
endif::[]
EOS
result = convert_string_to_embedded input
assert_xpath '//h2', result, 1
end
test 'heading title with multiline syntax cannot begin with a dot' do
title = '.My Title'
chars = '-' * title.length
using_memory_logger do |logger|
output = convert_string title + "\n" + chars
assert_xpath '//h2', output, 0
refute_empty logger
end
end
test 'with atx syntax' do
assert_xpath '//h2[@id="_my_title"][text() = "My Title"]', convert_string('== My Title')
end
test 'with atx symmetric syntax' do
assert_xpath '//h2[@id="_my_title"][text() = "My Title"]', convert_string('== My Title ==')
end
test 'with atx non-matching symmetric syntax' do
assert_xpath '//h2[@id="_my_title"][text() = "My Title ==="]', convert_string('== My Title ===')
end
test 'with XML entity' do
assert_xpath %(//h2[@id='_whats_new'][text() = "What#{decode_char 8217}s new?"]), convert_string(%q(== What's new?))
end
test 'with non-word character' do
assert_xpath '//h2[@id="_whats_new"][text() = "What’s new?"]', convert_string('== What’s new?')
end
test 'with sequential non-word characters' do
assert_xpath '//h2[@id="_what_the_is_this"][text() = "What the #@$ is this?"]', convert_string('== What the #@$ is this?')
end
test 'with trailing whitespace' do
assert_xpath '//h2[@id="_my_title"][text() = "My Title"]', convert_string('== My Title ')
end
test 'with custom blank idprefix' do
assert_xpath '//h2[@id="my_title"][text() = "My Title"]', convert_string(%(:idprefix:\n\n== My Title ))
end
test 'with custom non-blank idprefix' do
assert_xpath '//h2[@id="ref_my_title"][text() = "My Title"]', convert_string(%(:idprefix: ref_\n\n== My Title ))
end
test 'with multibyte characters' do
input = '== Asciidoctor in 中文'
output = convert_string input
assert_xpath '//h2[@id="_asciidoctor_in_中文"][text()="Asciidoctor in 中文"]', output
end
test 'with only multibyte characters' do
input = '== 视图'
output = convert_string_to_embedded input
assert_xpath '//h2[@id="_视图"][text()="视图"]', output
end
test 'multiline syntax with only multibyte characters' do
input = <<~'EOS'
视图
--
content
连接器
---
content
EOS
output = convert_string_to_embedded input
assert_xpath '//h2[@id="_视图"][text()="视图"]', output
assert_xpath '//h2[@id="_连接器"][text()="连接器"]', output
end
end
context 'Level 2' do
test 'with multiline syntax' do
assert_xpath '//h3[@id="_my_section"][text() = "My Section"]', convert_string(%(:fragment:\nMy Section\n~~~~~~~~~~~))
end
test 'with atx line syntax' do
assert_xpath '//h3[@id="_my_title"][text() = "My Title"]', convert_string(%(:fragment:\n=== My Title))
end
end
context 'Level 3' do
test 'with multiline syntax' do
assert_xpath '//h4[@id="_my_section"][text() = "My Section"]', convert_string(%(:fragment:\nMy Section\n^^^^^^^^^^))
end
test 'with atx line syntax' do
assert_xpath '//h4[@id="_my_title"][text() = "My Title"]', convert_string(%(:fragment:\n==== My Title))
end
end
context 'Level 4' do
test 'with multiline syntax' do
assert_xpath '//h5[@id="_my_section"][text() = "My Section"]', convert_string(%(:fragment:\nMy Section\n++++++++++))
end
test 'with atx line syntax' do
assert_xpath '//h5[@id="_my_title"][text() = "My Title"]', convert_string(%(:fragment:\n===== My Title))
end
end
context 'Level 5' do
test 'with atx line syntax' do
assert_xpath '//h6[@id="_my_title"][text() = "My Title"]', convert_string(%(:fragment:\n====== My Title))
end
end
end
context 'Substitutions' do
test 'should apply substitutions in normal order' do
input = <<~'EOS'
== {link-url}[{link-text}]{tm}
The one and only!
EOS
output = convert_string_to_embedded input, attributes: {
'link-url' => 'https://acme.com',
'link-text' => 'ACME',
'tm' => '(TM)',
}
assert_css 'h2', output, 1
assert_css 'h2 a[href="https://acme.com"]', output, 1
assert_xpath %(//h2[contains(text(),"#{decode_char 8482}")]), output, 1
end
end
context 'Nesting' do
test 'should warn if section title is out of sequence' do
input = <<~'EOS'
= Document Title
== Section A
==== Nested Section
content
== Section B
content
EOS
using_memory_logger do |logger|
result = convert_string_to_embedded input
assert_xpath '//h4[text()="Nested Section"]', result, 1
assert_message logger, :WARN, '<stdin>: line 5: section title out of sequence: expected level 2, got level 3', Hash
end
end
test 'should warn if chapter title is out of sequence' do
input = <<~'EOS'
= Document Title
:doctype: book
=== Not a Chapter
content
EOS
using_memory_logger do |logger|
result = convert_string_to_embedded input
assert_xpath '//h3[text()="Not a Chapter"]', result, 1
assert_message logger, :WARN, '<stdin>: line 4: section title out of sequence: expected levels 0 or 1, got level 2', Hash
end
end
test 'should not warn if top-level section title is out of sequence when fragment attribute is set on document' do
input = <<~'EOS'
= Document Title
=== First Section
content
EOS
using_memory_logger do |logger|
convert_string_to_embedded input, attributes: { 'fragment' => '' }
assert_empty logger
end
end
test 'should warn if nested section title is out of sequence when fragment attribute is set on document' do
input = <<~'EOS'
= Document Title
=== First Section
===== Nested Section
EOS
using_memory_logger do |logger|
convert_string_to_embedded input, attributes: { 'fragment' => '' }
assert_message logger, :WARN, '<stdin>: line 5: section title out of sequence: expected level 3, got level 4', Hash
end
end
test 'should log error if subsections are found in special sections in article that do not support subsections' do
input = <<~'EOS'
= Document Title
== Section
=== Subsection of Section
allowed
[appendix]
== Appendix
=== Subsection of Appendix
allowed
[glossary]
== Glossary
=== Subsection of Glossary
not allowed
[bibliography]
== Bibliography
=== Subsection of Bibliography
not allowed
EOS
using_memory_logger do |logger|
convert_string_to_embedded input
assert_messages logger, [
[:ERROR, '<stdin>: line 19: glossary sections do not support nested sections', Hash],
[:ERROR, '<stdin>: line 26: bibliography sections do not support nested sections', Hash],
]
end
end
test 'should log error if subsections are found in special sections in book that do not support subsections' do
input = <<~'EOS'
= Document Title
:doctype: book
[preface]
= Preface
=== Subsection of Preface
allowed
[colophon]
= Colophon
=== Subsection of Colophon
not allowed
[dedication]
= Dedication
=== Subsection of Dedication
not allowed
= Part 1
[abstract]
== Abstract
=== Subsection of Abstract
allowed
== Chapter 1
=== Subsection of Chapter
allowed
[appendix]
= Appendix
=== Subsection of Appendix
allowed
[glossary]
= Glossary