github.com/chrislusf/greenpack@v3.7.1-0.20170911073826-ad5bd10b7c47+incompatible/cmd/addzid/old_bambam_tests/marshal_test.go (about) 1 package main 2 3 import ( 4 "testing" 5 6 cv "github.com/glycerine/goconvey/convey" 7 ) 8 9 func TestCapnpStructNaming(t *testing.T) { 10 11 cv.Convey("Given go structs that we will marshal into capnp structs", t, func() { 12 cv.Convey("then the names of the two types in go code should be distinct: Cpn suffix attached to the capnp structs", func() { 13 14 ex0 := ` 15 type Extra struct { 16 A int 17 }` 18 cv.So(ExtractString2String(ex0), ShouldStartWithModuloWhiteSpace, `struct ExtraCapn { a @0: Int64; } `) 19 }) 20 }) 21 } 22 23 func TestMarshal(t *testing.T) { 24 25 cv.Convey("Given go struct Extra", t, func() { 26 cv.Convey("then the generated ExtraCapntoGo() code should copy content from an ExtraCapn to an Extra struct.", func() { 27 cv.Convey("and should handle int fields", func() { 28 ex0 := ` 29 type Extra struct { 30 A int 31 B int 32 }` 33 toGoCode := ExtractCapnToGoCode(ex0, "Extra") 34 cv.So(toGoCode, ShouldMatchModuloWhiteSpace, ` 35 func ExtraCapnToGo(src ExtraCapn, dest *Extra) *Extra { 36 if dest == nil { 37 dest = &Extra{} 38 } 39 dest.A = int(src.A()) 40 dest.B = int(src.B()) 41 return dest } 42 `) 43 44 toCapnCode := ExtractGoToCapnCode(ex0, "Extra") 45 cv.So(toCapnCode, ShouldMatchModuloWhiteSpace, ` 46 func ExtraGoToCapn(seg *capn.Segment, src *Extra) ExtraCapn { 47 dest := AutoNewExtraCapn(seg) 48 dest.SetA(int64(src.A)) 49 dest.SetB(int64(src.B)) 50 return dest } 51 `) 52 53 }) 54 cv.Convey("and should handle uint fields", func() { 55 ex0 := ` 56 type Extra struct { 57 A uint64 58 }` 59 toGoCode := ExtractCapnToGoCode(ex0, "Extra") 60 cv.So(toGoCode, ShouldMatchModuloWhiteSpace, ` 61 func ExtraCapnToGo(src ExtraCapn, dest *Extra) *Extra { 62 if dest == nil { 63 dest = &Extra{} 64 } 65 dest.A = src.A() 66 return dest } 67 `) 68 69 toCapnCode := ExtractGoToCapnCode(ex0, "Extra") 70 cv.So(toCapnCode, ShouldMatchModuloWhiteSpace, ` 71 func ExtraGoToCapn(seg *capn.Segment, src *Extra) ExtraCapn { 72 dest := AutoNewExtraCapn(seg) 73 dest.SetA(src.A) 74 return dest } 75 `) 76 }) 77 }) 78 }) 79 } 80 81 func TestUnMarshal(t *testing.T) { 82 83 cv.Convey("Given go structs", t, func() { 84 cv.Convey("then the generated Unmarshal() code should copy from the capnp into the go structs", func() { 85 86 }) 87 }) 88 }