From 9278dada9ce3300aa5820119aaf21cb1365c332e Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Sun, 14 Jul 2024 18:09:57 +0100 Subject: [PATCH] test: notifyChangedState --- internal/testcommon/matchers/room_matcher.go | 11 ++++- internal/testcommon/matchers/state_matcher.go | 37 ++++++++++------ pkg/game/game_test.go | 42 +++++++++++++++++++ 3 files changed, 76 insertions(+), 14 deletions(-) diff --git a/internal/testcommon/matchers/room_matcher.go b/internal/testcommon/matchers/room_matcher.go index cd19d81..98a90b5 100644 --- a/internal/testcommon/matchers/room_matcher.go +++ b/internal/testcommon/matchers/room_matcher.go @@ -17,8 +17,15 @@ func NewRoomMatcher(room *protocol.Room) *RoomMatcher { } func (m *RoomMatcher) Matches(x interface{}) bool { - room, ok := x.(*protocol.Room) - return ok && m.room.ToRoomID() == room.ToRoomID() + switch room := x.(type) { + case *protocol.Room: + return m.room.ToRoomID() == room.ToRoomID() + case protocol.Room: + return m.room.ToRoomID() == room.ToRoomID() + case protocol.RoomID: + return m.room.ToRoomID() == room + } + return false } func (m *RoomMatcher) String() string { diff --git a/internal/testcommon/matchers/state_matcher.go b/internal/testcommon/matchers/state_matcher.go index 8f73ab7..a983979 100644 --- a/internal/testcommon/matchers/state_matcher.go +++ b/internal/testcommon/matchers/state_matcher.go @@ -24,28 +24,41 @@ func NewStateMatcher(t *testing.T, cb Callback) *StateMatcher { } func (m *StateMatcher) Matches(x interface{}) bool { - if !m.MessageMatcher.Matches(x) { - return false - } + var state protocol.State - if m.message.Type != protocol.MessageTypeState { - return false - } + switch x := x.(type) { + case []byte: + if !m.MessageMatcher.Matches(x) { + return false + } + if m.message.Type != protocol.MessageTypeState { + return false + } + var stateMessage protocol.GameStateMessage + err := json.Unmarshal(m.payload, &stateMessage) + if err != nil { + return false + } + state = stateMessage.State + + case *protocol.State: + state = *x + + case protocol.State: + state = x - var stateMessage protocol.GameStateMessage - err := json.Unmarshal(m.payload, &stateMessage) - if err != nil { + default: return false } - m.state = stateMessage.State - m.triggered <- stateMessage.State + m.state = state + m.triggered <- state if m.cb == nil { return true } - return m.cb(&stateMessage.State) + return m.cb(&state) } func (m *StateMatcher) String() string { diff --git a/pkg/game/game_test.go b/pkg/game/game_test.go index 65b3d43..ccd88e3 100644 --- a/pkg/game/game_test.go +++ b/pkg/game/game_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "reflect" "testing" "time" @@ -18,6 +19,7 @@ import ( "github.com/six78/2-story-points-cli/internal/transport" mocktransport "github.com/six78/2-story-points-cli/internal/transport/mock" "github.com/six78/2-story-points-cli/pkg/protocol" + mockstorage "github.com/six78/2-story-points-cli/pkg/storage/mock" ) func TestGame(t *testing.T) { @@ -546,3 +548,43 @@ func (s *Suite) TestGameNotInitialized() { s.Require().NoError(err) s.Require().True(g.Initialized()) } + +func (s *Suite) TestNotifyChangedState() { + ctrl := gomock.NewController(s.T()) + storageMock := mockstorage.NewMockService(ctrl) + + options := []Option{ + WithContext(s.ctx), + WithTransport(s.transport), + WithClock(s.clock), + WithLogger(s.Logger), + WithStorage(storageMock), + WithAutoReveal(false, 0), + } + + g := NewGame(options) + s.Require().NotNil(g) + s.Require().False(g.Initialized()) + + room, err := protocol.NewRoom() + s.Require().NoError(err) + + var state protocol.State + err = gofakeit.Struct(state) + s.Require().NoError(err) + + g.isDealer = true + g.roomID = room.ToRoomID() + g.state = &state + + roomMatcher := matchers.NewRoomMatcher(room) + stateMatcher := matchers.NewStateMatcher(s.T(), func(receivedState *protocol.State) bool { + return reflect.DeepEqual(*receivedState, state) + }) + + storageMock.EXPECT(). + SaveRoomState(roomMatcher, stateMatcher). + Times(1) + + g.notifyChangedState(false) // WARNING: true +}