|
| 1 | +defmodule Together.Test.AssertionsTest do |
| 2 | + use ExUnit.Case |
| 3 | + |
| 4 | + import Together.Test.Assertions |
| 5 | + |
| 6 | + describe "assert_equal/2" do |
| 7 | + test "asserts that two values are equal" do |
| 8 | + assert_equal(2, 2) |
| 9 | + |
| 10 | + assert_raise ExUnit.AssertionError, fn -> |
| 11 | + assert_equal(2, 3) |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + test "allows chaining assertions" do |
| 16 | + 2 |
| 17 | + |> assert_equal(2) |
| 18 | + |> Kernel.+(1) |
| 19 | + |> assert_equal(3) |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + describe "assert_match/2" do |
| 24 | + test "asserts that the first argument matches the given pattern" do |
| 25 | + assert_match(%{"key" => "value"}, %{"key" => _}) |
| 26 | + |
| 27 | + assert_raise ExUnit.AssertionError, fn -> |
| 28 | + assert_match(%{"key" => "value"}, %{"key" => "other"}) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + test "allows chaining assertions" do |
| 33 | + %{"key" => "value"} |
| 34 | + |> assert_match(%{"key" => _}) |
| 35 | + |> Map.put("new_key", "new_value") |
| 36 | + |> assert_match(%{"new_key" => _}) |
| 37 | + end |
| 38 | + |
| 39 | + test "allows pinning in patterns" do |
| 40 | + value = "value" |
| 41 | + |
| 42 | + assert_match(%{"key" => "value"}, %{"key" => ^value}) |
| 43 | + |
| 44 | + assert_raise ExUnit.AssertionError, fn -> |
| 45 | + assert_match(%{"key" => "other"}, %{"key" => ^value}) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + test "allows extracting values in patterns" do |
| 50 | + assert_match(%{"key" => "value"}, %{"key" => extracted_value}) |
| 51 | + assert extracted_value == "value" |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + describe "assert_recent/2" do |
| 56 | + test "asserts that a timestamp is recent" do |
| 57 | + assert_recent(DateTime.utc_now()) |
| 58 | + |
| 59 | + assert_raise ExUnit.AssertionError, fn -> |
| 60 | + assert_recent(DateTime.add(DateTime.utc_now(), -20, :second), 10) |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + describe "assert_set_equal/2" do |
| 66 | + test "asserts that two sets are equal" do |
| 67 | + assert_set_equal([1, 2, 3], [2, 3, 1]) |
| 68 | + assert_set_equal([1, 2, 3], [2, 3, 1, 1]) |
| 69 | + |
| 70 | + assert_raise ExUnit.AssertionError, fn -> |
| 71 | + assert_set_equal([1, 2, 3], [1, 2]) |
| 72 | + end |
| 73 | + |
| 74 | + assert_raise ExUnit.AssertionError, fn -> |
| 75 | + assert_set_equal([1, 2, 3], [1, 2, 4]) |
| 76 | + end |
| 77 | + |
| 78 | + assert_raise ExUnit.AssertionError, fn -> |
| 79 | + assert_set_equal([1, 2, 3], [1, 2, 3, 4]) |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + describe "assert_set_match/2" do |
| 85 | + test "asserts that the first set matches the given pattern" do |
| 86 | + assert_set_match([1, 2, 3], [2, 3, _]) |
| 87 | + |
| 88 | + assert_raise ExUnit.AssertionError, fn -> |
| 89 | + assert_set_match([1, 2, 3], [1, 2]) |
| 90 | + end |
| 91 | + |
| 92 | + assert_raise ExUnit.AssertionError, fn -> |
| 93 | + assert_set_match([1, 2, 3], [1, 2, 4]) |
| 94 | + end |
| 95 | + |
| 96 | + assert_raise ExUnit.AssertionError, fn -> |
| 97 | + assert_set_match([1, 2, 3], [1, 2, 3, 4]) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + test "allows pinning in patterns" do |
| 102 | + value = 1 |
| 103 | + assert_set_match([1, 2, 3], [^value, 2, _]) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe "assert_contains/2" do |
| 108 | + test "asserts that a collection contains an item" do |
| 109 | + assert_contains([1, 2, 3], 2) |
| 110 | + |
| 111 | + assert_raise ExUnit.AssertionError, fn -> |
| 112 | + assert_contains([1, 2, 3], 4) |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + test "allows chaining assertions" do |
| 117 | + [1, 2] |
| 118 | + |> assert_contains(2) |
| 119 | + |> Enum.map(&(&1 * 2)) |
| 120 | + |> assert_contains(4) |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + describe "assert_contains_match/2" do |
| 125 | + test "asserts that a collection contains an item matching the pattern" do |
| 126 | + assert_contains_match([%{"key" => "value"}, %{"other_key" => "other"}], %{"key" => _}) |
| 127 | + |
| 128 | + assert_raise ExUnit.AssertionError, fn -> |
| 129 | + assert_contains_match([%{"key" => "value"}], %{"key" => "other"}) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + test "allows pinning in patterns" do |
| 134 | + value = "value" |
| 135 | + assert_contains_match([%{"key" => "value"}, %{"other_key" => "other"}], %{"key" => ^value}) |
| 136 | + end |
| 137 | + |
| 138 | + test "allows chaining assertions" do |
| 139 | + [%{"key" => "value"}, %{"other_key" => "other"}] |
| 140 | + |> assert_contains_match(%{"key" => _}) |
| 141 | + |> Enum.map(&Map.new(&1, fn {k, v} -> {String.upcase(k), v} end)) |
| 142 | + |> assert_contains_match(%{"KEY" => _}) |
| 143 | + end |
| 144 | + end |
| 145 | +end |
0 commit comments