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
|
require 'test_helper'
class ReaderTest < Test::Unit::TestCase
# setup for test
def setup
@src_data = File.readlines(sample_doc_path(:asciidoc_index))
@reader = Asciidoctor::Reader.new @src_data
end
context "has_more_lines?" do
test "returns false for empty document" do
assert !Asciidoctor::Reader.new.has_more_lines?
end
test "returns true with lines remaining" do
assert @reader.has_more_lines?, "Yo, didn't work"
end
end
context "with source data loaded" do
test "get_line returns next line" do
assert_equal @src_data[0], @reader.get_line
end
test "get_line consumes the line it returns" do
reader = Asciidoctor::Reader.new(["foo", "bar"])
_ = reader.get_line
second = reader.get_line
assert_equal "bar", second
end
test "peek_line does not consume the line it returns" do
reader = Asciidoctor::Reader.new(["foo", "bar"])
_ = reader.peek_line
second = reader.peek_line
assert_equal "foo", second
end
test "unshift puts line onto Reader instance for the next get_line" do
reader = Asciidoctor::Reader.new(["foo"])
reader.unshift("bar")
assert_equal "bar", reader.get_line
assert_equal "foo", reader.get_line
end
end
context "Grab lines" do
test "Grab until end" do
input = <<-EOS
This is one paragraph.
This is another paragraph.
EOS
lines = input.lines.entries
reader = Asciidoctor::Reader.new(lines)
result = reader.grab_lines_until
assert_equal 3, result.size
assert_equal lines, result
assert !reader.has_more_lines?
assert reader.empty?
end
test "Grab until blank line" do
input = <<-EOS
This is one paragraph.
This is another paragraph.
EOS
lines = input.lines.entries
reader = Asciidoctor::Reader.new(lines)
result = reader.grab_lines_until :break_on_blank_lines => true
assert_equal 1, result.size
assert_equal lines.first, result.first
assert_equal lines.last, reader.peek_line
end
test "Grab until blank line preserving last line" do
input = <<-EOS
This is one paragraph.
This is another paragraph.
EOS
lines = input.lines.entries
reader = Asciidoctor::Reader.new(lines)
result = reader.grab_lines_until :break_on_blank_lines => true, :preserve_last_line => true
assert_equal 1, result.size
assert_equal lines.first, result.first
assert_equal "\n", reader.peek_line
end
test "Grab until condition" do
input = <<-EOS
--
This is one paragraph inside the block.
This is another paragraph inside the block.
--
This is a paragraph outside the block.
EOS
lines = input.lines.entries
reader = Asciidoctor::Reader.new(lines)
reader.get_line
result = reader.grab_lines_until {|line| line.chomp == '--' }
assert_equal 3, result.size
assert_equal lines[1, 3], result
assert_equal "\n", reader.peek_line
end
test "Grab until condition with last line" do
input = <<-EOS
--
This is one paragraph inside the block.
This is another paragraph inside the block.
--
This is a paragraph outside the block.
EOS
lines = input.lines.entries
reader = Asciidoctor::Reader.new(lines)
reader.get_line
result = reader.grab_lines_until(:grab_last_line => true) {|line| line.chomp == '--' }
assert_equal 4, result.size
assert_equal lines[1, 4], result
assert_equal "\n", reader.peek_line
end
test "Grab until condition with last line and preserving last line" do
input = <<-EOS
--
This is one paragraph inside the block.
This is another paragraph inside the block.
--
This is a paragraph outside the block.
EOS
lines = input.lines.entries
reader = Asciidoctor::Reader.new(lines)
reader.get_line
result = reader.grab_lines_until(:grab_last_line => true, :preserve_last_line => true) {|line| line.chomp == '--' }
assert_equal 4, result.size
assert_equal lines[1, 4], result
assert_equal "--\n", reader.peek_line
end
end
context 'Include Macro' do
test 'include macro is disabled by default and becomes a link' do
input = <<-EOS
include::include-file.asciidoc[]
EOS
para = block_from_string input, :attributes => { 'include-depth' => 0 }
assert_equal 1, para.buffer.size
#assert_equal 'include::include-file.asciidoc[]', para.buffer.join
assert_equal 'link:include-file.asciidoc[include-file.asciidoc]', para.buffer.join
end
test 'include macro is enabled when safe mode is less than SECURE' do
input = <<-EOS
include::fixtures/include-file.asciidoc[]
EOS
doc = document_from_string input, :safe => Asciidoctor::SafeMode::SAFE, :attributes => {'docdir' => File.dirname(__FILE__)}
output = doc.render
assert_match(/included content/, output)
end
test 'missing file referenced by include macro does not crash processor' do
input = <<-EOS
include::fixtures/no-such-file.ad[]
EOS
begin
doc = document_from_string input, :safe => Asciidoctor::SafeMode::SAFE, :attributes => {'docdir' => File.dirname(__FILE__)}
assert_equal 0, doc.blocks.size
rescue
flunk('include macro should not raise exception on missing file')
end
end
test 'include macro supports line selection' do
input = <<-EOS
include::fixtures/include-file.asciidoc[lines=1;3..4;6..-1]
EOS
output = render_string input, :safe => Asciidoctor::SafeMode::SAFE, :header_footer => false, :attributes => {'docdir' => File.dirname(__FILE__)}
assert_match(/first line/, output)
assert_no_match(/second line/, output)
assert_match(/third line/, output)
assert_match(/fourth line/, output)
assert_no_match(/fifth line/, output)
assert_match(/sixth line/, output)
assert_match(/seventh line/, output)
assert_match(/eighth line/, output)
assert_match(/last line of included content/, output)
end
test 'include macro supports line selection using quoted attribute value' do
input = <<-EOS
include::fixtures/include-file.asciidoc[lines="1, 3..4 , 6 .. -1"]
EOS
output = render_string input, :safe => Asciidoctor::SafeMode::SAFE, :header_footer => false, :attributes => {'docdir' => File.dirname(__FILE__)}
assert_match(/first line/, output)
assert_no_match(/second line/, output)
assert_match(/third line/, output)
assert_match(/fourth line/, output)
assert_no_match(/fifth line/, output)
assert_match(/sixth line/, output)
assert_match(/seventh line/, output)
assert_match(/eighth line/, output)
assert_match(/last line of included content/, output)
end
test 'include macro supports tagged selection' do
input = <<-EOS
include::fixtures/include-file.asciidoc[tags=snippetA;snippetB]
EOS
output = render_string input, :safe => Asciidoctor::SafeMode::SAFE, :header_footer => false, :attributes => {'docdir' => File.dirname(__FILE__)}
assert_match(/snippetA content/, output)
assert_match(/snippetB content/, output)
assert_no_match(/non-tagged content/, output)
assert_no_match(/included content/, output)
end
test 'lines attribute takes precedence over tags attribute in include macro' do
input = <<-EOS
include::fixtures/include-file.asciidoc[lines=1, tags=snippetA;snippetB]
EOS
output = render_string input, :safe => Asciidoctor::SafeMode::SAFE, :header_footer => false, :attributes => {'docdir' => File.dirname(__FILE__)}
assert_match(/first line of included content/, output)
assert_no_match(/snippetA content/, output)
assert_no_match(/snippetB content/, output)
end
test 'indent of included file can be reset to size of indent attribute' do
input = <<-EOS
[source, xml]
----
include::fixtures/basic-docinfo.xml[lines=2..3, indent=0]
----
EOS
output = render_string input, :safe => Asciidoctor::SafeMode::SAFE, :header_footer => false, :attributes => {'docdir' => File.dirname(__FILE__)}
result = xmlnodes_at_xpath('//pre', output, 1).text
assert_equal "<year>2013</year>\n<holder>Acme, Inc.</holder>", result
end
test "block is called to handle an include macro" do
input = <<-EOS
first line
include::include-file.asciidoc[]
last line
EOS
doc = Asciidoctor::Document.new [], :safe => Asciidoctor::SafeMode::SAFE
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true) {|inc|
":includefile: #{inc}\n\nmiddle line".lines.entries
}
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_match(/^:includefile: include-file.asciidoc$/, lines.join)
end
test 'attributes are substituted in target of include macro' do
input = <<-EOS
:fixturesdir: fixtures
:ext: asciidoc
include::{fixturesdir}/include-file.{ext}[]
EOS
doc = document_from_string input, :safe => Asciidoctor::SafeMode::SAFE, :attributes => {'docdir' => File.dirname(__FILE__)}
output = doc.render
assert_match(/included content/, output)
end
test 'line is dropped if target of include macro resolves to empty' do
input = <<-EOS
include::{foodir}/include-file.asciidoc[]
EOS
output = render_embedded_string input, :safe => Asciidoctor::SafeMode::SAFE, :attributes => {'docdir' => File.dirname(__FILE__)}
assert output.strip.empty?
end
test 'line is dropped but not following line if target of include macro resolves to empty' do
input = <<-EOS
include::{foodir}/include-file.asciidoc[]
yo
EOS
output = render_embedded_string input, :safe => Asciidoctor::SafeMode::SAFE, :attributes => {'docdir' => File.dirname(__FILE__)}
assert_xpath '//p', output, 1
assert_xpath '//p[text()="yo"]', output, 1
end
test 'escaped include macro is left unprocessed' do
input = <<-EOS
\\include::include-file.asciidoc[]
EOS
para = block_from_string input
assert_equal 1, para.buffer.size
assert_equal 'include::include-file.asciidoc[]', para.buffer.join
end
test 'include macro not at start of line is ignored' do
input = <<-EOS
include::include-file.asciidoc[]
EOS
para = block_from_string input
assert_equal 1, para.buffer.size
# NOTE the space gets stripped because the line is treated as an inline literal
assert_equal :literal, para.context
assert_equal 'include::include-file.asciidoc[]', para.buffer.join
end
test 'include macro is disabled when include-depth attribute is 0' do
input = <<-EOS
include::include-file.asciidoc[]
EOS
para = block_from_string input, :safe => Asciidoctor::SafeMode::SAFE, :attributes => { 'include-depth' => 0 }
assert_equal 1, para.buffer.size
assert_equal 'include::include-file.asciidoc[]', para.buffer.join
end
test 'include-depth cannot be set by document' do
input = <<-EOS
:include-depth: 1
include::include-file.asciidoc[]
EOS
para = block_from_string input, :safe => Asciidoctor::SafeMode::SAFE, :attributes => { 'include-depth' => 0 }
assert_equal 1, para.buffer.size
assert_equal 'include::include-file.asciidoc[]', para.buffer.join
end
end
context 'build secure asset path' do
test 'allows us to specify a path relative to the current dir' do
doc = Asciidoctor::Document.new
Asciidoctor::Reader.new([], doc, true)
legit_path = Dir.pwd + "/foo"
assert_equal legit_path, doc.normalize_asset_path(legit_path)
end
test "keeps naughty absolute paths from getting outside" do
naughty_path = "#{disk_root}etc/passwd"
doc = Asciidoctor::Document.new
Asciidoctor::Reader.new([], doc, true)
secure_path = doc.normalize_asset_path(naughty_path)
assert naughty_path != secure_path
assert_match(/^#{doc.base_dir}/, secure_path)
end
test "keeps naughty relative paths from getting outside" do
naughty_path = "safe/ok/../../../../../etc/passwd"
doc = Asciidoctor::Document.new
Asciidoctor::Reader.new([], doc, true)
secure_path = doc.normalize_asset_path(naughty_path)
assert naughty_path != secure_path
assert_match(/^#{doc.base_dir}/, secure_path)
end
end
context 'Conditional Inclusions' do
test 'preprocess_next_line returns true if cursor advanced' do
input = <<-EOS
ifdef::asciidoctor[]
Asciidoctor!
endif::asciidoctor[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
assert reader.preprocess_next_line == true
end
test 'preprocess_next_line returns false if cursor not advanced' do
input = <<-EOS
content
ifdef::asciidoctor[]
Asciidoctor!
endif::asciidoctor[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
assert reader.preprocess_next_line == false
end
test 'preprocess_next_line returns nil if cursor advanced past end of source' do
input = <<-EOS
ifdef::foobar[]
swallowed content
endif::foobar[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
assert reader.preprocess_next_line.nil?
end
test 'ifdef with defined attribute includes block' do
input = <<-EOS
ifdef::holygrail[]
There is a holy grail!
endif::holygrail[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'holygrail' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'There is a holy grail!', lines.join.strip
end
test 'ifdef with defined attribute includes text in brackets' do
input = <<-EOS
On our quest we go...
ifdef::holygrail[There is a holy grail!]
There was much rejoicing.
EOS
doc = Asciidoctor::Document.new [], :attributes => {'holygrail' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal "On our quest we go...\nThere is a holy grail!\nThere was much rejoicing.", lines.join.strip
end
test 'ifndef with defined attribute does not include text in brackets' do
input = <<-EOS
On our quest we go...
ifndef::hardships[There is a holy grail!]
There was no rejoicing.
EOS
doc = Asciidoctor::Document.new [], :attributes => {'hardships' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal "On our quest we go...\nThere was no rejoicing.", lines.join.strip
end
test 'include with non-matching nested exclude' do
input = <<-EOS
ifdef::grail[]
holy
ifdef::swallow[]
swallow
endif::swallow[]
grail
endif::grail[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'grail' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal "holy\ngrail", lines.join.strip
end
test 'nested excludes with same condition' do
input = <<-EOS
ifndef::grail[]
ifndef::grail[]
not here
endif::grail[]
endif::grail[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'grail' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal '', lines.join.strip
end
test 'include with nested exclude of inverted condition' do
input = <<-EOS
ifdef::grail[]
holy
ifndef::grail[]
not here
endif::grail[]
grail
endif::grail[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'grail' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal "holy\ngrail", lines.join.strip
end
test 'exclude with matching nested exclude' do
input = <<-EOS
poof
ifdef::swallow[]
no
ifdef::swallow[]
swallow
endif::swallow[]
here
endif::swallow[]
gone
EOS
doc = Asciidoctor::Document.new [], :attributes => {'grail' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal "poof\ngone", lines.join.strip
end
test 'exclude with nested include using shorthand end' do
input = <<-EOS
poof
ifndef::grail[]
no grail
ifndef::swallow[]
or swallow
endif::[]
in here
endif::[]
gone
EOS
doc = Asciidoctor::Document.new [], :attributes => {'grail' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal "poof\ngone", lines.join.strip
end
test 'ifdef with one alternative attribute set includes content' do
input = <<-EOS
ifdef::holygrail,swallow[]
Our quest is complete!
endif::holygrail,swallow[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'swallow' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Our quest is complete!', lines.join.strip
end
test 'ifdef with no alternative attributes set does not include content' do
input = <<-EOS
ifdef::holygrail,swallow[]
Our quest is complete!
endif::holygrail,swallow[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal '', lines.join.strip
end
test 'ifdef with all required attributes set includes content' do
input = <<-EOS
ifdef::holygrail+swallow[]
Our quest is complete!
endif::holygrail+swallow[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'holygrail' => '', 'swallow' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Our quest is complete!', lines.join.strip
end
test 'ifdef with missing required attributes does not include content' do
input = <<-EOS
ifdef::holygrail+swallow[]
Our quest is complete!
endif::holygrail+swallow[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'holygrail' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal '', lines.join.strip
end
test 'ifndef with undefined attribute includes block' do
input = <<-EOS
ifndef::holygrail[]
Our quest continues to find the holy grail!
endif::holygrail[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Our quest continues to find the holy grail!', lines.join.strip
end
test 'ifndef with one alternative attribute set includes content' do
input = <<-EOS
ifndef::holygrail,swallow[]
Our quest is complete!
endif::holygrail,swallow[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'swallow' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Our quest is complete!', lines.join.strip
end
test 'ifndef with no alternative attributes set includes content' do
input = <<-EOS
ifndef::holygrail,swallow[]
Our quest is complete!
endif::holygrail,swallow[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Our quest is complete!', lines.join.strip
end
test 'ifndef with any required attributes set does not include content' do
input = <<-EOS
ifndef::holygrail+swallow[]
Our quest is complete!
endif::holygrail+swallow[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'swallow' => ''}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal '', lines.join.strip
end
test 'ifndef with no required attributes set includes content' do
input = <<-EOS
ifndef::holygrail+swallow[]
Our quest is complete!
endif::holygrail+swallow[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Our quest is complete!', lines.join.strip
end
test 'escaped ifdef is unescaped and ignored' do
input = <<-EOS
\\ifdef::holygrail[]
content
\\endif::holygrail[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal "ifdef::holygrail[]\ncontent\nendif::holygrail[]", lines.join.strip
end
test 'ifeval comparing double-quoted attribute to matching string is included' do
input = <<-EOS
ifeval::["{gem}" == "asciidoctor"]
Asciidoctor it is!
endif::[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'gem' => 'asciidoctor'}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Asciidoctor it is!', lines.join.strip
end
test 'ifeval comparing single-quoted attribute to matching string is included' do
input = <<-EOS
ifeval::['{gem}' == 'asciidoctor']
Asciidoctor it is!
endif::[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'gem' => 'asciidoctor'}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Asciidoctor it is!', lines.join.strip
end
test 'ifeval comparing quoted attribute to non-matching string is ignored' do
input = <<-EOS
ifeval::['{gem}' == 'asciidoctor']
Asciidoctor it is!
endif::[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'gem' => 'tilt'}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal '', lines.join.strip
end
test 'ifeval comparing attribute to lower version number is included' do
input = <<-EOS
ifeval::['{asciidoctor-version}' >= '0.1.0']
That version will do!
endif::[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'That version will do!', lines.join.strip
end
test 'ifeval comparing attribute to self is included' do
input = <<-EOS
ifeval::['{asciidoctor-version}' == '{asciidoctor-version}']
Of course it's the same!
endif::[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'Of course it\'s the same!', lines.join.strip
end
test 'ifeval arguments can be mirrored' do
input = <<-EOS
ifeval::["0.1.0" <= "{asciidoctor-version}"]
That version will do!
endif::[]
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'That version will do!', lines.join.strip
end
test 'ifeval matching numeric comparison is included' do
input = <<-EOS
ifeval::[{rings} == 1]
One ring to rule them all!
endif::[]
EOS
doc = Asciidoctor::Document.new [], :attributes => {'rings' => 1}
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal 'One ring to rule them all!', lines.join.strip
end
test 'ifdef with no target is ignored' do
input = <<-EOS
ifdef::[]
content
EOS
doc = Asciidoctor::Document.new
reader = Asciidoctor::Reader.new(input.lines.entries, doc, true)
lines = []
while reader.has_more_lines?
lines << reader.get_line
end
assert_equal "ifdef::[]\ncontent", lines.join.strip
end
end
context 'Text processing' do
test 'should clean CRLF from end of lines' do
input = <<-EOS
source\r
with\r
CRLF\r
endlines\r
EOS
reader = Asciidoctor::Reader.new(input.lines.entries, Asciidoctor::Document.new, true)
reader.lines.each do |line|
assert !line.end_with?("\r\n")
end
end
test 'sanitize attribute name' do
assert_equal 'foobar', @reader.sanitize_attribute_name("Foo Bar")
assert_equal 'foo', @reader.sanitize_attribute_name("foo")
assert_equal 'foo3-bar', @reader.sanitize_attribute_name("Foo 3^ # - Bar[")
end
end
end
|