summaryrefslogtreecommitdiff
path: root/lib/ex_unit/examples/difference.exs
blob: 6e105f71d5536a14ddd4d6ec48dbd602c42e57e0 (plain)
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
ExUnit.start(seed: 0)

defmodule Difference do
  @moduledoc """
  This module contains failing tests to see
  difference highlighting in action.
  """
  use ExUnit.Case

  defmodule User do
    defstruct [:age]
  end

  defmacrop one, do: 1

  describe "regular context" do
    test "integers" do
      assert 491_512_235 == 490_512_035
    end

    test "floats" do
      assert 42.0 == 43.0
    end

    test "strings" do
      string1 = "fox hops over \"the dog"
      string2 = "fox  jumps over the lazy cat"
      assert string1 == string2
    end

    test "whitespace" do
      list1 = [%{a: "abc "}, %{a: "def"}, %{c: "gh"}]
      list2 = [%{a: "abc"}, %{a: " def"}, %{c: "hi"}]
      assert list1 == list2
    end

    test "large strings" do
      string1 = "short"
      string2 = "really long string that should not emit diff"
      assert string1 == string2
    end

    test "large strings; inner" do
      tuple1 = {"short"}
      tuple2 = {"really long string that should not emit diff"}
      assert tuple1 == tuple2
    end

    test "lists" do
      list1 = ["Tvo", make_ref(), :ok, {}]
      list2 = ["Two", :ok, self(), {true}]
      assert list1 == list2
    end

    test "lists; missing entries" do
      assert [] == [1, 2, 3]
    end

    test "lists; surplus entries" do
      assert [1, 2, 3] == []
    end

    test "improper lists" do
      list1 = [1 | "b"]
      list2 = [1, "a"]
      assert list1 == list2
    end

    test "charlists" do
      charlist1 = 'fox hops over \'the dog'
      charlist2 = 'fox jumps over the lazy cat'
      assert charlist1 == charlist2
    end

    test "keyword lists" do
      assert [file: "nofile", line: 12] == [file: nil, lime: 10]
    end

    test "keyword lists; reverse order" do
      keyword1 = [port: 4000, max_connections: 1000]
      keyword2 = [max_connections: 1000, port: 4000]
      assert keyword1 == keyword2
    end

    test "tuples" do
      tuple1 = {:hex, "0.1", [{:ex_doc}]}
      tuple2 = {:hex, "1.1"}
      assert tuple1 == tuple2
    end

    test "maps; mixed diff" do
      map1 = Enum.into(1..15, %{}, &{&1, &1}) |> Map.delete(13)
      map2 = Enum.reduce(5..10, map1, &Map.delete(&2, &1)) |> Map.put(13, 13) |> Map.put(12, 32)
      assert map1 == map2
    end

    test "maps; missing pairs" do
      map1 = %{baz: 12}
      map2 = %{foo: 12, bar: 12, baz: 12}
      assert map1 == map2
    end

    test "maps; surplus pairs" do
      map1 = %{foo: 12, bar: 12, baz: 12}
      map2 = %{baz: 12}
      assert map1 == map2
    end

    test "maps; missing pair" do
      assert %{} == %{baz: 12}
    end

    test "maps; surplus pair" do
      assert %{baz: 12} == %{}
    end

    test "structs" do
      assert %User{age: 16} == %User{age: 21}
    end
  end

  describe "match context" do
    test "pins" do
      x = 1
      assert ^x = 2
    end

    test "vars" do
      assert {x, x} = {1, 2}
    end

    test "strings; concat" do
      assert "fox hops over the" <> x = "fox jumps over the lazy cat"
    end

    test "lists; head and tail" do
      assert ["Tvo", 1 | [:ok, {}]] = ["Two", :ok, self(), {true}]
    end

    test "lists; concat" do
      assert ["Tvo", 1] ++ [:ok, {}] = ["Two", :ok, self(), {true}]
    end

    test "maps" do
      assert %{baz: 12} = %{foo: 13, bar: 13, baz: 13}
    end

    test "macro" do
      assert one() = 2
    end

    test "underscore" do
      assert {_, 1} = {1, 2}
    end
  end

  describe "receive" do
    test "no messages in the mailbox" do
      assert_received x when x == :hello
    end

    test "only 1 message in the mailbox" do
      send(self(), {:message, 1})

      assert_received x when x == :hello
    end

    test "more than 1 messages in the mailbox" do
      for i <- 1..2, do: send(self(), {:message, i})

      assert_received x when x == :hello
    end

    test "more than 10 messages in the mailbox" do
      for i <- 1..11, do: send(self(), {:message, i})

      assert_received x when x == :hello
    end

    test "macro" do
      send(self(), 12)

      assert_received one()
    end
  end

  test "only one side with metadata" do
    assert "one" != "one"
  end
end