Ejemplo n.º 1
0
func MessageEqualsSpec(c gospec.Context) {
	msg0 := getTestMessage()
	msg1Real := *msg0
	msg1 := &msg1Real

	c.Specify("Messages are equal", func() {
		c.Expect(msg0, gs.Equals, msg1)
	})

	c.Specify("Messages w/ diff int values are not equal", func() {
		msg1.Severity--
		c.Expect(msg0, gs.Not(gs.Equals), msg1)
	})

	c.Specify("Messages w/ diff string values are not equal", func() {
		msg1.Payload = "Something completely different"
		c.Expect(msg0, gs.Not(gs.Equals), msg1)
	})

	c.Specify("Messages w/ diff maps are not equal", func() {
		msg1.Fields = map[string]interface{}{"sna": "foo"}
		c.Expect(msg0, gs.Not(gs.Equals), msg1)
	})
}
Ejemplo n.º 2
0
func BasicTypeRegistrySpec(c gospec.Context) {
	c.Specify("Test the coder.", func() {
		var tr core.TypeRegistry
		tr.Register(encodable1{})
		tr.Register(encodable2{})
		tr.Register(encodable3([]byte{}))
		tr.Register(encodable4{})
		tr.Register(encodable5{})
		tr.Register(encodable7{})
		tr.Register(encodable8{})
		tr.Register(encodable9{})
		tr.Register(encodable10{})
		tr.Register(encodable11{})
		tr.Register(encodable12{})
		tr.Register(encodable13{})
		tr.Register(encodable14{})
		tr.Complete()

		c.Specify("Test that encoder handles int32.", func() {
			e1 := encodable1{1, 2}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e1, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec1, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d1, ok := dec1.(encodable1)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec1.(ider).id(), gospec.Equals, 1)
			c.Expect(d1, gospec.Equals, e1)
		})

		c.Specify("Test that encoder handles byte.", func() {
			e2 := encodable2{14}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e2, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec2, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d2, ok := dec2.(encodable2)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec2.(ider).id(), gospec.Equals, 2)
			c.Expect(d2, gospec.Equals, e2)
		})

		c.Specify("Test that encoder handles string.", func() {
			e3 := encodable3("fudgeball")
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e3, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec3, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d3, ok := dec3.(encodable3)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec3.(ider).id(), gospec.Equals, 3)
			c.Expect(string(d3), gospec.Equals, string(e3))
		})

		c.Specify("Test that encoder handles composite structs.", func() {
			e4 := encodable4{
				A: e4sub1{
					B: 10,
					C: 20,
					D: e4sub2{
						E: "foo",
						F: "bar",
					},
				},
				G: 12345,
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e4, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec4, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d4, ok := dec4.(encodable4)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec4.(ider).id(), gospec.Equals, 4)
			c.Expect(d4, gospec.Equals, e4)
		})

		c.Specify("Test that encoder handles []byte.", func() {
			e5 := encodable5{
				A: []byte("Monkeyball"),
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e5, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec5, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d5, ok := dec5.(encodable5)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec5.(ider).id(), gospec.Equals, 5)
			c.Expect(string(d5.A), gospec.Equals, string(e5.A))
		})

		c.Specify("Test that encoder handles int and uint.", func() {
			e7 := encodable7{
				A: 1,
				B: 2,
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e7, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec7, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d7, ok := dec7.(encodable7)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec7.(ider).id(), gospec.Equals, 7)
			c.Expect(string(d7.A), gospec.Equals, string(e7.A))
			c.Expect(string(d7.B), gospec.Equals, string(e7.B))
		})

		c.Specify("Test that encoder handles []int and []uint.", func() {
			e8 := encodable8{
				A: []int{1, 2, 3},
				B: []uint{6, 7, 8},
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e8, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec8, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d8, ok := dec8.(encodable8)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec8.(ider).id(), gospec.Equals, 8)
			c.Expect(fmt.Sprintf("%v", d8.A), gospec.Equals, fmt.Sprintf("%v", e8.A))
			c.Expect(fmt.Sprintf("%v", d8.B), gospec.Equals, fmt.Sprintf("%v", e8.B))
		})

		c.Specify("Test that encoder handles pointers.", func() {
			sub := e9sub{
				B: 123,
			}
			psub := &sub
			ppsub := &psub
			e9 := encodable9{
				A: &ppsub,
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e9, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec9, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d9, ok := dec9.(encodable9)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec9.(ider).id(), gospec.Equals, 9)
			c.Assume(d9.A, gospec.Not(gospec.Equals), (*e9sub)(nil))
			c.Expect((**d9.A).B, gospec.Equals, (**e9.A).B)
		})

		c.Specify("Test that encoder can ignore unexported fields.", func() {
			e10 := encodable10{
				A: 10,
				b: 12,
				C: "foo",
				d: "bar",
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e10, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec10, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d10, ok := dec10.(encodable10)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec10.(ider).id(), gospec.Equals, 10)
			c.Expect(d10.A, gospec.Equals, e10.A)
			c.Expect(d10.b, gospec.Equals, int32(0))
			c.Expect(d10.C, gospec.Equals, e10.C)
			c.Expect(d10.d, gospec.Equals, "")
		})

		c.Specify("Test that encoder can handle nil pointers.", func() {
			e11 := encodable11{}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e11, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec11, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d11, ok := dec11.(encodable11)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec11.(ider).id(), gospec.Equals, 11)
			c.Expect(d11.A, gospec.Equals, (*e11sub1)(nil))
			c.Expect(d11.B, gospec.Equals, (*e11sub2)(nil))
			c.Expect(d11.C, gospec.Equals, (Barer)(nil))
		})

		c.Specify("Test that encoder can handle aliased primitives.", func() {
			e12 := encodable12{A: 123, B: 456}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e12, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec12, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d12, ok := dec12.(encodable12)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec12.(ider).id(), gospec.Equals, 12)
			c.Expect(d12.A, gospec.Equals, (LooksLikeAnInt)(123))
			c.Expect(d12.B, gospec.Equals, (LooksLikeAUInt)(456))
		})

		c.Specify("Test that encoder can handle slices of nil values.", func() {
			ints := make([]*int, 5)
			for i := range ints {
				n := i
				ints[i] = &n
			}
			e13 := encodable13{A: make([]*int, 5), B: ints}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e13, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec13, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d13, ok := dec13.(encodable13)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec13.(ider).id(), gospec.Equals, 13)
			c.Expect(len(d13.A), gospec.Equals, len(e13.A))
			for i := range d13.A {
				c.Expect(d13.A[i], gospec.Equals, e13.A[i])
			}
			c.Expect(len(d13.B), gospec.Equals, len(e13.B))
			for i := range d13.B {
				c.Expect(*d13.B[i], gospec.Equals, *e13.B[i])
			}
		})

		c.Specify("Test that encoder can handle bools.", func() {
			e14 := encodable14{A: true, B: false}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e14, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec14, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d14, ok := dec14.(encodable14)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec14.(ider).id(), gospec.Equals, 14)
			c.Expect(d14.A, gospec.Equals, e14.A)
			c.Expect(d14.B, gospec.Equals, e14.B)
		})
	})
}