-
Notifications
You must be signed in to change notification settings - Fork 12
/
marshal_test.go
88 lines (75 loc) · 2.11 KB
/
marshal_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
package main
import (
"testing"
cv "github.com/glycerine/goconvey/convey"
)
func TestCapnpStructNaming(t *testing.T) {
cv.Convey("Given go structs that we will marshal into capnp structs", t, func() {
cv.Convey("then the names of the two types in go code should be distinct: Cpn suffix attached to the capnp structs", func() {
ex0 := `
type Extra struct {
A int
}`
cv.So(ExtractString2String(ex0), ShouldStartWithModuloWhiteSpace, `struct ExtraCapn { a @0: Int64; } `)
})
})
}
func TestMarshal(t *testing.T) {
cv.Convey("Given go struct Extra", t, func() {
cv.Convey("then the generated ExtraCapntoGo() code should copy content from an ExtraCapn to an Extra struct.", func() {
cv.Convey("and should handle int fields", func() {
ex0 := `
type Extra struct {
A int
B int
}`
toGoCode := ExtractCapnToGoCode(ex0, "Extra")
cv.So(toGoCode, ShouldMatchModuloWhiteSpace, `
func ExtraCapnToGo(src ExtraCapn, dest *Extra) *Extra {
if dest == nil {
dest = &Extra{}
}
dest.A = int(src.A())
dest.B = int(src.B())
return dest }
`)
toCapnCode := ExtractGoToCapnCode(ex0, "Extra")
cv.So(toCapnCode, ShouldMatchModuloWhiteSpace, `
func ExtraGoToCapn(seg *capn.Segment, src *Extra) ExtraCapn {
dest := AutoNewExtraCapn(seg)
dest.SetA(int64(src.A))
dest.SetB(int64(src.B))
return dest }
`)
})
cv.Convey("and should handle uint fields", func() {
ex0 := `
type Extra struct {
A uint64
}`
toGoCode := ExtractCapnToGoCode(ex0, "Extra")
cv.So(toGoCode, ShouldMatchModuloWhiteSpace, `
func ExtraCapnToGo(src ExtraCapn, dest *Extra) *Extra {
if dest == nil {
dest = &Extra{}
}
dest.A = src.A()
return dest }
`)
toCapnCode := ExtractGoToCapnCode(ex0, "Extra")
cv.So(toCapnCode, ShouldMatchModuloWhiteSpace, `
func ExtraGoToCapn(seg *capn.Segment, src *Extra) ExtraCapn {
dest := AutoNewExtraCapn(seg)
dest.SetA(src.A)
return dest }
`)
})
})
})
}
func TestUnMarshal(t *testing.T) {
cv.Convey("Given go structs", t, func() {
cv.Convey("then the generated Unmarshal() code should copy from the capnp into the go structs", func() {
})
})
}