TEST: Hall && RoomController
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package container
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetHall(t *testing.T) {
|
||||
hall:=GetHall()
|
||||
if hall ==nil{
|
||||
t.Errorf("get nil hall")
|
||||
}
|
||||
hall2:=GetHall()
|
||||
if hall!=hall2{
|
||||
t.Errorf("get different hall")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHall_CreateRoom(t *testing.T) {
|
||||
hall := GetHall()
|
||||
roomID := hall.CreateRoom(0001, "test room", "ppp")
|
||||
if roomID == -1 {
|
||||
t.Errorf("failed to create first room")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHall_JoinRoom(t *testing.T) {
|
||||
hall := GetHall()
|
||||
roomID := hall.CreateRoom(0002, "test room", "ppp")
|
||||
if !hall.JoinRoom(0003, roomID) {
|
||||
t.Errorf("2nd player joined failed")
|
||||
}
|
||||
if !hall.JoinRoom(0004, roomID) {
|
||||
t.Errorf("3rd player joined failed")
|
||||
}
|
||||
if !hall.JoinRoom(0005, roomID) {
|
||||
t.Errorf("4th player joined failed")
|
||||
}
|
||||
if hall.JoinRoom(0006, roomID) {
|
||||
t.Errorf("5th player joined room")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHall_GetRoomID(t *testing.T) {
|
||||
hall:=GetHall()
|
||||
roomID := hall.CreateRoom(0007, "test room", "ppp")
|
||||
if hall.GetRoomID(7)!=roomID{
|
||||
t.Errorf("get wrong roomID")
|
||||
}
|
||||
if hall.GetRoomID(8)>=0{
|
||||
t.Errorf("get ghost roomID")
|
||||
}
|
||||
hall.JoinRoom(8, roomID)
|
||||
if hall.GetRoomID(8)!=roomID{
|
||||
t.Errorf("get wrong roomID")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/c-my/MahjongServer/container"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRoomCreateHandler(t *testing.T) {
|
||||
body, _ := json.Marshal(createRoomMsg{UserID: 123, RoomName: "test room", Passwd: "ppp"})
|
||||
req, err := http.NewRequest("POST", "/room", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(RoomCreateHandler)
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
var msg result
|
||||
err = json.Unmarshal([]byte(rr.Body.String()), &msg)
|
||||
|
||||
if err != nil || msg.Success != true {
|
||||
t.Errorf("failed to create first room")
|
||||
} else {
|
||||
log.Print("roomID: ", msg.RoomID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoomJoinHandler(t *testing.T) {
|
||||
container.GetHall().CreateRoom(003, "test", "pp")
|
||||
body, _ := json.Marshal(joinRoomMsg{
|
||||
UserID: 004,
|
||||
RoomID: 0,
|
||||
})
|
||||
req, err := http.NewRequest("PUT", "/room", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(RoomJoinHandler)
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
var msg result
|
||||
err = json.Unmarshal([]byte(rr.Body.String()), &msg)
|
||||
if err != nil || msg.Success != true {
|
||||
t.Errorf("failed to join room as 2nd player")
|
||||
}
|
||||
|
||||
body, _ = json.Marshal(joinRoomMsg{
|
||||
UserID: 005,
|
||||
RoomID: 0,
|
||||
})
|
||||
req, err = http.NewRequest("PUT", "/room", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr = httptest.NewRecorder()
|
||||
handler = http.HandlerFunc(RoomJoinHandler)
|
||||
handler.ServeHTTP(rr, req)
|
||||
err = json.Unmarshal([]byte(rr.Body.String()), &msg)
|
||||
if err != nil || msg.Success != true {
|
||||
t.Errorf("failed to join room as 3rd player")
|
||||
}
|
||||
body, _ = json.Marshal(joinRoomMsg{
|
||||
UserID: 006,
|
||||
RoomID: 0,
|
||||
})
|
||||
req, err = http.NewRequest("PUT", "/room", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr = httptest.NewRecorder()
|
||||
handler = http.HandlerFunc(RoomJoinHandler)
|
||||
handler.ServeHTTP(rr, req)
|
||||
err = json.Unmarshal([]byte(rr.Body.String()), &msg)
|
||||
if err != nil || msg.Success != true {
|
||||
t.Errorf("failed to join room as 4th player")
|
||||
}
|
||||
body, _ = json.Marshal(joinRoomMsg{
|
||||
UserID: 006,
|
||||
RoomID: 0,
|
||||
})
|
||||
req, err = http.NewRequest("PUT", "/room", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr = httptest.NewRecorder()
|
||||
handler = http.HandlerFunc(RoomJoinHandler)
|
||||
handler.ServeHTTP(rr, req)
|
||||
err = json.Unmarshal([]byte(rr.Body.String()), &msg)
|
||||
if err != nil || msg.Success == true {
|
||||
t.Errorf("joined room as 5th player")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user