From 3be9b8d908c468c4cb5d4a1a542f62cfda879a31 Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Wed, 4 Sep 2024 11:44:04 +0200 Subject: [PATCH] Updated unit tests --- apps/amethyst/lib/apps/game_coordinator.ex | 7 +-- apps/amethyst/test/game_coordinator_test.exs | 53 +++++++++++++------- apps/amethyst/test/game_registry_test.exs | 33 ------------ 3 files changed, 39 insertions(+), 54 deletions(-) delete mode 100644 apps/amethyst/test/game_registry_test.exs diff --git a/apps/amethyst/lib/apps/game_coordinator.ex b/apps/amethyst/lib/apps/game_coordinator.ex index 44a6004..b21c16c 100644 --- a/apps/amethyst/lib/apps/game_coordinator.ex +++ b/apps/amethyst/lib/apps/game_coordinator.ex @@ -76,7 +76,7 @@ defmodule Amethyst.GameCoordinator do @impl true def handle_cast({:remove, gid}, state) do - {games, state} = _remove(gid, state) + {_, state} = _remove(gid, state) {:noreply, state} end @@ -99,12 +99,12 @@ defmodule Amethyst.GameCoordinator do mod: type, refs: refs, opts: [], gid: state.gid } 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}} end 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} {_, game} -> {game, state} end @@ -112,6 +112,7 @@ defmodule Amethyst.GameCoordinator do defp _remove(gid, state) do games = state.games |> Map.delete(gid) + Logger.info("Removed game with gid #{gid}") {games, %State{state | games: games}} end diff --git a/apps/amethyst/test/game_coordinator_test.exs b/apps/amethyst/test/game_coordinator_test.exs index 4af8e7e..160b64c 100644 --- a/apps/amethyst/test/game_coordinator_test.exs +++ b/apps/amethyst/test/game_coordinator_test.exs @@ -1,5 +1,5 @@ defmodule GameCoordinatorTestGame do - use Amethyst.API.Game + @behaviour Amethyst.API.Game @moduledoc """ This module is a sample game for the purpose of @@ -7,13 +7,23 @@ defmodule GameCoordinatorTestGame do """ @impl true - def instantiate() do - {:ok, :example_state} + def instantiate(supervisor) do + {:ok, %{test: "test"}} end @impl true - def login(_connection_process, _player_cfg, state) do - {:ok, state} + def login(from, cfg, refs) do + :ok + end + + @impl true + def joinable?(refs) do + true + end + + @impl true + def player_position(from, {x, y, z}, refs) do + :ok end end @@ -24,20 +34,27 @@ defmodule GameCoordinatorTest do This module includes tests for Amethyst.GameCoordinator """ - test "Create an instance of a game with create/1 then kill it" do - pid = Amethyst.GameCoordinator.create(GameCoordinatorTestGame) - assert Process.alive?(pid) - assert Amethyst.GameCoordinator.find(GameCoordinatorTestGame) == pid - Process.exit(pid, :kill) - assert Amethyst.GameCoordinator.find(GameCoordinatorTestGame) == nil + test "Create an instance of a game with create/1 then try to find it" do + %Amethyst.GameCoordinator.Game{refs: refs, gid: gid, mod: mod} = Amethyst.GameCoordinator.create(GameCoordinatorTestGame) + assert mod == GameCoordinatorTestGame + assert refs[:test] == "test" + + 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 - test "Create new instance with find_or_create/1 if one doesn't exist, otherwise use it" do - pid = Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame) - assert Process.alive?(pid) - assert Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame) == pid - Process.exit(pid, :kill) - assert Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame) != pid - Amethyst.GameCoordinator.find(GameCoordinatorTestGame) |> Process.exit(:kill) + test "Create an instance of a game with find_or_create/1 and then find it with find_or_create/1" do + %Amethyst.GameCoordinator.Game{refs: refs, gid: gid, mod: mod} = Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame) + assert mod == GameCoordinatorTestGame + assert refs[:test] == "test" + + found_game = Amethyst.GameCoordinator.find_or_create(GameCoordinatorTestGame) + assert found_game.mod == GameCoordinatorTestGame + assert found_game.refs[:test] == "test" + assert found_game.gid == gid + Amethyst.GameCoordinator.remove(gid) end end diff --git a/apps/amethyst/test/game_registry_test.exs b/apps/amethyst/test/game_registry_test.exs deleted file mode 100644 index e54a89e..0000000 --- a/apps/amethyst/test/game_registry_test.exs +++ /dev/null @@ -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