From e189bf1f4708695555283e842fbe799ae7b6f850 Mon Sep 17 00:00:00 2001 From: Mile Druzijanic <153705375+zedGGs@users.noreply.github.com> Date: Tue, 13 Feb 2024 09:19:25 +0100 Subject: [PATCH] add initial unit tests for dedicated and protostream (#44) --- Makefile | 2 +- dedicated/dedicated_test.go | 85 ++++++++++++++++++++++++++++ game/protostream/protostream_test.go | 26 +++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 dedicated/dedicated_test.go create mode 100644 game/protostream/protostream_test.go diff --git a/Makefile b/Makefile index 0b629e60..a67498d8 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/dedicated/dedicated_test.go b/dedicated/dedicated_test.go new file mode 100644 index 00000000..d8582dae --- /dev/null +++ b/dedicated/dedicated_test.go @@ -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)) + } + }) + } +} diff --git a/game/protostream/protostream_test.go b/game/protostream/protostream_test.go new file mode 100644 index 00000000..4568db97 --- /dev/null +++ b/game/protostream/protostream_test.go @@ -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) + } + }) + } +}