diff --git a/lib/koans/02_strings.ex b/lib/koans/02_strings.ex index 8f9320d..6a82fc9 100644 --- a/lib/koans/02_strings.ex +++ b/lib/koans/02_strings.ex @@ -10,11 +10,11 @@ defmodule Strings do end koan "Or pulled apart when needed" do - assert ___ == String.split("hello world") + assert ["hello", "world"] == String.split(___, " ") end koan "Be careful, a message may be altered" do - assert ___ == String.replace("An awful day", "awful", "incredible") + assert String.replace("An awful day", "awful", "incredible") == ___ end koan "But strings never lie about themselves" do @@ -26,7 +26,7 @@ defmodule Strings do end koan "Other times a little cleaning is in order" do - assert ___ == String.strip(" \n banana\n ") + assert String.strip(" \n banana\n ") == ___ end koan "Repetition is the mother of learning" do diff --git a/lib/koans/03_tuples.ex b/lib/koans/03_tuples.ex index 13dc439..c756892 100644 --- a/lib/koans/03_tuples.ex +++ b/lib/koans/03_tuples.ex @@ -1,14 +1,14 @@ defmodule Tuples do use Koans - koan "Tuples have a size" do - assert tuple_size({:a, :b, :c}) == ___ - end - koan "Tuples can contain different things" do assert {:a, 1, "hi"} == ___ end + koan "Tuples have a size" do + assert tuple_size({:a, :b, :c}) == ___ + end + koan "You can pull out individual elements" do assert elem({:a, "hi"}, 1) == ___ end diff --git a/lib/koans/04_lists.ex b/lib/koans/04_lists.ex index 669264c..df61e50 100644 --- a/lib/koans/04_lists.ex +++ b/lib/koans/04_lists.ex @@ -33,10 +33,6 @@ defmodule Lists do assert List.flatten([1, [2, 3], 4, [5]]) == ___ end - koan "Same rules apply to new members that arrive late" do - assert List.flatten([1, [2, 3]], [4]) == ___ - end - koan "Order can also be specified for new members" do assert List.insert_at([1, 2, 3], 1, 4) == ___ end @@ -45,7 +41,7 @@ defmodule Lists do assert List.replace_at([1, 2, 3], 0, 10) == ___ end - koan "Replacing something which is not" do + koan "When a replacement cannot be found, the list remains the same" do assert List.replace_at([1, 2, 3], 10, 0) == ___ end @@ -62,10 +58,6 @@ defmodule Lists do end koan "Wrapping other values is a handy option" do - assert List.wrap("value") == :___ - end - - koan "Zipping can be a useful operation" do - assert List.zip([[1, 2], [3, 4], [5, 6]]) == :___ + assert List.wrap("value") == ___ end end diff --git a/lib/koans/06_structs.ex b/lib/koans/06_structs.ex index 27d719d..a7442f6 100644 --- a/lib/koans/06_structs.ex +++ b/lib/koans/06_structs.ex @@ -10,7 +10,7 @@ defmodule Structs do assert person == ___ end - koan "You can access the fields of a struct" do + koan "Unless previously defined, fields begin as nil" do nobody = %Person{} assert nobody.age == ___ end @@ -26,17 +26,6 @@ defmodule Structs do assert older.age == ___ end - koan "The original struct is not affected by updates" do - joe = %Person{name: "Joe", age: 23} - assert %{ joe | age: joe.age + 10}.age == ___ - assert joe.age == ___ - end - - koan "You can pattern match into the fields of a struct" do - %Person{age: age} = %Person{age: 22, name: "Silvia"} - assert age == ___ - end - defmodule Plane do defstruct passengers: 0, maker: :boeing end @@ -49,7 +38,7 @@ defmodule Structs do assert plane?(%Person{}) == ___ end - koan "Are basically maps" do + koan "Struct can be treated like maps" do silvia = %Person{age: 22, name: "Silvia"} assert Map.fetch!(silvia, :age) == ___ diff --git a/lib/koans/07_pattern_matching.ex b/lib/koans/07_pattern_matching.ex index b36cde8..1c46e4d 100644 --- a/lib/koans/07_pattern_matching.ex +++ b/lib/koans/07_pattern_matching.ex @@ -56,21 +56,32 @@ defmodule PatternMatching do def make_noise(_anything), do: "Eh?" koan "Functions can declare what kind of arguments they accept" do - 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"} + dog = %{type: "dog"} + cat = %{type: "cat"} + snake = %{type: "snake"} - assert make_noise(dog) == ___ assert make_noise(cat) == ___ + assert make_noise(dog) == ___ assert make_noise(snake) == ___ end koan "Errors are shaped differently than sucessful results" do - result = case Map.fetch(%{}, :obviously_not_a_key) do + dog = %{type: "dog"} + + result = case Map.fetch(dog, :type) do + {:ok, value} -> value :error -> "not present" - _ -> flunk("I should not happen") end assert result == ___ end + + defmodule Animal do + defstruct [:kind, :name] + end + + koan "You can pattern match into the fields of a struct" do + %Animal{name: name} = %Animal{kind: "dog", name: "Max"} + assert name == ___ + end end diff --git a/lib/koans/08_functions.ex b/lib/koans/08_functions.ex index 9daa766..8631ba4 100644 --- a/lib/koans/08_functions.ex +++ b/lib/koans/08_functions.ex @@ -11,7 +11,7 @@ defmodule Functions do def multiply(a, b), do: a * b koan "Single line functions are cool, but mind the comma and the colon!" do - assert multiply(2, ___) == 6 + assert 6 == multiply(2, ___) end def first(foo, bar), do: "#{foo} and #{bar}" diff --git a/lib/koans/09_enums.ex b/lib/koans/09_enums.ex index eb77eb8..565ac3e 100644 --- a/lib/koans/09_enums.ex +++ b/lib/koans/09_enums.ex @@ -6,7 +6,7 @@ defmodule Enums do end koan "Depending on the type, it counts pairs" do - assert Enum.count(%{ :a => :foo, :b => :bar}) == ___ + assert Enum.count(%{ a: :foo, b: :bar}) == ___ end def less_than_five?(n), do: n < 5 diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index b4fd0ae..d7e8728 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -30,9 +30,8 @@ defmodule Processes do koan "Waiting for a message can get boring" do parent = self() spawn(fn -> receive do - _anything -> flunk "I really wasn't expecting messages" after - 10 -> send parent, {:waited_too_long, "I am impatient"} + 5 -> send parent, {:waited_too_long, "I am impatient"} end end) diff --git a/lib/koans/11_tasks.ex b/lib/koans/11_tasks.ex index 2f59a42..d28744c 100644 --- a/lib/koans/11_tasks.ex +++ b/lib/koans/11_tasks.ex @@ -25,7 +25,9 @@ defmodule Tasks do :timer.sleep(100) 3 * 3 end) - assert Task.shutdown(handle) == ___ + %Task{pid: pid} = handle + Task.shutdown(handle) + assert Process.alive?(pid) == ___ end koan "Shutdown will give you an answer if it has it" do diff --git a/lib/koans/12_agents.ex b/lib/koans/12_agents.ex index 2d9b96c..3abe8a2 100644 --- a/lib/koans/12_agents.ex +++ b/lib/koans/12_agents.ex @@ -27,10 +27,10 @@ defmodule Agents do end koan "Somebody has to switch off the light at the end of the day" do - Agent.start_link(fn() -> ["Milk"] end, name: __MODULE__) + {:ok, pid} = Agent.start_link(fn() -> ["Milk"] end, name: __MODULE__) - result = Agent.stop(__MODULE__) + Agent.stop(__MODULE__) - assert result == ___ + assert Process.alive?(pid) == ___ end end diff --git a/test/koans_harness_test.exs b/test/koans_harness_test.exs index fd8c8cd..3021674 100644 --- a/test/koans_harness_test.exs +++ b/test/koans_harness_test.exs @@ -19,7 +19,7 @@ defmodule KoansHarnessTest do answers = [ "hello", "hello ", - ["hello", "world"], + "hello world", "An incredible day", "incredible", "banana", @@ -33,8 +33,8 @@ defmodule KoansHarnessTest do test "Tuples" do answers = [ - 3, {:a, 1, "hi"}, + 3, "hi", {:a, "bye"}, {:a, :new_thing, "hi"}, @@ -47,7 +47,8 @@ defmodule KoansHarnessTest do end test "Lists" do - answers = [1, + answers = [ + 1, 3, [1, 2, :a, "b"], [1,2], @@ -55,7 +56,6 @@ defmodule KoansHarnessTest do [1,3], ["life", "life", "life"], [1, 2, 3, 4, 5], - [1, 2, 3, 4], [1, 4, 2, 3], [10, 2, 3], [1, 2, 3], @@ -63,7 +63,6 @@ defmodule KoansHarnessTest do [1, 2, 3, 4], {1, 2, 3}, ["value"], - [{1, 3, 5}, {2, 4, 6}] ] test_all(Lists, answers) @@ -93,8 +92,6 @@ defmodule KoansHarnessTest do nil, "Joe", 33, - {:multiple, [33, 23]}, - 22, {:multiple, [true, false]}, 22, ] @@ -113,8 +110,9 @@ defmodule KoansHarnessTest do "eggs, milk", "Honda", [1,2,3], - {:multiple, ["Woof", "Meow", "Eh?",]}, - "not present" + {:multiple, ["Meow", "Woof", "Eh?",]}, + "dog", + "Max" ] test_all(PatternMatching, answers) @@ -185,7 +183,7 @@ defmodule KoansHarnessTest do 10, :ok, nil, - nil, + false, 9, [1,4,9,16] ] @@ -198,7 +196,7 @@ defmodule KoansHarnessTest do "Hi there", "HI THERE", {:multiple, [["Milk"], ["Bread", "Milk"]]}, - :ok, + false ] test_all(Agents, answers)