Esempio n. 1
0
func TestZserverWithAccessors(t *testing.T) {

	exp := CapnpEncode(`(waitingjobs = [(cmd = "abc"), (cmd = "xyz")])`, "Zserver")

	cv.Convey("Given an Zserver with a custom list", t, func() {
		cv.Convey("then all the accessors should work as expected", func() {

			seg := capn.NewBuffer(nil)
			scratch := capn.NewBuffer(nil)

			server := air.NewRootZserver(seg)

			joblist := air.NewZjobList(seg, 2)

			// .Set(int, item)
			zjob := air.NewZjob(scratch)
			zjob.SetCmd("abc")
			joblist.Set(0, zjob)

			zjob = air.NewZjob(scratch)
			zjob.SetCmd("xyz")
			joblist.Set(1, zjob)

			// .At(int)
			cv.So(joblist.At(0).Cmd(), cv.ShouldEqual, "abc")
			cv.So(joblist.At(1).Cmd(), cv.ShouldEqual, "xyz")

			// .Len()
			cv.So(joblist.Len(), cv.ShouldEqual, 2)

			// .ToArray()
			cv.So(len(joblist.ToArray()), cv.ShouldEqual, 2)
			cv.So(joblist.ToArray()[0].Cmd(), cv.ShouldEqual, "abc")
			cv.So(joblist.ToArray()[1].Cmd(), cv.ShouldEqual, "xyz")

			server.SetWaitingjobs(joblist)

			buf := bytes.Buffer{}
			seg.WriteTo(&buf)

			act := buf.Bytes()
			fmt.Printf("          actual:\n")
			ShowBytes(act, 10)
			fmt.Printf("act decoded by capnp: '%s'\n", string(CapnpDecode(act, "Zserver")))
			save(act, "myact")

			fmt.Printf("expected:\n")
			ShowBytes(exp, 10)
			fmt.Printf("exp decoded by capnp: '%s'\n", string(CapnpDecode(exp, "Zserver")))
			save(exp, "myexp")

			cv.So(act, cv.ShouldResemble, exp)
		})
	})
}
Esempio n. 2
0
func TestZserverWithOneEmptyJob(t *testing.T) {

	exp := CapnpEncode(`(waitingjobs = [()])`, "Zserver")

	cv.Convey("Given an Zserver with one empty job", t, func() {
		cv.Convey("then the go-capnproto serialization should match the capnp c++ serialization", func() {

			seg := capn.NewBuffer(nil)
			scratch := capn.NewBuffer(nil)
			server := air.NewRootZserver(seg)

			joblist := air.NewZjobList(seg, 1)
			plist := capn.PointerList(joblist)

			ShowSeg("          pre NewZjob, segment seg is:", seg)

			zjob := air.NewZjob(scratch)
			plist.Set(0, capn.Object(zjob))

			ShowSeg("          pre SetWaitingjobs, segment seg is:", seg)

			fmt.Printf("Then we do the SetWaitingjobs:\n")
			server.SetWaitingjobs(joblist)

			// save
			buf := bytes.Buffer{}
			seg.WriteTo(&buf)
			act := buf.Bytes()
			save(act, "my.act.zserver")

			// show
			ShowSeg("          actual:\n", seg)

			fmt.Printf("act decoded by capnp: '%s'\n", string(CapnpDecode(act, "Zserver")))

			fmt.Printf("expected:\n")
			ShowBytes(exp, 10)
			fmt.Printf("exp decoded by capnp: '%s'\n", string(CapnpDecode(exp, "Zserver")))

			cv.So(act, cv.ShouldResemble, exp)
		})
	})
}
Esempio n. 3
0
func TestZserverWithOneFullJob(t *testing.T) {

	exp := CapnpEncode(`(waitingjobs = [(cmd = "abc", args = ["xyz"])])`, "Zserver")

	cv.Convey("Given an Zserver with one empty job", t, func() {
		cv.Convey("then the go-capnproto serialization should match the capnp c++ serialization", func() {

			seg := capn.NewBuffer(nil)
			scratch := capn.NewBuffer(nil)

			server := air.NewRootZserver(seg)

			joblist := air.NewZjobList(seg, 1)
			plist := capn.PointerList(joblist)

			zjob := air.NewZjob(scratch)
			zjob.SetCmd("abc")
			tl := scratch.NewTextList(1)
			tl.Set(0, "xyz")
			zjob.SetArgs(tl)

			plist.Set(0, capn.Object(zjob))

			server.SetWaitingjobs(joblist)

			buf := bytes.Buffer{}
			seg.WriteTo(&buf)

			act := buf.Bytes()
			fmt.Printf("          actual:\n")
			ShowBytes(act, 10)
			fmt.Printf("act decoded by capnp: '%s'\n", string(CapnpDecode(act, "Zserver")))
			save(act, "myact")

			fmt.Printf("expected:\n")
			ShowBytes(exp, 10)
			fmt.Printf("exp decoded by capnp: '%s'\n", string(CapnpDecode(exp, "Zserver")))
			save(exp, "myexp")

			cv.So(act, cv.ShouldResemble, exp)
		})
	})
}