Implement tests for GameRegistry
Some checks failed
Build & Test / nix-build (push) Failing after 41s

This commit is contained in:
Kodi Craft 2024-08-19 00:00:03 +02:00
parent 78442a94af
commit 450ca4d53a
Signed by: kodi
GPG Key ID: 69D9EED60B242822
7 changed files with 55 additions and 20 deletions

View File

@ -36,21 +36,23 @@ defmodule Amethyst.API.Game do
Amethyst.GameRegistry.register(__MODULE__, unquote(meta)) Amethyst.GameRegistry.register(__MODULE__, unquote(meta))
Process.sleep(:infinity) Process.sleep(:infinity)
end end
def start(state) do end
loop(state) end
end
defp loop(state) do @spec start(Amethyst.API.Game, term()) :: no_return()
receive do def start(mod, state) do
{:login, caller, cfg} -> loop(mod, state)
case login(caller, cfg, state) do end
{:ok, state} -> defp loop(mod, state) do
send(caller, :ok) receive do
loop(state) {:login, caller, cfg} ->
{:refuse, state} -> case mod.login(caller, cfg, state) do
send(caller, :refuse) {:ok, state} ->
loop(state) send(caller, :ok)
end loop(mod, state)
end {:refuse, state} ->
send(caller, :refuse)
loop(mod, state)
end end
end end
end end

View File

@ -54,7 +54,7 @@ defmodule Amethyst.GameCoordinator do
defp _create(type, games) do defp _create(type, games) do
state = type.instantiate() state = type.instantiate()
pid = spawn(type, :start, [state]) pid = spawn(Amethyst.API.Game, :start, [type, state])
games = [{type, pid, []} | games] games = [{type, pid, []} | games]
{pid, games} {pid, games}
end end

View File

@ -30,9 +30,10 @@ defmodule Amethyst.GameRegistry do
@impl true @impl true
def handle_call({:register, module, meta}, _, state) do def handle_call({:register, module, meta}, _, state) do
if Keyword.get(meta, :default, false) && find_default(state) != nil 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 end
{:reply, :ok, [{module, meta} | state]}
end end
@impl true @impl true

View 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

View File

@ -9,7 +9,7 @@ defmodule Example.Game do
@impl true @impl true
def login(from, cfg, state) do 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} {:ok, state}
end end
end end

View File

@ -1 +0,0 @@
ExUnit.start()