-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrop_test.exs
More file actions
85 lines (68 loc) · 2.01 KB
/
rop_test.exs
File metadata and controls
85 lines (68 loc) · 2.01 KB
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
defmodule RopTest do
use ExSpec, async: true
import ExUnit.CaptureIO
use Rop
doctest Rop
describe ">>> macro" do
test "returns the piped value in case of happy path" do
assert (0 |> inc >>> inc >>> inc) == {:ok, 3}
end
test "returns the value of first error" do
assert (0 |> inc >>> error) == {:error, "Error at 1"}
end
test "propagates the error" do
assert (0 |> inc >>> error >>> inc >>> inc) == {:error, "Error at 1"}
end
test "propagates the correct error message" do
assert (0 |> inc >>> inc >>> error >>> error >>> inc) == {:error, "Error at 2"}
end
end
describe "try_catch macro" do
test "catches and returns raised errors in a tagged tupple { :error, %SomeError{} } if something breaks" do
assert (:raise |> try_catch(arithmetic_error)) == {:error, %ArithmeticError{}}
end
test "returns the piped value otherwise" do
assert (:pass |> try_catch(arithmetic_error)) == {:ok, 1}
end
end
describe "tee" do
test "passes the arguments through after executing them in the function" do
a = (fn ->
0 |> simple_inc |> simple_inc |> tee(simple_sideeffect)
end)
assert a.() == {:ok, 2}
end
test "the sideeffect in the function is executed" do
a = (fn ->
0 |> simple_inc |> simple_inc |> tee(simple_sideeffect)
end)
assert capture_io(a) == "2\n"
end
end
describe "bind" do
test "wraps a function to return a tagged tuple `{:ok, result}` from the returned value" do
a = 0 |> simple_inc |> bind(simple_inc)
assert a == {:ok, 2}
end
end
# returns an unrelated to passed-in arguments value + has a side-effect (logging)
defp simple_sideeffect(a) do
IO.inspect a
:unrelated
end
defp inc(cnt) do
{:ok, simple_inc(cnt)}
end
defp simple_inc(cnt) do
cnt + 1
end
defp error(v) do
{:error, "Error at #{v}"}
end
defp arithmetic_error(:pass) do
1
end
defp arithmetic_error(:raise) do
raise ArithmeticError
end
end