summaryrefslogtreecommitdiff
path: root/test/text_test.rb
blob: 5501e5d047379bad89b204cca71f5896283e98ed (plain) (blame)
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
# encoding: UTF-8
unless defined? ASCIIDOCTOR_PROJECT_DIR
  $: << File.dirname(__FILE__); $:.uniq!
  require 'test_helper'
end

context "Text" do
  test "proper encoding to handle utf8 characters in document using html backend" do
    output = example_document(:encoding).render
    assert_xpath '//p', output, 4
    assert_xpath '//a', output, 1
  end

  test "proper encoding to handle utf8 characters in embedded document using html backend" do
    output = example_document(:encoding, :header_footer => false).render
    assert_xpath '//p', output, 4
    assert_xpath '//a', output, 1
  end

  test "proper encoding to handle utf8 characters in document using docbook45 backend" do
    output = example_document(:encoding, :attributes => {'backend' => 'docbook45'}).render
    assert_xpath '//xmlns:simpara', output, 4
    assert_xpath '//xmlns:ulink', output, 1
  end

  test "proper encoding to handle utf8 characters in embedded document using docbook45 backend" do
    output = example_document(:encoding, :header_footer => false, :attributes => {'backend' => 'docbook45'}).render
    assert_xpath '//simpara', output, 4
    assert_xpath '//ulink', output, 1
  end

  # NOTE this test ensures we have the encoding line on block templates too
  test 'proper encoding to handle utf8 characters in arbitrary block' do
    input = []
    input << "[verse]\n"
    input.concat(File.readlines(sample_doc_path(:encoding)))
    doc = empty_document
    reader = Asciidoctor::PreprocessorReader.new doc, input
    block = Asciidoctor::Parser.next_block(reader, doc)
    assert_xpath '//pre', block.render.gsub(/^\s*\n/, ''), 1
  end

  test 'proper encoding to handle utf8 characters from included file' do
    input = <<-EOS
include::fixtures/encoding.asciidoc[tags=romé]
    EOS
    doc = empty_safe_document :base_dir => File.expand_path(File.dirname(__FILE__))
    reader = Asciidoctor::PreprocessorReader.new doc, input
    block = Asciidoctor::Parser.next_block(reader, doc)
    output = block.render
    assert_css '.paragraph', output, 1
  end

  test 'escaped text markup' do
    assert_match(/All your &lt;em&gt;inline&lt;\/em&gt; markup belongs to &lt;strong&gt;us&lt;\/strong&gt;!/,
        render_string('All your <em>inline</em> markup belongs to <strong>us</strong>!'))
  end

  test "line breaks" do
    assert_xpath "//br", render_string("Well this is +\njust fine and dandy, isn't it?"), 1
  end

  test "single- and double-quoted text" do
    rendered = render_string("``Where?,'' she said, flipping through her copy of `The New Yorker.'")
    assert_match(/&#8220;Where\?,&#8221;/, rendered)
    assert_match(/&#8216;The New Yorker.&#8217;/, rendered)
  end

  test 'horizontal rule' do
    input = <<-EOS
This line is separated by a horizontal rule...

'''

...from this line.
    EOS
    output = render_embedded_string input
    assert_xpath "//hr", output, 1
    assert_xpath "/*[@class='paragraph']", output, 2
    assert_xpath "(/*[@class='paragraph'])[1]/following-sibling::hr", output, 1
    assert_xpath "/hr/following-sibling::*[@class='paragraph']", output, 1
  end

  test 'markdown horizontal rules' do
    variants = [
      '---',
      '- - -',
      '***',
      '* * *',
      '___',
      '_ _ _'
    ]

    offsets = [
      '',
      ' ',
      '  ',
      '   '
    ]

    variants.each do |variant|
      offsets.each do |offset|
        input = <<-EOS
This line is separated by a horizontal rule...

#{offset}#{variant}

...from this line.
        EOS
        output = render_embedded_string input
        assert_xpath "//hr", output, 1
        assert_xpath "/*[@class='paragraph']", output, 2
        assert_xpath "(/*[@class='paragraph'])[1]/following-sibling::hr", output, 1
        assert_xpath "/hr/following-sibling::*[@class='paragraph']", output, 1
      end
    end
  end

  test 'markdown horizontal rules negative case' do

    bad_variants = [
      '- - - -',
      '* * * *',
      '_ _ _ _'
    ]

    good_offsets = [
      '',
      ' ',
      '  ',
      '   '
    ]

    bad_variants.each do |variant|
      good_offsets.each do |offset|
        input = <<-EOS
This line is separated something that is not a horizontal rule...

#{offset}#{variant}

...from this line.
        EOS
        output = render_embedded_string input
        assert_xpath '//hr', output, 0
      end
    end

    good_variants = [
      '- - -',
      '* * *',
      '_ _ _'
    ]

    bad_offsets = [
      "\t",
      '    '
    ]

    good_variants.each do |variant|
      bad_offsets.each do |offset|
        input = <<-EOS
This line is separated something that is not a horizontal rule...

#{offset}#{variant}

...from this line.
        EOS
        output = render_embedded_string input
        assert_xpath '//hr', output, 0
      end
    end
  end

  test "emphasized text" do
    assert_xpath "//em", render_string("An 'emphatic' no")
  end

  test "emphasized text with single quote" do
    assert_xpath "//em[text()=\"Johnny#{[8217].pack('U*')}s\"]", render_string("It's 'Johnny's' phone")
  end

  test "emphasized text with escaped single quote" do
    assert_xpath "//em[text()=\"Johnny's\"]", render_string("It's 'Johnny\\'s' phone")
  end

  test "escaped single quote is restored as single quote" do
    assert_xpath "//p[contains(text(), \"Let's do it!\")]", render_string("Let\\'s do it!")
  end

  test "emphasized text at end of line" do
    assert_xpath "//em", render_string("This library is 'awesome'")
  end

  test "emphasized text at beginning of line" do
    assert_xpath "//em", render_string("'drop' it")
  end

  test "emphasized text across line" do
    assert_xpath "//em", render_string("'check it'")
  end

  test "unquoted text" do
    assert_no_match(/#/, render_string("An #unquoted# word"))
  end

  test "backtick-escaped text followed by single-quoted text" do
    assert_match(/<code>foo<\/code>/, render_string(%Q(run `foo` 'dog')))
  end

  context "basic styling" do
    setup do
      @rendered = render_string("A *BOLD* word.  An _italic_ word.  A +mono+ word.  ^superscript!^ and some ~subscript~.")
    end

    test "strong" do
      assert_xpath "//strong", @rendered
    end

    test "italic" do
      assert_xpath "//em", @rendered
    end

    test "monospaced" do
      assert_xpath "//code", @rendered
    end

    test "superscript" do
      assert_xpath "//sup", @rendered
    end

    test "subscript" do
      assert_xpath "//sub", @rendered
    end

    test "backticks" do
      assert_xpath "//code", render_string("This is `totally cool`.")
    end

    test "nested styles" do
      rendered = render_string("Winning *big _time_* in the +city *boyeeee*+.")

      assert_xpath "//strong/em", rendered
      assert_xpath "//code/strong", rendered
    end

    test "unconstrained quotes" do
      rendered_chars = render_string("**B**__I__++M++")
      assert_xpath "//strong", rendered_chars
      assert_xpath "//em", rendered_chars
      assert_xpath "//code", rendered_chars
    end
  end

  test 'should format Asian characters as words' do
    assert_xpath '//strong', (render_embedded_string 'bold *要* bold')
    assert_xpath '//strong', (render_embedded_string 'bold *素* bold')
    assert_xpath '//strong', (render_embedded_string 'bold *要素* bold')
  end
end