From c5440f50e7b4c1232602f567be00f2cfd032ce85 Mon Sep 17 00:00:00 2001 From: Volte6 <143822+Volte6@users.noreply.github.com> Date: Sun, 5 Jan 2025 22:53:57 -0800 Subject: [PATCH] Some unit tests for room/corpse --- internal/rooms/rooms_test.go | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 internal/rooms/rooms_test.go diff --git a/internal/rooms/rooms_test.go b/internal/rooms/rooms_test.go new file mode 100644 index 0000000..2f6ed4c --- /dev/null +++ b/internal/rooms/rooms_test.go @@ -0,0 +1,96 @@ +package rooms + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/volte6/gomud/internal/characters" +) + +func TestRoom_AddCorpse(t *testing.T) { + r := &Room{} + assert.Empty(t, r.Corpses, "Expected no corpses initially") + + corpse := Corpse{ + MobId: 0, + UserId: 123, + Character: characters.Character{Name: "TestPlayer"}, + RoundCreated: 10, + } + r.AddCorpse(corpse) + assert.Len(t, r.Corpses, 1, "Expected exactly one corpse after adding") + assert.Equal(t, corpse, r.Corpses[0], "Expected the added corpse to match") +} + +func TestRoom_RemoveCorpse(t *testing.T) { + r := &Room{} + corpse1 := Corpse{ + MobId: 0, + UserId: 123, + Character: characters.Character{Name: "PlayerOne"}, + RoundCreated: 10, + } + corpse2 := Corpse{ + MobId: 456, + UserId: 0, + Character: characters.Character{Name: "MobOne"}, + RoundCreated: 11, + } + + r.AddCorpse(corpse1) + r.AddCorpse(corpse2) + + // Removing existing corpse + removed := r.RemoveCorpse(corpse1) + assert.True(t, removed, "Expected to remove an existing corpse successfully") + assert.Len(t, r.Corpses, 1, "Expected exactly one corpse remaining") + + // Try removing a corpse that does not exist + nonExistent := Corpse{ + MobId: 999, + UserId: 999, + Character: characters.Character{Name: "Ghost"}, + RoundCreated: 99, + } + removed = r.RemoveCorpse(nonExistent) + assert.False(t, removed, "Expected removal to fail for non-existent corpse") + assert.Len(t, r.Corpses, 1, "Expected no change in corpses") +} + +func TestRoom_FindCorpse(t *testing.T) { + r := &Room{} + + playerCorpse := Corpse{ + UserId: 123, + Character: characters.Character{Name: "PlayerOne"}, + RoundCreated: 5, + Prunable: false, + } + mobCorpse := Corpse{ + MobId: 456, + Character: characters.Character{Name: "MobOne"}, + RoundCreated: 6, + Prunable: false, + } + r.AddCorpse(playerCorpse) + r.AddCorpse(mobCorpse) + + // Exact search + found, ok := r.FindCorpse("PlayerOne corpse") + assert.True(t, ok, "Expected to find player corpse by exact name") + assert.Equal(t, "PlayerOne", found.Character.Name, "Expected found corpse to match the correct character") + + // Searching for mob + found, ok = r.FindCorpse("MobOne corpse") + assert.True(t, ok, "Expected to find mob corpse by exact name") + assert.Equal(t, "MobOne", found.Character.Name, "Expected found corpse to match the correct character") + + // Searching partial name (depends on your util.FindMatchIn logic) + found, ok = r.FindCorpse("player") + assert.True(t, ok, "Expected to find a close match for player corpse") + assert.Equal(t, "PlayerOne", found.Character.Name, "Expected found corpse to be the player's") + + // Non-existent + found, ok = r.FindCorpse("NonExistent") + assert.False(t, ok, "Expected not to find a missing corpse") +}