func TestNewGame(t *testing.T) { game := NewGame() testUtil.AssertEqual(0, game.GetPlayerCount(), "TestNewGame", t) testUtil.AssertEqual(1, game.GetId(), "TestNewGame", t) game = NewGame() testUtil.AssertEqual(0, game.GetPlayerCount(), "TestNewGame", t) testUtil.AssertEqual(2, game.GetId(), "TestNewGame", t) }
func TestJoinGame(t *testing.T) { manager := game.GetPlayerManagerInstance() manager.RemoveAllPlayer() players := joinGame("linwei", "gameId") testUtil.AssertEqual(1, manager.GetPlayerCount(), "TestJoinGame", t) testUtil.AssertEqual(1, len(players), "TestJoinGame", t) testUtil.AssertEqual("linwei", players[0].GetName(), "TestJoinGame", t) }
func TestQuitGame(t *testing.T) { game := NewGame() player := NewPlayer("worge", 0, 0) game.Join(player) testUtil.AssertEqual(1, game.GetPlayerCount(), "TestJoinGame", t) game.Quit("worge") testUtil.AssertEqual(0, game.GetPlayerCount(), "TestJoinGame", t) }
func TestGetSetPos(t *testing.T) { player := NewPlayer("name", 1, 1) testUtil.AssertEqual(1, player.GetPosX(), "TestGetSetPos", t) testUtil.AssertEqual(1, player.GetPosY(), "TestGetSetPos", t) player.PosX = 3 player.PosY = 2 testUtil.AssertEqual(3, player.GetPosX(), "TestGetSetPos", t) testUtil.AssertEqual(2, player.GetPosY(), "TestGetSetPos", t) }
func TestGetToSendMsgChan(t *testing.T) { player := NewPlayer("name", 1, 1) go sendMsgToPlayer(player) select { case msg := <-player.toSendMsgChan: testUtil.AssertEqual("type", msg.Type, "TestGetToSendMsgChan", t) testUtil.AssertEqual("worge", msg.From, "TestGetToSendMsgChan", t) testUtil.AssertEqual("content", msg.Content, "TestGetToSendMsgChan", t) case <-time.After(5 * time.Second): t.Fail() } }
func TestRemovePlayer(t *testing.T) { manager := GetPlayerManagerInstance() manager.RemoveAllPlayer() testUtil.AssertEqual(0, manager.GetPlayerCount(), "TestAddPlayer", t) player1 := NewPlayer("name1", 0, 0) manager.AddPlayer(player1) testUtil.AssertEqual(1, manager.GetPlayerCount(), "TestAddPlayer", t) manager.RemovePlayer("name1") testUtil.AssertEqual(0, manager.GetPlayerCount(), "TestAddPlayer", t) }
func TestGetPlayerCount(t *testing.T) { manager := GetPlayerManagerInstance() manager.RemoveAllPlayer() testUtil.AssertEqual(0, manager.GetPlayerCount(), "TestGetPlayerCount", t) player1 := NewPlayer("n1", 0, 0) manager.AddPlayer(player1) testUtil.AssertEqual(1, manager.GetPlayerCount(), "TestGetPlayerCount", t) player2 := NewPlayer("n2", 0, 0) manager.AddPlayer(player2) testUtil.AssertEqual(2, manager.GetPlayerCount(), "TestGetPlayerCount", t) }
func TestGetPlayer(t *testing.T) { manager := GetPlayerManagerInstance() manager.RemoveAllPlayer() player1 := NewPlayer("n1", 0, 0) manager.AddPlayer(player1) findPlayer := manager.GetPlayer("n1") testUtil.AssertEqual("n1", findPlayer.GetName(), "TestAddPlayer", t) player2 := NewPlayer("n2", 0, 0) manager.AddPlayer(player2) findPlayer = manager.GetPlayer("n2") testUtil.AssertEqual("n2", findPlayer.GetName(), "TestAddPlayer", t) }
func TestBroadcastMsg(t *testing.T) { game := NewGame() player := NewPlayer("worge", 0, 0) game.Join(player) testUtil.AssertEqual(1, game.GetPlayerCount(), "TestBroadcastMsg", t) player2 := NewPlayer("linwei", 0, 0) game.Join(player2) testUtil.AssertEqual(2, game.GetPlayerCount(), "TestBroadcastMsg", t) newMsgs := player.getNewMsgs() testUtil.AssertEqual(1, len(newMsgs), "TestBroadcastMsg", t) testUtil.AssertEqual("linwei", newMsgs[0].From, "TestBroadcastMsg", t) testUtil.AssertEqual("joinGame", newMsgs[0].Type, "TestBroadcastMsg", t) }
func TestGetAllPlayerForGame(t *testing.T) { game := NewGame() testUtil.AssertEqual(0, len(game.GetAllPlayer()), "TestGetAllPlayerForGame", t) player := NewPlayer("worge", 0, 0) game.Join(player) testUtil.AssertEqual(1, len(game.GetAllPlayer()), "TestGetAllPlayerForGame", t) player2 := NewPlayer("worge", 0, 0) game.Join(player2) testUtil.AssertEqual(1, len(game.GetAllPlayer()), "TestGetAllPlayerForGame", t) player3 := NewPlayer("linwei", 0, 0) game.Join(player3) testUtil.AssertEqual(2, len(game.GetAllPlayer()), "TestGetAllPlayerForGame", t) }
func TestGetAllPlayer(t *testing.T) { playerManager := game.GetPlayerManagerInstance() playerManager.RemoveAllPlayer() player := game.NewPlayer("na1", 0, 0) playerManager.AddPlayer(player) jsonContent := getAllPlayer() testUtil.AssertEqual("[{\"Name\":\"na1\",\"PosX\":0,\"PosY\":0}]", jsonContent, "TestGetJson", t) player2 := game.NewPlayer("na2", 2, 2) playerManager.AddPlayer(player2) jsonContent = getAllPlayer() testUtil.AssertEqual("[{\"Name\":\"na1\",\"PosX\":0,\"PosY\":0},{\"Name\":\"na2\",\"PosX\":2,\"PosY\":2}]", jsonContent, "TestGetJson", t) }
func TestGetAllPlayer(t *testing.T) { manager := GetPlayerManagerInstance() manager.RemoveAllPlayer() allPlayers := manager.GetAllPlayer() testUtil.AssertEqual(0, len(allPlayers), "TestGetAllPlayer", t) player1 := NewPlayer("n1", 0, 0) manager.AddPlayer(player1) allPlayers = manager.GetAllPlayer() testUtil.AssertEqual(1, len(allPlayers), "TestGetAllPlayer", t) testUtil.AssertEqual("n1", allPlayers[0].GetName(), "TestGetAllPlayer", t) player2 := NewPlayer("n2", 0, 0) manager.AddPlayer(player2) allPlayers = manager.GetAllPlayer() testUtil.AssertEqual(2, len(allPlayers), "TestGetAllPlayer", t) testUtil.AssertEqual("n2", allPlayers[1].GetName(), "TestGetAllPlayer", t) }
func TestJoinGame(t *testing.T) { game := NewGame() player := NewPlayer("worge", 0, 0) game.Join(player) testUtil.AssertEqual(1, game.GetPlayerCount(), "TestJoinGame", t) testUtil.AssertEqual("worge", game.GetPlayer("worge").GetName(), "TestJoinGame", t) player2 := NewPlayer("linwei", 0, 0) game.Join(player2) testUtil.AssertEqual(2, game.GetPlayerCount(), "TestJoinGame", t) testUtil.AssertEqual("worge", game.GetPlayer("worge").GetName(), "TestJoinGame", t) player3 := NewPlayer("worge", 0, 0) game.Join(player3) testUtil.AssertEqual(2, game.GetPlayerCount(), "TestJoinGame", t) testUtil.AssertEqual("worge", game.GetPlayer("worge").GetName(), "TestJoinGame", t) }
func TestAddGame(t *testing.T) { game := NewGame() manager := GetGameManagerInstance() manager.AddGame(game) testUtil.AssertEqual(1, manager.GetGameCount(), "TestAddGame", t) }
func TestGetGame(t *testing.T) { game := NewGame() manager := GetGameManagerInstance() manager.AddGame(game) testUtil.AssertEqual(game.GetId(), manager.GetGame(game.GetId()).GetId(), "TestGetGame", t) }