コード例 #1
0
ファイル: struct_test.go プロジェクト: hodduc/go-capnproto
func TestObjectWithListOfTextBetweenSegments(t *testing.T) {

	exp := CapnpEncode(`(counter = (size = 9, wordlist = ["hello","bye"]))`, "Bag")

	cv.Convey("Given an Counter in one segment and a Bag with text in another", t, func() {
		cv.Convey("we should be able to copy from one segment to the other with SetCounter() on a Bag", func() {

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

			// in seg
			segbag := air.NewRootBag(seg)

			// in scratch
			xc := air.NewRootCounter(scratch)
			xc.SetSize(9)
			tl := scratch.NewTextList(2)
			tl.Set(0, "hello")
			tl.Set(1, "bye")
			xc.SetWordlist(tl)

			xbuf := bytes.Buffer{}
			scratch.WriteTo(&xbuf)

			x := xbuf.Bytes()
			save(x, "myscratch")
			fmt.Printf("scratch segment (%p):\n", scratch)
			ShowBytes(x, 10)
			fmt.Printf("scratch segment (%p) with Counter decoded by capnp: '%s'\n", scratch, string(CapnpDecode(x, "Counter")))

			prebuf := bytes.Buffer{}
			seg.WriteTo(&prebuf)
			fmt.Printf("Bag only segment seg (%p), pre-transfer:\n", seg)
			ShowBytes(prebuf.Bytes(), 10)

			// now for the actual test:
			// copy from scratch to seg
			segbag.SetCounter(xc)

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

			act := buf.Bytes()
			save(act, "myact")
			save(exp, "myexp")

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

			fmt.Printf("          actual:\n")
			ShowBytes(act, 10)
			fmt.Printf("act decoded by capnp: '%s'\n", string(CapnpDecode(act, "Bag")))

			cv.So(act, cv.ShouldResemble, exp)
		})
	})
}
コード例 #2
0
ファイル: struct_test.go プロジェクト: hodduc/go-capnproto
func TestSetBetweenSegments(t *testing.T) {

	exp := CapnpEncode(`(counter = (size = 9, words = "abc", wordlist = ["hello","byenow"]))`, "Bag")

	cv.Convey("Given an struct with Text and List(Text) in one segment", t, func() {
		cv.Convey("assigning it to a struct in a different segment should recursively import", func() {

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

			// in seg
			segbag := air.NewRootBag(seg)

			// in scratch
			xc := air.NewRootCounter(scratch)
			xc.SetSize(9)
			tl := scratch.NewTextList(2)
			tl.Set(0, "hello")
			tl.Set(1, "byenow")
			xc.SetWordlist(tl)
			xc.SetWords("abc")

			fmt.Printf("\n\n starting copy from scratch to seg \n\n")

			// copy from scratch to seg
			segbag.SetCounter(xc)

			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, "Bag")))
			save(act, "myact")

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

			cv.So(act, cv.ShouldResemble, exp)
		})
	})
}
コード例 #3
0
ファイル: struct_test.go プロジェクト: hodduc/go-capnproto
func TestObjectWithTextBetweenSegments(t *testing.T) {

	exp := CapnpEncode(`(counter = (size = 9, words = "hello"))`, "Bag")

	cv.Convey("Given an Counter in one segment and a Bag with text in another", t, func() {
		cv.Convey("we should be able to copy from one segment to the other with SetCounter() on a Bag", func() {

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

			// in seg
			segbag := air.NewRootBag(seg)

			// in scratch
			xc := air.NewRootCounter(scratch)
			xc.SetSize(9)
			xc.SetWords("hello")

			// copy from scratch to seg
			segbag.SetCounter(xc)

			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, "Bag")))
			save(act, "myact")

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

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