func TestVec2_Variables(t *testing.T) { c := test.Checker(t, test.Summary("spec testing exported variables")) c.Expect(test.EQ, Vector{0, 0}, Origin, "origin") c.Expect(test.EQ, Vector{1, 0}, X, "x-axis") c.Expect(test.EQ, Vector{0, 1}, Y, "y-axis") }
func TestMat2_Construct(t *testing.T) { tests := []struct { summary string args interface{} exp Matrix }{ { summary: "an slice of float32", args: []float32{0, 1, 2, 3, 4, 5, 6, 7, 8}, exp: Matrix{0, 1, 2, 3, 4, 5, 6, 7, 8}, }, { summary: "an [16]float32", args: [9]float32{0, 1}, exp: Matrix{0, 1}, }, { summary: "a Matrix", args: Matrix{7, 12, 15, -1, -1, -1, 0, 8, 7}, exp: Matrix{7, 12, 15, -1, -1, -1, 0, 8, 7}, }, { summary: "a *Matrix", args: &Matrix{10, 9, 8, 7, 6, 5, 4, 3, 2}, exp: Matrix{10, 9, 8, 7, 6, 5, 4, 3, 2}, }, { summary: "a []Row", args: []Row{{1, 2, 0}, {0, 1, 2}, {2, 0, 1}}, exp: Matrix{1, 2, 0, 0, 1, 2, 2, 0, 1}, }, { summary: "a [3]Row", args: [3]Row{{1, 2, 0}, {0, 1, 2}, {2, 0, 1}}, exp: Matrix{1, 2, 0, 0, 1, 2, 2, 0, 1}, }, { summary: "a []Col", args: []Col{{1, 2, 0}, {0, 1, 2}, {2, 0, 1}}, exp: Matrix{1, 0, 2, 2, 1, 0, 0, 2, 1}, }, { summary: "a [3]Col", args: [3]Col{{1, 2, 0}, {0, 1, 2}, {2, 0, 1}}, exp: Matrix{1, 0, 2, 2, 1, 0, 0, 2, 1}, }, } for i, tt := range tests { c := test.Checker(t, test.Summary("with test %v: %v", i, tt.summary)) c.Expect(test.EQ, tt.exp, Construct(tt.args)) } }
func TestMat2_ConstructPanicsOnInvalidTypes(t *testing.T) { tests := []struct { f func() msg string }{ { msg: "invalid type passed to Construct: mat2.invalid", f: func() { type invalid int; Construct(invalid(5)) }, }, { msg: "Construct(a []float32) must have len(a)==9", f: func() { Construct([]float32{}) }, }, { msg: "Construct(a []float32) must have len(a)==9", f: func() { Construct([]float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) }, }, { msg: "Construct(a []Row) must have len(a)==3", f: func() { Construct([]Row{}) }, }, { msg: "Construct(a []Row) must have len(a)==3", f: func() { Construct([]Row{{}, {}, {}, {}}) }, }, { msg: "Construct(a []Col) must have len(a)==3", f: func() { Construct([]Col{}) }, }, { msg: "Construct(a []Col) must have len(a)==3", f: func() { Construct([]Col{{}, {}, {}, {}}) }, }, } for i, tt := range tests { c := test.Checker(t, test.Summary("with test %v", i)) c.Expect(test.PanicEQ, tt.msg, tt.f) } }
func TestSerialization(t *testing.T) { tests := []struct { summary string testFn func(c *test.Chkr, obj, data interface{}) data []byte objPtr interface{} }{ { summary: "serializing uses golang's encoding/json serializer", objPtr: &testType{}, data: []byte(`{"I": 5, "F": 3.14, "S": "string", "B": true}`), testFn: func(c *test.Chkr, obj, data interface{}) { SerializeInPlace(obj, data) typ := obj.(*testType) c.Expect(test.EQ, 5, typ.I) c.Expect(test.FloatEQ, 3.14, typ.F) c.Expect(test.EQ, "string", typ.S) c.Expect(test.EQ, true, typ.B) }, }, { summary: "tag support isn't in yet", objPtr: &testType{}, data: []byte(`{"b": true}`), testFn: func(c *test.Chkr, obj, data interface{}) { c.Expect(test.Panic, func() { SerializeInPlace(obj, data) }) }, }, { summary: "compound objects can be serialized in ", objPtr: &testType{}, data: []byte(` {"T": { "I": 1} }`), // data: []byte(` {"T": {"Type": "serialization.testType", "B": true } }`), testFn: func(c *test.Chkr, obj, data interface{}) { tf1 := testType{ T: &testType{ B: true, }, } b, err := json.MarshalIndent(tf1, " ", " ") if err != nil { panic(err) } fmt.Println(string(b)) tf2 := testType{} err = json.Unmarshal(b, &tf2) if err != nil { panic(err) } fmt.Println(tf2, *tf2.T) // SerializeInPlace(obj, data) // typ := obj.(*testType) c.Assert(test.True, false, "not finished") // c.Expect(test.EQ, true, typ.T.B) }, }, } for i, tt := range tests { c := test.Checker(t, test.Summary("with test %v: %v", i, tt.summary)) holder, err := support.ReadData(tt.data) if err != nil { panic(err) } tt.testFn(c, tt.objPtr, holder) } }