Merge pull request #73 from ukutaht/feedback-from-uku

Make sure blank is on the right and improve koans
This commit is contained in:
Uku Taht
2016-04-23 12:13:08 +01:00
11 changed files with 46 additions and 55 deletions

View File

@@ -10,11 +10,11 @@ defmodule Strings do
end end
koan "Or pulled apart when needed" do koan "Or pulled apart when needed" do
assert ___ == String.split("hello world") assert ["hello", "world"] == String.split(___, " ")
end end
koan "Be careful, a message may be altered" do 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 end
koan "But strings never lie about themselves" do koan "But strings never lie about themselves" do
@@ -26,7 +26,7 @@ defmodule Strings do
end end
koan "Other times a little cleaning is in order" do koan "Other times a little cleaning is in order" do
assert ___ == String.strip(" \n banana\n ") assert String.strip(" \n banana\n ") == ___
end end
koan "Repetition is the mother of learning" do koan "Repetition is the mother of learning" do

View File

@@ -1,14 +1,14 @@
defmodule Tuples do defmodule Tuples do
use Koans use Koans
koan "Tuples have a size" do
assert tuple_size({:a, :b, :c}) == ___
end
koan "Tuples can contain different things" do koan "Tuples can contain different things" do
assert {:a, 1, "hi"} == ___ assert {:a, 1, "hi"} == ___
end end
koan "Tuples have a size" do
assert tuple_size({:a, :b, :c}) == ___
end
koan "You can pull out individual elements" do koan "You can pull out individual elements" do
assert elem({:a, "hi"}, 1) == ___ assert elem({:a, "hi"}, 1) == ___
end end

View File

@@ -33,10 +33,6 @@ defmodule Lists do
assert List.flatten([1, [2, 3], 4, [5]]) == ___ assert List.flatten([1, [2, 3], 4, [5]]) == ___
end 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 koan "Order can also be specified for new members" do
assert List.insert_at([1, 2, 3], 1, 4) == ___ assert List.insert_at([1, 2, 3], 1, 4) == ___
end end
@@ -45,7 +41,7 @@ defmodule Lists do
assert List.replace_at([1, 2, 3], 0, 10) == ___ assert List.replace_at([1, 2, 3], 0, 10) == ___
end 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) == ___ assert List.replace_at([1, 2, 3], 10, 0) == ___
end end
@@ -62,10 +58,6 @@ defmodule Lists do
end end
koan "Wrapping other values is a handy option" do koan "Wrapping other values is a handy option" do
assert List.wrap("value") == :___ assert List.wrap("value") == ___
end
koan "Zipping can be a useful operation" do
assert List.zip([[1, 2], [3, 4], [5, 6]]) == :___
end end
end end

View File

@@ -10,7 +10,7 @@ defmodule Structs do
assert person == ___ assert person == ___
end end
koan "You can access the fields of a struct" do koan "Unless previously defined, fields begin as nil" do
nobody = %Person{} nobody = %Person{}
assert nobody.age == ___ assert nobody.age == ___
end end
@@ -26,17 +26,6 @@ defmodule Structs do
assert older.age == ___ assert older.age == ___
end 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 defmodule Plane do
defstruct passengers: 0, maker: :boeing defstruct passengers: 0, maker: :boeing
end end
@@ -49,7 +38,7 @@ defmodule Structs do
assert plane?(%Person{}) == ___ assert plane?(%Person{}) == ___
end end
koan "Are basically maps" do koan "Struct can be treated like maps" do
silvia = %Person{age: 22, name: "Silvia"} silvia = %Person{age: 22, name: "Silvia"}
assert Map.fetch!(silvia, :age) == ___ assert Map.fetch!(silvia, :age) == ___

View File

@@ -56,21 +56,32 @@ defmodule PatternMatching do
def make_noise(_anything), do: "Eh?" def make_noise(_anything), do: "Eh?"
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"}
cat = %{type: "cat", legs: 4, age: 3, color: "grey"} cat = %{type: "cat"}
snake = %{type: "snake", legs: 0, age: 20, color: "black"} snake = %{type: "snake"}
assert make_noise(dog) == ___
assert make_noise(cat) == ___ assert make_noise(cat) == ___
assert make_noise(dog) == ___
assert make_noise(snake) == ___ 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 dog = %{type: "dog"}
result = case Map.fetch(dog, :type) do
{:ok, value} -> value
:error -> "not present" :error -> "not present"
_ -> flunk("I should not happen")
end end
assert result == ___ assert result == ___
end 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 end

View File

@@ -11,7 +11,7 @@ defmodule Functions do
def multiply(a, b), do: a * b def multiply(a, b), do: a * b
koan "Single line functions are cool, but mind the comma and the colon!" do koan "Single line functions are cool, but mind the comma and the colon!" do
assert multiply(2, ___) == 6 assert 6 == multiply(2, ___)
end end
def first(foo, bar), do: "#{foo} and #{bar}" def first(foo, bar), do: "#{foo} and #{bar}"

View File

@@ -6,7 +6,7 @@ defmodule Enums do
end end
koan "Depending on the type, it counts pairs" do koan "Depending on the type, it counts pairs" do
assert Enum.count(%{ :a => :foo, :b => :bar}) == ___ assert Enum.count(%{ a: :foo, b: :bar}) == ___
end end
def less_than_five?(n), do: n < 5 def less_than_five?(n), do: n < 5

View File

@@ -30,9 +30,8 @@ defmodule Processes do
koan "Waiting for a message can get boring" do koan "Waiting for a message can get boring" do
parent = self() parent = self()
spawn(fn -> receive do spawn(fn -> receive do
_anything -> flunk "I really wasn't expecting messages"
after after
10 -> send parent, {:waited_too_long, "I am impatient"} 5 -> send parent, {:waited_too_long, "I am impatient"}
end end
end) end)

View File

@@ -25,7 +25,9 @@ defmodule Tasks do
:timer.sleep(100) :timer.sleep(100)
3 * 3 3 * 3
end) end)
assert Task.shutdown(handle) == ___ %Task{pid: pid} = handle
Task.shutdown(handle)
assert Process.alive?(pid) == ___
end end
koan "Shutdown will give you an answer if it has it" do koan "Shutdown will give you an answer if it has it" do

View File

@@ -27,10 +27,10 @@ defmodule Agents do
end end
koan "Somebody has to switch off the light at the end of the day" do 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
end end

View File

@@ -19,7 +19,7 @@ defmodule KoansHarnessTest do
answers = [ answers = [
"hello", "hello",
"hello ", "hello ",
["hello", "world"], "hello world",
"An incredible day", "An incredible day",
"incredible", "incredible",
"banana", "banana",
@@ -33,8 +33,8 @@ defmodule KoansHarnessTest do
test "Tuples" do test "Tuples" do
answers = [ answers = [
3,
{:a, 1, "hi"}, {:a, 1, "hi"},
3,
"hi", "hi",
{:a, "bye"}, {:a, "bye"},
{:a, :new_thing, "hi"}, {:a, :new_thing, "hi"},
@@ -47,7 +47,8 @@ defmodule KoansHarnessTest do
end end
test "Lists" do test "Lists" do
answers = [1, answers = [
1,
3, 3,
[1, 2, :a, "b"], [1, 2, :a, "b"],
[1,2], [1,2],
@@ -55,7 +56,6 @@ defmodule KoansHarnessTest do
[1,3], [1,3],
["life", "life", "life"], ["life", "life", "life"],
[1, 2, 3, 4, 5], [1, 2, 3, 4, 5],
[1, 2, 3, 4],
[1, 4, 2, 3], [1, 4, 2, 3],
[10, 2, 3], [10, 2, 3],
[1, 2, 3], [1, 2, 3],
@@ -63,7 +63,6 @@ defmodule KoansHarnessTest do
[1, 2, 3, 4], [1, 2, 3, 4],
{1, 2, 3}, {1, 2, 3},
["value"], ["value"],
[{1, 3, 5}, {2, 4, 6}]
] ]
test_all(Lists, answers) test_all(Lists, answers)
@@ -93,8 +92,6 @@ defmodule KoansHarnessTest do
nil, nil,
"Joe", "Joe",
33, 33,
{:multiple, [33, 23]},
22,
{:multiple, [true, false]}, {:multiple, [true, false]},
22, 22,
] ]
@@ -113,8 +110,9 @@ defmodule KoansHarnessTest do
"eggs, milk", "eggs, milk",
"Honda", "Honda",
[1,2,3], [1,2,3],
{:multiple, ["Woof", "Meow", "Eh?",]}, {:multiple, ["Meow", "Woof", "Eh?",]},
"not present" "dog",
"Max"
] ]
test_all(PatternMatching, answers) test_all(PatternMatching, answers)
@@ -185,7 +183,7 @@ defmodule KoansHarnessTest do
10, 10,
:ok, :ok,
nil, nil,
nil, false,
9, 9,
[1,4,9,16] [1,4,9,16]
] ]
@@ -198,7 +196,7 @@ defmodule KoansHarnessTest do
"Hi there", "Hi there",
"HI THERE", "HI THERE",
{:multiple, [["Milk"], ["Bread", "Milk"]]}, {:multiple, [["Milk"], ["Bread", "Milk"]]},
:ok, false
] ]
test_all(Agents, answers) test_all(Agents, answers)