"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/test/text_test.rb" (1 Jun 2019, 10791 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 require_relative 'test_helper'
    3 
    4 context "Text" do
    5   test "proper encoding to handle utf8 characters in document using html backend" do
    6     output = example_document(:encoding).convert
    7     assert_xpath '//p', output, 4
    8     assert_xpath '//a', output, 1
    9   end
   10 
   11   test "proper encoding to handle utf8 characters in embedded document using html backend" do
   12     output = example_document(:encoding, standalone: false).convert
   13     assert_xpath '//p', output, 4
   14     assert_xpath '//a', output, 1
   15   end
   16 
   17   test 'proper encoding to handle utf8 characters in document using docbook backend' do
   18     output = example_document(:encoding, attributes: { 'backend' => 'docbook', 'xmlns' => '' }).convert
   19     assert_xpath '//xmlns:simpara', output, 4
   20     assert_xpath '//xmlns:link', output, 1
   21   end
   22 
   23   test 'proper encoding to handle utf8 characters in embedded document using docbook backend' do
   24     output = example_document(:encoding, standalone: false, attributes: { 'backend' => 'docbook' }).convert
   25     assert_xpath '//simpara', output, 4
   26     assert_xpath '//link', output, 1
   27   end
   28 
   29   # NOTE this test ensures we have the encoding line on block templates too
   30   test 'proper encoding to handle utf8 characters in arbitrary block' do
   31     input = []
   32     input << "[verse]\n"
   33     input += (File.readlines (sample_doc_path :encoding), mode: Asciidoctor::FILE_READ_MODE)
   34     doc = empty_document
   35     reader = Asciidoctor::PreprocessorReader.new doc, input, nil, normalize: true
   36     block = Asciidoctor::Parser.next_block(reader, doc)
   37     assert_xpath '//pre', block.convert.gsub(/^\s*\n/, ''), 1
   38   end
   39 
   40   test 'proper encoding to handle utf8 characters from included file' do
   41     input = 'include::fixtures/encoding.adoc[tags=romé]'
   42     doc = empty_safe_document base_dir: testdir
   43     reader = Asciidoctor::PreprocessorReader.new doc, input, nil, normalize: true
   44     block = Asciidoctor::Parser.next_block(reader, doc)
   45     output = block.convert
   46     assert_css '.paragraph', output, 1
   47   end
   48 
   49   test 'escaped text markup' do
   50     assert_match(/All your &lt;em&gt;inline&lt;\/em&gt; markup belongs to &lt;strong&gt;us&lt;\/strong&gt;!/,
   51         convert_string('All your <em>inline</em> markup belongs to <strong>us</strong>!'))
   52   end
   53 
   54   test "line breaks" do
   55     assert_xpath "//br", convert_string("Well this is +\njust fine and dandy, isn't it?"), 1
   56   end
   57 
   58   test 'single- and double-quoted text' do
   59     output = convert_string_to_embedded(%q(``Where?,'' she said, flipping through her copy of `The New Yorker.'), attributes: { 'compat-mode' => '' })
   60     assert_match(/&#8220;Where\?,&#8221;/, output)
   61     assert_match(/&#8216;The New Yorker.&#8217;/, output)
   62 
   63     output = convert_string_to_embedded(%q("`Where?,`" she said, flipping through her copy of '`The New Yorker.`'))
   64     assert_match(/&#8220;Where\?,&#8221;/, output)
   65     assert_match(/&#8216;The New Yorker.&#8217;/, output)
   66   end
   67 
   68   test 'multiple double-quoted text on a single line' do
   69     assert_equal '&#8220;Our business is constantly changing&#8221; or &#8220;We need faster time to market.&#8221;',
   70         convert_inline_string(%q(``Our business is constantly changing'' or ``We need faster time to market.''), attributes: { 'compat-mode' => '' })
   71     assert_equal '&#8220;Our business is constantly changing&#8221; or &#8220;We need faster time to market.&#8221;',
   72         convert_inline_string(%q("`Our business is constantly changing`" or "`We need faster time to market.`"))
   73   end
   74 
   75   test 'horizontal rule' do
   76     input = <<~'EOS'
   77     This line is separated by a horizontal rule...
   78 
   79     '''
   80 
   81     ...from this line.
   82     EOS
   83     output = convert_string_to_embedded input
   84     assert_xpath "//hr", output, 1
   85     assert_xpath "/*[@class='paragraph']", output, 2
   86     assert_xpath "(/*[@class='paragraph'])[1]/following-sibling::hr", output, 1
   87     assert_xpath "/hr/following-sibling::*[@class='paragraph']", output, 1
   88   end
   89 
   90   test 'markdown horizontal rules' do
   91     variants = [
   92       '---',
   93       '- - -',
   94       '***',
   95       '* * *',
   96       '___',
   97       '_ _ _'
   98     ]
   99 
  100     offsets = [
  101       '',
  102       ' ',
  103       '  ',
  104       '   '
  105     ]
  106 
  107     variants.each do |variant|
  108       offsets.each do |offset|
  109         input = <<~EOS
  110         This line is separated by a horizontal rule...
  111 
  112         #{offset}#{variant}
  113 
  114         ...from this line.
  115         EOS
  116         output = convert_string_to_embedded input
  117         assert_xpath "//hr", output, 1
  118         assert_xpath "/*[@class='paragraph']", output, 2
  119         assert_xpath "(/*[@class='paragraph'])[1]/following-sibling::hr", output, 1
  120         assert_xpath "/hr/following-sibling::*[@class='paragraph']", output, 1
  121       end
  122     end
  123   end
  124 
  125   test 'markdown horizontal rules negative case' do
  126 
  127     bad_variants = [
  128       '- - - -',
  129       '* * * *',
  130       '_ _ _ _'
  131     ]
  132 
  133     good_offsets = [
  134       '',
  135       ' ',
  136       '  ',
  137       '   '
  138     ]
  139 
  140     bad_variants.each do |variant|
  141       good_offsets.each do |offset|
  142         input = <<~EOS
  143         This line is separated by something that is not a horizontal rule...
  144 
  145         #{offset}#{variant}
  146 
  147         ...from this line.
  148         EOS
  149         output = convert_string_to_embedded input
  150         assert_xpath '//hr', output, 0
  151       end
  152     end
  153 
  154     good_variants = [
  155       '- - -',
  156       '* * *',
  157       '_ _ _'
  158     ]
  159 
  160     bad_offsets = [
  161       "\t",
  162       '    '
  163     ]
  164 
  165     good_variants.each do |variant|
  166       bad_offsets.each do |offset|
  167         input = <<~EOS
  168         This line is separated by something that is not a horizontal rule...
  169 
  170         #{offset}#{variant}
  171 
  172         ...from this line.
  173         EOS
  174         output = convert_string_to_embedded input
  175         assert_xpath '//hr', output, 0
  176       end
  177     end
  178   end
  179 
  180   test "emphasized text using underscore characters" do
  181     assert_xpath "//em", convert_string("An _emphatic_ no")
  182   end
  183 
  184   test 'emphasized text with single quote using apostrophe characters' do
  185     rsquo = decode_char 8217
  186     assert_xpath %(//em[text()="Johnny#{rsquo}s"]), convert_string(%q(It's 'Johnny's' phone), attributes: { 'compat-mode' => '' })
  187     assert_xpath %(//p[text()="It#{rsquo}s 'Johnny#{rsquo}s' phone"]), convert_string(%q(It's 'Johnny's' phone))
  188   end
  189 
  190   test 'emphasized text with escaped single quote using apostrophe characters' do
  191     assert_xpath %(//em[text()="Johnny's"]), convert_string(%q(It's 'Johnny\\'s' phone), attributes: { 'compat-mode' => '' })
  192     assert_xpath %(//p[text()="It's 'Johnny's' phone"]), convert_string(%q(It\\'s 'Johnny\\'s' phone))
  193   end
  194 
  195   test "escaped single quote is restored as single quote" do
  196     assert_xpath "//p[contains(text(), \"Let's do it!\")]", convert_string("Let\\'s do it!")
  197   end
  198 
  199   test 'unescape escaped single quote emphasis in compat mode only' do
  200     assert_xpath %(//p[text()="A 'single quoted string' example"]), convert_string_to_embedded(%(A \\'single quoted string' example), attributes: { 'compat-mode' => '' })
  201     assert_xpath %(//p[text()="'single quoted string'"]), convert_string_to_embedded(%(\\'single quoted string'), attributes: { 'compat-mode' => '' })
  202 
  203     assert_xpath %(//p[text()="A \\'single quoted string' example"]), convert_string_to_embedded(%(A \\'single quoted string' example))
  204     assert_xpath %(//p[text()="\\'single quoted string'"]), convert_string_to_embedded(%(\\'single quoted string'))
  205   end
  206 
  207   test "emphasized text at end of line" do
  208     assert_xpath "//em", convert_string("This library is _awesome_")
  209   end
  210 
  211   test "emphasized text at beginning of line" do
  212     assert_xpath "//em", convert_string("_drop_ it")
  213   end
  214 
  215   test "emphasized text across line" do
  216     assert_xpath "//em", convert_string("_check it_")
  217   end
  218 
  219   test "unquoted text" do
  220     refute_match(/#/, convert_string("An #unquoted# word"))
  221   end
  222 
  223   test 'backticks and straight quotes in text' do
  224     backslash = '\\'
  225     assert_equal %q(run <code>foo</code> <em>dog</em>), convert_inline_string(%q(run `foo` 'dog'), attributes: { 'compat-mode' => '' })
  226     assert_equal %q(run <code>foo</code> 'dog'), convert_inline_string(%q(run `foo` 'dog'))
  227     assert_equal %q(run `foo` 'dog'), convert_inline_string(%(run #{backslash}`foo` 'dog'))
  228     assert_equal %q(run &#8216;foo` 'dog&#8217;), convert_inline_string(%q(run '`foo` 'dog`'))
  229     assert_equal %q(run '`foo` 'dog`'), convert_inline_string(%(run #{backslash}'`foo` 'dog#{backslash}`'))
  230   end
  231 
  232   test 'plus characters inside single plus passthrough' do
  233     assert_xpath '//p[text()="+"]', convert_string_to_embedded('+++')
  234     assert_xpath '//p[text()="+="]', convert_string_to_embedded('++=+')
  235   end
  236 
  237   test 'plus passthrough escapes entity reference' do
  238     assert_match(/&amp;#44;/, convert_string_to_embedded('+&#44;+'))
  239     assert_match(/one&amp;#44;two/, convert_string_to_embedded('one++&#44;++two'))
  240   end
  241 
  242   context "basic styling" do
  243     setup do
  244       @output = convert_string("A *BOLD* word.  An _italic_ word.  A `mono` word.  ^superscript!^ and some ~subscript~.")
  245     end
  246 
  247     test "strong" do
  248       assert_xpath "//strong", @output, 1
  249     end
  250 
  251     test "italic" do
  252       assert_xpath "//em", @output, 1
  253     end
  254 
  255     test "monospaced" do
  256       assert_xpath "//code", @output, 1
  257     end
  258 
  259     test "superscript" do
  260       assert_xpath "//sup", @output, 1
  261     end
  262 
  263     test "subscript" do
  264       assert_xpath "//sub", @output, 1
  265     end
  266 
  267     test "passthrough" do
  268       assert_xpath "//code", convert_string("This is +passed through+."), 0
  269       assert_xpath "//code", convert_string("This is +passed through and monospaced+.", attributes: { 'compat-mode' => '' }), 1
  270     end
  271 
  272     test "nested styles" do
  273       output = convert_string("Winning *big _time_* in the +city *boyeeee*+.", attributes: { 'compat-mode' => '' })
  274 
  275       assert_xpath "//strong/em", output
  276       assert_xpath "//code/strong", output
  277 
  278       output = convert_string("Winning *big _time_* in the `city *boyeeee*`.")
  279 
  280       assert_xpath "//strong/em", output
  281       assert_xpath "//code/strong", output
  282     end
  283 
  284     test 'unconstrained quotes' do
  285       output = convert_string('**B**__I__++M++[role]++M++', attributes: { 'compat-mode' => '' })
  286       assert_xpath '//strong', output, 1
  287       assert_xpath '//em', output, 1
  288       assert_xpath '//code[not(@class)]', output, 1
  289       assert_xpath '//code[@class="role"]', output, 1
  290 
  291       output = convert_string('**B**__I__``M``[role]``M``')
  292       assert_xpath '//strong', output, 1
  293       assert_xpath '//em', output, 1
  294       assert_xpath '//code[not(@class)]', output, 1
  295       assert_xpath '//code[@class="role"]', output, 1
  296     end
  297   end
  298 
  299   test 'should format Asian characters as words' do
  300     assert_xpath '//strong', (convert_string_to_embedded 'bold ** bold')
  301     assert_xpath '//strong', (convert_string_to_embedded 'bold ** bold')
  302     assert_xpath '//strong', (convert_string_to_embedded 'bold *要素* bold')
  303   end
  304 end