forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsections_test.rb
2521 lines (1862 loc) · 64.2 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
# encoding: UTF-8
unless defined? ASCIIDOCTOR_PROJECT_DIR
$: << File.dirname(__FILE__); $:.uniq!
require 'test_helper'
end
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 'synthetic id replaces non-word characters with underscores' do
sec = block_from_string("== We're back!")
assert_equal '_we_re_back', sec.id
end
test 'synthetic id removes repeating underscores' 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 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 set to blank' do
sec = block_from_string(":idseparator:\n\n== Section One")
assert_equal '_sectionone', sec.id
end
test 'synthetic ids can be disabled' do
sec = block_from_string(":sectids!:\n\n== Section One\n")
assert sec.id.nil?
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 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 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 '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
reftext = doc.references[:ids]['install']
refute_nil reftext
assert_equal 'Install Procedure', reftext
end
test 'should use specified reftext when registering section reference' do
input = <<-EOS
[reftext="Install Procedure"]
== Install
content
EOS
doc = document_from_string input
reftext = doc.references[:ids]['_install']
refute_nil reftext
assert_equal 'Install Procedure', reftext
end
end
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']", render_string(title + "\n" + chars)
assert_xpath "//h1[not(@id)][text() = 'My Title']", render_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']", render_string(title + "\n" + chars)
assert_xpath "//h1[not(@id)][text() = 'My Title']", render_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']", render_string(title + "\n" + chars)
assert_xpath "//h1[not(@id)][text() = 'My Title']", render_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 = render_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)
assert_xpath '//h1', render_string(title + "\n" + chars), 0
assert_xpath '//h1', render_string(title + "\n" + chars + "\n"), 0
end
test "too many chars for a multiline document title" do
title = "My Title"
chars = "=" * (title.length + 2)
assert_xpath '//h1', render_string(title + "\n" + chars), 0
assert_xpath '//h1', render_string(title + "\n" + chars + "\n"), 0
end
test "document title with multiline syntax cannot begin with a dot" do
title = ".My Title"
chars = "=" * title.length
assert_xpath '//h1', render_string(title + "\n" + chars), 0
end
test "document title with single-line syntax" do
assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string("= My Title")
end
test "document title with symmetric syntax" do
assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string("= My Title =")
end
end
context "level 1" do
test "with multiline syntax" do
assert_xpath "//h2[@id='_my_section'][text() = 'My Section']", render_string("My Section\n-----------")
end
test "heading title with multiline syntax cannot begin with a dot" do
title = ".My Title"
chars = "-" * title.length
assert_xpath '//h2', render_string(title + "\n" + chars), 0
end
test "with single-line syntax" do
assert_xpath "//h2[@id='_my_title'][text() = 'My Title']", render_string("== My Title")
end
test "with single-line symmetric syntax" do
assert_xpath "//h2[@id='_my_title'][text() = 'My Title']", render_string("== My Title ==")
end
test "with single-line non-matching symmetric syntax" do
assert_xpath "//h2[@id='_my_title'][text() = 'My Title ===']", render_string("== My Title ===")
end
test "with XML entity" do
assert_xpath "//h2[@id='_where_s_the_love'][text() = \"Where#{[8217].pack('U*')}s the love?\"]", render_string("== Where's the love?")
end
test "with non-word character" do
assert_xpath "//h2[@id='_where_s_the_love'][text() = \"Where’s the love?\"]", render_string("== Where’s the love?")
end
test "with sequential non-word characters" do
assert_xpath "//h2[@id='_what_the_is_this'][text() = 'What the \#@$ is this?']", render_string('== What the #@$ is this?')
end
test "with trailing whitespace" do
assert_xpath "//h2[@id='_my_title'][text() = 'My Title']", render_string("== My Title ")
end
test "with custom blank idprefix" do
assert_xpath "//h2[@id='my_title'][text() = 'My Title']", render_string(":idprefix:\n\n== My Title ")
end
test "with custom non-blank idprefix" do
assert_xpath "//h2[@id='ref_my_title'][text() = 'My Title']", render_string(":idprefix: ref_\n\n== My Title ")
end
test 'with multibyte characters' do
input = <<-EOS
== Asciidoctor in 中文
EOS
output = render_string input
if ::RUBY_MIN_VERSION_1_9
assert_xpath '//h2[@id="_asciidoctor_in_中文"][text()="Asciidoctor in 中文"]', output
else
assert_xpath '//h2[@id="_asciidoctor_in"][text()="Asciidoctor in 中文"]', output
end
end
test 'with only multibyte characters' do
input = <<-EOS
== 视图
EOS
output = render_embedded_string input
assert_xpath '//h2[@id="_视图"][text()="视图"]', output
end if ::RUBY_MIN_VERSION_1_9
test 'multiline syntax with only multibyte characters' do
input = <<-EOS
视图
--
content
连接器
---
content
EOS
output = render_embedded_string input
assert_xpath '//h2[@id="_视图"][text()="视图"]', output
assert_xpath '//h2[@id="_连接器"][text()="连接器"]', output
end if ::RUBY_MIN_VERSION_1_9
end
context "level 2" do
test "with multiline syntax" do
assert_xpath "//h3[@id='_my_section'][text() = 'My Section']", render_string(":fragment:\nMy Section\n~~~~~~~~~~~")
end
test "with single line syntax" do
assert_xpath "//h3[@id='_my_title'][text() = 'My Title']", render_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']", render_string(":fragment:\nMy Section\n^^^^^^^^^^")
end
test "with single line syntax" do
assert_xpath "//h4[@id='_my_title'][text() = 'My Title']", render_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']", render_string(":fragment:\nMy Section\n++++++++++")
end
test "with single line syntax" do
assert_xpath "//h5[@id='_my_title'][text() = 'My Title']", render_string(":fragment:\n===== My Title")
end
end
context "level 5" do
test "with single line syntax" do
assert_xpath "//h6[@id='_my_title'][text() = 'My Title']", render_string(":fragment:\n====== My Title")
end
end
context 'Markdown-style headings' do
test 'single-line document title with leading marker' do
input = <<-EOS
# Document Title
EOS
output = render_string input
assert_xpath "//h1[not(@id)][text() = 'Document Title']", output, 1
end
test 'single-line document title with symmetric markers' do
input = <<-EOS
# Document Title #
EOS
output = render_string input
assert_xpath "//h1[not(@id)][text() = 'Document Title']", output, 1
end
test 'single-line section title with leading marker' do
input = <<-EOS
## Section One
blah blah
EOS
output = render_string input
assert_xpath "//h2[@id='_section_one'][text() = 'Section One']", output, 1
end
test 'single-line section title with symmetric markers' do
input = <<-EOS
## Section One ##
blah blah
EOS
output = render_string input
assert_xpath "//h2[@id='_section_one'][text() = 'Section One']", output, 1
end
end
context 'Floating Title' do
test 'should create floating title if style is float' do
input = <<-EOS
[float]
= Independent Heading!
not in section
EOS
output = render_embedded_string input
assert_xpath '/h1[@id="_independent_heading"]', output, 1
assert_xpath '/h1[@class="float"]', output, 1
assert_xpath %(/h1[@class="float"][text()="Independent Heading!"]), output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]', output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]/p', output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]/p[text()="not in section"]', output, 1
end
test 'should create floating title if style is discrete' do
input = <<-EOS
[discrete]
=== Independent Heading!
not in section
EOS
output = render_embedded_string input
assert_xpath '/h3', output, 1
assert_xpath '/h3[@id="_independent_heading"]', output, 1
assert_xpath '/h3[@class="discrete"]', output, 1
assert_xpath %(/h3[@class="discrete"][text()="Independent Heading!"]), output, 1
assert_xpath '/h3/following-sibling::*[@class="paragraph"]', output, 1
assert_xpath '/h3/following-sibling::*[@class="paragraph"]/p', output, 1
assert_xpath '/h3/following-sibling::*[@class="paragraph"]/p[text()="not in section"]', output, 1
end
test 'should create floating title if style is float with shorthand role and id' do
input = <<-EOS
[float.independent#first]
= Independent Heading!
not in section
EOS
output = render_embedded_string input
assert_xpath '/h1[@id="first"]', output, 1
assert_xpath '/h1[@class="float independent"]', output, 1
assert_xpath %(/h1[@class="float independent"][text()="Independent Heading!"]), output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]', output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]/p', output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]/p[text()="not in section"]', output, 1
end
test 'should create floating title if style is discrete with shorthand role and id' do
input = <<-EOS
[discrete.independent#first]
= Independent Heading!
not in section
EOS
output = render_embedded_string input
assert_xpath '/h1[@id="first"]', output, 1
assert_xpath '/h1[@class="discrete independent"]', output, 1
assert_xpath %(/h1[@class="discrete independent"][text()="Independent Heading!"]), output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]', output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]/p', output, 1
assert_xpath '/h1/following-sibling::*[@class="paragraph"]/p[text()="not in section"]', output, 1
end
test 'floating title should be a block with context floating_title' do
input = <<-EOS
[float]
=== Independent Heading!
not in section
EOS
doc = document_from_string input
floatingtitle = doc.blocks.first
assert floatingtitle.is_a?(Asciidoctor::Block)
assert floatingtitle.context != :section
assert_equal :floating_title, floatingtitle.context
assert_equal '_independent_heading', floatingtitle.id
assert doc.references[:ids].has_key?('_independent_heading')
end
test 'can assign explicit id to floating title' do
input = <<-EOS
[[unchained]]
[float]
=== Independent Heading!
not in section
EOS
doc = document_from_string input
floating_title = doc.blocks.first
assert_equal 'unchained', floating_title.id
assert doc.references[:ids].has_key?('unchained')
end
test 'should not include floating title in toc' do
input = <<-EOS
:toc:
== Section One
[float]
=== Miss Independent
== Section Two
EOS
output = render_string input
assert_xpath '//*[@id="toc"]', output, 1
assert_xpath %(//*[@id="toc"]//a[contains(text(), "Section ")]), output, 2
assert_xpath %(//*[@id="toc"]//a[text()="Miss Independent"]), output, 0
end
test 'should not set id on floating title if sectids attribute is unset' do
input = <<-EOS
[float]
=== Independent Heading!
not in section
EOS
output = render_embedded_string input, :attributes => {'sectids' => nil}
assert_xpath '/h3', output, 1
assert_xpath '/h3[@id="_independent_heading"]', output, 0
assert_xpath '/h3[@class="float"]', output, 1
end
test 'should use explicit id for floating title if specified' do
input = <<-EOS
[[free]]
[float]
== Independent Heading!
not in section
EOS
output = render_embedded_string input
assert_xpath '/h2', output, 1
assert_xpath '/h2[@id="free"]', output, 1
assert_xpath '/h2[@class="float"]', output, 1
end
test 'should add role to class attribute on floating title' do
input = <<-EOS
[float, role="isolated"]
== Independent Heading!
not in section
EOS
output = render_embedded_string input
assert_xpath '/h2', output, 1
assert_xpath '/h2[@id="_independent_heading"]', output, 1
assert_xpath '/h2[@class="float isolated"]', output, 1
end
test 'should use specified id and reftext when registering discrete section reference' do
input = <<-EOS
[[install,Install Procedure]]
[discrete]
== Install
content
EOS
doc = document_from_string input
reftext = doc.references[:ids]['install']
refute_nil reftext
assert_equal 'Install Procedure', reftext
end
test 'should use specified reftext when registering discrete section reference' do
input = <<-EOS
[reftext="Install Procedure"]
[discrete]
== Install
content
EOS
doc = document_from_string input
reftext = doc.references[:ids]['_install']
refute_nil reftext
assert_equal 'Install Procedure', reftext
end
end
context 'Level offset' do
test 'should print error if standalone document is included without level offset' do
input = <<-EOS
= Master Document
Doc Writer
text in master
// begin simulated include::[]
= Standalone Document
:author: Junior Writer
text in standalone
// end simulated include::[]
EOS
output, errors = nil
redirect_streams do |out, err|
output = render_string input
errors = err.string
end
assert !errors.empty?
assert_match(/only book doctypes can contain level 0 sections/, errors)
end
test 'should add level offset to section level' do
input = <<-EOS
= Master Document
Doc Writer
Master document written by {author}.
:leveloffset: 1
// begin simulated include::[]
= Standalone Document
:author: Junior Writer
Standalone document written by {author}.
== Section in Standalone
Standalone section text.
// end simulated include::[]
:leveloffset!:
== Section in Master
Master section text.
EOS
output = nil
errors = nil
redirect_streams do |out, err|
output = render_string input
errors = out.string
end
assert errors.empty?
assert_match(/Master document written by Doc Writer/, output)
assert_match(/Standalone document written by Junior Writer/, output)
assert_xpath '//*[@class="sect1"]/h2[text() = "Standalone Document"]', output, 1
assert_xpath '//*[@class="sect2"]/h3[text() = "Section in Standalone"]', output, 1
assert_xpath '//*[@class="sect1"]/h2[text() = "Section in Master"]', output, 1
end
test 'level offset should be added to floating title' do
input = <<-EOS
= Master Document
Doc Writer
:leveloffset: 1
[float]
= Floating Title
EOS
output = render_string input
assert_xpath '//h2[@class="float"][text() = "Floating Title"]', output, 1
end
test 'should be able to reset level offset' do
input = <<-EOS
= Master Document
Doc Writer
Master preamble.
:leveloffset: 1
= Standalone Document
Standalone preamble.
:leveloffset!:
== Level 1 Section
EOS
output = render_string input
assert_xpath '//*[@class = "sect1"]/h2[text() = "Standalone Document"]', output, 1
assert_xpath '//*[@class = "sect1"]/h2[text() = "Level 1 Section"]', output, 1
end
test 'should add relative offset value to current leveloffset' do
input = <<-EOS
= Master Document
Doc Writer
Master preamble.
:leveloffset: 1
= Chapter 1
content
:leveloffset: +1
= Standalone Section
content
EOS
output = render_string input
assert_xpath '//*[@class = "sect1"]/h2[text() = "Chapter 1"]', output, 1
assert_xpath '//*[@class = "sect2"]/h3[text() = "Standalone Section"]', output, 1
end
end
context 'Section Numbering' do
test 'should create section number with one entry for level 1' do
sect1 = Asciidoctor::Section.new
assert_equal '1.', sect1.sectnum
end
test 'should create section number with two entries for level 2' do
sect1 = Asciidoctor::Section.new
sect1_1 = Asciidoctor::Section.new(sect1)
sect1 << sect1_1
assert_equal '1.1.', sect1_1.sectnum
end
test 'should create section number with three entries for level 3' do
sect1 = Asciidoctor::Section.new
sect1_1 = Asciidoctor::Section.new(sect1)
sect1 << sect1_1
sect1_1_1 = Asciidoctor::Section.new(sect1_1)
sect1_1 << sect1_1_1
assert_equal '1.1.1.', sect1_1_1.sectnum
end
test 'should create section number for second section in level' do
sect1 = Asciidoctor::Section.new
sect1_1 = Asciidoctor::Section.new(sect1)
sect1 << sect1_1
sect1_2 = Asciidoctor::Section.new(sect1)
sect1 << sect1_2
assert_equal '1.2.', sect1_2.sectnum
end
test 'sectnum should use specified delimiter and append string' do
sect1 = Asciidoctor::Section.new
sect1_1 = Asciidoctor::Section.new(sect1)
sect1 << sect1_1
sect1_1_1 = Asciidoctor::Section.new(sect1_1)
sect1_1 << sect1_1_1
assert_equal '1,1,1,', sect1_1_1.sectnum(',')
assert_equal '1:1:1', sect1_1_1.sectnum(':', false)
end
test 'should render section numbers when sectnums attribute is set' do
input = <<-EOS
= Title
:sectnums:
== Section_1
text
=== Section_1_1
text
==== Section_1_1_1
text
== Section_2
text
=== Section_2_1
text
=== Section_2_2
text
EOS
output = render_string input
assert_xpath '//h2[@id="_section_1"][starts-with(text(), "1. ")]', output, 1
assert_xpath '//h3[@id="_section_1_1"][starts-with(text(), "1.1. ")]', output, 1
assert_xpath '//h4[@id="_section_1_1_1"][starts-with(text(), "1.1.1. ")]', output, 1
assert_xpath '//h2[@id="_section_2"][starts-with(text(), "2. ")]', output, 1
assert_xpath '//h3[@id="_section_2_1"][starts-with(text(), "2.1. ")]', output, 1
assert_xpath '//h3[@id="_section_2_2"][starts-with(text(), "2.2. ")]', output, 1
end
test 'should render section numbers when numbered attribute is set' do
input = <<-EOS
= Title
:numbered:
== Section_1
text
=== Section_1_1
text
==== Section_1_1_1
text
== Section_2
text
=== Section_2_1
text
=== Section_2_2
text
EOS
output = render_string input
assert_xpath '//h2[@id="_section_1"][starts-with(text(), "1. ")]', output, 1
assert_xpath '//h3[@id="_section_1_1"][starts-with(text(), "1.1. ")]', output, 1
assert_xpath '//h4[@id="_section_1_1_1"][starts-with(text(), "1.1.1. ")]', output, 1
assert_xpath '//h2[@id="_section_2"][starts-with(text(), "2. ")]', output, 1
assert_xpath '//h3[@id="_section_2_1"][starts-with(text(), "2.1. ")]', output, 1
assert_xpath '//h3[@id="_section_2_2"][starts-with(text(), "2.2. ")]', output, 1
end
test 'blocks should have level' do
input = <<-EOS
= Title
preamble
== Section 1
paragraph
=== Section 1.1
paragraph
EOS
doc = document_from_string input
assert_equal 0, doc.blocks[0].level
assert_equal 1, doc.blocks[1].level
assert_equal 1, doc.blocks[1].blocks[0].level
assert_equal 2, doc.blocks[1].blocks[1].level
assert_equal 2, doc.blocks[1].blocks[1].blocks[0].level
end
test 'section numbers should not increment when numbered attribute is turned off within document' do
input = <<-EOS
= Document Title
:numbered:
:numbered!:
== Colophon Section
== Another Colophon Section
== Final Colophon Section
:numbered:
== Section One
=== Section One Subsection
== Section Two
== Section Three
EOS
output = render_string input
assert_xpath '//h1[text()="Document Title"]', output, 1
assert_xpath '//h2[@id="_colophon_section"][text()="Colophon Section"]', output, 1
assert_xpath '//h2[@id="_another_colophon_section"][text()="Another Colophon Section"]', output, 1
assert_xpath '//h2[@id="_final_colophon_section"][text()="Final Colophon Section"]', output, 1
assert_xpath '//h2[@id="_section_one"][text()="1. Section One"]', output, 1
assert_xpath '//h3[@id="_section_one_subsection"][text()="1.1. Section One Subsection"]', output, 1
assert_xpath '//h2[@id="_section_two"][text()="2. Section Two"]', output, 1
assert_xpath '//h2[@id="_section_three"][text()="3. Section Three"]', output, 1
end
test 'section numbers can be toggled even if numbered attribute is enable via the API' do
input = <<-EOS
= Document Title
:numbered!:
== Colophon Section
== Another Colophon Section
== Final Colophon Section
:numbered:
== Section One
=== Section One Subsection
== Section Two
== Section Three
EOS
output = render_string input, :attributes => {'numbered' => ''}
assert_xpath '//h1[text()="Document Title"]', output, 1
assert_xpath '//h2[@id="_colophon_section"][text()="Colophon Section"]', output, 1
assert_xpath '//h2[@id="_another_colophon_section"][text()="Another Colophon Section"]', output, 1
assert_xpath '//h2[@id="_final_colophon_section"][text()="Final Colophon Section"]', output, 1
assert_xpath '//h2[@id="_section_one"][text()="1. Section One"]', output, 1
assert_xpath '//h3[@id="_section_one_subsection"][text()="1.1. Section One Subsection"]', output, 1
assert_xpath '//h2[@id="_section_two"][text()="2. Section Two"]', output, 1
assert_xpath '//h2[@id="_section_three"][text()="3. Section Three"]', output, 1
end
test 'section numbers cannot be toggled even if numbered attribute is disabled via the API' do
input = <<-EOS
= Document Title
:numbered!:
== Colophon Section
== Another Colophon Section
== Final Colophon Section
:numbered:
== Section One
=== Section One Subsection
== Section Two
== Section Three
EOS
output = render_string input, :attributes => {'numbered!' => ''}
assert_xpath '//h1[text()="Document Title"]', output, 1
assert_xpath '//h2[@id="_colophon_section"][text()="Colophon Section"]', output, 1
assert_xpath '//h2[@id="_another_colophon_section"][text()="Another Colophon Section"]', output, 1
assert_xpath '//h2[@id="_final_colophon_section"][text()="Final Colophon Section"]', output, 1
assert_xpath '//h2[@id="_section_one"][text()="Section One"]', output, 1
assert_xpath '//h3[@id="_section_one_subsection"][text()="Section One Subsection"]', output, 1
assert_xpath '//h2[@id="_section_two"][text()="Section Two"]', output, 1
assert_xpath '//h2[@id="_section_three"][text()="Section Three"]', output, 1
end
# NOTE AsciiDoc fails this test because it does not properly check for a None value when looking up the numbered attribute
test 'section numbers should not increment until numbered attribute is turned back on' do
input = <<-EOS
= Document Title
:numbered!:
== Colophon Section
== Another Colophon Section
== Final Colophon Section
:numbered:
== Section One
=== Section One Subsection
== Section Two
== Section Three
EOS
output = render_string input
assert_xpath '//h1[text()="Document Title"]', output, 1
assert_xpath '//h2[@id="_colophon_section"][text()="Colophon Section"]', output, 1
assert_xpath '//h2[@id="_another_colophon_section"][text()="Another Colophon Section"]', output, 1
assert_xpath '//h2[@id="_final_colophon_section"][text()="Final Colophon Section"]', output, 1
assert_xpath '//h2[@id="_section_one"][text()="1. Section One"]', output, 1
assert_xpath '//h3[@id="_section_one_subsection"][text()="1.1. Section One Subsection"]', output, 1
assert_xpath '//h2[@id="_section_two"][text()="2. Section Two"]', output, 1
assert_xpath '//h2[@id="_section_three"][text()="3. Section Three"]', output, 1
end
test 'table with asciidoc content should not disable numbering of subsequent sections' do
input = <<-EOS
= Document Title
:numbered: