forked from jmckaskill/go-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_test.go
101 lines (80 loc) · 2.93 KB
/
write_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package capn_test
import (
"bytes"
"fmt"
"os"
"testing"
capn "github.com/glycerine/go-capnproto"
air "github.com/glycerine/go-capnproto/aircraftlib"
cv "github.com/smartystreets/goconvey/convey"
)
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 readalbe 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, air.AIRPORT_JFK)
uint16list.Set(1, 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
}
func TestAircraftWrite(t *testing.T) {
observedText := ExampleAirplaneWrite()
expectedText := `(aircraft = (b737 = (base = (name = "Henrietta", homes = [jfk, lax], rating = 100, canFly = true, capacity = 0, maxSpeed = 876))))`
cv.Convey("When we run the ExampleAirplaneWrite() function in write_test.go", t, func() {
cv.Convey("Then we should see the human readable B737 example struct we expect", func() {
cv.So(observedText, cv.ShouldEqual, expectedText)
})
})
}