File: inline_code_test.exs

package info (click to toggle)
elixir-earmark-parser 1.4.44-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,148 kB
  • sloc: makefile: 9
file content (156 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download
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
defmodule Acceptance.Ast.InlineCodeTest do
  use ExUnit.Case, async: true
  import Support.Helpers, only: [as_ast: 1, parse_html: 1]
  import EarmarkAstDsl

  describe "Inline Code" do
    test "plain simple" do
      markdown = "`foo`\n"
      html = "<p><code class=\"inline\">foo</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "plain simple, right?" do
      markdown = "`hi`lo`\n"
      html = "<p><code class=\"inline\">hi</code>lo`</p>\n"
      ast = parse_html(html)
      messages = [{:warning, 1, "Closing unclosed backquotes ` at end of input"}]

      assert as_ast(markdown) == {:error, ast, messages}
    end

    test "this time you got it right" do
      markdown = "`a\nb`c\n"
      html = "<p><code class=\"inline\">a b</code>c</p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "and again!!!" do
      markdown = "+ ``a `\n`\n b``c"
      html = "<ul>\n<li><code class=\"inline\">a ` ` b</code>c</li>\n</ul>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end

  describe "Inline Code with escapes" do
    test "a lone escaped backslash" do
      markdown = "`\\\\`"
      html = "<p><code class=\"inline\">\\\\</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "with company" do
      markdown = "`hello \\\\ world`"
      html = "<p><code class=\"inline\">hello \\\\ world</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "unescaped escape" do
      markdown = "`\\`"
      html = "<p><code class=\"inline\">\\</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "backtix cannot be escaped" do
      markdown = "`` \\` ``"
      html = "<p><code class=\"inline\">\\`</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "unless at the beginning" do
      markdown = "\\``code\\`"
      html = "<p>`<code class=\"inline\">code\\</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end

  describe "whitespace treatment" do
    test "squashing" do
      markdown = "`alpha   beta`"
      html = "<p><code class=\"inline\">alpha beta</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "remove at start" do
      markdown = "`  alpha beta`"
      html = "<p><code class=\"inline\">alpha beta</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "remove and squash" do
      markdown = "`  alpha   beta `"
      html = "<p><code class=\"inline\">alpha beta</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "remove and squash newlines too" do
      markdown = "`\n  alpha  \n\n beta `"
      html = "<p><code class=\"inline\">alpha beta</code></p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "inline code inside lists (was regtest #48)" do
      markdown = " * `a\n * b`"
      html = ~s[<ul>\n<li><code class="inline">a * b</code>\n</li>\n</ul>\n]
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end

  describe "closing and reopening" do
    test "two codes" do
      markdown = """
      ` Single Opens
      ` Single Closes ``Double reopens
      """

      ast = [
        p([tag("code", "Single Opens", [class: "inline"], %{line: 1}), " Single Closes ``Double reopens"])
      ]

      messages = [{:warning, 2, "Closing unclosed backquotes `` at end of input"}]

      assert as_ast(markdown) == {:error, ast, messages}
    end
  end
end

# SPDX-License-Identifier: Apache-2.0