Skip to content

Commit

Permalink
add initial unit tests for dedicated and protostream (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
miledxz authored Feb 13, 2024
1 parent 9a54e08 commit e189bf1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ upgrade:
# unit test
.PHONY: test
test:
go test ./frontend/ ./mmf/ ./director/ ./dedicated/
go test ./frontend/ ./mmf/ ./director/ ./dedicated/ ./game/protostream/

# integration test
.PHONY: integration-test
Expand Down
85 changes: 85 additions & 0 deletions dedicated/dedicated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"testing"

"github.com/googleforgames/space-agon/game/pb"
"github.com/stretchr/testify/assert"
)

func TestIsMemoRecipient(t *testing.T) {

tests := []struct {
cid int64
memo *pb.Memo
name string
isPanic bool
correctRecipient bool
}{
{
cid: int64(1),
memo: &pb.Memo{
Recipient: &pb.Memo_To{
To: int64(1),
},
},
name: "correct Memo_to recipient",
isPanic: false,
correctRecipient: true,
},
{
cid: int64(1),
memo: &pb.Memo{
Recipient: &pb.Memo_To{
To: int64(2),
},
},
name: "incorrect Memo_to recipient",
isPanic: false,
correctRecipient: false,
},
{
cid: int64(2),
memo: &pb.Memo{
Recipient: &pb.Memo_EveryoneBut{
EveryoneBut: int64(1),
},
},
name: "correct Memo_EveryoneBut recipient",
isPanic: false,
correctRecipient: true,
},
{
cid: int64(1),
memo: &pb.Memo{
Recipient: &pb.Memo_EveryoneBut{
EveryoneBut: int64(1),
},
},
name: "incorrect Memo_EveryoneBut recipient",
isPanic: false,
correctRecipient: false,
},
{
cid: int64(12),
memo: &pb.Memo{},
name: "unknown memo recipient",
isPanic: true,
correctRecipient: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.isPanic {
assert.Panics(t, func() {
isMemoRecipient(test.cid, test.memo)
})
} else if test.correctRecipient {
assert.True(t, isMemoRecipient(test.cid, test.memo))
} else {
assert.False(t, isMemoRecipient(test.cid, test.memo))
}
})
}
}
26 changes: 26 additions & 0 deletions game/protostream/protostream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package protostream

import (
"testing"
)

func TestDecodeVarint(t *testing.T) {
tests := []struct {
input []byte
expected uint64
length int
name string
}{
{[]byte{0x00}, 0, 1, "zero value"},
{[]byte{0x01}, 1, 1, "positive value"},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, length := DecodeVarint(test.input)
if result != test.expected || length != test.length {
t.Errorf("got result: %v, length: %v, expected result: %v, length: %v)", result, length, test.expected, test.length)
}
})
}
}

0 comments on commit e189bf1

Please sign in to comment.