Merge pull request #106 from iamvery/keyword-list-koan
Keyword list koan
This commit is contained in:
40
lib/koans/06_keyword_lists.ex
Normal file
40
lib/koans/06_keyword_lists.ex
Normal 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
|
||||
@@ -5,6 +5,7 @@ defmodule Runner do
|
||||
Atoms,
|
||||
Tuples,
|
||||
Lists,
|
||||
KeywordLists,
|
||||
Maps,
|
||||
Structs,
|
||||
PatternMatching,
|
||||
|
||||
17
test/koans/keyword_lists_koans_test.exs
Normal file
17
test/koans/keyword_lists_koans_test.exs
Normal 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
|
||||
Reference in New Issue
Block a user