summaryrefslogtreecommitdiff
path: root/test/manpage_test.rb
blob: 0feb1469fcf333a5927a564ef21704193a99b1e0 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
# 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 replace invalid characters in mantitle in info comment' do
      input = <<~'EOS'
      = foo\--<bar> (1)
      Author Name
      :doctype: manpage
      :man manual: Foo Bar Manual
      :man source: Foo Bar 1.0

      == NAME

      foo-bar - puts the foo in your bar
      EOS

      doc = Asciidoctor.load input, backend: :manpage, standalone: true
      output = doc.convert
      assert_includes output, %(Title: foo--bar\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\n)
        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 &lt; &gt;\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 preserve break between paragraphs in normal table cell' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      [cols=3*]
      |===
      |single paragraph
      |first paragraph

      second paragraph
      |foo

      more foo

      even more foo
      |===
      EOS

      expected_coda = <<~'EOS'.chop
      .TS
      allbox tab(:);
      lt lt lt.
      T{
      single paragraph
      T}:T{
      first paragraph
      .sp
      second paragraph
      T}:T{
      foo
      .sp
      more foo
      .sp
      even more foo
      T}
      .TE
      .sp
      EOS
      output = Asciidoctor.convert input, backend: :manpage
      assert output.end_with? expected_coda
    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

    test 'should not uppercase monospace span in section titles' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      does stuff

      == `show` option
      EOS
      output = Asciidoctor.convert input, backend: :manpage
      assert_includes output, '.SH "\f(CRshow\fP OPTION"'
    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:doc@example.org[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 doc@example.org.
      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(:);
      ltB.
      T{
      Header
      T}
      .T&
      lt.
      T{
      Body 1
      T}
      T{
      Body 2
      T}
      T{
      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(:);
      ltB ltB ltB.
      T{
      Name
      T}:T{
      Description
      T}:T{
      Default
      T}
      .T&
      lt lt lt.
      T{
      dim
      T}:T{
      dimension of the object
      T}:T{
      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{
      a
      T}:T{
      .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 manify alt text of block image' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      image::rainbow.jpg["That's a double rainbow, otherwise known as rainbow{pp}!"]
      EOS

      output = Asciidoctor.convert input, backend: :manpage
      assert output.end_with? %/\n.sp\n[That\\(cqs a double rainbow, otherwise known as rainbow++!]/
    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
      EOS
      output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
      assert output.end_with? expected_coda
    end

    test 'should format each key in sequence as monospaced text separated by +' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      == UI Macros

      kbd:[Ctrl,s]
      EOS
      expected_coda = <<~'EOS'.chop
      .SH "UI MACROS"
      .sp
      \f(CRCtrl\0+\0s\fP
      EOS
      output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
      assert output.end_with? expected_coda
    end

    test 'should format single menu reference in italic' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      == UI Macros

      menu:File[]
      EOS
      expected_coda = <<~'EOS'.chop
      .SH "UI MACROS"
      .sp
      \fIFile\fP
      EOS
      output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
      assert output.end_with? expected_coda
    end

    test 'should format menu sequence in italic separated by carets' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      == UI Macros

      menu:File[New Tab]
      EOS
      expected_coda = <<~'EOS'.chop
      .SH "UI MACROS"
      .sp
      \fIFile\0\(fc\0New Tab\fP
      EOS
      output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
      assert output.end_with? expected_coda
    end

    test 'should format menu sequence with submenu in italic separated by carets' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      == UI Macros

      menu:View[Zoom > Zoom In]
      EOS
      expected_coda = <<~'EOS'.chop
      .SH "UI MACROS"
      .sp
      \fIView\fP\0\(fc\0\fIZoom\fP\0\(fc\0\fIZoom In\fP
      EOS
      output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
      assert output.end_with? expected_coda
    end
  end

  context 'xrefs' do
    test 'should populate automatic link text for internal xref' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      You can access this information using the options listed under <<_generic_program_information>>.

      == Options

      === Generic Program Information

      --help:: Output a usage message and exit.

      -V, --version:: Output the version number of grep and exit.
      EOS
      output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
      assert_includes output, 'You can access this information using the options listed under Generic Program Information.'
    end

    test 'should populate automatic link text for each occurrence of internal xref' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      You can access this information using the options listed under <<_generic_program_information>>.

      The options listed in <<_generic_program_information>> should always be used by themselves.

      == Options

      === Generic Program Information

      --help:: Output a usage message and exit.

      -V, --version:: Output the version number of grep and exit.
      EOS
      output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
      assert_includes output, 'You can access this information using the options listed under Generic Program Information.'
      assert_includes output, 'The options listed in Generic Program Information should always be used by themselves.'
    end

    test 'should uppercase the reftext for level-2 section titles if the reftext matches the section title' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      If you read nothing else, read the <<_foo_bar>> section.

      === Options

      --foo-bar _foobar_::
      Puts the foo in your bar.
      See <<_foo_bar>> section for details.

      == Foo Bar

      Foo goes with bar, not baz.
      EOS

      output = Asciidoctor.convert input, backend: :manpage, attributes: { 'experimental' => '' }
      assert_includes output, 'If you read nothing else, read the FOO BAR section.'
      assert_includes output, 'See FOO BAR section for details.'
    end
  end

  context 'Footnotes' do
    test 'should generate list of footnotes using numbered list with numbers enclosed in brackets' do
      [true, false].each do |standalone|
        input = <<~EOS.chop
        #{SAMPLE_MANPAGE_HEADER}

        text.footnote:[first footnote]

        more text.footnote:[second footnote]
        EOS
        expected_coda = <<~'EOS'.chop
        .sp
        text.[1]
        .sp
        more text.[2]
        .SH "NOTES"
        .IP [1]
        first footnote
        .IP [2]
        second footnote
        EOS
        if standalone
          expected_coda = <<~EOS.chop
          #{expected_coda}
          .SH "AUTHOR"
          .sp
          Author Name
          EOS
        end
        output = Asciidoctor.convert input, backend: :manpage, standalone: standalone
        assert output.end_with? expected_coda
      end
    end

    test 'should number footnotes according to footnote index' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      text.footnote:fn1[first footnote]footnote:[second footnote]

      more text.footnote:fn1[]
      EOS
      expected_coda = <<~'EOS'.chop
      .sp
      text.[1][2]
      .sp
      more text.[1]
      .SH "NOTES"
      .IP [1]
      first footnote
      .IP [2]
      second footnote
      EOS
      output = Asciidoctor.convert input, backend: :manpage
      assert output.end_with? expected_coda
    end

    test 'should format footnote with bare URL' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      text.footnote:[https://example.org]
      EOS
      expected_coda = <<~'EOS'.chop
      .SH "NOTES"
      .IP [1]
      .URL "https://example.org" "" ""
      EOS
      output = Asciidoctor.convert input, backend: :manpage
      assert output.end_with? expected_coda
    end

    test 'should format footnote with text before bare URL' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      text.footnote:[see https://example.org]
      EOS
      expected_coda = <<~'EOS'.chop
      .SH "NOTES"
      .IP [1]
      see \c
      .URL "https://example.org" "" ""
      EOS
      output = Asciidoctor.convert input, backend: :manpage
      assert output.end_with? expected_coda
    end

    test 'should format footnote with text after bare URL' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      text.footnote:[https://example.org is the place]
      EOS
      expected_coda = <<~'EOS'.chop
      .SH "NOTES"
      .IP [1]
      .URL "https://example.org" "" ""
      is the place
      EOS
      output = Asciidoctor.convert input, backend: :manpage
      assert output.end_with? expected_coda
    end

    test 'should format footnote with URL macro' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      text.footnote:[go to https://example.org[example site].]
      EOS
      expected_coda = <<~'EOS'.chop
      .SH "NOTES"
      .IP [1]
      go to \c
      .URL "https://example.org" "example site" "."
      EOS
      output = Asciidoctor.convert input, backend: :manpage
      assert output.end_with? expected_coda
    end

    test 'should produce a warning message and output fallback text at location of macro of unresolved footnote' do
      input = <<~EOS.chop
      #{SAMPLE_MANPAGE_HEADER}

      text.footnote:does-not-exist[]
      EOS
      expected_coda = <<~'EOS'.chop
      .sp
      text.[does\-not\-exist]
      EOS
      using_memory_logger do |logger|
        output = Asciidoctor.convert input, backend: :manpage
        assert output.end_with? expected_coda
        assert_message logger, :WARN, 'invalid footnote reference: does-not-exist'
      end
    end
  end

  context 'Environment' do
    test 'should use SOURCE_DATE_EPOCH as modified time of input file and local time' do
      old_source_date_epoch = ENV.delete 'SOURCE_DATE_EPOCH'
      begin
        ENV['SOURCE_DATE_EPOCH'] = '1234123412'
        output = Asciidoctor.convert SAMPLE_MANPAGE_HEADER, backend: :manpage, standalone: true
        assert_match(/Date: 2009-02-08/, output)
        assert_match(/^\.TH "COMMAND" "1" "2009-02-08" "Command 1.2.3" "Command Manual"$/, output)
      ensure
        if old_source_date_epoch
          ENV['SOURCE_DATE_EPOCH'] = old_source_date_epoch
        else
          ENV.delete 'SOURCE_DATE_EPOCH'
        end
      end
    end

    test 'should fail if SOURCE_DATE_EPOCH is malformed' do
      old_source_date_epoch = ENV['SOURCE_DATE_EPOCH']
      begin
        ENV['SOURCE_DATE_EPOCH'] = 'aaaaaaaa'
        assert_raises ArgumentError do
          Asciidoctor.convert SAMPLE_MANPAGE_HEADER, backend: :manpage, standalone: true
        end
      ensure
        if old_source_date_epoch
          ENV['SOURCE_DATE_EPOCH'] = old_source_date_epoch
        else
          ENV.delete 'SOURCE_DATE_EPOCH'
        end
      end
    end
  end
end