File: html_formatter_test.exs

package info (click to toggle)
elixir-makeup 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 384 kB
  • sloc: javascript: 24; makefile: 9
file content (84 lines) | stat: -rw-r--r-- 2,671 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
defmodule MakeupTest.Lexer.HTMLFormatterTest do
  use ExUnit.Case, async: true
  import ExUnitProperties
  alias Makeup.Formatters.HTML.HTMLFormatter

  test "edge case - character with value = 128" do
    # Handles an edge case in the HTML formatter (already fixed),
    # in which the case c == 128 wasn't handled.
    # The previous version would raise an error.

    # Encode the right hand side as a concatenation of binaries
    # to make it more obvious:
    assert HTMLFormatter.format_as_binary([{:string, %{}, [128]}]) ==
      ~S[<pre class="highlight"><code>] <>
      ~S[<span class="s">] <>
      << 128 :: utf8 >> <>
      ~S[</span>] <>
      ~S[</code></pre>]
  end

  test "encode ASCII character (c <= 127)" do
    # Handles an edge case in the HTML formatter (already fixed),
    # in which the case c == 128 wasn't handled.
    # The previous version would raise an error.

    # Encode the right hand side as a concatenation of binaries
    # to make it more obvious:
    check all c <- StreamData.integer(1..127),
              not(c in [?&, ?<, ?>, ?", ?']) do
      html =
        ~S[<pre class="highlight"><code>] <>
        ~S[<span class="s">] <>
        << c >> <>
        ~S[</span>] <>
        ~S[</code></pre>]

      assert HTMLFormatter.format_as_binary([{:string, %{}, [c]}]) == html
      assert HTMLFormatter.format_as_binary([{:string, %{}, c}]) == html
    end
  end

  test "encode ASCII character (c >= 128)" do
    # Some of these characters won't be valid unicode but that doesn't matter

    # Encode the right hand side as a concatenation of binaries
    # to make it more obvious:
    check all c <- StreamData.integer(128..16666) do
      html =
        ~S[<pre class="highlight"><code>] <>
        ~S[<span class="s">] <>
        << c :: utf8 >> <>
        ~S[</span>] <>
        ~S[</code></pre>]

      assert HTMLFormatter.format_as_binary([{:string, %{}, [c]}]) == html
      assert HTMLFormatter.format_as_binary([{:string, %{}, c}]) == html
    end
  end

  test "special characters are correctly encoded" do
    for {c, encoded} <- [
      {?&, "&amp;"},
      {?<, "&lt;"},
      {?>, "&gt;"},
      {?", "&quot;"},
      {?', "&#39;"}
    ] do
      assert HTMLFormatter.format_as_binary([{:string, %{}, [c]}]) ==
        ~S[<pre class="highlight"><code>] <>
        ~S[<span class="s">] <>
        encoded <>
        ~S[</span>] <>
        ~S[</code></pre>]
    end
  end

  test "raise exception when token is not an iolist" do
    msg = "Found `%{}` inside what should be an iolist"

    assert_raise RuntimeError, msg, fn ->
      assert HTMLFormatter.format_as_binary([{:string, %{}, %{}}])
    end
  end
end