Extract answers from PatternMatching module
This commit is contained in:
@@ -2,56 +2,60 @@ defmodule PatternMatching do
|
|||||||
use Koans
|
use Koans
|
||||||
|
|
||||||
koan "one matches one" do
|
koan "one matches one" do
|
||||||
assert match?(1, 1)
|
assert match?(1, :__)
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "a pattern can change" do
|
koan "a pattern can change" do
|
||||||
a = 1
|
a = 1
|
||||||
assert a = 2
|
assert a = :__
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO not sure about this koan?
|
||||||
koan "a pattern can also be strict" do
|
koan "a pattern can also be strict" do
|
||||||
a = 1
|
a = 1
|
||||||
assert ^a = 2
|
assert ^a = :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "patterns can be used to pull things apart" do
|
koan "patterns can be used to pull things apart" do
|
||||||
[head | tail] = [1,2,3,4]
|
[head | _tail] = [1,2,3,4]
|
||||||
|
|
||||||
assert head == 1
|
assert head == :__
|
||||||
assert tail == [2,3,4]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "or put them back together" do
|
koan "...whichever side you actually need" do
|
||||||
|
[_head | tail] = [1,2,3,4]
|
||||||
|
|
||||||
|
assert tail == :__
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
koan "and then put them back together" do
|
||||||
head = 1
|
head = 1
|
||||||
tail = [2,3,4]
|
tail = [2,3,4]
|
||||||
|
|
||||||
assert [1,2,3,4] == [head | tail]
|
assert :__ == [head | tail]
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "Some values can be irrelevant" do
|
koan "Some values can be irrelevant" do
|
||||||
[_first, _second, third, _fourth] = [1,2,3,4]
|
[_first, _second, third, _fourth] = [1,2,3,4]
|
||||||
|
|
||||||
assert third == 3
|
assert third == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "strings come apart just a easily" do
|
koan "strings come apart just a easily" do
|
||||||
"Shopping list: " <> items = "Shopping list: eggs, milk"
|
"Shopping list: " <> items = "Shopping list: eggs, milk"
|
||||||
|
|
||||||
assert items == "eggs, milk"
|
assert items == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "patterns show what you really care about" do
|
koan "patterns show what you really care about" do
|
||||||
%{make: make} = %{type: "car", year: 2016, make: "Honda", color: "black"}
|
%{make: make} = %{type: "car", year: 2016, make: "Honda", color: "black"}
|
||||||
|
|
||||||
assert make == "Honda"
|
assert make == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "the pattern can make assertions about what it expects" do
|
koan "the pattern can make assertions about what it expects" do
|
||||||
the_list = [1, 2, 3]
|
assert match?([1, _, _], :__)
|
||||||
|
|
||||||
assert match?([1, _, _], the_list)
|
|
||||||
refute match?([2, _, _], the_list)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_noise(%{type: "cat"}), do: "Meow"
|
def make_noise(%{type: "cat"}), do: "Meow"
|
||||||
@@ -60,20 +64,26 @@ defmodule PatternMatching do
|
|||||||
|
|
||||||
koan "functions can declare what kind of arguments they accept" do
|
koan "functions can declare what kind of arguments they accept" do
|
||||||
dog = %{type: "dog", legs: 4, age: 9, color: "brown"}
|
dog = %{type: "dog", legs: 4, age: 9, color: "brown"}
|
||||||
cat = %{type: "cat", legs: 4, age: 3, color: "grey"}
|
|
||||||
snake = %{type: "snake", legs: 0, age: 20, color: "black"}
|
|
||||||
|
|
||||||
assert make_noise(dog) == "Woof"
|
assert make_noise(dog) == :__
|
||||||
assert make_noise(cat) == "Meow"
|
end
|
||||||
assert make_noise(snake) == "Eh?"
|
|
||||||
|
koan "...and for cats..." do
|
||||||
|
cat = %{type: "cat", legs: 4, age: 3, color: "grey"}
|
||||||
|
assert make_noise(cat) == :__
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "...and for snakes..." do
|
||||||
|
snake = %{type: "snake", legs: 0, age: 20, color: "black"}
|
||||||
|
assert make_noise(snake) == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "errors are shaped differently than sucessful results" do
|
koan "errors are shaped differently than sucessful results" do
|
||||||
result = case Map.fetch(%{}, :obviously_not_a_key) do
|
result = case Map.fetch(%{}, :obviously_not_a_key) do
|
||||||
{:ok, val} -> val
|
|
||||||
:error -> "not present"
|
:error -> "not present"
|
||||||
|
_ -> flunk("I should not happen")
|
||||||
end
|
end
|
||||||
|
|
||||||
assert result == "not present"
|
assert result == :__
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -163,6 +163,27 @@ defmodule KoansHarnessTest do
|
|||||||
test_all(Structs, answers)
|
test_all(Structs, answers)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "Pattern Matching" do
|
||||||
|
answers = [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
[2,3,4],
|
||||||
|
[1,2,3,4],
|
||||||
|
3,
|
||||||
|
"eggs, milk",
|
||||||
|
"Honda",
|
||||||
|
[1,2,3],
|
||||||
|
"Woof",
|
||||||
|
"Meow",
|
||||||
|
"Eh?",
|
||||||
|
"not present"
|
||||||
|
]
|
||||||
|
|
||||||
|
test_all(PatternMatching, 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