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

	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	airc := air.AutoNewAircraft(seg)
	b737 := air.AutoNewB737(seg)
	base := air.AutoNewPlaneBase(seg)
	base.SetName("helen")
	base.SetMaxSpeed(0.5)
	homes := air.NewAirportList(seg, 2)
	homes.Set(0, air.AIRPORT_JFK)
	homes.Set(1, air.AIRPORT_SFO)
	base.SetHomes(homes)
	b737.SetBase(base)
	airc.SetB737(b737)
	z.SetAircraft(airc)
	lit, err := z.MarshalCapLit()
	panicOn(err)
	json, err := z.MarshalJSON()
	panicOn(err)

	cv.Convey("Given the aircraftlib schema (and an Aircraft value), we should generate a MarshalCapLit() function that returns a literal representation in bytes for the given Aircraft value. And the MarshalJSON() should return the expected format too.", t, func() {
		cv.So(string(lit), cv.ShouldEqual, `(aircraft = (b737 = (base = (name = "helen", homes = [jfk, sfo], rating = 0, canFly = false, capacity = 0, maxSpeed = 0.5))))`)
		cv.So(string(json), cv.ShouldEqual, `{"aircraft":{"b737":{"base":{"name":"helen","homes":["jfk", "sfo"],"rating":0,"canFly":false,"capacity":0,"maxSpeed":0.5}}}}`)
	})

}
コード例 #2
0
ファイル: enum_test.go プロジェクト: hodduc/go-capnproto
// For enums like Airport, their lists should have a set method
func TestEnumListHasSet(t *testing.T) {

	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	airc := air.AutoNewAircraft(seg)
	b737 := air.AutoNewB737(seg)
	base := air.AutoNewPlaneBase(seg)
	base.SetName("helen")
	homes := air.NewAirportList(seg, 2)

	// test is here!!
	// these next two lines should compile, because there should be a Set method.
	homes.Set(0, air.AIRPORT_JFK)
	homes.Set(1, air.AIRPORT_SFO)
	base.SetHomes(homes)
	b737.SetBase(base)
	airc.SetB737(b737)
	z.SetAircraft(airc)
	j, err := z.MarshalCapLit()
	panicOn(err)

	cv.Convey("To confirm that enum lists have a Set() method: Given the aircraftlib schema (and an Aircraft value), we should generate a MarshalCapLit() function that returns a literal representation in bytes for the given Aircraft value ", t, func() {
		cv.So(string(j), cv.ShouldEqual, `(aircraft = (b737 = (base = (name = "helen", homes = [jfk, sfo], rating = 0, canFly = false, capacity = 0, maxSpeed = 0))))`)
	})

}
コード例 #3
0
ファイル: write_test.go プロジェクト: hodduc/go-capnproto
func ExampleAirplaneWrite() string {

	fname := "out.write_test.airplane.cpz"

	// make a brand new, empty segment (message)
	seg := capn.NewBuffer(nil)

	// If you want runtime-type identification, this is easily obtained. Just
	// wrap everything in a struct that contains a single anoymous union (e.g. struct Z).
	// Then always set a Z as the root object in you message/first segment.
	// The cost of the extra word of storage is usually worth it, as
	// then human readable output is easily obtained via a shell command such as
	//
	// $ cat binary.cpz | capnp decode aircraft.capnp Z
	//
	// If you need to conserve space, and know your content in advance, it
	// isn't necessary to use an anonymous union. Just supply the type name
	// in place of 'Z' in the decode command above.

	z := air.NewRootZ(seg) // root should be allocated first.
	// There must be only one root.

	// then non-root objects:
	aircraft := air.NewAircraft(seg)
	b737 := air.NewB737(seg)
	planebase := air.NewPlaneBase(seg)

	// how to create a list. Requires a cast at the moment.
	homes := air.NewAirportList(seg, 2)
	uint16list := capn.UInt16List(homes) // cast to the underlying type
	uint16list.Set(0, uint16(air.AIRPORT_JFK))
	uint16list.Set(1, uint16(air.AIRPORT_LAX))

	// set the primitive fields
	planebase.SetCanFly(true)
	planebase.SetName("Henrietta")
	planebase.SetRating(100)
	planebase.SetMaxSpeed(876) // km/hr
	// if we don't set capacity, it will get the default value, in this case 0.
	//planebase.SetCapacity(26020) // Liters fuel

	// set a list field
	planebase.SetHomes(homes)

	// wire up the pointers between objects
	b737.SetBase(planebase)
	aircraft.SetB737(b737)
	z.SetAircraft(aircraft)

	// ready to write

	// example of writing to memory
	buf := bytes.Buffer{}
	seg.WriteTo(&buf)

	// example of writing to file. Just use WriteTo().
	// We could have used SegToFile(seg, fname) from
	// util_test.go intead, but this makes it clear how easy it is.
	file, err := os.Create(fname)
	defer file.Close()
	if err != nil {
		panic(err)
	}
	seg.WriteTo(file)

	// readback and view that file in human readable format. Defined in util_test.go
	text, err := CapnFileToText(fname, "aircraftlib/aircraft.capnp", "")
	if err != nil {
		panic(err)
	}
	fmt.Printf("here is our aircraft:\n")
	fmt.Printf("%s\n", text)

	return text
}