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))
Process.sleep(:infinity)
end
def start(state) do
loop(state)
end
defp loop(state) do
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 login(caller, cfg, state) do
case mod.login(caller, cfg, state) do
{:ok, state} ->
send(caller, :ok)
loop(state)
loop(mod, state)
{:refuse, state} ->
send(caller, :refuse)
loop(state)
end
end
loop(mod, state)
end
end
end

View File

@ -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

View File

@ -30,10 +30,11 @@ 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}
end
{:reply, {:error, :default_exists}, state}
else
{:reply, :ok, [{module, meta} | state]}
end
end
@impl true
def handle_call({:list}, _, state) do

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
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

View File

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