From 8efe4770565fdcd3747b7a33fbcff858ab9c4bf0 Mon Sep 17 00:00:00 2001 From: c-my Date: Fri, 27 Mar 2020 12:46:56 +0800 Subject: [PATCH] TEST: Hall && RoomController --- container/Hall_test.go | 54 +++++++++++++++++ controller/RoomController_test.go | 98 +++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 container/Hall_test.go create mode 100644 controller/RoomController_test.go diff --git a/container/Hall_test.go b/container/Hall_test.go new file mode 100644 index 0000000..1fdc05b --- /dev/null +++ b/container/Hall_test.go @@ -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") + } +} diff --git a/controller/RoomController_test.go b/controller/RoomController_test.go new file mode 100644 index 0000000..e0992f6 --- /dev/null +++ b/controller/RoomController_test.go @@ -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") + } +}