github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/goagen/codegen/import_spec_test.go (about) 1 package codegen_test 2 3 import ( 4 . "github.com/goadesign/goa/design" 5 . "github.com/goadesign/goa/design/apidsl" 6 "github.com/goadesign/goa/dslengine" 7 "github.com/goadesign/goa/goagen/codegen" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("AttributeImports", func() { 13 Context("given an attribute definition with fields", func() { 14 var att *AttributeDefinition 15 var st string 16 var object Object 17 18 Context("of object", func() { 19 20 It("produces the import slice", func() { 21 var imports []*codegen.ImportSpec 22 object = Object{ 23 "foo": &AttributeDefinition{Type: String}, 24 "bar": &AttributeDefinition{Type: Integer}, 25 } 26 object["foo"].Metadata = dslengine.MetadataDefinition{ 27 "struct:field:type": []string{"json.RawMessage", "encoding/json"}, 28 } 29 att = new(AttributeDefinition) 30 att.Type = object 31 imports = codegen.AttributeImports(att, imports, nil) 32 33 i := []*codegen.ImportSpec{&codegen.ImportSpec{ 34 Path: "encoding/json", 35 }, 36 } 37 st = i[0].Path 38 39 Ω(st).Should(Equal(imports[0].Path)) 40 }) 41 }) 42 43 Context("of recursive object", func() { 44 45 It("produces the import slice", func() { 46 var imports []*codegen.ImportSpec 47 o := Object{ 48 "foo": &AttributeDefinition{Type: String}, 49 } 50 o["foo"].Metadata = dslengine.MetadataDefinition{ 51 "struct:field:type": []string{"json.RawMessage", "encoding/json"}, 52 } 53 child := &AttributeDefinition{Type: o} 54 55 po := Object{"child": child} 56 po["child"].Metadata = dslengine.MetadataDefinition{ 57 "struct:field:type": []string{"json.RawMessage", "encoding/json"}, 58 } 59 parent := &AttributeDefinition{Type: po} 60 61 o["parent"] = parent 62 63 att = new(AttributeDefinition) 64 att.Type = po 65 imports = codegen.AttributeImports(att, imports, nil) 66 67 i := []*codegen.ImportSpec{&codegen.ImportSpec{ 68 Path: "encoding/json", 69 }, 70 } 71 st = i[0].Path 72 l := len(imports) 73 74 Ω(st).Should(Equal(imports[0].Path)) 75 Ω(l).Should(Equal(1)) 76 }) 77 }) 78 79 Context("of hash", func() { 80 81 It("produces the import slice", func() { 82 var imports []*codegen.ImportSpec 83 elemType := &AttributeDefinition{Type: Integer} 84 elemType.Metadata = dslengine.MetadataDefinition{ 85 "struct:field:type": []string{"json.RawMessage", "encoding/json"}, 86 } 87 keyType := &AttributeDefinition{Type: Integer} 88 elemType.Metadata = dslengine.MetadataDefinition{ 89 "struct:field:type": []string{"json.RawMessage", "encoding/json"}, 90 } 91 hash := &Hash{KeyType: keyType, ElemType: elemType} 92 93 att = new(AttributeDefinition) 94 att.Type = hash 95 imports = codegen.AttributeImports(att, imports, nil) 96 97 i := []*codegen.ImportSpec{&codegen.ImportSpec{ 98 Path: "encoding/json", 99 }, 100 } 101 st = i[0].Path 102 l := len(imports) 103 104 Ω(st).Should(Equal(imports[0].Path)) 105 Ω(l).Should(Equal(1)) 106 }) 107 }) 108 109 Context("of array", func() { 110 It("produces the import slice", func() { 111 var imports []*codegen.ImportSpec 112 elemType := &AttributeDefinition{Type: Integer} 113 elemType.Metadata = dslengine.MetadataDefinition{ 114 "struct:field:type": []string{"json.RawMessage", "encoding/json"}, 115 } 116 array := &Array{ElemType: elemType} 117 118 att = new(AttributeDefinition) 119 att.Type = array 120 imports = codegen.AttributeImports(att, imports, nil) 121 122 i := []*codegen.ImportSpec{&codegen.ImportSpec{ 123 Path: "encoding/json", 124 }, 125 } 126 st = i[0].Path 127 128 Ω(st).Should(Equal(imports[0].Path)) 129 }) 130 }) 131 132 Context("of UserTypeDefinition", func() { 133 134 It("produces the import slice", func() { 135 var imports []*codegen.ImportSpec 136 object = Object{ 137 "bar": &AttributeDefinition{Type: String}, 138 } 139 object["bar"].Metadata = dslengine.MetadataDefinition{ 140 "struct:field:type": []string{"json.RawMessage", "encoding/json"}, 141 } 142 143 u := &UserTypeDefinition{ 144 AttributeDefinition: &AttributeDefinition{Type: object}, 145 } 146 147 att = u.AttributeDefinition 148 imports = codegen.AttributeImports(att, imports, nil) 149 150 i := []*codegen.ImportSpec{&codegen.ImportSpec{ 151 Path: "encoding/json", 152 }, 153 } 154 st = i[0].Path 155 156 Ω(st).Should(Equal(imports[0].Path)) 157 }) 158 }) 159 160 Context("of MediaTypeDefinition", func() { 161 It("produces the import slice", func() { 162 var imports []*codegen.ImportSpec 163 elemType := &AttributeDefinition{Type: Integer} 164 elemType.Metadata = dslengine.MetadataDefinition{ 165 "struct:field:type": []string{"json.RawMessage", "encoding/json"}, 166 } 167 array := &Array{ElemType: elemType} 168 u := &UserTypeDefinition{ 169 AttributeDefinition: &AttributeDefinition{Type: array}, 170 } 171 m := &MediaTypeDefinition{ 172 UserTypeDefinition: u, 173 } 174 175 att = m.AttributeDefinition 176 imports = codegen.AttributeImports(att, imports, nil) 177 178 i := []*codegen.ImportSpec{&codegen.ImportSpec{ 179 Path: "encoding/json", 180 }, 181 } 182 st = i[0].Path 183 184 Ω(st).Should(Equal(imports[0].Path)) 185 }) 186 }) 187 }) 188 })