test/extensions_test.rb in asciidoctor-1.5.7.1 vs test/extensions_test.rb in asciidoctor-1.5.8

- old
+ new

@@ -102,10 +102,17 @@ document end end end +class SelfSigningTreeProcessor < Asciidoctor::Extensions::TreeProcessor + def process document + document << (create_paragraph document, self.class.name, {}) + nil + end +end + class StripAttributesPostprocessor < Asciidoctor::Extensions::Postprocessor def process document, output output.gsub(/<(\w+).*?>/m, "<\\1>") end end @@ -190,10 +197,13 @@ process do |parent, target, attrs| image_attrs = {} unless target.nil_or_empty? image_attrs['target'] = %(cat-in-sink-day-#{target}.png) end + if (title = attrs.delete 'title') + image_attrs['title'] = title + end if (alt = attrs.delete 1) image_attrs['alt'] = alt end create_image_block parent, image_attrs end @@ -656,11 +666,11 @@ begin Asciidoctor::Extensions.register do include_processor BoilerplateTextIncludeProcessor end - result = render_string input, :safe => :server + result = convert_string input, :safe => :server assert_css '.paragraph > p', result, 3 assert_includes result, 'before' assert_includes result, 'Lorem ipsum' assert_includes result, 'after' ensure @@ -795,11 +805,53 @@ ensure Asciidoctor::Extensions.unregister_all end end - test 'should invoke postprocessors after rendering document' do + test 'should be able to register preferred tree processor' do + begin + Asciidoctor::Extensions.register do + tree_processor do + process do |doc| + doc << (create_paragraph doc, 'd', {}) + nil + end + end + + tree_processor do + prefer + process do |doc| + doc << (create_paragraph doc, 'c', {}) + nil + end + end + + prefer :tree_processor do + process do |doc| + doc << (create_paragraph doc, 'b', {}) + nil + end + end + + prefer tree_processor { + process do |doc| + doc << (create_paragraph doc, 'a', {}) + nil + end + } + + prefer :tree_processor, SelfSigningTreeProcessor + end + + (doc = empty_document).convert + assert_equal %w(SelfSigningTreeProcessor a b c d), doc.blocks.map {|b| b.lines[0] } + ensure + Asciidoctor::Extensions.unregister_all + end + end + + test 'should invoke postprocessors after converting document' do input = <<-EOS * one * two * three EOS @@ -807,11 +859,11 @@ begin Asciidoctor::Extensions.register do postprocessor StripAttributesPostprocessor end - output = render_string input + output = convert_string input refute_match(/<div class="ulist">/, output) ensure Asciidoctor::Extensions.unregister_all end end @@ -825,11 +877,11 @@ begin Asciidoctor::Extensions.register do block UppercaseBlock end - output = render_embedded_string input + output = convert_string_to_embedded input assert_xpath '//p', output, 1 assert_xpath '//p[text()="HI THERE!"]', output, 1 ensure Asciidoctor::Extensions.unregister_all end @@ -847,11 +899,11 @@ begin Asciidoctor::Extensions.register do block UppercaseBlock end - output = render_embedded_string input + output = convert_string_to_embedded input assert_xpath '/table//p', output, 1 assert_xpath '/table//p[text()="HI THERE!"]', output, 1 ensure Asciidoctor::Extensions.unregister_all end @@ -875,11 +927,11 @@ nil end end end - render_embedded_string input + convert_string_to_embedded input assert_equal :sidebar, cloaked_context ensure Asciidoctor::Extensions.unregister_all end end @@ -892,17 +944,62 @@ begin Asciidoctor::Extensions.register do block_macro SnippetMacro, :snippet end - output = render_embedded_string input + output = convert_string_to_embedded input assert_includes output, '<script src="http://example.com/12345.js?_mode=edit"></script>' ensure Asciidoctor::Extensions.unregister_all end end + test 'should substitute attributes in target of custom block macro' do + input = <<-EOS +snippet::{gist-id}[mode=edit] + EOS + + begin + Asciidoctor::Extensions.register do + block_macro SnippetMacro, :snippet + end + + output = convert_string_to_embedded input, :attributes => { 'gist-id' => '12345' } + assert_includes output, '<script src="http://example.com/12345.js?_mode=edit"></script>' + ensure + Asciidoctor::Extensions.unregister_all + end + end + + test 'should drop block macro line if target references missing attribute and attribute-missing is drop-line' do + input = <<-EOS +[.rolename] +snippet::{gist-ns}12345[mode=edit] + +following paragraph + EOS + + begin + Asciidoctor::Extensions.register do + block_macro SnippetMacro, :snippet + end + + doc, output = nil, nil + using_memory_logger do |logger| + doc = document_from_string input, :attributes => { 'attribute-missing' => 'drop-line' } + assert_equal 1, doc.blocks.size + assert_equal :paragraph, doc.blocks[0].context + output = doc.convert + assert_message logger, :WARN, 'dropping line containing reference to missing attribute: gist-ns' + end + assert_css '.paragraph', output, 1 + assert_css '.rolename', output, 0 + ensure + Asciidoctor::Extensions.unregister_all + end + end + test 'should invoke processor for custom block macro in an AsciiDoc table cell' do input = <<-EOS |=== a|message::hi[] |=== @@ -915,53 +1012,107 @@ create_paragraph parent, target.upcase, {} end end end - output = render_embedded_string input + output = convert_string_to_embedded input assert_xpath '/table//p[text()="HI"]', output, 1 ensure Asciidoctor::Extensions.unregister_all end end test 'should match short form of block macro' do input = <<-EOS -custom_toc::[] +custom-toc::[] EOS resolved_target = nil begin Asciidoctor::Extensions.register do block_macro do - named :custom_toc + named 'custom-toc' process do |parent, target, attrs| resolved_target = target create_pass_block parent, '<!-- custom toc goes here -->', {}, :content_model => :raw end end end - output = render_embedded_string input + output = convert_string_to_embedded input assert_equal '<!-- custom toc goes here -->', output assert_equal '', resolved_target ensure Asciidoctor::Extensions.unregister_all end end + test 'should fail to convert if name of block macro is illegal' do + input = 'illegal name::target[]' + + begin + Asciidoctor::Extensions.register do + block_macro do + named 'illegal name' + process do |parent, target, attrs| + nil + end + end + end + + assert_raises ArgumentError do + convert_string_to_embedded input + end + ensure + Asciidoctor::Extensions.unregister_all + end + end + + test 'should be able to set header attribute in block macro processor' do + begin + Asciidoctor::Extensions.register do + block_macro do + named :attribute + resolves_attributes '1:value' + process do |parent, target, attrs| + parent.document.set_attr target, attrs['value'] + nil + end + end + block_macro do + named :header_attribute + resolves_attributes '1:value' + process do |parent, target, attrs| + parent.document.set_header_attribute target, attrs['value'] + nil + end + end + end + input = <<-EOS +attribute::yin[yang] + +header_attribute::foo[bar] + EOS + doc = document_from_string input + assert_nil doc.attr 'yin' + assert_equal 'bar', (doc.attr 'foo') + ensure + Asciidoctor::Extensions.unregister_all + end + end + test 'should invoke processor for custom inline macro' do begin Asciidoctor::Extensions.register do inline_macro TemperatureMacro, :deg end - output = render_embedded_string 'Room temperature is deg:25[C,precision=0].', :attributes => { 'temperature-unit' => 'F' } + output = convert_string_to_embedded 'Room temperature is deg:25[C,precision=0].', :attributes => { 'temperature-unit' => 'F' } assert_includes output, 'Room temperature is 25 &#176;C.' - output = render_embedded_string 'Normal body temperature is deg:37[].', :attributes => { 'temperature-unit' => 'F' } + output = convert_string_to_embedded 'Normal body temperature is deg:37[].', :attributes => { 'temperature-unit' => 'F' } assert_includes output, 'Normal body temperature is 98.6 &#176;F.' ensure Asciidoctor::Extensions.unregister_all end end @@ -977,11 +1128,11 @@ %(<label>#{target}</label>) end end end - output = render_embedded_string 'label:[Checkbox]' + output = convert_string_to_embedded 'label:[Checkbox]' assert_includes output, '<label>Checkbox</label>' ensure Asciidoctor::Extensions.unregister_all end end @@ -1006,19 +1157,19 @@ %(target=#{target.inspect}, attributes=#{attrs.sort_by {|k, _| k.to_s }.inspect}) end end inline_macro do - named :full_attributes + named :'full-attributes' resolves_attributes '1:name' => nil process do |parent, target, attrs| %(target=#{target.inspect}, attributes=#{attrs.sort_by {|k, _| k.to_s }.inspect}) end end inline_macro do - named :full_text + named :'full-text' resolves_attributes false process do |parent, target, attrs| %(target=#{target.inspect}, attributes=#{attrs.sort_by {|k, _| k.to_s }.inspect}) end end @@ -1038,14 +1189,14 @@ ++++ short_attributes:[] short_attributes:[value,key=val] short_text:[] short_text:[[text\\]] -full_attributes:target[] -full_attributes:target[value,key=val] -full_text:target[] -full_text:target[[text\\]] +full-attributes:target[] +full-attributes:target[value,key=val] +full-text:target[] +full-text:target[[text\\]] @target ++++ EOS expected = <<-EOS.chomp target="", attributes=[] @@ -1056,11 +1207,11 @@ target="target", attributes=[[1, "value"], ["key", "val"], ["name", "value"]] target="target", attributes=[["text", ""]] target="target", attributes=[["text", "[text]"]] target="target", attributes=[] EOS - output = render_embedded_string input + output = convert_string_to_embedded input assert_equal expected, output ensure Asciidoctor::Extensions.unregister_all end end @@ -1078,36 +1229,36 @@ create_anchor parent, text, :type => :link, :target => %(https://github.com/#{target}) end end end - output = render_embedded_string 'mention:mojavelinux[Dan]' + output = convert_string_to_embedded 'mention:mojavelinux[Dan]' assert_includes output, '<a href="https://github.com/mojavelinux">Dan</a>' ensure Asciidoctor::Extensions.unregister_all end end test 'should not carry over attributes if block processor returns nil' do begin Asciidoctor::Extensions.register do block do - named :skipme + named 'skip-me' on_context :paragraph parses_content_as :raw process do |parent, reader, attrs| nil end end end input = <<-EOS .unused title -[skipme] -not rendered +[skip-me] +not shown -- -rendered +shown -- EOS doc = document_from_string input assert_equal 1, doc.blocks.size assert_nil doc.blocks[0].attributes['title'] @@ -1131,14 +1282,14 @@ end end input = <<-EOS .unused title [ignore] -not rendered +not shown -- -rendered +shown -- EOS doc = document_from_string input refute process_method_called assert_equal 1, doc.blocks.size @@ -1332,24 +1483,41 @@ ensure Asciidoctor::Extensions.unregister_all end end + test 'should return extension instance after registering' do + begin + exts = [] + Asciidoctor::Extensions.register do + exts.push preprocessor SamplePreprocessor + exts.push include_processor SampleIncludeProcessor + exts.push tree_processor SampleTreeProcessor + exts.push docinfo_processor SampleDocinfoProcessor + exts.push postprocessor SamplePostprocessor + end + empty_document + exts.each do |ext| + assert_kind_of Asciidoctor::Extensions::ProcessorExtension, ext + end + ensure + Asciidoctor::Extensions.unregister_all + end + end + test 'should raise an exception if mandatory target attribute is not provided for image block' do input = <<-EOS -.Cat in Sink? cat_in_sink::[] EOS exception = assert_raises ArgumentError do - render_embedded_string input, :extension_registry => create_cat_in_sink_block_macro + convert_string_to_embedded input, :extension_registry => create_cat_in_sink_block_macro end assert_match(/target attribute is required/, exception.message) end test 'should assign alt attribute to image block if alt is not provided' do input = <<-EOS -.Cat in Sink? cat_in_sink::25[] EOS doc = document_from_string input, :header_footer => false, :extension_registry => create_cat_in_sink_block_macro image = doc.blocks[0] assert_equal 'cat in sink day 25', (image.attr 'alt') @@ -1358,17 +1526,35 @@ assert_includes output, '<img src="cat-in-sink-day-25.png" alt="cat in sink day 25">' end test 'should create an image block if mandatory attributes are provided' do input = <<-EOS -.Cat in Sink? cat_in_sink::30[cat in sink (yes)] EOS doc = document_from_string input, :header_footer => false, :extension_registry => create_cat_in_sink_block_macro image = doc.blocks[0] assert_equal 'cat in sink (yes)', (image.attr 'alt') refute(image.attr? 'default-alt') output = doc.convert assert_includes output, '<img src="cat-in-sink-day-30.png" alt="cat in sink (yes)">' + end + + test 'should not assign caption on image block if title is not set on custom block macro' do + input = <<-EOS +cat_in_sink::30[] + EOS + doc = document_from_string input, :header_footer => false, :extension_registry => create_cat_in_sink_block_macro + output = doc.convert + assert_xpath '/*[@class="imageblock"]/*[@class="title"]', output, 0 + end + + test 'should assign caption on image block if title is set on custom block macro' do + input = <<-EOS +.Cat in Sink? +cat_in_sink::30[] + EOS + doc = document_from_string input, :header_footer => false, :extension_registry => create_cat_in_sink_block_macro + output = doc.convert + assert_xpath '/*[@class="imageblock"]/*[@class="title"][text()="Figure 1. Cat in Sink?"]', output, 1 end end end