github.com/shogo82148/goa-v1@v1.6.2/design/apidsl/type_test.go (about)

     1  package apidsl_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo"
     5  	. "github.com/onsi/gomega"
     6  	"github.com/shogo82148/goa-v1/design"
     7  	"github.com/shogo82148/goa-v1/design/apidsl"
     8  	"github.com/shogo82148/goa-v1/dslengine"
     9  )
    10  
    11  var _ = Describe("Type", func() {
    12  	var name string
    13  	var dsl func()
    14  
    15  	var ut *design.UserTypeDefinition
    16  
    17  	BeforeEach(func() {
    18  		dslengine.Reset()
    19  		name = ""
    20  		dsl = nil
    21  	})
    22  
    23  	JustBeforeEach(func() {
    24  		apidsl.Type(name, dsl)
    25  		dslengine.Run()
    26  		ut = design.Design.Types[name]
    27  	})
    28  
    29  	Context("with no dsl and no name", func() {
    30  		It("produces an invalid type definition", func() {
    31  			Ω(ut).ShouldNot(BeNil())
    32  			Ω(ut.Validate("test", design.Design)).Should(HaveOccurred())
    33  		})
    34  	})
    35  
    36  	Context("with no dsl", func() {
    37  		BeforeEach(func() {
    38  			name = "foo"
    39  		})
    40  
    41  		It("produces a valid type definition", func() {
    42  			Ω(ut).ShouldNot(BeNil())
    43  			Ω(ut.Validate("test", design.Design)).ShouldNot(HaveOccurred())
    44  		})
    45  	})
    46  
    47  	Context("with attributes", func() {
    48  		const attName = "att"
    49  
    50  		BeforeEach(func() {
    51  			name = "foo"
    52  			dsl = func() {
    53  				apidsl.Attribute(attName)
    54  			}
    55  		})
    56  
    57  		It("sets the attributes", func() {
    58  			Ω(ut).ShouldNot(BeNil())
    59  			Ω(ut.Validate("test", design.Design)).ShouldNot(HaveOccurred())
    60  			Ω(ut.AttributeDefinition).ShouldNot(BeNil())
    61  			Ω(ut.Type).Should(BeAssignableToTypeOf(design.Object{}))
    62  			o := ut.Type.(design.Object)
    63  			Ω(o).Should(HaveLen(1))
    64  			Ω(o).Should(HaveKey(attName))
    65  		})
    66  	})
    67  
    68  	Context("with a name and uuid datatype", func() {
    69  		const attName = "att"
    70  		BeforeEach(func() {
    71  			name = "foo"
    72  			dsl = func() {
    73  				apidsl.Attribute(attName, design.UUID)
    74  			}
    75  		})
    76  
    77  		It("produces an attribute of date type", func() {
    78  			Ω(ut).ShouldNot(BeNil())
    79  			Ω(ut.Validate("test", design.Design)).ShouldNot(HaveOccurred())
    80  			Ω(ut.AttributeDefinition).ShouldNot(BeNil())
    81  			Ω(ut.Type).Should(BeAssignableToTypeOf(design.Object{}))
    82  			o := ut.Type.(design.Object)
    83  			Ω(o).Should(HaveLen(1))
    84  			Ω(o).Should(HaveKey(attName))
    85  			Ω(o[attName].Type).Should(Equal(design.UUID))
    86  		})
    87  	})
    88  
    89  	Context("with a name and date datatype", func() {
    90  		const attName = "att"
    91  		BeforeEach(func() {
    92  			name = "foo"
    93  			dsl = func() {
    94  				apidsl.Attribute(attName, design.DateTime)
    95  			}
    96  		})
    97  
    98  		It("produces an attribute of date type", func() {
    99  			Ω(ut).ShouldNot(BeNil())
   100  			Ω(ut.Validate("test", design.Design)).ShouldNot(HaveOccurred())
   101  			Ω(ut.AttributeDefinition).ShouldNot(BeNil())
   102  			Ω(ut.Type).Should(BeAssignableToTypeOf(design.Object{}))
   103  			o := ut.Type.(design.Object)
   104  			Ω(o).Should(HaveLen(1))
   105  			Ω(o).Should(HaveKey(attName))
   106  			Ω(o[attName].Type).Should(Equal(design.DateTime))
   107  		})
   108  	})
   109  })
   110  
   111  var _ = Describe("ArrayOf", func() {
   112  	Context("used on a global variable", func() {
   113  		var (
   114  			ut *design.UserTypeDefinition
   115  			ar *design.Array
   116  		)
   117  		BeforeEach(func() {
   118  			dslengine.Reset()
   119  			ut = apidsl.Type("example", func() {
   120  				apidsl.Attribute("id")
   121  			})
   122  			ar = apidsl.ArrayOf(ut)
   123  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   124  		})
   125  
   126  		JustBeforeEach(func() {
   127  			dslengine.Run()
   128  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   129  		})
   130  
   131  		It("produces a array type", func() {
   132  			Ω(ar).ShouldNot(BeNil())
   133  			Ω(ar.Kind()).Should(Equal(design.ArrayKind))
   134  			Ω(ar.ElemType.Type).Should(Equal(ut))
   135  		})
   136  	})
   137  
   138  	Context("with a DSL", func() {
   139  		var (
   140  			pattern = "foo"
   141  			ar      *design.Array
   142  		)
   143  
   144  		BeforeEach(func() {
   145  			dslengine.Reset()
   146  			ar = apidsl.ArrayOf(design.String, func() {
   147  				apidsl.Pattern(pattern)
   148  			})
   149  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   150  		})
   151  
   152  		It("records the validations", func() {
   153  			Ω(ar).ShouldNot(BeNil())
   154  			Ω(ar.Kind()).Should(Equal(design.ArrayKind))
   155  			Ω(ar.ElemType.Type).Should(Equal(design.String))
   156  			Ω(ar.ElemType.Validation).ShouldNot(BeNil())
   157  			Ω(ar.ElemType.Validation.Pattern).Should(Equal(pattern))
   158  		})
   159  	})
   160  
   161  	Context("defined with the type name", func() {
   162  		var ar *design.UserTypeDefinition
   163  		BeforeEach(func() {
   164  			dslengine.Reset()
   165  			apidsl.Type("name", func() {
   166  				apidsl.Attribute("id")
   167  			})
   168  			ar = apidsl.Type("names", func() {
   169  				apidsl.Attribute("ut", apidsl.ArrayOf("name"))
   170  			})
   171  		})
   172  
   173  		JustBeforeEach(func() {
   174  			dslengine.Run()
   175  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   176  		})
   177  
   178  		It("produces a user type", func() {
   179  			Ω(ar).ShouldNot(BeNil())
   180  			Ω(ar.TypeName).Should(Equal("names"))
   181  			Ω(ar.Type).ShouldNot(BeNil())
   182  			Ω(ar.Type.ToObject()).ShouldNot(BeNil())
   183  			Ω(ar.Type.ToObject()).Should(HaveKey("ut"))
   184  			ut := ar.Type.ToObject()["ut"]
   185  			Ω(ut.Type).ShouldNot(BeNil())
   186  			Ω(ut.Type).Should(BeAssignableToTypeOf(&design.Array{}))
   187  			et := ut.Type.ToArray().ElemType
   188  			Ω(et).ShouldNot(BeNil())
   189  			Ω(et.Type).Should(BeAssignableToTypeOf(&design.UserTypeDefinition{}))
   190  			Ω(et.Type.(*design.UserTypeDefinition).TypeName).Should(Equal("name"))
   191  		})
   192  	})
   193  
   194  	Context("defined with a media type name", func() {
   195  		var mt *design.MediaTypeDefinition
   196  		BeforeEach(func() {
   197  			dslengine.Reset()
   198  			mt = apidsl.MediaType("application/vnd.test", func() {
   199  				apidsl.Attributes(func() {
   200  					apidsl.Attribute("ut", apidsl.ArrayOf("application/vnd.test"))
   201  				})
   202  				apidsl.View("default", func() {
   203  					apidsl.Attribute("ut")
   204  				})
   205  			})
   206  		})
   207  
   208  		JustBeforeEach(func() {
   209  			dslengine.Run()
   210  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   211  		})
   212  
   213  		It("produces a user type", func() {
   214  			Ω(mt).ShouldNot(BeNil())
   215  			Ω(mt.TypeName).Should(Equal("Test"))
   216  			Ω(mt.Type).ShouldNot(BeNil())
   217  			Ω(mt.Type.ToObject()).ShouldNot(BeNil())
   218  			Ω(mt.Type.ToObject()).Should(HaveKey("ut"))
   219  			ut := mt.Type.ToObject()["ut"]
   220  			Ω(ut.Type).ShouldNot(BeNil())
   221  			Ω(ut.Type).Should(BeAssignableToTypeOf(&design.Array{}))
   222  			et := ut.Type.ToArray().ElemType
   223  			Ω(et).ShouldNot(BeNil())
   224  			Ω(et.Type).Should(BeAssignableToTypeOf(&design.MediaTypeDefinition{}))
   225  			Ω(et.Type.(*design.MediaTypeDefinition).TypeName).Should(Equal("Test"))
   226  		})
   227  	})
   228  })
   229  
   230  var _ = Describe("HashOf", func() {
   231  	Context("used on a global variable", func() {
   232  		var (
   233  			kt *design.UserTypeDefinition
   234  			vt *design.UserTypeDefinition
   235  			ha *design.Hash
   236  		)
   237  		BeforeEach(func() {
   238  			dslengine.Reset()
   239  			kt = apidsl.Type("key", func() {
   240  				apidsl.Attribute("id")
   241  			})
   242  			vt = apidsl.Type("val", func() {
   243  				apidsl.Attribute("id")
   244  			})
   245  			ha = apidsl.HashOf(kt, vt)
   246  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   247  		})
   248  
   249  		JustBeforeEach(func() {
   250  			dslengine.Run()
   251  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   252  		})
   253  
   254  		It("produces a hash type", func() {
   255  			Ω(ha).ShouldNot(BeNil())
   256  			Ω(ha.Kind()).Should(Equal(design.HashKind))
   257  			Ω(ha.KeyType.Type).Should(Equal(kt))
   258  			Ω(ha.ElemType.Type).Should(Equal(vt))
   259  		})
   260  	})
   261  
   262  	Context("with DSLs", func() {
   263  		var (
   264  			kp = "foo"
   265  			vp = "bar"
   266  			ha *design.Hash
   267  		)
   268  
   269  		BeforeEach(func() {
   270  			dslengine.Reset()
   271  			ha = apidsl.HashOf(design.String, design.String, func() { apidsl.Pattern(kp) }, func() { apidsl.Pattern(vp) })
   272  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   273  		})
   274  
   275  		JustBeforeEach(func() {
   276  			dslengine.Run()
   277  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   278  		})
   279  
   280  		It("records the validations", func() {
   281  			Ω(ha).ShouldNot(BeNil())
   282  			Ω(ha.Kind()).Should(Equal(design.HashKind))
   283  			Ω(ha.KeyType.Type).Should(Equal(design.String))
   284  			Ω(ha.KeyType.Validation).ShouldNot(BeNil())
   285  			Ω(ha.KeyType.Validation.Pattern).Should(Equal(kp))
   286  			Ω(ha.ElemType.Type).Should(Equal(design.String))
   287  			Ω(ha.ElemType.Validation).ShouldNot(BeNil())
   288  			Ω(ha.ElemType.Validation.Pattern).Should(Equal(vp))
   289  		})
   290  	})
   291  })