"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/test/paragraphs_test.rb" (1 Jun 2019, 19159 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 'Paragraphs' do
    5   context 'Normal' do
    6     test 'should treat plain text separated by blank lines as paragraphs' do
    7       input = <<~'EOS'
    8       Plain text for the win!
    9 
   10       Yep. Text. Plain and simple.
   11       EOS
   12       output = convert_string_to_embedded input
   13       assert_css 'p', output, 2
   14       assert_xpath '(//p)[1][text() = "Plain text for the win!"]', output, 1
   15       assert_xpath '(//p)[2][text() = "Yep. Text. Plain and simple."]', output, 1
   16     end
   17 
   18     test 'should associate block title with paragraph' do
   19       input = <<~'EOS'
   20       .Titled
   21       Paragraph.
   22 
   23       Winning.
   24       EOS
   25       output = convert_string_to_embedded input
   26 
   27       assert_css 'p', output, 2
   28       assert_xpath '(//p)[1]/preceding-sibling::*[@class = "title"]', output, 1
   29       assert_xpath '(//p)[1]/preceding-sibling::*[@class = "title"][text() = "Titled"]', output, 1
   30       assert_xpath '(//p)[2]/preceding-sibling::*[@class = "title"]', output, 0
   31     end
   32 
   33     test 'no duplicate block before next section' do
   34       input = <<~'EOS'
   35       = Title
   36 
   37       Preamble
   38 
   39       == First Section
   40 
   41       Paragraph 1
   42 
   43       Paragraph 2
   44 
   45       == Second Section
   46 
   47       Last words
   48       EOS
   49 
   50       output = convert_string input
   51       assert_xpath '//p[text() = "Paragraph 2"]', output, 1
   52     end
   53 
   54     test 'does not treat wrapped line as a list item' do
   55       input = <<~'EOS'
   56       paragraph
   57       . wrapped line
   58       EOS
   59 
   60       output = convert_string_to_embedded input
   61       assert_css 'p', output, 1
   62       assert_xpath %(//p[text()="paragraph\n. wrapped line"]), output, 1
   63     end
   64 
   65     test 'does not treat wrapped line as a block title' do
   66       input = <<~'EOS'
   67       paragraph
   68       .wrapped line
   69       EOS
   70 
   71       output = convert_string_to_embedded input
   72       assert_css 'p', output, 1
   73       assert_xpath %(//p[text()="paragraph\n.wrapped line"]), output, 1
   74     end
   75 
   76     test 'interprets normal paragraph style as normal paragraph' do
   77       input = <<~'EOS'
   78       [normal]
   79       Normal paragraph.
   80       Nothing special.
   81       EOS
   82 
   83       output = convert_string_to_embedded input
   84       assert_css 'p', output, 1
   85     end
   86 
   87     test 'removes indentation from literal paragraph marked as normal' do
   88       # NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
   89       input = <<~EOS
   90       [normal]
   91         Normal paragraph.
   92           Nothing special.
   93         Last line.
   94       EOS
   95 
   96       output = convert_string_to_embedded input
   97       assert_css 'p', output, 1
   98       assert_xpath %(//p[text()="Normal paragraph.\n  Nothing special.\nLast line."]), output, 1
   99     end
  100 
  101     test 'normal paragraph terminates at block attribute list' do
  102       input = <<~'EOS'
  103       normal text
  104       [literal]
  105       literal text
  106       EOS
  107       output = convert_string_to_embedded input
  108       assert_css '.paragraph:root', output, 1
  109       assert_css '.literalblock:root', output, 1
  110     end
  111 
  112     test 'normal paragraph terminates at block delimiter' do
  113       input = <<~'EOS'
  114       normal text
  115       --
  116       text in open block
  117       --
  118       EOS
  119       output = convert_string_to_embedded input
  120       assert_css '.paragraph:root', output, 1
  121       assert_css '.openblock:root', output, 1
  122     end
  123 
  124     test 'normal paragraph terminates at list continuation' do
  125       input = <<~'EOS'
  126       normal text
  127       +
  128       EOS
  129       output = convert_string_to_embedded input
  130       assert_css '.paragraph:root', output, 2
  131       assert_xpath %((/*[@class="paragraph"])[1]/p[text() = "normal text"]), output, 1
  132       assert_xpath %((/*[@class="paragraph"])[2]/p[text() = "+"]), output, 1
  133     end
  134 
  135     test 'normal style turns literal paragraph into normal paragraph' do
  136       input = <<~'EOS'
  137       [normal]
  138        normal paragraph,
  139        despite the leading indent
  140       EOS
  141 
  142       output = convert_string_to_embedded input
  143       assert_css '.paragraph:root > p', output, 1
  144     end
  145 
  146     test 'automatically promotes index terms in DocBook output if indexterm-promotion-option is set' do
  147       input = <<~'EOS'
  148       Here is an index entry for ((tigers)).
  149       indexterm:[Big cats,Tigers,Siberian Tiger]
  150       Here is an index entry for indexterm2:[Linux].
  151       (((Operating Systems,Linux,Fedora)))
  152       Note that multi-entry terms generate separate index entries.
  153       EOS
  154 
  155       output = convert_string_to_embedded input, backend: 'docbook', attributes: { 'indexterm-promotion-option' => '' }
  156       assert_xpath '/simpara', output, 1
  157       term1 = xmlnodes_at_xpath '(//indexterm)[1]', output, 1
  158       assert_equal %(<indexterm>\n<primary>tigers</primary>\n</indexterm>), term1.to_s
  159       assert term1.next.content.start_with?('tigers')
  160 
  161       term2 = xmlnodes_at_xpath '(//indexterm)[2]', output, 1
  162       term2_elements = term2.elements
  163       assert_equal 3, term2_elements.size
  164       assert_equal '<primary>Big cats</primary>', term2_elements[0].to_s
  165       assert_equal '<secondary>Tigers</secondary>', term2_elements[1].to_s
  166       assert_equal '<tertiary>Siberian Tiger</tertiary>', term2_elements[2].to_s
  167 
  168       term3 = xmlnodes_at_xpath '(//indexterm)[3]', output, 1
  169       term3_elements = term3.elements
  170       assert_equal 2, term3_elements.size
  171       assert_equal '<primary>Tigers</primary>', term3_elements[0].to_s
  172       assert_equal '<secondary>Siberian Tiger</secondary>', term3_elements[1].to_s
  173 
  174       term4 = xmlnodes_at_xpath '(//indexterm)[4]', output, 1
  175       term4_elements = term4.elements
  176       assert_equal 1, term4_elements.size
  177       assert_equal '<primary>Siberian Tiger</primary>', term4_elements[0].to_s
  178 
  179       term5 = xmlnodes_at_xpath '(//indexterm)[5]', output, 1
  180       assert_equal %(<indexterm>\n<primary>Linux</primary>\n</indexterm>), term5.to_s
  181       assert term5.next.content.start_with?('Linux')
  182 
  183       assert_xpath '(//indexterm)[6]/*', output, 3
  184       assert_xpath '(//indexterm)[7]/*', output, 2
  185       assert_xpath '(//indexterm)[8]/*', output, 1
  186     end
  187 
  188     test 'does not automatically promote index terms in DocBook output if indexterm-promotion-option is not set' do
  189       input = <<~'EOS'
  190       The Siberian Tiger is one of the biggest living cats.
  191       indexterm:[Big cats,Tigers,Siberian Tiger]
  192       EOS
  193 
  194       output = convert_string_to_embedded input, backend: 'docbook'
  195 
  196       assert_css 'indexterm', output, 1
  197 
  198       term1 = xmlnodes_at_css 'indexterm', output, 1
  199       term1_elements = term1.elements
  200       assert_equal 3, term1_elements.size
  201       assert_equal '<primary>Big cats</primary>', term1_elements[0].to_s
  202       assert_equal '<secondary>Tigers</secondary>', term1_elements[1].to_s
  203       assert_equal '<tertiary>Siberian Tiger</tertiary>', term1_elements[2].to_s
  204     end
  205 
  206     test 'normal paragraph should honor explicit subs list' do
  207       input = <<~'EOS'
  208       [subs="specialcharacters"]
  209       *<Hey Jude>*
  210       EOS
  211 
  212       output = convert_string_to_embedded input
  213       assert_includes output, '*&lt;Hey Jude&gt;*'
  214     end
  215 
  216     test 'normal paragraph should honor specialchars shorthand' do
  217       input = <<~'EOS'
  218       [subs="specialchars"]
  219       *<Hey Jude>*
  220       EOS
  221 
  222       output = convert_string_to_embedded input
  223       assert_includes output, '*&lt;Hey Jude&gt;*'
  224     end
  225 
  226     test 'should add a hardbreak at end of each line when hardbreaks option is set' do
  227       input = <<~'EOS'
  228       [%hardbreaks]
  229       read
  230       my
  231       lips
  232       EOS
  233 
  234       output = convert_string_to_embedded input
  235       assert_css 'br', output, 2
  236       assert_xpath '//p', output, 1
  237       assert_includes output, "<p>read<br>\nmy<br>\nlips</p>"
  238     end
  239 
  240     test 'should be able to toggle hardbreaks by setting hardbreaks-option on document' do
  241       input = <<~'EOS'
  242       :hardbreaks-option:
  243 
  244       make
  245       it
  246       so
  247 
  248       :!hardbreaks:
  249 
  250       roll it back
  251       EOS
  252 
  253       output = convert_string_to_embedded input
  254       assert_xpath '(//p)[1]/br', output, 2
  255       assert_xpath '(//p)[2]/br', output, 0
  256     end
  257   end
  258 
  259   context 'Literal' do
  260     test 'single-line literal paragraphs' do
  261       # NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
  262       input = <<~EOS
  263       you know what?
  264 
  265        LITERALS
  266 
  267        ARE LITERALLY
  268 
  269        AWESOME!
  270       EOS
  271       output = convert_string_to_embedded input
  272       assert_xpath '//pre', output, 3
  273     end
  274 
  275     test 'multi-line literal paragraph' do
  276       # NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
  277       input = <<~EOS
  278       Install instructions:
  279 
  280        yum install ruby rubygems
  281        gem install asciidoctor
  282 
  283       You're good to go!
  284       EOS
  285       output = convert_string_to_embedded input
  286       assert_xpath '//pre', output, 1
  287       # indentation should be trimmed from literal block
  288       assert_xpath %(//pre[text() = "yum install ruby rubygems\ngem install asciidoctor"]), output, 1
  289     end
  290 
  291     test 'literal paragraph' do
  292       input = <<~'EOS'
  293       [literal]
  294       this text is literally literal
  295       EOS
  296       output = convert_string_to_embedded input
  297       assert_xpath %(/*[@class="literalblock"]//pre[text()="this text is literally literal"]), output, 1
  298     end
  299 
  300     test 'should read content below literal style verbatim' do
  301       input = <<~'EOS'
  302       [literal]
  303       image::not-an-image-block[]
  304       EOS
  305       output = convert_string_to_embedded input
  306       assert_xpath %(/*[@class="literalblock"]//pre[text()="image::not-an-image-block[]"]), output, 1
  307       assert_css 'img', output, 0
  308     end
  309 
  310     test 'listing paragraph' do
  311       input = <<~'EOS'
  312       [listing]
  313       this text is a listing
  314       EOS
  315       output = convert_string_to_embedded input
  316       assert_xpath %(/*[@class="listingblock"]//pre[text()="this text is a listing"]), output, 1
  317     end
  318 
  319     test 'source paragraph' do
  320       input = <<~'EOS'
  321       [source]
  322       use the source, luke!
  323       EOS
  324       output = convert_string_to_embedded input
  325       assert_xpath %(/*[@class="listingblock"]//pre[@class="highlight"]/code[text()="use the source, luke!"]), output, 1
  326     end
  327 
  328     test 'source code paragraph with language' do
  329       input = <<~'EOS'
  330       [source, perl]
  331       die 'zomg perl is tough';
  332       EOS
  333       output = convert_string_to_embedded input
  334       assert_xpath %(/*[@class="listingblock"]//pre[@class="highlight"]/code[@class="language-perl"][@data-lang="perl"][text()="die 'zomg perl is tough';"]), output, 1
  335     end
  336 
  337     test 'literal paragraph terminates at block attribute list' do
  338       # NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
  339       input = <<~EOS
  340        literal text
  341       [normal]
  342       normal text
  343       EOS
  344       output = convert_string_to_embedded input
  345       assert_xpath %(/*[@class="literalblock"]), output, 1
  346       assert_xpath %(/*[@class="paragraph"]), output, 1
  347     end
  348 
  349     test 'literal paragraph terminates at block delimiter' do
  350       # NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
  351       input = <<~EOS
  352        literal text
  353       --
  354       normal text
  355       --
  356       EOS
  357       output = convert_string_to_embedded input
  358       assert_xpath %(/*[@class="literalblock"]), output, 1
  359       assert_xpath %(/*[@class="openblock"]), output, 1
  360     end
  361 
  362     test 'literal paragraph terminates at list continuation' do
  363       # NOTE cannot use single-quoted heredoc because of https://github.com/jruby/jruby/issues/4260
  364       input = <<~EOS
  365        literal text
  366       +
  367       EOS
  368       output = convert_string_to_embedded input
  369       assert_xpath %(/*[@class="literalblock"]), output, 1
  370       assert_xpath %(/*[@class="literalblock"]//pre[text() = "literal text"]), output, 1
  371       assert_xpath %(/*[@class="paragraph"]), output, 1
  372       assert_xpath %(/*[@class="paragraph"]/p[text() = "+"]), output, 1
  373     end
  374   end
  375 
  376   context 'Quote' do
  377     test "single-line quote paragraph" do
  378       input = <<~'EOS'
  379       [quote]
  380       Famous quote.
  381       EOS
  382       output = convert_string input
  383       assert_xpath '//*[@class = "quoteblock"]', output, 1
  384       assert_xpath '//*[@class = "quoteblock"]//p', output, 0
  385       assert_xpath '//*[@class = "quoteblock"]//*[contains(text(), "Famous quote.")]', output, 1
  386     end
  387 
  388     test 'quote paragraph terminates at list continuation' do
  389       input = <<~'EOS'
  390       [quote]
  391       A famouse quote.
  392       +
  393       EOS
  394       output = convert_string_to_embedded input
  395       assert_css '.quoteblock:root', output, 1
  396       assert_css '.paragraph:root', output, 1
  397       assert_xpath %(/*[@class="paragraph"]/p[text() = "+"]), output, 1
  398     end
  399 
  400     test "verse paragraph" do
  401       output = convert_string("[verse]\nFamous verse.")
  402       assert_xpath '//*[@class = "verseblock"]', output, 1
  403       assert_xpath '//*[@class = "verseblock"]/pre', output, 1
  404       assert_xpath '//*[@class = "verseblock"]//p', output, 0
  405       assert_xpath '//*[@class = "verseblock"]/pre[normalize-space(text()) = "Famous verse."]', output, 1
  406     end
  407 
  408     test 'should perform normal subs on a verse paragraph' do
  409       input = <<~'EOS'
  410       [verse]
  411       _GET /groups/link:#group-id[\{group-id\}]_
  412       EOS
  413 
  414       output = convert_string_to_embedded input
  415       assert_includes output, '<pre class="content"><em>GET /groups/<a href="#group-id">{group-id}</a></em></pre>'
  416     end
  417 
  418     test 'quote paragraph should honor explicit subs list' do
  419       input = <<~'EOS'
  420       [subs="specialcharacters"]
  421       [quote]
  422       *Hey Jude*
  423       EOS
  424 
  425       output = convert_string_to_embedded input
  426       assert_includes output, '*Hey Jude*'
  427     end
  428   end
  429 
  430   context "special" do
  431     test "note multiline syntax" do
  432       Asciidoctor::ADMONITION_STYLES.each do |style|
  433         assert_xpath "//div[@class='admonitionblock #{style.downcase}']", convert_string("[#{style}]\nThis is a winner.")
  434       end
  435     end
  436 
  437     test "note block syntax" do
  438       Asciidoctor::ADMONITION_STYLES.each do |style|
  439         assert_xpath "//div[@class='admonitionblock #{style.downcase}']", convert_string("[#{style}]\n====\nThis is a winner.\n====")
  440       end
  441     end
  442 
  443     test "note inline syntax" do
  444       Asciidoctor::ADMONITION_STYLES.each do |style|
  445         assert_xpath "//div[@class='admonitionblock #{style.downcase}']", convert_string("#{style}: This is important, fool!")
  446       end
  447     end
  448 
  449     test 'should process preprocessor conditional in paragraph content' do
  450       input = <<~'EOS'
  451       ifdef::asciidoctor-version[]
  452       [sidebar]
  453       First line of sidebar.
  454       ifdef::backend[The backend is {backend}.]
  455       Last line of sidebar.
  456       endif::[]
  457       EOS
  458 
  459       expected = <<~'EOS'.chop
  460       <div class="sidebarblock">
  461       <div class="content">
  462       First line of sidebar.
  463       The backend is html5.
  464       Last line of sidebar.
  465       </div>
  466       </div>
  467       EOS
  468 
  469       result = convert_string_to_embedded input
  470       assert_equal expected, result
  471     end
  472 
  473     context 'Styled Paragraphs' do
  474       test 'should wrap text in simpara for styled paragraphs when converted to DocBook' do
  475         input = <<~'EOS'
  476         = Book
  477         :doctype: book
  478 
  479         [preface]
  480         = About this book
  481 
  482         [abstract]
  483         An abstract for the book.
  484 
  485         = Part 1
  486 
  487         [partintro]
  488         An intro to this part.
  489 
  490         == Chapter 1
  491 
  492         [sidebar]
  493         Just a side note.
  494 
  495         [example]
  496         As you can see here.
  497 
  498         [quote]
  499         Wise words from a wise person.
  500 
  501         [open]
  502         Make it what you want.
  503         EOS
  504 
  505         output = convert_string input, backend: 'docbook'
  506         assert_css 'abstract > simpara', output, 1
  507         assert_css 'partintro > simpara', output, 1
  508         assert_css 'sidebar > simpara', output, 1
  509         assert_css 'informalexample > simpara', output, 1
  510         assert_css 'blockquote > simpara', output, 1
  511         assert_css 'chapter > simpara', output, 1
  512       end
  513 
  514       test 'should convert open paragraph to open block' do
  515         input = <<~'EOS'
  516         [open]
  517         Make it what you want.
  518         EOS
  519 
  520         output = convert_string_to_embedded input
  521         assert_css '.openblock', output, 1
  522         assert_css '.openblock p', output, 0
  523       end
  524 
  525       test 'should wrap text in simpara for styled paragraphs with title when converted to DocBook' do
  526         input = <<~'EOS'
  527         = Book
  528         :doctype: book
  529 
  530         [preface]
  531         = About this book
  532 
  533         [abstract]
  534         .Abstract title
  535         An abstract for the book.
  536 
  537         = Part 1
  538 
  539         [partintro]
  540         .Part intro title
  541         An intro to this part.
  542 
  543         == Chapter 1
  544 
  545         [sidebar]
  546         .Sidebar title
  547         Just a side note.
  548 
  549         [example]
  550         .Example title
  551         As you can see here.
  552 
  553         [quote]
  554         .Quote title
  555         Wise words from a wise person.
  556         EOS
  557 
  558         output = convert_string input, backend: 'docbook'
  559         assert_css 'abstract > title', output, 1
  560         assert_xpath '//abstract/title[text() = "Abstract title"]', output, 1
  561         assert_css 'abstract > title + simpara', output, 1
  562         assert_css 'partintro > title', output, 1
  563         assert_xpath '//partintro/title[text() = "Part intro title"]', output, 1
  564         assert_css 'partintro > title + simpara', output, 1
  565         assert_css 'sidebar > title', output, 1
  566         assert_xpath '//sidebar/title[text() = "Sidebar title"]', output, 1
  567         assert_css 'sidebar > title + simpara', output, 1
  568         assert_css 'example > title', output, 1
  569         assert_xpath '//example/title[text() = "Example title"]', output, 1
  570         assert_css 'example > title + simpara', output, 1
  571         assert_css 'blockquote > title', output, 1
  572         assert_xpath '//blockquote/title[text() = "Quote title"]', output, 1
  573         assert_css 'blockquote > title + simpara', output, 1
  574       end
  575     end
  576 
  577     context 'Inline doctype' do
  578       test 'should only format and output text in first paragraph when doctype is inline' do
  579         input = "http://asciidoc.org[AsciiDoc] is a _lightweight_ markup language...\n\nignored"
  580         output = convert_string input, doctype: 'inline'
  581         assert_equal '<a href="http://asciidoc.org">AsciiDoc</a> is a <em>lightweight</em> markup language&#8230;&#8203;', output
  582       end
  583 
  584       test 'should output nil and warn if first block is not a paragraph' do
  585         input = '* bullet'
  586         using_memory_logger do |logger|
  587           output = convert_string input, doctype: 'inline'
  588           assert_nil output
  589           assert_message logger, :WARN, '~no inline candidate'
  590         end
  591       end
  592     end
  593   end
  594 
  595   context 'Custom' do
  596     test 'should not warn if paragraph style is unregisted' do
  597       input = <<~'EOS'
  598       [foo]
  599       bar
  600       EOS
  601       using_memory_logger do |logger|
  602         convert_string_to_embedded input
  603         assert_empty logger.messages
  604       end
  605     end
  606 
  607     test 'should log debug message if paragraph style is unknown and debug level is enabled' do
  608       input = <<~'EOS'
  609       [foo]
  610       bar
  611       EOS
  612       using_memory_logger Logger::Severity::DEBUG do |logger|
  613         convert_string_to_embedded input
  614         assert_message logger, :DEBUG, '<stdin>: line 2: unknown style for paragraph: foo', Hash
  615       end
  616     end
  617   end
  618 end