require 'test_helper'
# TODO
# - test negatives
# - test role on every quote type
context 'Substitutions' do
context 'Dispatcher' do
test 'apply normal substitutions' do
para = block_from_string("[blue]'http://asciidoc.org[AsciiDoc]' & [red]*Ruby*\n§ Making +++documentation+++ together +\nsince (C) {inception_year}.")
para.document.attributes['inception_year'] = '2012'
result = para.apply_normal_subs(para.buffer)
assert_equal %{AsciiDoc & Ruby\n§ Making documentation together
\nsince © 2012.}, result
end
end
context 'Quotes' do
test 'single-line double-quoted string' do
para = block_from_string(%q{``a few quoted words''})
assert_equal '“a few quoted words”', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line double-quoted string' do
para = block_from_string(%q{\``a few quoted words''})
assert_equal %q(‘`a few quoted words’'), para.sub_quotes(para.buffer.join)
end
test 'multi-line double-quoted string' do
para = block_from_string(%Q{``a few\nquoted words''})
assert_equal "“a few\nquoted words”", para.sub_quotes(para.buffer.join)
end
test 'double-quoted string with inline single quote' do
para = block_from_string(%q{``Here's Johnny!''})
assert_equal %q{“Here's Johnny!”}, para.sub_quotes(para.buffer.join)
end
test 'double-quoted string with inline backquote' do
para = block_from_string(%q{``Here`s Johnny!''})
assert_equal %q{“Here`s Johnny!”}, para.sub_quotes(para.buffer.join)
end
test 'single-line single-quoted string' do
para = block_from_string(%q{`a few quoted words'})
assert_equal '‘a few quoted words’', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line single-quoted string' do
para = block_from_string(%q{\`a few quoted words'})
assert_equal %(`a few quoted words'), para.sub_quotes(para.buffer.join)
end
test 'multi-line single-quoted string' do
para = block_from_string(%Q{`a few\nquoted words'})
assert_equal "‘a few\nquoted words’", para.sub_quotes(para.buffer.join)
end
test 'single-quoted string with inline single quote' do
para = block_from_string(%q{`That isn't what I did.'})
assert_equal %q{‘That isn't what I did.’}, para.sub_quotes(para.buffer.join)
end
test 'single-quoted string with inline backquote' do
para = block_from_string(%q{`Here`s Johnny!'})
assert_equal %q{‘Here`s Johnny!’}, para.sub_quotes(para.buffer.join)
end
test 'single-line constrained unquoted string' do
para = block_from_string(%q{#a few words#})
assert_equal 'a few words', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line constrained unquoted string' do
para = block_from_string(%q{\#a few words#})
assert_equal '#a few words#', para.sub_quotes(para.buffer.join)
end
test 'multi-line constrained unquoted string' do
para = block_from_string(%Q{#a few\nwords#})
assert_equal "a few\nwords", para.sub_quotes(para.buffer.join)
end
test 'single-line unconstrained unquoted string' do
para = block_from_string(%q{##--anything goes ##})
assert_equal '--anything goes ', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line unconstrained unquoted string' do
para = block_from_string(%q{\##--anything goes ##})
assert_equal '#--anything goes #', para.sub_quotes(para.buffer.join)
end
test 'multi-line unconstrained unquoted string' do
para = block_from_string(%Q{##--anything\ngoes ##})
assert_equal "--anything\ngoes ", para.sub_quotes(para.buffer.join)
end
test 'single-line constrained strong string' do
para = block_from_string(%q{*a few strong words*})
assert_equal 'a few strong words', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line constrained strong string' do
para = block_from_string(%q{\*a few strong words*})
assert_equal '*a few strong words*', para.sub_quotes(para.buffer.join)
end
test 'multi-line constrained strong string' do
para = block_from_string(%Q{*a few\nstrong words*})
assert_equal "a few\nstrong words", para.sub_quotes(para.buffer.join)
end
test 'constrained strong string containing an asterisk' do
para = block_from_string(%q{*bl*ck*-eye})
assert_equal 'bl*ck-eye', para.sub_quotes(para.buffer.join)
end
test 'single-line constrained quote variation emphasized string' do
para = block_from_string(%q{'a few emphasized words'})
assert_equal 'a few emphasized words', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line constrained quote variation emphasized string' do
para = block_from_string(%q{\'a few emphasized words'})
assert_equal %q('a few emphasized words'), para.sub_quotes(para.buffer.join)
end
test 'multi-line constrained emphasized quote variation string' do
para = block_from_string(%Q{'a few\nemphasized words'})
assert_equal "a few\nemphasized words", para.sub_quotes(para.buffer.join)
end
test 'single-quoted string containing an emphasized phrase' do
para = block_from_string(%q{`I told him, 'Just go for it!''})
assert_equal '‘I told him, Just go for it!’', para.sub_quotes(para.buffer.join)
end
test 'escaped single-quotes inside emphasized words are restored' do
para = block_from_string(%q{'Here\'s Johnny!'})
# NOTE the \' is replaced with ' by the :replacements substitution, later in the substitution pipeline
assert_equal %q{Here\'s Johnny!}, para.sub_quotes(para.buffer.join)
end
test 'single-line constrained emphasized underline variation string' do
para = block_from_string(%q{_a few emphasized words_})
assert_equal 'a few emphasized words', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line constrained emphasized underline variation string' do
para = block_from_string(%q{\_a few emphasized words_})
assert_equal '_a few emphasized words_', para.sub_quotes(para.buffer.join)
end
test 'multi-line constrained emphasized underline variation string' do
para = block_from_string(%Q{_a few\nemphasized words_})
assert_equal "a few\nemphasized words", para.sub_quotes(para.buffer.join)
end
test 'single-line constrained monospaced string' do
para = block_from_string(%q{`a few <\{monospaced\}> words`})
# NOTE must use apply_normal_subs because constrained monospaced is handled as a passthrough
assert_equal 'a few <{monospaced}> words', para.apply_normal_subs(para.buffer)
end
test 'escaped single-line constrained monospaced string' do
para = block_from_string(%q{\`a few words`})
# NOTE must use apply_normal_subs because constrained monospaced is handled as a passthrough
assert_equal '`a few <monospaced> words`', para.apply_normal_subs(para.buffer)
end
test 'multi-line constrained monospaced string' do
para = block_from_string(%Q{`a few\n<\{monospaced\}> words`})
# NOTE must use apply_normal_subs because constrained monospaced is handled as a passthrough
assert_equal "a few\n<{monospaced}> words", para.apply_normal_subs(para.buffer)
end
test 'single-line unconstrained strong chars' do
para = block_from_string(%q{**Git**Hub})
assert_equal 'GitHub', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line unconstrained strong chars' do
para = block_from_string(%q{\**Git**Hub})
assert_equal '*Git*Hub', para.sub_quotes(para.buffer.join)
end
test 'multi-line unconstrained strong chars' do
para = block_from_string(%Q{**G\ni\nt\n**Hub})
assert_equal "G\ni\nt\nHub", para.sub_quotes(para.buffer.join)
end
test 'unconstrained strong chars with inline asterisk' do
para = block_from_string(%q{**bl*ck**-eye})
assert_equal 'bl*ck-eye', para.sub_quotes(para.buffer.join)
end
test 'unconstrained strong chars with role' do
para = block_from_string(%q{Git[blue]**Hub**})
assert_equal %q{GitHub}, para.sub_quotes(para.buffer.join)
end
# TODO this is not the same result as AsciiDoc, though I don't understand why AsciiDoc gets what it gets
test 'escaped unconstrained strong chars with role' do
para = block_from_string(%q{Git\[blue]**Hub**})
assert_equal %q{Git[blue]*Hub*}, para.sub_quotes(para.buffer.join)
end
test 'single-line unconstrained emphasized chars' do
para = block_from_string(%q{__Git__Hub})
assert_equal 'GitHub', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line unconstrained emphasized chars' do
para = block_from_string(%q{\__Git__Hub})
assert_equal '__Git__Hub', para.sub_quotes(para.buffer.join)
end
test 'multi-line unconstrained emphasized chars' do
para = block_from_string(%Q{__G\ni\nt\n__Hub})
assert_equal "G\ni\nt\nHub", para.sub_quotes(para.buffer.join)
end
test 'unconstrained emphasis chars with role' do
para = block_from_string(%q{[gray]__Git__Hub})
assert_equal %q{GitHub}, para.sub_quotes(para.buffer.join)
end
test 'escaped unconstrained emphasis chars with role' do
para = block_from_string(%q{\[gray]__Git__Hub})
assert_equal %q{[gray]__Git__Hub}, para.sub_quotes(para.buffer.join)
end
test 'single-line unconstrained monospaced chars' do
para = block_from_string(%q{Git++Hub++})
assert_equal 'GitHub', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line unconstrained monospaced chars' do
para = block_from_string(%q{Git\++Hub++})
assert_equal 'Git+Hub+', para.sub_quotes(para.buffer.join)
end
test 'multi-line unconstrained monospaced chars' do
para = block_from_string(%Q{Git++\nH\nu\nb++})
assert_equal "Git\nH\nu\nb", para.sub_quotes(para.buffer.join)
end
test 'single-line superscript chars' do
para = block_from_string(%q{x^2^ = x * x, e = mc^2^, there's a 1^st^ time for everything})
assert_equal 'x2 = x * x, e = mc2, there\'s a 1st time for everything', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line superscript chars' do
para = block_from_string(%q{x\^2^ = x * x})
assert_equal 'x^2^ = x * x', para.sub_quotes(para.buffer.join)
end
test 'multi-line superscript chars' do
para = block_from_string(%Q{x^(n\n+\n1)^})
assert_equal "x(n\n+\n1)", para.sub_quotes(para.buffer.join)
end
test 'single-line subscript chars' do
para = block_from_string(%q{H~2~O})
assert_equal 'H2O', para.sub_quotes(para.buffer.join)
end
test 'escaped single-line subscript chars' do
para = block_from_string(%q{H\~2~O})
assert_equal 'H~2~O', para.sub_quotes(para.buffer.join)
end
test 'multi-line subscript chars' do
para = block_from_string(%Q{project~ view\non\nGitHub~})
assert_equal "project view\non\nGitHub", para.sub_quotes(para.buffer.join)
end
end
context 'Macros' do
test 'a single-line link macro should be interpreted as a link' do
para = block_from_string('link:/home.html[]')
assert_equal %q{/home.html}, para.sub_macros(para.buffer.join)
end
test 'a single-line link macro with text should be interpreted as a link' do
para = block_from_string('link:/home.html[Home]')
assert_equal %q{Home}, para.sub_macros(para.buffer.join)
end
test 'a single-line raw url should be interpreted as a link' do
para = block_from_string('http://google.com')
assert_equal %q{http://google.com}, para.sub_macros(para.buffer.join)
end
test 'a single-line raw url with text should be interpreted as a link' do
para = block_from_string('http://google.com[Google]')
assert_equal %q{Google}, para.sub_macros(para.buffer.join)
end
test 'a multi-line raw url with text should be interpreted as a link' do
para = block_from_string("http://google.com[Google\nHomepage]")
assert_equal %{Google\nHomepage}, para.sub_macros(para.buffer.join)
end
test 'a multi-line raw url with attribute as text should be interpreted as a link with resolved attribute' do
para = block_from_string("http://google.com[{google_homepage}]")
para.document.attributes['google_homepage'] = 'Google Homepage'
assert_equal %q{Google Homepage}, para.sub_macros(para.buffer.join)
end
test 'a single-line escaped raw url should not be interpreted as a link' do
para = block_from_string('\http://google.com')
assert_equal %q{http://google.com}, para.sub_macros(para.buffer.join)
end
test 'a single-line image macro should be interpreted as an image' do
para = block_from_string('image:tiger.png[]')
assert_equal %{\n \n}, para.sub_macros(para.buffer.join)
end
test 'a single-line image macro with text should be interpreted as an image with alt text' do
para = block_from_string('image:tiger.png[Tiger]')
assert_equal %{\n \n}, para.sub_macros(para.buffer.join)
end
test 'a single-line image macro with text and dimensions should be interpreted as an image with alt text and dimensions' do
para = block_from_string('image:tiger.png[Tiger, 200, 100]')
assert_equal %{\n \n}, para.sub_macros(para.buffer.join)
end
test 'a single-line image macro with text and link should be interpreted as a linked image with alt text' do
para = block_from_string('image:tiger.png[Tiger, link="http://en.wikipedia.org/wiki/Tiger"]')
assert_equal %{\n \n}, para.sub_macros(para.buffer.join)
end
end
context 'Passthroughs' do
test 'collect inline triple plus passthroughs' do
para = block_from_string('+++inline code
+++')
result = para.extract_passthroughs(para.buffer.join)
assert_equal "\x0" + '0' + "\x0", result
assert_equal 1, para.passthroughs.size
assert_equal 'inline code
', para.passthroughs.first[:text]
assert para.passthroughs.first[:subs].empty?
end
test 'collect multi-line inline triple plus passthroughs' do
para = block_from_string("+++inline\ncode
+++")
result = para.extract_passthroughs(para.buffer.join)
assert_equal "\x0" + '0' + "\x0", result
assert_equal 1, para.passthroughs.size
assert_equal "inline\ncode
", para.passthroughs.first[:text]
assert para.passthroughs.first[:subs].empty?
end
test 'collect inline double dollar passthroughs' do
para = block_from_string('$${code}
$$')
result = para.extract_passthroughs(para.buffer.join)
assert_equal "\x0" + '0' + "\x0", result
assert_equal 1, para.passthroughs.size
assert_equal '{code}
', para.passthroughs.first[:text]
assert_equal [:specialcharacters], para.passthroughs.first[:subs]
end
test 'collect multi-line inline double dollar passthroughs' do
para = block_from_string("$$\n{code}\n
$$")
result = para.extract_passthroughs(para.buffer.join)
assert_equal "\x0" + '0' + "\x0", result
assert_equal 1, para.passthroughs.size
assert_equal "\n{code}\n
", para.passthroughs.first[:text]
assert_equal [:specialcharacters], para.passthroughs.first[:subs]
end
test 'collect passthroughs from inline pass macro' do
para = block_from_string(%Q{pass:specialcharacters,quotes[['code'\\]
]})
result = para.extract_passthroughs(para.buffer.join)
assert_equal "\x0" + '0' + "\x0", result
assert_equal 1, para.passthroughs.size
assert_equal %q{['code']
}, para.passthroughs.first[:text]
assert_equal [:specialcharacters, :quotes], para.passthroughs.first[:subs]
end
test 'collect multi-line passthroughs from inline pass macro' do
para = block_from_string(%Q{pass:specialcharacters,quotes[['more\ncode'\\]
]})
result = para.extract_passthroughs(para.buffer.join)
assert_equal "\x0" + '0' + "\x0", result
assert_equal 1, para.passthroughs.size
assert_equal %Q{['more\ncode']
}, para.passthroughs.first[:text]
assert_equal [:specialcharacters, :quotes], para.passthroughs.first[:subs]
end
test 'restore inline passthroughs without subs' do
para = block_from_string("\x0" + '0' + "\x0")
para.passthroughs << {:text => 'inline code
', :subs => []}
result = para.restore_passthroughs(para.buffer.join)
assert_equal 'inline code
', result
end
# TODO add two entries to ensure index lookup is working correctly (0 indx could be ambiguous)
test 'restore inline passthroughs with subs' do
para = block_from_string("\x0" + '0' + "\x0")
para.passthroughs << {:text => '{code}
', :subs => [:specialcharacters]}
result = para.restore_passthroughs(para.buffer.join)
assert_equal '<code>{code}</code>', result
end
end
context 'Post replacements' do
test 'line break' do
para = block_from_string("First line +\nSecond line")
result = para.apply_subs(para.buffer, :post_replacements)
assert_equal "First line
\n", result.first
end
end
end