Skip to content

Commit

Permalink
test: notifyChangedState
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Jul 14, 2024
1 parent ab515b4 commit 9278dad
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
11 changes: 9 additions & 2 deletions internal/testcommon/matchers/room_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 25 additions & 12 deletions internal/testcommon/matchers/state_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
42 changes: 42 additions & 0 deletions pkg/game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"
"testing"
"time"

Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

0 comments on commit 9278dad

Please sign in to comment.