Implement tests for GameRegistry
Some checks failed
Build & Test / nix-build (push) Failing after 41s
Some checks failed
Build & Test / nix-build (push) Failing after 41s
This commit is contained in:
parent
78442a94af
commit
450ca4d53a
@ -36,21 +36,23 @@ defmodule Amethyst.API.Game do
|
||||
Amethyst.GameRegistry.register(__MODULE__, unquote(meta))
|
||||
Process.sleep(:infinity)
|
||||
end
|
||||
def start(state) do
|
||||
loop(state)
|
||||
end
|
||||
defp loop(state) do
|
||||
receive do
|
||||
{:login, caller, cfg} ->
|
||||
case login(caller, cfg, state) do
|
||||
{:ok, state} ->
|
||||
send(caller, :ok)
|
||||
loop(state)
|
||||
{:refuse, state} ->
|
||||
send(caller, :refuse)
|
||||
loop(state)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@spec start(Amethyst.API.Game, term()) :: no_return()
|
||||
def start(mod, state) do
|
||||
loop(mod, state)
|
||||
end
|
||||
defp loop(mod, state) do
|
||||
receive do
|
||||
{:login, caller, cfg} ->
|
||||
case mod.login(caller, cfg, state) do
|
||||
{:ok, state} ->
|
||||
send(caller, :ok)
|
||||
loop(mod, state)
|
||||
{:refuse, state} ->
|
||||
send(caller, :refuse)
|
||||
loop(mod, state)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -54,7 +54,7 @@ defmodule Amethyst.GameCoordinator do
|
||||
|
||||
defp _create(type, games) do
|
||||
state = type.instantiate()
|
||||
pid = spawn(type, :start, [state])
|
||||
pid = spawn(Amethyst.API.Game, :start, [type, state])
|
||||
games = [{type, pid, []} | games]
|
||||
{pid, games}
|
||||
end
|
||||
|
@ -30,9 +30,10 @@ defmodule Amethyst.GameRegistry do
|
||||
@impl true
|
||||
def handle_call({:register, module, meta}, _, state) do
|
||||
if Keyword.get(meta, :default, false) && find_default(state) != nil do
|
||||
{:reply, :error, :default_exists}
|
||||
{:reply, {:error, :default_exists}, state}
|
||||
else
|
||||
{:reply, :ok, [{module, meta} | state]}
|
||||
end
|
||||
{:reply, :ok, [{module, meta} | state]}
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
33
apps/amethyst/test/game_registry_test.exs
Normal file
33
apps/amethyst/test/game_registry_test.exs
Normal file
@ -0,0 +1,33 @@
|
||||
defmodule GameRegistryTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
@moduledoc """
|
||||
This module includes tests for Amethyst.GameRegistry
|
||||
"""
|
||||
|
||||
test "Register a game" do
|
||||
Amethyst.GameRegistry.register(:fake_game, [:some_meta])
|
||||
assert Amethyst.GameRegistry.list() |> Enum.member?({:fake_game, [:some_meta]})
|
||||
end
|
||||
|
||||
test "Register many games" do
|
||||
Range.new(0, 50) |> Enum.map(fn game ->
|
||||
game = String.to_atom("game#{game}")
|
||||
Amethyst.GameRegistry.register(game, [])
|
||||
game
|
||||
end) |> Enum.map(fn game ->
|
||||
assert Amethyst.GameRegistry.list() |> Enum.member?({game, []})
|
||||
end)
|
||||
end
|
||||
|
||||
test "Register and get default game" do
|
||||
Amethyst.GameRegistry.register(:fake_game, [default: true])
|
||||
assert Amethyst.GameRegistry.get_default() == {:fake_game, [default: true]}
|
||||
end
|
||||
|
||||
test "Fail to register multiple games" do
|
||||
Amethyst.GameRegistry.register(:fake_game, [default: true])
|
||||
assert Amethyst.GameRegistry.register(:other_game, [default: true]) == {:error, :default_exists}
|
||||
assert Amethyst.GameRegistry.get_default() == {:fake_game, [default: true]}
|
||||
end
|
||||
end
|
@ -9,7 +9,7 @@ defmodule Example.Game do
|
||||
|
||||
@impl true
|
||||
def login(from, cfg, state) do
|
||||
Logger.debug("Player logged in from #{inspect(from)}: #{inspect(cfg)}")
|
||||
Logger.info("Player logged in from #{inspect(from)}: #{inspect(cfg)}")
|
||||
{:ok, state}
|
||||
end
|
||||
end
|
||||
|
@ -1 +0,0 @@
|
||||
ExUnit.start()
|
Loading…
Reference in New Issue
Block a user