aboutsummaryrefslogtreecommitdiff
path: root/Server_test.go
diff options
context:
space:
mode:
authorMartin Ashby <martin@martin-laptop.lan>2018-05-18 21:09:14 +0100
committerMartin Ashby <martin@martin-laptop.lan>2018-05-18 21:09:14 +0100
commitb3e76b0207bc4232b4b72626f8c4c802dcc63802 (patch)
tree91ae10d4a80b1f458d1711a947ca71651baad5ab /Server_test.go
parentc2faad3ac834f622956787c87713ccf6ab9b61e0 (diff)
downloadunicornpaint-b3e76b0207bc4232b4b72626f8c4c802dcc63802.tar.gz
unicornpaint-b3e76b0207bc4232b4b72626f8c4c802dcc63802.tar.bz2
unicornpaint-b3e76b0207bc4232b4b72626f8c4c802dcc63802.tar.xz
unicornpaint-b3e76b0207bc4232b4b72626f8c4c802dcc63802.zip
Server ported to go, 50%, just need to do the actual unicorn implementation.
Diffstat (limited to 'Server_test.go')
-rw-r--r--Server_test.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/Server_test.go b/Server_test.go
new file mode 100644
index 0000000..66ce45b
--- /dev/null
+++ b/Server_test.go
@@ -0,0 +1,85 @@
+package main
+
+import (
+ "testing"
+ "encoding/json"
+ "reflect"
+)
+
+func TestStateJson(t *testing.T) {
+ testPixels := make([][]uint8arr, 16)
+ for x:=0; x<16; x++ {
+ testPixels[x] = make([]uint8arr, 16)
+ for y:=0; y<16; y++ {
+ testPixels[x][y] = uint8arr{0, 2, 3}
+ }
+ }
+
+ testSaves := []string{"bob", "sally", "blah"}
+ s1 := &State{
+ Pixels: testPixels,
+ Saves: testSaves,
+ }
+
+ data, err := json.Marshal(s1)
+ if err != nil {
+ t.Errorf("Failed to write state to JSON %v", err)
+ }
+
+ s2 := &State{}
+ err = json.Unmarshal(data, s2)
+ if err != nil {
+ t.Errorf("Failed to read state from JSON %v", err)
+ }
+
+ if !reflect.DeepEqual(s1, s2) {
+ t.Errorf("Differences after serializing state %v %v", s1, s2)
+ }
+}
+
+func TestCommandJson(t *testing.T) {
+ cmd := Command{
+ Type: noop,
+ X: uint8(1),
+ Y: uint8(1),
+ R: uint8(1),
+ G: uint8(1),
+ B: uint8(1),
+ SaveName: "testing",
+ }
+
+ data, err := json.Marshal(cmd)
+ if err != nil {
+ t.Errorf("Error encoding command to JSON")
+ }
+
+ cmd2 := Command{}
+ err = json.Unmarshal(data, &cmd2)
+ if err != nil {
+ t.Errorf("Error decoding command from JSON")
+ }
+
+ if !reflect.DeepEqual(cmd, cmd2) {
+ t.Errorf("Differences after encoding JSON %v %v", cmd, cmd2)
+ }
+
+ cmd3 := Command{}
+ testData := []byte(`{ "type": "SET_PIXEL", "x": 1, "y": 2, "r": 255, "g": 255, "b": 255 }`)
+ err = json.Unmarshal(testData, &cmd3)
+ if err != nil {
+ t.Errorf("Error unmarshalling test JSON %v %s", err, testData)
+ }
+
+ cmd4 := Command{
+ Type: setPixel,
+ X: uint8(1),
+ Y: uint8(2),
+ R: uint8(255),
+ G: uint8(255),
+ B: uint8(255),
+ }
+
+ if !reflect.DeepEqual(cmd3, cmd4) {
+ t.Errorf("Json unmarshalled incorrectly to %v", cmd4)
+ }
+} \ No newline at end of file