-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamed_arguments_test.exs
74 lines (53 loc) · 2.16 KB
/
named_arguments_test.exs
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
defmodule NamedArgumentsTest do
use ExUnit.Case
defmodule TestModule do
use NamedArguments
def no_args, do: :ok
def no_default_args(opts), do: opts
def default_arg(opts \\ [name: "Samar", age: 18]), do: opts
def default_list_arg(nums \\ [1, 2, 3]), do: nums
def multi_args(arg1, arg2), do: {arg1, arg2}
def multi_last_default_arg(arg1, opts \\ [name: "Samar", age: 18]), do: {arg1, opts}
def multi_default_args(age \\ 18, name \\ "Samar"), do: {age, name}
def multi_kw_default_args(person \\ [name: "Samar", age: 18], opts \\ [power: :nothing]),
do: {person, opts}
def map_args(opts \\ %{name: "Samar", age: 18}), do: opts
end
test "no args" do
assert TestModule.no_args() === :ok
end
test "no default args" do
assert TestModule.no_default_args("hello") === "hello"
assert TestModule.no_default_args([1, 2]) === [1, 2]
assert TestModule.no_default_args(name: "Samar", age: 18) === [name: "Samar", age: 18]
end
test "default arg" do
assert TestModule.default_arg(name: "Samar", age: 18) === TestModule.default_arg()
assert TestModule.default_arg(name: "Hello") === [age: 18, name: "Hello"]
end
test "default normal list arg" do
assert TestModule.default_list_arg() === [1, 2, 3]
assert TestModule.default_list_arg([1, 2]) === [1, 2]
end
test "multi args" do
assert TestModule.multi_args(1, 2) === {1, 2}
end
test "multi with last default args" do
assert TestModule.multi_last_default_arg(:person) === {:person, [name: "Samar", age: 18]}
assert TestModule.multi_last_default_arg(:person, name: "Hello") ===
{:person, [age: 18, name: "Hello"]}
end
test "multi default args" do
assert TestModule.multi_default_args() === TestModule.multi_default_args(18, "Samar")
assert TestModule.multi_default_args(26) === {26, "Samar"}
end
test "multi keyword list default args" do
person = [name: "Hello", age: 25]
opts = []
assert TestModule.multi_kw_default_args(person, opts) === {person, [power: :nothing]}
end
test "map default args" do
opts = %{name: "Hello"}
assert %{name: "Hello", age: 18} === TestModule.map_args(opts)
end
end