Merge pull request #106 from iamvery/keyword-list-koan

Keyword list koan
This commit is contained in:
Felipe Seré
2016-05-10 18:40:16 +01:00
11 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
defmodule KeywordLists do
use Koans
koan "Like maps, keyword lists are key-value pairs" do
kw_list = [foo: "bar"]
assert kw_list[:foo] == ___
end
koan "Keys may be repeated, but only the first is accessed" do
kw_list = [foo: "bar", foo: "baz"]
assert kw_list[:foo] == ___
end
koan "You could access a second key by removing the first" do
kw_list = [foo: "bar", foo: "baz"]
[_|kw_list] = kw_list
assert kw_list[:foo] == ___
end
koan "Keyword lists just special syntax for lists of two-element tuples" do
assert [foo: "bar"] == [{___, ___}]
end
koan "But unlike maps, the keys in keyword lists must be atoms" do
not_kw_list = [{"foo", "bar"}]
assert_raise ArgumentError, fn -> not_kw_list[___] end
end
koan "Conveniently keyword lists can be used for function options" do
transform = fn str, opts ->
if opts[:upcase], do: String.upcase(str)
end
assert transform.("good", upcase: true) == ___
end
end

View File

@@ -5,6 +5,7 @@ defmodule Runner do
Atoms,
Tuples,
Lists,
KeywordLists,
Maps,
Structs,
PatternMatching,

View File

@@ -0,0 +1,17 @@
defmodule KeywordListsTests do
use ExUnit.Case
import TestHarness
test "KeywordLists" do
answers = [
"bar",
"bar",
"baz",
{:multiple, [:foo, "bar"]},
"foo",
"GOOD",
]
test_all(KeywordLists, answers)
end
end