Created
August 15, 2021 09:35
-
-
Save angelikatyborska/15e00919e0e8c846732b619e5dab6f65 to your computer and use it in GitHub Desktop.
Protocol consolidation vs mix test --no-compile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# test/rpg_test.exs | |
defmodule RPGTest do | |
use ExUnit.Case | |
alias RPG.{Edible, LoafOfBread} | |
describe "LoafOfBread" do | |
test "implements the Edible protocol" do | |
{:consolidated, modules} = Edible.__protocol__(:impls) | |
assert LoafOfBread in modules | |
end | |
end | |
end | |
# lib/rpg.ex | |
defmodule RPG do | |
defmodule Character do | |
defstruct health: 100, mana: 0 | |
end | |
defmodule LoafOfBread do | |
defstruct [] | |
end | |
defprotocol Edible do | |
def eat(item, character) | |
end | |
defimpl Edible, for: LoafOfBread do | |
def eat(_loaf_of_bread, %Character{} = character) do | |
updated_character = %{character | health: character.health + 5} | |
{nil, updated_character} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment