Merge pull request #124 from elixirkoans/protocols

Protocols
This commit is contained in:
Felipe Seré
2016-05-27 14:09:53 +01:00
4 changed files with 68 additions and 2 deletions

51
lib/koans/16_protocols.ex Normal file
View 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

View File

@@ -16,6 +16,7 @@ defmodule Runner do
Processes,
Tasks,
Agents,
Protocols,
]
def koan?(koan), do: Enum.member?(@modules, koan)

View File

@@ -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], []}}

View 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