aboutsummaryrefslogtreecommitdiff
path: root/Server_test.go
blob: 66ce45b39e60a1e4e6e2b073b6ddcfbf2bf2e5d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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)
	}
}