Skip to content

Commit

Permalink
test(cardinal): improve component type safety and message testing
Browse files Browse the repository at this point in the history
Co-Authored-By: Scott Sunarto <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and Scott Sunarto committed Dec 11, 2024
1 parent f89eca0 commit 8b88f63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
10 changes: 10 additions & 0 deletions cardinal/testsuite/components.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package testsuite

import (
"pkg.world.dev/world-engine/cardinal/types"
)

// LocationComponent is a test component for location-based tests
type LocationComponent struct {
X, Y uint64
}

var _ types.Component = (*LocationComponent)(nil)

func (l LocationComponent) Name() string {
return "location"
}
Expand All @@ -14,6 +20,8 @@ type ValueComponent struct {
Value int64
}

var _ types.Component = (*ValueComponent)(nil)

func (v ValueComponent) Name() string {
return "value"
}
Expand All @@ -23,6 +31,8 @@ type PowerComponent struct {
Power int64
}

var _ types.Component = (*PowerComponent)(nil)

func (p PowerComponent) Name() string {
return "power"
}
22 changes: 12 additions & 10 deletions cardinal/testsuite/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package testsuite

import (
"errors"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down Expand Up @@ -148,38 +148,40 @@ func (t *testOutputMsg) GetInFieldInformation() map[string]any { return map[stri
func TestGetMessage(t *testing.T) {
tests := []struct {
name string
msgType string
msgID types.MessageID
shouldError bool
}{
{
name: "get registered message",
msgType: "test.test_input_msg",
msgID: 1,
shouldError: false,
},
{
name: "get unregistered message",
msgType: "test.unregistered_msg",
msgID: 999,
shouldError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a test world
world := cardinal.NewTestWorld(t)
world := NewTestWorld(t)

// Register test messages
err := world.RegisterMessage(&testInputMsg{})
err := world.RegisterMessage(&testInputMsg{}, reflect.TypeOf(testInputMsg{}))
require.NoError(t, err)
err = world.RegisterMessage(&testOutputMsg{})
err = world.RegisterMessage(&testOutputMsg{}, reflect.TypeOf(testOutputMsg{}))
require.NoError(t, err)

// Test message retrieval
_, err = world.GetMessage(tt.msgType)
msg, found := world.GetMessageByID(tt.msgID)
if tt.shouldError {
require.Error(t, err)
assert.False(t, found)
assert.Nil(t, msg)
} else {
require.NoError(t, err)
assert.True(t, found)
assert.NotNil(t, msg)
}
})
}
Expand Down

0 comments on commit 8b88f63

Please sign in to comment.