Merge remote-tracking branch 'remotes/upstream/master' into add_map_sets_koans
Conflicts: lib/runner.ex
This commit is contained in:
16
CONTRIBUTORS.md
Normal file
16
CONTRIBUTORS.md
Normal file
@@ -0,0 +1,16 @@
|
||||
"Thank you!" to all the contributors (alphabetical order):
|
||||
|
||||
* Alex
|
||||
* Fabien Townsend
|
||||
* Felipe Seré
|
||||
* gemcfadyen
|
||||
* Jay Hayes
|
||||
* kamidev
|
||||
* Mahmut Surekci
|
||||
* Makis Otman
|
||||
* Rabea Gleissner
|
||||
* Ria Cataquian
|
||||
* snikolau
|
||||
* Trung Lê
|
||||
* Uku Taht
|
||||
* Zander Mackie
|
||||
@@ -43,3 +43,5 @@ $ mix meditate --no-clear-screen
|
||||
We welcome contributions! If something does not make sense along the way or you feel
|
||||
like an important lesson is missing from the koans, feel free to fork the project
|
||||
and open a pull request.
|
||||
|
||||
List of [contributors](CONTRIBUTORS.md).
|
||||
|
||||
51
lib/koans/16_protocols.ex
Normal file
51
lib/koans/16_protocols.ex
Normal file
@@ -0,0 +1,51 @@
|
||||
defmodule Protocols do
|
||||
use Koans
|
||||
|
||||
@intro "Wan't to follow the rules? Adhere to the protocol!"
|
||||
|
||||
defprotocol School, do: def enrol(person)
|
||||
|
||||
defimpl School, for: Any do
|
||||
def enrol(_) do
|
||||
"Pupil enrolled at school"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Student do
|
||||
@derive School
|
||||
defstruct name: ""
|
||||
end
|
||||
|
||||
defmodule Musician, do: defstruct name: "", instrument: ""
|
||||
defmodule Dancer, do: defstruct name: "", dance_style: ""
|
||||
defmodule Baker, do: defstruct name: ""
|
||||
|
||||
defimpl School, for: Musician do
|
||||
def enrol(musician) do
|
||||
"#{musician.name} signed up for #{musician.instrument}"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl School, for: Dancer do
|
||||
def enrol(dancer), do: "#{dancer.name} enrolled for #{dancer.dance_style}"
|
||||
end
|
||||
|
||||
koan "Sharing an interface is the secret at school" do
|
||||
musician = %Musician{name: "Andre", instrument: "violin"}
|
||||
dancer = %Dancer{name: "Darcy", dance_style: "ballet"}
|
||||
|
||||
assert School.enrol(musician) == ___
|
||||
assert School.enrol(dancer) == ___
|
||||
end
|
||||
|
||||
koan "Sometimes we all use the same" do
|
||||
student = %Student{name: "Emily"}
|
||||
assert School.enrol(student) == ___
|
||||
end
|
||||
|
||||
koan "If you don't comply you can't get in" do
|
||||
assert_raise ___, fn ->
|
||||
School.enrol(%Baker{name: "Delia"})
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -16,6 +16,7 @@ defmodule Runner do
|
||||
Processes,
|
||||
Tasks,
|
||||
Agents,
|
||||
Protocols,
|
||||
MapSets,
|
||||
]
|
||||
|
||||
|
||||
4
mix.lock
4
mix.lock
@@ -1,2 +1,2 @@
|
||||
%{"exfswatch": {:hex, :exfswatch, "0.1.1"},
|
||||
"fs": {:hex, :fs, "0.9.2"}}
|
||||
%{"exfswatch": {:hex, :exfswatch, "0.1.1", "7ccf6fc9b443d04dada3e50b21f910b4d0e4546ce7772dc6d4181fb929ea186f", [:mix], [{:fs, "~> 0.9", [hex: :fs, optional: false]}]},
|
||||
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []}}
|
||||
|
||||
14
test/koans/protocols_koans_test.exs
Normal file
14
test/koans/protocols_koans_test.exs
Normal file
@@ -0,0 +1,14 @@
|
||||
defmodule ProtocolsTests do
|
||||
use ExUnit.Case
|
||||
import TestHarness
|
||||
|
||||
test "Protocols" do
|
||||
answers = [
|
||||
{:multiple, ["Andre signed up for violin", "Darcy enrolled for ballet"]},
|
||||
"Pupil enrolled at school",
|
||||
Protocol.UndefinedError
|
||||
]
|
||||
|
||||
test_all(Protocols, answers)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user