diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml new file mode 100644 index 0000000..646bcdf --- /dev/null +++ b/.github/workflows/elixir.yml @@ -0,0 +1,35 @@ +name: Elixir CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + name: Build and test + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: System dependencies + run: | + sudo apt update + sudo apt install -y inotify-tools + - name: Set up Elixir + uses: erlef/setup-beam@988e02bfe678367a02564f65ca2e37726dc0268f + with: + elixir-version: '1.12.3' # Define the elixir version [required] + otp-version: '24.1' # Define the OTP version [required] + - name: Restore dependencies cache + uses: actions/cache@v2 + with: + path: deps + key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} + restore-keys: ${{ runner.os }}-mix- + - name: Install dependencies + run: mix deps.get + - name: Run tests + run: mix test diff --git a/lib/koans/17_agents.ex b/lib/koans/17_agents.ex index ec0ce28..c6e17f2 100644 --- a/lib/koans/17_agents.ex +++ b/lib/koans/17_agents.ex @@ -14,31 +14,31 @@ defmodule Agents do end koan "Update to update the state" do - Agent.start_link(fn -> "Hi there" end, name: __MODULE__) + Agent.start_link(fn -> "Hi there" end, name: :greeter) - Agent.update(__MODULE__, fn old -> + Agent.update(:greeter, fn old -> String.upcase(old) end) - assert Agent.get(__MODULE__, & &1) == ___ + assert Agent.get(:greeter, & &1) == ___ end koan "Use get_and_update when you need to read and change a value in one go" do - Agent.start_link(fn -> ["Milk"] end, name: __MODULE__) + Agent.start_link(fn -> ["Milk"] end, name: :groceries) old_list = - Agent.get_and_update(__MODULE__, fn old -> + Agent.get_and_update(:groceries, fn old -> {old, ["Bread" | old]} end) assert old_list == ___ - assert Agent.get(__MODULE__, & &1) == ___ + assert Agent.get(:groceries, & &1) == ___ end koan "Somebody has to switch off the light at the end of the day" do - {:ok, pid} = Agent.start_link(fn -> ["Milk"] end, name: __MODULE__) + {:ok, pid} = Agent.start_link(fn -> "Fin." end, name: :stoppable) - Agent.stop(__MODULE__) + Agent.stop(:stoppable) assert Process.alive?(pid) == ___ end diff --git a/lib/koans/18_genservers.ex b/lib/koans/18_genservers.ex index 8e78761..ab6d7dc 100644 --- a/lib/koans/18_genservers.ex +++ b/lib/koans/18_genservers.ex @@ -12,9 +12,13 @@ defmodule GenServers do {:ok, args} end - def start_link(init_password) do + def start(init_password) do # The __MODULE__ macro returns the current module name as an atom - GenServer.start_link(__MODULE__, init_password, name: __MODULE__) + GenServer.start(__MODULE__, init_password, name: __MODULE__) + end + + def stop do + GenServer.stop(__MODULE__) end def unlock(password) do @@ -134,20 +138,24 @@ defmodule GenServers do end koan "Our server works but it's pretty ugly to use; so lets use a cleaner interface" do - Laptop.start_link("EL!73") + Laptop.start("EL!73") assert Laptop.unlock("EL!73") == ___ end koan "Let's use the remaining functions in the external API" do - Laptop.start_link("EL!73") + Laptop.start("EL!73") + {_, response} = Laptop.unlock("EL!73") assert response == ___ Laptop.change_password("EL!73", "Elixir") + {_, response} = Laptop.unlock("EL!73") assert response == ___ {_, response} = Laptop.owner_name() assert response == ___ + + :ok = Laptop.stop() end end diff --git a/test/koans/map_sets_koans_test.exs b/test/koans/map_sets_koans_test.exs index 043001d..327ebf9 100644 --- a/test/koans/map_sets_koans_test.exs +++ b/test/koans/map_sets_koans_test.exs @@ -4,7 +4,6 @@ defmodule MapSetsTest do test "MapSets" do answers = [ - 1, 3, {:multiple, [false, true]}, true, diff --git a/test/koans/protocols_koans_test.exs b/test/koans/protocols_koans_test.exs index 88dcbb7..c23513a 100644 --- a/test/koans/protocols_koans_test.exs +++ b/test/koans/protocols_koans_test.exs @@ -4,8 +4,8 @@ defmodule ProtocolsTests do test "Protocols" do answers = [ - {:multiple, ["Andre signed up for violin", "Darcy enrolled for ballet"]}, - "Pupil enrolled at school", + {:multiple, ["Andre played violin", "Darcy performed ballet"]}, + "Artist showed performance", Protocol.UndefinedError ] diff --git a/test/test_helper.exs b/test/test_helper.exs index 56d98f4..2006538 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,4 +1,5 @@ -ExUnit.start() +timeout = 1000 # ms +ExUnit.start(timeout: timeout) defmodule TestHarness do import ExUnit.Assertions