diff --git a/lib/koans/07_pattern_matching.ex b/lib/koans/07_pattern_matching.ex index 1c46e4d..387635f 100644 --- a/lib/koans/07_pattern_matching.ex +++ b/lib/koans/07_pattern_matching.ex @@ -56,8 +56,8 @@ defmodule PatternMatching do def make_noise(_anything), do: "Eh?" koan "Functions can declare what kind of arguments they accept" do - dog = %{type: "dog"} cat = %{type: "cat"} + dog = %{type: "dog"} snake = %{type: "snake"} assert make_noise(cat) == ___ diff --git a/test/koans/enum_koans_test.exs b/test/koans/enum_koans_test.exs new file mode 100644 index 0000000..9ed3a63 --- /dev/null +++ b/test/koans/enum_koans_test.exs @@ -0,0 +1,27 @@ +defmodule EnumTests do + use ExUnit.Case + use TestHarness + + test "Enums" do + answers = [ + 3, + 2, + {:multiple, [true, false]}, + {:multiple, [true, false]}, + {:multiple, [true, false]}, + [10,20,30], + [1,3], + [2], + [1,2,3], + [1,2,3,4,5], + [1,2,3], + [{1, :a}, {2, :b}, {3, :c}], + 2, + nil, + :no_such_element, + 6 + ] + + test_all(Enums, answers) + end +end diff --git a/test/koans/equalities_koan_test.exs b/test/koans/equalities_koan_test.exs new file mode 100644 index 0000000..7b75959 --- /dev/null +++ b/test/koans/equalities_koan_test.exs @@ -0,0 +1,18 @@ +defmodule EqualitiesTests do + use ExUnit.Case + use TestHarness + + test "Equalities" do + answers = [ + true, + false, + 1, + 2, + 1, + 4, + 2 + ] + + test_all(Equalities, answers) + end +end diff --git a/test/koans/functions_koans_test.exs b/test/koans/functions_koans_test.exs new file mode 100644 index 0000000..7b6f1b6 --- /dev/null +++ b/test/koans/functions_koans_test.exs @@ -0,0 +1,22 @@ +defmodule FunctionsTests do + use ExUnit.Case + use TestHarness + + test "Functions" do + answers = [ + "Hello, World!", + 3, + {:multiple, ["One and Two", "Only One"]}, + {:multiple, ["Hello Hello Hello Hello Hello ","Hello Hello "]}, + {:multiple, [:entire_list, :single_thing]}, + {:multiple, ["10 is bigger than 5", "4 is not bigger than 27"]}, + {:multiple, ["It was zero", "The length was 5"]}, + 6, + 6, + 100, + "Full Name" + ] + + test_all(Functions, answers) + end +end diff --git a/test/koans/lists_koans_test.exs b/test/koans/lists_koans_test.exs new file mode 100644 index 0000000..c857026 --- /dev/null +++ b/test/koans/lists_koans_test.exs @@ -0,0 +1,25 @@ +defmodule ListsTests do + use ExUnit.Case + use TestHarness + + test "Lists" do + answers = [1, + 3, + [1, 2, :a, "b"], + [1,2], + [1,2,3], + [1,3], + ["life", "life", "life"], + [1, 2, 3, 4, 5], + [1, 4, 2, 3], + [10, 2, 3], + [1, 2, 3], + [1, 2, 3, 4], + [1, 2, 3, 4], + {1, 2, 3}, + ["value"], + ] + + test_all(Lists, answers) + end +end diff --git a/test/koans/maps_koans_test.exs b/test/koans/maps_koans_test.exs new file mode 100644 index 0000000..24c49a9 --- /dev/null +++ b/test/koans/maps_koans_test.exs @@ -0,0 +1,22 @@ +defmodule MapsTests do + use ExUnit.Case + use TestHarness + + test "Maps" do + answers = [ + "Jon", + {:multiple, [[:age, :last_name, :name], [27, "Jon", "Snow"]]}, + {:ok, 27}, + :error, + {:ok, "Kayaking"}, + {:ok, 37}, + {:ok, 16}, + [:last_name, :name], + %{:name => "Jon", :last_name => "Snow"}, + {:ok, "Baratheon"}, + %{ :name => "Jon", :last_name => "Snow"} + ] + + test_all(Maps, answers) + end +end diff --git a/test/koans/patterns_koans_test.exs b/test/koans/patterns_koans_test.exs new file mode 100644 index 0000000..b1c2e9a --- /dev/null +++ b/test/koans/patterns_koans_test.exs @@ -0,0 +1,23 @@ +defmodule PatternsTests do + use ExUnit.Case + use TestHarness + + test "Pattern Matching" do + answers = [ + 1, + 2, + 1, + {:multiple, [1, [2,3,4]]}, + [1,2,3,4], + 3, + "eggs, milk", + "Honda", + [1,2,3], + {:multiple, ["Meow", "Woof", "Eh?",]}, + "dog", + "Max" + ] + + test_all(PatternMatching, answers) + end +end diff --git a/test/koans/processes_koans_test.exs b/test/koans/processes_koans_test.exs new file mode 100644 index 0000000..45be268 --- /dev/null +++ b/test/koans/processes_koans_test.exs @@ -0,0 +1,23 @@ +defmodule ProcessesTests do + use ExUnit.Case + use TestHarness + + test "Processes" do + answers = [ + true, + :running, + "hola!", + :how_are_you?, + {:waited_too_long, "I am inpatient"}, + false, + {:multiple, [true, false]}, + {:exited, :random_reason}, + true, + false, + {:exited, :normal}, + {:exited, :normal} + ] + + test_all(Processes, answers) + end +end diff --git a/test/koans/strings_koan_test.exs b/test/koans/strings_koan_test.exs new file mode 100644 index 0000000..ea180b0 --- /dev/null +++ b/test/koans/strings_koan_test.exs @@ -0,0 +1,20 @@ +defmodule StringTests do + use ExUnit.Case + use TestHarness + + test "Strings" do + answers = [ + "hello", + "hello ", + "hello world", + "An incredible day", + "incredible", + "banana", + "banana", + "String", + "listen" + ] + + test_all(Strings, answers) + end +end diff --git a/test/koans/structs_koans_test.exs b/test/koans/structs_koans_test.exs new file mode 100644 index 0000000..a02a4a0 --- /dev/null +++ b/test/koans/structs_koans_test.exs @@ -0,0 +1,17 @@ +defmodule StructsTests do + use ExUnit.Case + use TestHarness + + test "Structs" do + answers = [ + %Structs.Person{}, + nil, + "Joe", + 33, + {:multiple, [true, false]}, + 22, + ] + + test_all(Structs, answers) + end +end diff --git a/test/koans/tasks_koans_test.exs b/test/koans/tasks_koans_test.exs new file mode 100644 index 0000000..d91d480 --- /dev/null +++ b/test/koans/tasks_koans_test.exs @@ -0,0 +1,17 @@ +defmodule TasksTests do + use ExUnit.Case + use TestHarness + + test "Tasks" do + answers = [ + 10, + :ok, + nil, + false, + 9, + [1,4,9,16] + ] + + test_all(Tasks, answers) + end +end diff --git a/test/koans/tuples_koans_test.exs b/test/koans/tuples_koans_test.exs new file mode 100644 index 0000000..126b772 --- /dev/null +++ b/test/koans/tuples_koans_test.exs @@ -0,0 +1,19 @@ +defmodule TupleTests do + use ExUnit.Case + use TestHarness + + test "Tuples" do + answers = [ + {:a, 1, "hi"}, + 3, + "hi", + {:a, "bye"}, + {:a, :new_thing, "hi"}, + {"Huey", "Dewey", "Louie"}, + {:this, :is, :awesome}, + [:this, :can, :be, :a, :list] + ] + + test_all(Tuples, answers) + end +end diff --git a/test/koans_harness_test.exs b/test/koans_harness_test.exs deleted file mode 100644 index 3021674..0000000 --- a/test/koans_harness_test.exs +++ /dev/null @@ -1,214 +0,0 @@ -defmodule KoansHarnessTest do - use ExUnit.Case - - test "Equalities" do - answers = [ - true, - false, - 1, - 2, - 1, - 4, - 2 - ] - - test_all(Equalities, answers) - end - - test "Strings" do - answers = [ - "hello", - "hello ", - "hello world", - "An incredible day", - "incredible", - "banana", - "banana", - "String", - "listen" - ] - - test_all(Strings, answers) - end - - test "Tuples" do - answers = [ - {:a, 1, "hi"}, - 3, - "hi", - {:a, "bye"}, - {:a, :new_thing, "hi"}, - {"Huey", "Dewey", "Louie"}, - {:this, :is, :awesome}, - [:this, :can, :be, :a, :list] - ] - - test_all(Tuples, answers) - end - - test "Lists" do - answers = [ - 1, - 3, - [1, 2, :a, "b"], - [1,2], - [1,2,3], - [1,3], - ["life", "life", "life"], - [1, 2, 3, 4, 5], - [1, 4, 2, 3], - [10, 2, 3], - [1, 2, 3], - [1, 2, 3, 4], - [1, 2, 3, 4], - {1, 2, 3}, - ["value"], - ] - - test_all(Lists, answers) - end - - test "Maps" do - answers = [ - "Jon", - {:multiple, [[:age, :last_name, :name], [27, "Jon", "Snow"]]}, - {:ok, 27}, - :error, - {:ok, "Kayaking"}, - {:ok, 37}, - {:ok, 16}, - [:last_name, :name], - %{:name => "Jon", :last_name => "Snow"}, - {:ok, "Baratheon"}, - %{ :name => "Jon", :last_name => "Snow"} - ] - - test_all(Maps, answers) - end - - test "Structs" do - answers = [ - %Structs.Person{}, - nil, - "Joe", - 33, - {:multiple, [true, false]}, - 22, - ] - - test_all(Structs, answers) - end - - test "Pattern Matching" do - answers = [ - 1, - 2, - 1, - {:multiple, [1, [2,3,4]]}, - [1,2,3,4], - 3, - "eggs, milk", - "Honda", - [1,2,3], - {:multiple, ["Meow", "Woof", "Eh?",]}, - "dog", - "Max" - ] - - test_all(PatternMatching, answers) - end - - test "Functions" do - answers = [ - "Hello, World!", - 3, - {:multiple, ["One and Two", "Only One"]}, - {:multiple, ["Hello Hello Hello Hello Hello ","Hello Hello "]}, - {:multiple, [:entire_list, :single_thing]}, - {:multiple, ["10 is bigger than 5", "4 is not bigger than 27"]}, - {:multiple, ["It was zero", "The length was 5"]}, - 6, - 6, - 100, - "Full Name" - ] - - test_all(Functions, answers) - end - - test "Enums" do - answers = [ - 3, - 2, - {:multiple, [true, false]}, - {:multiple, [true, false]}, - {:multiple, [true, false]}, - [10,20,30], - [1,3], - [2], - [1,2,3], - [1,2,3,4,5], - [1,2,3], - [{1, :a}, {2, :b}, {3, :c}], - 2, - nil, - :no_such_element, - 6 - ] - - test_all(Enums, answers) - end - - test "Processes" do - answers = [ - true, - :running, - "hola!", - :how_are_you?, - {:waited_too_long, "I am inpatient"}, - false, - {:multiple, [true, false]}, - {:exited, :random_reason}, - true, - false, - {:exited, :normal}, - {:exited, :normal} - ] - - test_all(Processes, answers) - end - - test "Tasks" do - answers = [ - 10, - :ok, - nil, - false, - 9, - [1,4,9,16] - ] - - test_all(Tasks, answers) - end - - test "Agents" do - answers = [ - "Hi there", - "HI THERE", - {:multiple, [["Milk"], ["Bread", "Milk"]]}, - false - ] - - test_all(Agents, answers) - end - - def test_all(module, answers) do - module.all_koans - |> Enum.zip(answers) - |> run_all(module) - end - - def run_all(pairs, module) do - Enum.map(pairs, fn ({koan, answer}) -> Execute.run_koan(module, koan, [answer]) end) - end -end diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..66704ff 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1,17 @@ ExUnit.start() + +defmodule TestHarness do + defmacro __using__(_) do + quote do + def test_all(module, answers) do + module.all_koans + |> Enum.zip(answers) + |> run_all(module) + end + + def run_all(pairs, module) do + Enum.map(pairs, fn ({koan, answer}) -> Execute.run_koan(module, koan, [answer]) end) + end + end + end +end