forked from seven1m/30-days-of-elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-record.exs
55 lines (42 loc) · 1.14 KB
/
06-record.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
ExUnit.start
defmodule User do
defstruct email: nil, password: nil
end
defimpl String.Chars, for: User do
def to_string(%User{email: email}) do
email
end
end
defmodule RecordTest do
use ExUnit.Case
defmodule ScopeTest do
use ExUnit.Case
require Record
Record.defrecordp :person, first_name: nil, last_name: nil, age: nil
test "defrecordp" do
p = person(first_name: "Kai", last_name: "Morgan", age: 5) # regular function call
assert p == {:person, "Kai", "Morgan", 5} # just a tuple!
end
end
# CompileError
# test "defrecordp out of scope" do
# person()
# end
def sample do
%User{email: "[email protected]", password: "trains"} # special % syntax for struct creation
end
test "defstruct" do
assert sample == %{__struct__: User, email: "[email protected]", password: "trains"}
end
test "property" do
assert sample.email == "[email protected]"
end
test "update" do
u = sample
u2 = %User{u | email: "[email protected]"}
assert u2 == %User{email: "[email protected]", password: "trains"}
end
test "protocol" do
assert to_string(sample) == "[email protected]"
end
end