Extract answers from Structs module
This commit is contained in:
@@ -5,38 +5,31 @@ defmodule Structs do
|
|||||||
defstruct [:name, :age ]
|
defstruct [:name, :age ]
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "structs are defined and named after a module" do
|
|
||||||
person = %Person{}
|
|
||||||
assert person == %Person{}
|
|
||||||
end
|
|
||||||
|
|
||||||
koan "you can access the fields of a struct" do
|
koan "you can access the fields of a struct" do
|
||||||
nobody = %Person{}
|
nobody = %Person{}
|
||||||
assert nobody.age == nil
|
assert nobody.age == :__
|
||||||
assert nobody.name == nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "You can pass initial values to structs" do
|
koan "You can pass initial values to structs" do
|
||||||
joe = %Person{name: "Joe", age: 23}
|
joe = %Person{name: "Joe", age: 23}
|
||||||
assert joe.age == 23
|
assert joe.name == :__
|
||||||
assert joe.name == "Joe"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "update fields with the pipe '|' operator" do
|
koan "update fields with the pipe '|' operator" do
|
||||||
joe = %Person{name: "Joe", age: 23}
|
joe = %Person{name: "Joe", age: 23}
|
||||||
older = %{ joe | age: joe.age + 10}
|
older = %{ joe | age: joe.age + 10}
|
||||||
assert older.age == 33
|
assert older.age == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "the original struct is not affected by updates" do
|
koan "the original struct is not affected by updates" do
|
||||||
joe = %Person{name: "Joe", age: 23}
|
joe = %Person{name: "Joe", age: 23}
|
||||||
assert %{ joe | age: joe.age + 10}.age == 33
|
assert %{ joe | age: joe.age + 10}.age == 33
|
||||||
assert joe.age == 23
|
assert joe.age == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "you can pattern match into the fields of a struct" do
|
koan "you can pattern match into the fields of a struct" do
|
||||||
%Person{age: age} = %Person{age: 22, name: "Silvia"}
|
%Person{age: age} = %Person{age: 22, name: "Silvia"}
|
||||||
assert age == 22
|
assert age == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule Plane do
|
defmodule Plane do
|
||||||
@@ -44,16 +37,14 @@ defmodule Structs do
|
|||||||
end
|
end
|
||||||
|
|
||||||
koan "or onto the type of the struct itself" do
|
koan "or onto the type of the struct itself" do
|
||||||
assert is_a_plane(%Plane{passengers: 417, maker: :boeing})
|
assert is_a_plane(%Plane{passengers: 417, maker: :boeing}) == :__
|
||||||
refute is_a_plane(%Person{age: 22, name: "Silvia"})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_a_plane(%Plane{}), do: true
|
def is_a_plane(%Plane{}), do: true
|
||||||
def is_a_plane(_), do: false
|
|
||||||
|
|
||||||
koan "are basically maps" do
|
koan "are basically maps" do
|
||||||
silvia = %Person{age: 22, name: "Silvia"}
|
silvia = %Person{age: 22, name: "Silvia"}
|
||||||
|
|
||||||
assert Map.fetch!(silvia, :age) == 22
|
assert Map.fetch!(silvia, :age) == :__
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -149,6 +149,20 @@ defmodule KoansHarnessTest do
|
|||||||
test_all(Arithmetic, answers)
|
test_all(Arithmetic, answers)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "Structs" do
|
||||||
|
answers = [
|
||||||
|
nil,
|
||||||
|
"Joe",
|
||||||
|
33,
|
||||||
|
23,
|
||||||
|
22,
|
||||||
|
true,
|
||||||
|
22,
|
||||||
|
]
|
||||||
|
|
||||||
|
test_all(Structs, answers)
|
||||||
|
end
|
||||||
|
|
||||||
def test_all(module, answers) do
|
def test_all(module, answers) do
|
||||||
module.all_koans
|
module.all_koans
|
||||||
|> Enum.zip(answers)
|
|> Enum.zip(answers)
|
||||||
|
|||||||
Reference in New Issue
Block a user