Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Volte6 committed Jan 6, 2025
1 parent 1f8a0b9 commit 8f6cf94
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ require (
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d
github.com/gorilla/websocket v1.5.3
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/stretchr/testify v1.8.0
)

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/text v0.3.8 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4M
github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic=
github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
Expand Down Expand Up @@ -39,6 +40,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down Expand Up @@ -77,5 +81,6 @@ gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
116 changes: 116 additions & 0 deletions internal/rooms/corpse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package rooms

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/volte6/gomud/internal/characters"
"github.com/volte6/gomud/internal/gametime"
)

// Test that if the corpse is already prunable, calling Update does nothing.
func TestCorpseUpdate_PrunableAlreadyTrue(t *testing.T) {
corpse := &Corpse{
UserId: 1,
MobId: 2,
Character: characters.Character{},
RoundCreated: 100,
Prunable: true,
}

// Even if we call Update with a large roundNow, prunable should remain true.
corpse.Update(9999, "1 week")
assert.True(t, corpse.Prunable, "Corpse should remain prunable when already set to true")
}

// Test that if decayRate is empty, it defaults to "1 week". We assume "1 week"
// is longer than the provided roundNow in this example, so it should remain false.
func TestCorpseUpdate_EmptyDecayRateNotEnoughTime(t *testing.T) {
// Suppose RoundCreated=0 and roundNow=5000,
// we assume "1 week" (whatever that translates to in rounds) is > 5000.
corpse := &Corpse{
UserId: 1,
MobId: 2,
Character: characters.Character{},
RoundCreated: 5000,
Prunable: false,
}

corpse.Update(5000, "")
assert.False(t, corpse.Prunable, "Corpse should not be prunable yet if not enough time has elapsed for default decay rate")
}

// Test that if decayRate is empty, it defaults to "1 week". We assume "1 week"
// is shorter than the provided roundNow, so it becomes prunable.
func TestCorpseUpdate_EmptyDecayRateSufficientTime(t *testing.T) {
// Suppose RoundCreated=0 and roundNow=20000,
// we assume "1 week" < 20000 (whatever that is in your game logic).
corpse := &Corpse{
UserId: 1,
MobId: 2,
Character: characters.Character{},
RoundCreated: 0,
Prunable: false,
}

corpse.Update(20000, "")
assert.True(t, corpse.Prunable, "Corpse should become prunable if enough time has elapsed for default decay rate")
}

// Test a custom decay rate, ensuring that when roundNow < decay threshold,
// the corpse is not prunable, and once roundNow >= threshold, it becomes prunable.
func TestCorpseUpdate_CustomDecayRate(t *testing.T) {
// For demonstration, let's assume "2 weeks" is a certain number of rounds,
// but the exact number depends on your game logic.
// We'll test a transition point: just before and just after the threshold.
corpse := &Corpse{
UserId: 1,
MobId: 2,
Character: characters.Character{},
RoundCreated: 1000, // The starting round
Prunable: false,
}

// Just before decay threshold
// e.g., if "2 weeks" from round 1000 is 21000, pass in 20999
corpse.Update(1005, "2 weeks")
assert.False(t, corpse.Prunable, "Corpse should not be prunable just before custom decay threshold")

// At or after decay threshold
// e.g., pass in 21000 or greater
corpse.Update(21000, "2 weeks")
assert.True(t, corpse.Prunable, "Corpse should become prunable at or after custom decay threshold")
}

// Example test that checks repeated updates before and after crossing the threshold.
func TestCorpseUpdate_MultipleCalls(t *testing.T) {
// For demonstration, let's assume "1 week" is N rounds in your logic.
corpse := &Corpse{
RoundCreated: 100,
Prunable: false,
}

// First update: not enough rounds have passed
corpse.Update(105, "1 week")
assert.False(t, corpse.Prunable, "Corpse should still not be prunable if not enough rounds elapsed")

// Second update: enough rounds have passed
corpse.Update(20000, "1 week")
assert.True(t, corpse.Prunable, "Corpse should become prunable after sufficient rounds elapsed")
}

// If you want to specifically test or mock the gametime package, you can do so
// in a separate test or by creating a mock gametime function. This example
// simply relies on the assumption that gametime.GetDate and its AddPeriod logic
// are correct. If those are crucial, consider additional testing around them.
func TestCorpseUpdate_GametimeIntegration(t *testing.T) {
// If you have a need to test how gametime transforms RoundCreated + decayRate,
// you might do something like:

gd := gametime.GetDate(0)
decayRound := gd.AddPeriod("1 week")

// Ensure that the decayRound is what you expect...
// But that might be tested better in the gametime package itself.
assert.NotZero(t, decayRound, "Decay round should not be zero")
}

0 comments on commit 8f6cf94

Please sign in to comment.