unicornpaint

A web-based painting app for raspberry PI and pimoroni Unicorn Hat HD
Log | Files | Refs | README

Server_test.go (1658B)


      1 package main
      2 
      3 import (
      4 	"testing"
      5 	"encoding/json"
      6 	"reflect"
      7 )
      8 
      9 func TestStateJson(t *testing.T) {
     10 	testPixels := make([][]uint8arr, 16)
     11 	for x:=0; x<16; x++ {
     12 		testPixels[x] = make([]uint8arr, 16)
     13 		for y:=0; y<16; y++ {
     14 			testPixels[x][y] = uint8arr{0, 2, 3}
     15 		}
     16 	}
     17 
     18 	testSaves := []string{"bob", "sally", "blah"}
     19 	s1 := &State{
     20 		Pixels: testPixels,
     21 		Saves: testSaves,
     22 	}
     23 
     24 	data, err := json.Marshal(s1)
     25 	if err != nil {
     26 		t.Errorf("Failed to write state to JSON %v", err)
     27 	}
     28 
     29 	s2 := &State{}
     30 	err = json.Unmarshal(data, s2)
     31 	if err != nil {
     32 		t.Errorf("Failed to read state from JSON %v", err)
     33 	}
     34 
     35 	if !reflect.DeepEqual(s1, s2) {
     36 		t.Errorf("Differences after serializing state %v %v", s1, s2)
     37 	}
     38 }
     39 
     40 func TestCommandJson(t *testing.T) {
     41 	cmd := Command{
     42 		Type: noop,
     43 		X: uint8(1),
     44 		Y: uint8(1),
     45 		R: uint8(1),
     46 		G: uint8(1),
     47 		B: uint8(1),
     48 		SaveName: "testing",
     49 	}
     50 
     51 	data, err := json.Marshal(cmd)
     52 	if err != nil {
     53 		t.Errorf("Error encoding command to JSON")
     54 	}
     55 
     56 	cmd2 := Command{}
     57 	err = json.Unmarshal(data, &cmd2)
     58 	if err != nil {
     59 		t.Errorf("Error decoding command from JSON")
     60 	}
     61 
     62 	if !reflect.DeepEqual(cmd, cmd2) {
     63 		t.Errorf("Differences after encoding JSON %v %v", cmd, cmd2)
     64 	}
     65 
     66 	cmd3 := Command{}
     67 	testData := []byte(`{ "type": "SET_PIXEL", "x": 1, "y": 2, "r": 255, "g": 255, "b": 255 }`)
     68 	err = json.Unmarshal(testData, &cmd3)
     69 	if err != nil {
     70 		t.Errorf("Error unmarshalling test JSON %v %s", err, testData)
     71 	}
     72 
     73 	cmd4 := Command{
     74 		Type: setPixel,
     75 		X: uint8(1),
     76 		Y: uint8(2),
     77 		R: uint8(255),
     78 		G: uint8(255), 
     79 		B: uint8(255),
     80 	}
     81 
     82 	if !reflect.DeepEqual(cmd3, cmd4) {
     83 		t.Errorf("Json unmarshalled incorrectly to %v", cmd4)
     84 	}
     85 }