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