From ddba726e38a5ce3be60e2ac3488137d4341cf7d7 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 17 Mar 2016 11:24:27 +0000 Subject: [PATCH] Adds koans about tuples --- lib/blank_assertions.ex | 4 ++++ lib/koans/12_tuples.ex | 43 +++++++++++++++++++++++++++++++++++++ test/koans_harness_test.exs | 17 +++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/koans/12_tuples.ex diff --git a/lib/blank_assertions.ex b/lib/blank_assertions.ex index 7458040..acace05 100644 --- a/lib/blank_assertions.ex +++ b/lib/blank_assertions.ex @@ -39,6 +39,10 @@ defmodule BlankAssertions do end end + def assert_raise(exception, callback) do + ExUnit.Assertions.assert_raise exception, fn -> callback.call() end + end + def assert(value, opts) do ExUnit.Assertions.assert(value, opts) end diff --git a/lib/koans/12_tuples.ex b/lib/koans/12_tuples.ex new file mode 100644 index 0000000..cbe154f --- /dev/null +++ b/lib/koans/12_tuples.ex @@ -0,0 +1,43 @@ +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 "you can pull out individual elements" do + assert elem({:a, "hi"}, 1) == :__ + end + + koan "you can change individual elements of a tuple" do + assert put_elem({:a, "hi"}, 1, "bye") == :__ + end + + koan "you can also simply extend a tuple with new stuff" do + assert Tuple.insert_at({:a, "hi"}, 1, :new_thing) == :__ + end + + koan "...avoid falling of the edge" do + assert_raise :__, fn -> Tuple.insert_at({:a, "hi"}, 12, :new_thing) end + end + + koan "add things at the end" do + assert Tuple.append({"Huey", "Dewey"}, "Louie") == :__ + end + + koan "or also remove them" do + assert Tuple.delete_at({:this, :is, :not, :awesome}, 2) == :__ + end + + koan "you can't delete what you don't have" do + assert_raise :__, fn -> Tuple.delete_at({:a, "hi"}, 12) end + end + + koan "turn it into a list in case you need it" do + assert Tuple.to_list({:this, :can, :be, :a, :list}) == :__ + end +end diff --git a/test/koans_harness_test.exs b/test/koans_harness_test.exs index 0865c8f..015e960 100644 --- a/test/koans_harness_test.exs +++ b/test/koans_harness_test.exs @@ -217,6 +217,23 @@ defmodule KoansHarnessTest do test_all(Tasks, answers) end + test "Tuples" do + answers = [ + 3, + {:a, 1, "hi"}, + "hi", + {:a, "bye"}, + {:a, :new_thing, "hi"}, + ArgumentError, + {"Huey", "Dewey", "Louie"}, + {:this, :is, :awesome}, + ArgumentError, + [:this, :can, :be, :a, :list] + ] + + test_all(Tuples, answers) + end + def test_all(module, answers) do module.all_koans |> Enum.zip(answers)