github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/goagen/codegen/publicizer_test.go (about) 1 package codegen_test 2 3 import ( 4 "fmt" 5 6 "github.com/goadesign/goa/design" 7 "github.com/goadesign/goa/goagen/codegen" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("Struct publicize code generation", func() { 13 Describe("Publicizer", func() { 14 var att *design.AttributeDefinition 15 var sourceField, targetField string 16 Context("given a simple field", func() { 17 BeforeEach(func() { 18 att = &design.AttributeDefinition{Type: design.Integer} 19 sourceField = "source" 20 targetField = "target" 21 }) 22 Context("with init false", func() { 23 It("simply copies the field over", func() { 24 publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false) 25 Ω(publication).Should(Equal(fmt.Sprintf("%s = %s", targetField, sourceField))) 26 }) 27 }) 28 Context("with init true", func() { 29 It("initializes and copies the field copies the field over", func() { 30 publication := codegen.Publicizer(att, sourceField, targetField, false, 0, true) 31 Ω(publication).Should(Equal(fmt.Sprintf("%s := %s", targetField, sourceField))) 32 }) 33 }) 34 }) 35 Context("given an object field", func() { 36 BeforeEach(func() { 37 att = &design.AttributeDefinition{ 38 Type: design.Object{ 39 "foo": &design.AttributeDefinition{Type: design.String}, 40 }, 41 } 42 sourceField = "source" 43 targetField = "target" 44 }) 45 It("copies the struct fields", func() { 46 publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false) 47 Ω(publication).Should(Equal(objectPublicizeCode)) 48 }) 49 }) 50 Context("given a user type", func() { 51 BeforeEach(func() { 52 att = &design.AttributeDefinition{ 53 Type: &design.UserTypeDefinition{ 54 AttributeDefinition: &design.AttributeDefinition{ 55 Type: &design.Object{ 56 "foo": &design.AttributeDefinition{Type: design.String}, 57 }, 58 }, 59 TypeName: "TheUserType", 60 }, 61 } 62 sourceField = "source" 63 targetField = "target" 64 }) 65 It("calls Publicize on the source field", func() { 66 publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false) 67 Ω(publication).Should(Equal(fmt.Sprintf("%s = %s.Publicize()", targetField, sourceField))) 68 }) 69 }) 70 Context("given an array field", func() { 71 Context("that contains primitive fields", func() { 72 BeforeEach(func() { 73 att = &design.AttributeDefinition{ 74 Type: &design.Array{ 75 ElemType: &design.AttributeDefinition{ 76 Type: design.String, 77 }, 78 }, 79 } 80 sourceField = "source" 81 targetField = "target" 82 }) 83 It("copies the array fields", func() { 84 publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false) 85 Ω(publication).Should(Equal(fmt.Sprintf("%s = %s", targetField, sourceField))) 86 }) 87 }) 88 Context("that contains user defined fields", func() { 89 BeforeEach(func() { 90 att = &design.AttributeDefinition{ 91 Type: &design.Array{ 92 ElemType: &design.AttributeDefinition{ 93 Type: &design.UserTypeDefinition{ 94 AttributeDefinition: &design.AttributeDefinition{ 95 Type: design.Object{ 96 "foo": &design.AttributeDefinition{Type: design.String}, 97 }, 98 }, 99 TypeName: "TheUserType", 100 }, 101 }, 102 }, 103 } 104 sourceField = "source" 105 targetField = "target" 106 }) 107 It("copies the array fields", func() { 108 publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false) 109 Ω(publication).Should(Equal(arrayPublicizeCode)) 110 }) 111 }) 112 }) 113 Context("given a hash field", func() { 114 Context("that contains primitive fields", func() { 115 BeforeEach(func() { 116 att = &design.AttributeDefinition{ 117 Type: &design.Hash{ 118 KeyType: &design.AttributeDefinition{ 119 Type: design.String, 120 }, 121 ElemType: &design.AttributeDefinition{ 122 Type: design.String, 123 }, 124 }, 125 } 126 sourceField = "source" 127 targetField = "target" 128 }) 129 It("copies the hash fields", func() { 130 publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false) 131 Ω(publication).Should(Equal(fmt.Sprintf("%s = %s", targetField, sourceField))) 132 }) 133 }) 134 Context("that contains user defined fields", func() { 135 BeforeEach(func() { 136 att = &design.AttributeDefinition{ 137 Type: &design.Hash{ 138 KeyType: &design.AttributeDefinition{ 139 Type: &design.UserTypeDefinition{ 140 AttributeDefinition: &design.AttributeDefinition{ 141 Type: &design.Object{ 142 "foo": &design.AttributeDefinition{Type: design.String}, 143 }, 144 }, 145 TypeName: "TheKeyType", 146 }, 147 }, 148 ElemType: &design.AttributeDefinition{ 149 Type: &design.UserTypeDefinition{ 150 AttributeDefinition: &design.AttributeDefinition{ 151 Type: &design.Object{ 152 "bar": &design.AttributeDefinition{Type: design.String}, 153 }, 154 }, 155 TypeName: "TheElemType", 156 }, 157 }, 158 }, 159 } 160 sourceField = "source" 161 targetField = "target" 162 }) 163 It("copies the hash fields", func() { 164 publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false) 165 Ω(publication).Should(Equal(hashPublicizeCode)) 166 }) 167 }) 168 }) 169 }) 170 }) 171 172 const ( 173 objectPublicizeCode = `target = &struct { 174 Foo *string ` + "`" + `form:"foo,omitempty" json:"foo,omitempty" xml:"foo,omitempty"` + "`" + ` 175 }{} 176 if source.Foo != nil { 177 target.Foo = source.Foo 178 }` 179 180 arrayPublicizeCode = `target = make([]*TheUserType, len(source)) 181 for i0, elem0 := range source { 182 target[i0] = elem0.Publicize() 183 }` 184 185 hashPublicizeCode = `target = make(map[*TheKeyType]*TheElemType, len(source)) 186 for k0, v0 := range source { 187 var pubk0 *TheKeyType 188 if k0 != nil { 189 pubk0 = k0.Publicize() 190 } 191 var pubv0 *TheElemType 192 if v0 != nil { 193 pubv0 = v0.Publicize() 194 } 195 target[pubk0] = pubv0 196 }` 197 )