Updated unit tests
Some checks failed
Build & Test / nix-build (push) Failing after 18s

This commit is contained in:
Kodi Craft 2024-09-04 11:44:04 +02:00
parent 4c5f0370a0
commit 3be9b8d908
Signed by: kodi
GPG Key ID: 69D9EED60B242822
3 changed files with 39 additions and 54 deletions

View File

@ -76,7 +76,7 @@ defmodule Amethyst.GameCoordinator do
@impl true @impl true
def handle_cast({:remove, gid}, state) do def handle_cast({:remove, gid}, state) do
{games, state} = _remove(gid, state) {_, state} = _remove(gid, state)
{:noreply, state} {:noreply, state}
end end
@ -99,12 +99,12 @@ defmodule Amethyst.GameCoordinator do
mod: type, refs: refs, opts: [], gid: state.gid mod: type, refs: refs, opts: [], gid: state.gid
} }
games = state.games |> Map.put(state.gid, game) games = state.games |> Map.put(state.gid, game)
Logger.info("Created new game #{inspect(game)}") Logger.info("Created new game of type #{inspect(type)} with gid #{state.gid}")
{game, %State{gid: state.gid + 1, games: games}} {game, %State{gid: state.gid + 1, games: games}}
end end
defp _find(type, state) do defp _find(type, state) do
case state.games |> Enum.find(fn {_, game} -> game.mod == type && game[:mod].joinable?(game[:refs]) end) do case state.games |> Enum.find(fn {_, game} -> game.mod == type && game.mod.joinable?(game.refs) end) do
nil -> {nil, state} nil -> {nil, state}
{_, game} -> {game, state} {_, game} -> {game, state}
end end
@ -112,6 +112,7 @@ defmodule Amethyst.GameCoordinator do
defp _remove(gid, state) do defp _remove(gid, state) do
games = state.games |> Map.delete(gid) games = state.games |> Map.delete(gid)
Logger.info("Removed game with gid #{gid}")
{games, %State{state | games: games}} {games, %State{state | games: games}}
end end

View File

@ -1,5 +1,5 @@
defmodule GameCoordinatorTestGame do defmodule GameCoordinatorTestGame do
use Amethyst.API.Game @behaviour Amethyst.API.Game
@moduledoc """ @moduledoc """
This module is a sample game for the purpose of This module is a sample game for the purpose of
@ -7,13 +7,23 @@ defmodule GameCoordinatorTestGame do
""" """
@impl true @impl true
def instantiate() do def instantiate(supervisor) do
{:ok, :example_state} {:ok, %{test: "test"}}
end end
@impl true @impl true
def login(_connection_process, _player_cfg, state) do def login(from, cfg, refs) do
{:ok, state} :ok
end
@impl true
def joinable?(refs) do
true
end
@impl true
def player_position(from, {x, y, z}, refs) do
:ok
end end
end end
@ -24,20 +34,27 @@ defmodule GameCoordinatorTest do
This module includes tests for Amethyst.GameCoordinator This module includes tests for Amethyst.GameCoordinator
""" """
test "Create an instance of a game with create/1 then kill it" do test "Create an instance of a game with create/1 then try to find it" do
pid = Amethyst.GameCoordinator.create(GameCoordinatorTestGame) %Amethyst.GameCoordinator.Game{refs: refs, gid: gid, mod: mod} = Amethyst.GameCoordinator.create(GameCoordinatorTestGame)
assert Process.alive?(pid) assert mod == GameCoordinatorTestGame
assert Amethyst.GameCoordinator.find(GameCoordinatorTestGame) == pid assert refs[:test] == "test"
Process.exit(pid, :kill)
assert Amethyst.GameCoordinator.find(GameCoordinatorTestGame) == nil found_game = Amethyst.GameCoordinator.find(GameCoordinatorTestGame)
assert found_game.mod == GameCoordinatorTestGame
assert found_game.refs[:test] == "test"
assert found_game.gid == gid
Amethyst.GameCoordinator.remove(gid)
end end
test "Create new instance with find_or_create/1 if one doesn't exist, otherwise use it" do test "Create an instance of a game with find_or_create/1 and then find it with find_or_create/1" do
pid = Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame) %Amethyst.GameCoordinator.Game{refs: refs, gid: gid, mod: mod} = Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame)
assert Process.alive?(pid) assert mod == GameCoordinatorTestGame
assert Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame) == pid assert refs[:test] == "test"
Process.exit(pid, :kill)
assert Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame) != pid found_game = Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame)
Amethyst.GameCoordinator.find(GameCoordinatorTestGame) |> Process.exit(:kill) assert found_game.mod == GameCoordinatorTestGame
assert found_game.refs[:test] == "test"
assert found_game.gid == gid
Amethyst.GameCoordinator.remove(gid)
end end
end end

View File

@ -1,33 +0,0 @@
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