forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manpage_test.rb
1309 lines (1104 loc) · 35.7 KB
/
manpage_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 'Manpage' do
SAMPLE_MANPAGE_HEADER = <<~'EOS'.chop
= command (1)
Author Name
:doctype: manpage
:man manual: Command Manual
:man source: Command 1.2.3
== NAME
command - does stuff
== SYNOPSIS
*command* [_OPTION_]... _FILE_...
== DESCRIPTION
EOS
context 'Configuration' do
test 'should set proper manpage-related attributes' do
input = SAMPLE_MANPAGE_HEADER
doc = Asciidoctor.load input, backend: :manpage
assert_equal 'man', doc.attributes['filetype']
assert_equal '', doc.attributes['filetype-man']
assert_equal '1', doc.attributes['manvolnum']
assert_equal '.1', doc.attributes['outfilesuffix']
assert_equal 'command', doc.attributes['manname']
assert_equal 'command', doc.attributes['mantitle']
assert_equal 'does stuff', doc.attributes['manpurpose']
assert_equal 'command', doc.attributes['docname']
end
test 'should not escape hyphen when printing manname in NAME section' do
input = SAMPLE_MANPAGE_HEADER.sub(/^command - /, 'git-describe - ')
output = Asciidoctor.convert input, backend: :manpage, standalone: true
assert_includes output, %(\n.SH "NAME"\ngit-describe \\- does stuff\n)
end
test 'should output multiple mannames in NAME section' do
input = SAMPLE_MANPAGE_HEADER.sub(/^command - /, 'command, alt_command - ')
output = Asciidoctor.convert input, backend: :manpage, standalone: true
assert_includes output.lines, %(command, alt_command \\- does stuff\n)
end
test 'should substitute attributes in manname and manpurpose in NAME section' do
input = <<~'EOS'
= {cmdname} (1)
Author Name
:doctype: manpage
:man manual: Foo Bar Manual
:man source: Foo Bar 1.0
== NAME
{cmdname} - {cmdname} puts the foo in your bar
EOS
doc = Asciidoctor.load input, backend: :manpage, standalone: true, attributes: { 'cmdname' => 'foobar' }
assert_equal 'foobar', (doc.attr 'manname')
assert_equal ['foobar'], (doc.attr 'mannames')
assert_equal 'foobar puts the foo in your bar', (doc.attr 'manpurpose')
assert_equal 'foobar', (doc.attr 'docname')
end
test 'should not parse NAME section if manname and manpurpose attributes are set' do
input = <<~'EOS'
= foobar (1)
Author Name
:doctype: manpage
:man manual: Foo Bar Manual
:man source: Foo Bar 1.0
== SYNOPSIS
*foobar* [_OPTIONS_]...
== DESCRIPTION
When you need to put some foo on the bar.
EOS
attrs = { 'manname' => 'foobar', 'manpurpose' => 'puts some foo on the bar' }
doc = Asciidoctor.load input, backend: :manpage, standalone: true, attributes: attrs
assert_equal 'foobar', (doc.attr 'manname')
assert_equal ['foobar'], (doc.attr 'mannames')
assert_equal 'puts some foo on the bar', (doc.attr 'manpurpose')
assert_equal 'SYNOPSIS', doc.sections[0].title
end
test 'should normalize whitespace and skip line comments before and inside NAME section' do
input = <<~'EOS'
= foobar (1)
Author Name
:doctype: manpage
:man manual: Foo Bar Manual
:man source: Foo Bar 1.0
// this is the name section
== NAME
// it follows the form `name - description`
foobar - puts some foo
on the bar
// a little bit of this, a little bit of that
== SYNOPSIS
*foobar* [_OPTIONS_]...
== DESCRIPTION
When you need to put some foo on the bar.
EOS
doc = Asciidoctor.load input, backend: :manpage, standalone: true
assert_equal 'puts some foo on the bar', (doc.attr 'manpurpose')
end
test 'should parse malformed document with warnings' do
input = 'garbage in'
using_memory_logger do |logger|
doc = Asciidoctor.load input, backend: :manpage, standalone: true, attributes: { 'docname' => 'cmd' }
assert_equal 'cmd', doc.attr('manname')
assert_equal ['cmd'], doc.attr('mannames')
assert_equal '.1', doc.attr('outfilesuffix')
output = doc.convert
refute_empty logger.messages
assert_includes output, 'Title: cmd'
assert output.end_with?('garbage in')
end
end
test 'should warn if document title is non-conforming' do
input = <<~'EOS'
= command
== Name
command - does stuff
EOS
using_memory_logger do |logger|
document_from_string input, backend: :manpage
assert_message logger, :ERROR, '<stdin>: line 1: non-conforming manpage title', Hash
end
end
test 'should warn if first section is not name section' do
input = <<~'EOS'
= command(1)
== Synopsis
Does stuff.
EOS
using_memory_logger do |logger|
doc = document_from_string input, backend: :manpage
assert_message logger, :ERROR, '<stdin>: line 3: non-conforming name section body', Hash
refute_nil doc.sections[0]
assert_equal 'Synopsis', doc.sections[0].title
end
end
test 'should break circular reference in section title' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
[#a]
== A <<b>>
[#b]
== B <<a>>
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_match %r/^\.SH "A B \[A\]"$/, output
assert_match %r/^\.SH "B \[A\]"$/, output
end
test 'should define default linkstyle' do
input = SAMPLE_MANPAGE_HEADER
output = Asciidoctor.convert input, backend: :manpage, standalone: true
assert_includes output.lines, %(. LINKSTYLE blue R < >\n)
end
test 'should use linkstyle defined by man-linkstyle attribute' do
input = SAMPLE_MANPAGE_HEADER
output = Asciidoctor.convert input, backend: :manpage, standalone: true, attributes: { 'man-linkstyle' => 'cyan B \[fo] \[fc]' }
assert_includes output.lines, %(. LINKSTYLE cyan B \\[fo] \\[fc]\n)
end
test 'should require specialchars in value of man-linkstyle attribute defined in document to be escaped' do
input = <<~EOS.chop
:man-linkstyle: cyan R < >
#{SAMPLE_MANPAGE_HEADER}
EOS
output = Asciidoctor.convert input, backend: :manpage, standalone: true
assert_includes output.lines, %(. LINKSTYLE cyan R < >\n)
input = <<~EOS.chop
:man-linkstyle: pass:[cyan R < >]
#{SAMPLE_MANPAGE_HEADER}
EOS
output = Asciidoctor.convert input, backend: :manpage, standalone: true
assert_includes output.lines, %(. LINKSTYLE cyan R < >\n)
end
end
context 'Manify' do
test 'should unescape literal ampersand' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
(C) & (R) are translated to character references, but not the &.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal '\\(co & \\(rg are translated to character references, but not the &.', output.lines.last.chomp
end
test 'should replace numeric character reference for plus' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
A {plus} B
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal 'A + B', output.lines.last.chomp
end
test 'should replace numeric character reference for degree sign' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
0{deg} is freezing
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal '0\(de is freezing', output.lines.last.chomp
end
test 'should replace em dashes' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
go -- to
go--to
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_includes output, 'go \\(em to'
assert_includes output, 'go\\(emto'
end
test 'should replace quotes' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
'command'
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_includes output, '\*(Aqcommand\*(Aq'
end
test 'should escape lone period' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal '\&.', output.lines.last.chomp
end
test 'should escape raw macro' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
AAA this line of text should be show
.if 1 .nx
BBB this line and the one above it should be visible
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal '\&.if 1 .nx', output.lines[-2].chomp
end
test 'should escape ellipsis at start of line' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
-x::
Ao gravar o commit, acrescente uma linha que diz "(cherry picked from commit
...)" à mensagem de commit original para indicar qual commit esta mudança
foi escolhida. Isso é feito apenas para picaretas de cereja sem conflitos.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal '\&...', output.lines[-3][0..4].chomp
end
test 'should not escape ellipsis in the middle of a line' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
-x::
Ao gravar o commit, acrescente uma linha que diz
"(cherry picked from commit...)" à mensagem de commit
original para indicar qual commit esta mudança
foi escolhida. Isso é feito apenas para picaretas
de cereja sem conflitos.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_include 'commit...', output.lines[-5]
end
test 'should normalize whitespace in a paragraph' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
Oh, here it goes again
I should have known,
should have known,
should have known again
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_includes output, %(Oh, here it goes again\nI should have known,\nshould have known,\nshould have known again)
end
test 'should normalize whitespace in a list item' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
* Oh, here it goes again
I should have known,
should have known,
should have known again
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_includes output, %(Oh, here it goes again\nI should have known,\nshould have known,\nshould have known again)
end
test 'should drop principal text of list item in ulist if empty' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
* {empty}
+
the main text
EOS
expected_coda = <<~'EOS'.chop
.\}
the main text
.RE
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
test 'should drop principal text of list item in olist if empty' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
. {empty}
+
the main text
EOS
expected_coda = <<~'EOS'.chop
.\}
the main text
.RE
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
test 'should not add extra space before block content if dlist item has no text' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
term::
+
description
EOS
expected_coda = <<~'EOS'.chop
term
.RS 4
description
.RE
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
test 'should honor start attribute on ordered list' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
[start=5]
. five
. six
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_match %r/IP " 5\.".*five/m, output
assert_match %r/IP " 6\.".*six/m, output
end
test 'should collapse whitespace in the man manual and man source' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
Describe this thing.
EOS
output = Asciidoctor.convert input, backend: :manpage, standalone: true, attributes: {
'manmanual' => %(General\nCommands\nManual),
'mansource' => %(Control\nAll\nThe\nThings\n5.0),
}
assert_includes output, 'Manual: General Commands Manual'
assert_includes output, 'Source: Control All The Things 5.0'
assert_includes output, '"Control All The Things 5.0" "General Commands Manual"'
end
test 'should uppercase section titles without mangling formatting macros' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
does stuff
== "`Main`" _<Options>_
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_includes output, '.SH "\(lqMAIN\(rq \fI<OPTIONS>\fP"'
end
end
context 'Backslash' do
test 'should not escape spaces for empty manual or source fields' do
input = SAMPLE_MANPAGE_HEADER.lines.reject {|l| l.start_with? ':man ' }
output = Asciidoctor.convert input, backend: :manpage, standalone: true
assert_match ' Manual: \ \&', output
assert_match ' Source: \ \&', output
assert_match(/^\.TH "COMMAND" .* "\\ \\&" "\\ \\&"$/, output)
end
test 'should preserve backslashes in escape sequences' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
"`hello`" '`goodbye`' *strong* _weak_ `even`
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal '\(lqhello\(rq \(oqgoodbye\(cq \fBstrong\fP \fIweak\fP \f(CReven\fP', output.lines.last.chomp
end
test 'should preserve literal backslashes in content' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
\\.foo \\ bar \\\\ baz\\
more
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal '\(rs.foo \(rs bar \(rs\(rs baz\(rs', output.lines[-2].chomp
end
test 'should escape literal escape sequence' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
\\fB makes text bold
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_match '\(rsfB makes text bold', output
end
test 'should preserve inline breaks' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
Before break. +
After break.
EOS
expected = <<~'EOS'.chop
Before break.
.br
After break.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-3..-1].join
end
end
context 'URL macro' do
test 'should not leave blank line before URL macro' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
First paragraph.
http://asciidoc.org[AsciiDoc]
EOS
expected = <<~'EOS'.chop
.sp
First paragraph.
.sp
.URL "http://asciidoc.org" "AsciiDoc" ""
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-4..-1].join
end
test 'should not swallow content following URL' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
http://asciidoc.org[AsciiDoc] can be used to create man pages.
EOS
expected = <<~'EOS'.chop
.URL "http://asciidoc.org" "AsciiDoc" ""
can be used to create man pages.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-2..-1].join
end
test 'should pass adjacent character as final argument of URL macro' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
This is http://asciidoc.org[AsciiDoc].
EOS
expected = <<~'EOS'.chop
This is \c
.URL "http://asciidoc.org" "AsciiDoc" "."
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-2..-1].join
end
test 'should pass adjacent character as final argument of URL macro and move trailing content to next line' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
This is http://asciidoc.org[AsciiDoc], which can be used to write content.
EOS
expected = <<~'EOS'.chop
This is \c
.URL "http://asciidoc.org" "AsciiDoc" ","
which can be used to write content.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-3..-1].join
end
test 'should not leave blank lines between URLs on contiguous lines of input' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
The corresponding implementations are
http://clisp.sf.net[CLISP],
http://ccl.clozure.com[Clozure CL],
http://cmucl.org[CMUCL],
http://ecls.sf.net[ECL],
and http://sbcl.sf.net[SBCL].
EOS
expected = <<~'EOS'.chop
.sp
The corresponding implementations are
.URL "http://clisp.sf.net" "CLISP" ","
.URL "http://ccl.clozure.com" "Clozure CL" ","
.URL "http://cmucl.org" "CMUCL" ","
.URL "http://ecls.sf.net" "ECL" ","
and \c
.URL "http://sbcl.sf.net" "SBCL" "."
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-8..-1].join
end
test 'should not leave blank lines between URLs on same line of input' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
The corresponding implementations are http://clisp.sf.net[CLISP], http://ccl.clozure.com[Clozure CL], http://cmucl.org[CMUCL], http://ecls.sf.net[ECL], and http://sbcl.sf.net[SBCL].
EOS
expected = <<~'EOS'.chop
.sp
The corresponding implementations are \c
.URL "http://clisp.sf.net" "CLISP" ","
.URL "http://ccl.clozure.com" "Clozure CL" ","
.URL "http://cmucl.org" "CMUCL" ","
.URL "http://ecls.sf.net" "ECL" ","
and
.URL "http://sbcl.sf.net" "SBCL" "."
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-8..-1].join
end
test 'should not insert space between link and non-whitespace characters surrounding it' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
Please search |link:http://discuss.asciidoctor.org[the forums]| before asking.
EOS
expected = <<~'EOS'.chop
.sp
Please search |\c
.URL "http://discuss.asciidoctor.org" "the forums" "|"
before asking.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-4..-1].join
end
test 'should be able to use monospaced text inside a link' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
Enter the link:cat[`cat`] command.
EOS
expected = <<~'EOS'.chop
.sp
Enter the \c
.URL "cat" "\f(CRcat\fP" ""
command.
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-4..-1].join
end
end
context 'MTO macro' do
test 'should convert inline email macro into MTO macro' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
First paragraph.
mailto:[email protected][Contact the doc]
EOS
expected = <<~'EOS'.chop
.sp
First paragraph.
.sp
.MTO "doc\(atexample.org" "Contact the doc" ""
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_equal expected, output.lines[-4..-1].join
end
test 'should set text of MTO macro to blank for implicit email' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
Bugs fixed daily by [email protected].
EOS
expected_coda = <<~'EOS'.chop
Bugs fixed daily by \c
.MTO "doc\(atexample.org" "" "."
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
end
context 'Table' do
test 'should create header, body, and footer rows in correct order' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
[%header%footer]
|===
|Header
|Body 1
|Body 2
|Footer
|===
EOS
expected_coda = <<~'EOS'.chop
allbox tab(:);
lt.
T{
.sp
Header
T}
T{
.sp
Body 1
T}
T{
.sp
Body 2
T}
T{
.sp
Footer
T}
.TE
.sp
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
test 'should manify normal table cell content' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
|===
|*Col A* |_Col B_
|*bold* |`mono`
|_italic_ | #mark#
|===
EOS
output = Asciidoctor.convert input, backend: :manpage
refute_match(/<\/?BOUNDARY>/, output)
end
test 'should manify table title' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
.Table of options
|===
| Name | Description | Default
| dim
| dimension of the object
| 3
|===
EOS
expected_coda = <<~'EOS'.chop
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B Table 1. Table of options
.TS
allbox tab(:);
lt lt lt.
T{
.sp
Name
T}:T{
.sp
Description
T}:T{
.sp
Default
T}
T{
.sp
dim
T}:T{
.sp
dimension of the object
T}:T{
.sp
3
T}
.TE
.sp
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
test 'should manify and preserve whitespace in literal table cell' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
|===
|a l|b
c _d_
.
|===
EOS
expected_coda = <<~'EOS'.chop
.TS
allbox tab(:);
lt lt.
T{
.sp
a
T}:T{
.sp
.nf
b
c _d_
\&.
.fi
T}
.TE
.sp
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
end
context 'Images' do
test 'should replace block image with alt text enclosed in square brackets' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
Behold the wisdom of the Magic 8 Ball!
image::signs-point-to-yes.jpg[]
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? %(\n.sp\n[signs point to yes])
end
test 'should replace inline image with alt text enclosed in square brackets' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
The Magic 8 Ball says image:signs-point-to-yes.jpg[].
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_includes output, 'The Magic 8 Ball says [signs point to yes].'
end
test 'should place link after alt text for inline image if link is defined' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
The Magic 8 Ball says image:signs-point-to-yes.jpg[link=https://en.wikipedia.org/wiki/Magic_8-Ball].
EOS
output = Asciidoctor.convert input, backend: :manpage
assert_includes output, 'The Magic 8 Ball says [signs point to yes] <https://en.wikipedia.org/wiki/Magic_8\-Ball>.'
end
test 'should reference image with title usign styled xref' do
input = <<~EOS.chomp
#{SAMPLE_MANPAGE_HEADER}
To get your fortune, see <<magic-8-ball>>.
.Magic 8-Ball
[#magic-8-ball]
image::signs-point-to-yes.jpg[]
EOS
output = Asciidoctor.convert input, backend: :manpage, attributes: { 'xrefstyle' => 'full' }
lines = output.lines.map(&:chomp)
assert_includes lines, 'To get your fortune, see Figure 1, \(lqMagic 8\-Ball\(rq.'
assert_includes lines, '.B Figure 1. Magic 8\-Ball'
end
end
context 'Quote Block' do
test 'should indent quote block' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
[,James Baldwin]
____
Not everything that is faced can be changed.
But nothing can be changed until it is faced.
____
EOS
expected_coda = <<~'EOS'.chop
.RS 3
.ll -.6i
.sp
Not everything that is faced can be changed.
But nothing can be changed until it is faced.
.br
.RE
.ll
.RS 5
.ll -.10i
\(em James Baldwin
.RE
.ll
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
end
context 'Verse Block' do
test 'should preserve hard line breaks in verse block' do
input = SAMPLE_MANPAGE_HEADER.lines
synopsis_idx = input.find_index {|it| it == %(== SYNOPSIS\n) } + 2
input[synopsis_idx..synopsis_idx] = <<~'EOS'.lines
[verse]
_command_ [_OPTION_]... _FILE_...
EOS
input = <<~EOS.chop
#{input.join}
description
EOS
expected_coda = <<~'EOS'.chop
.SH "SYNOPSIS"
.sp
.nf
\fIcommand\fP [\fIOPTION\fP]... \fIFILE\fP...
.fi
.br
.SH "DESCRIPTION"
.sp
description
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
end
context 'Callout List' do
test 'should generate callout list using proper formatting commands' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
----
$ gem install asciidoctor # <1>
----
<1> Installs the asciidoctor gem from RubyGems.org
EOS
expected_coda = <<~'EOS'.chop
.TS
tab(:);
r lw(\n(.lu*75u/100u).
\fB(1)\fP\h'-2n':T{
Installs the asciidoctor gem from RubyGems.org
T}
.TE
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
end
context 'Page breaks' do
test 'should insert page break at location of page break macro' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
== Section With Break
before break
<<<
after break
EOS
expected_coda = <<~'EOS'.chop
.SH "SECTION WITH BREAK"
.sp
before break
.bp
.sp
after break
EOS
output = Asciidoctor.convert input, backend: :manpage
assert output.end_with? expected_coda
end
end
context 'UI macros' do
test 'should enclose button in square brackets and format as bold' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
== UI Macros
btn:[Save]
EOS
expected_coda = <<~'EOS'.chop
.SH "UI MACROS"
.sp
\fB[\0Save\0]\fP
EOS
output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
assert output.end_with? expected_coda
end
test 'should format single key in monospaced text' do
input = <<~EOS.chop
#{SAMPLE_MANPAGE_HEADER}
== UI Macros
kbd:[Enter]
EOS
expected_coda = <<~'EOS'.chop
.SH "UI MACROS"
.sp
\f(CREnter\fP