github.com/chrislusf/greenpack@v3.7.1-0.20170911073826-ad5bd10b7c47+incompatible/cmd/addzid/old_bambam_tests/struct1_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  
     6  	cv "github.com/glycerine/goconvey/convey"
     7  )
     8  
     9  type EmptyStruct struct {
    10  }
    11  type OneStruct struct {
    12  	One int
    13  }
    14  type HalfPrivStruct struct {
    15  	One int
    16  	two int
    17  }
    18  
    19  type NestingStruct struct {
    20  	One    int
    21  	Nester OneStruct
    22  }
    23  
    24  type EmbeddingStruct struct {
    25  	OneStruct
    26  }
    27  
    28  // An example struct with a comment
    29  // second line of comment.
    30  /*
    31   C comments attached too
    32  */
    33  type ExampleStruct1 struct {
    34  	Str string
    35  	N   int
    36  }
    37  
    38  func TestSimpleStructExtraction(t *testing.T) {
    39  
    40  	cv.Convey("Given a parsable golang source file", t, func() {
    41  		cv.Convey("then we can extract an empty struct", func() {
    42  
    43  			ex0 := `
    44  type Empty1 struct {
    45  }`
    46  			cv.So(ExtractString2String(ex0), ShouldStartWithModuloWhiteSpace, `struct Empty1Capn { } `)
    47  
    48  		})
    49  		cv.Convey("then we can extract simple structs, without recursion or embedding", func() {
    50  
    51  			ex1 := `
    52  type ExampleStruct1 struct {
    53  	Str string
    54  	N   int
    55  }`
    56  			cv.So(ExtractString2String(ex1), ShouldStartWithModuloWhiteSpace, `struct ExampleStruct1Capn { str @0: Text; n @1: Int64; } `)
    57  		})
    58  		cv.Convey("then we can extract structs that have other named structs inside", func() {
    59  
    60  			exNest := `
    61  type OneStruct struct {
    62  	One int
    63  }
    64  type NestingStruct struct {
    65  	One    int
    66  	Nester OneStruct
    67  }`
    68  			cv.So(ExtractString2String(exNest), ShouldStartWithModuloWhiteSpace, `struct NestingStructCapn { one @0: Int64; nester @1: OneStructCapn; } struct OneStructCapn { one @0: Int64; }  `)
    69  
    70  		})
    71  
    72  	})
    73  }
    74  
    75  func TestEmbedded(t *testing.T) {
    76  
    77  	cv.Convey("Given a parsable golang source file", t, func() {
    78  		cv.Convey("then we can extract structs with anonymous (embedded) structs inside", func() {
    79  
    80  			exEmbed := `
    81  type OneStruct struct {
    82  	One int
    83  }
    84  type EmbedsOne struct {
    85  	OneStruct
    86  }`
    87  
    88  			cv.So(ExtractString2String(exEmbed), ShouldStartWithModuloWhiteSpace, `struct EmbedsOneCapn { oneStruct @0: OneStructCapn; } struct OneStructCapn { one @0: Int64; } `)
    89  
    90  		})
    91  
    92  	})
    93  }