github.com/goldeneggg/goa@v1.3.1/design/dup_test.go (about)

     1  package design_test
     2  
     3  import (
     4  	. "github.com/goadesign/goa/design"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  )
     8  
     9  var _ = Describe("Dup", func() {
    10  	var dt DataType
    11  	var dup DataType
    12  
    13  	JustBeforeEach(func() {
    14  		dup = Dup(dt)
    15  	})
    16  
    17  	Context("with a primitive type", func() {
    18  		BeforeEach(func() {
    19  			dt = Integer
    20  		})
    21  
    22  		It("returns the same value", func() {
    23  			Ω(dup).Should(Equal(dt))
    24  		})
    25  	})
    26  
    27  	Context("with an array type", func() {
    28  		var elemType = Integer
    29  
    30  		BeforeEach(func() {
    31  			dt = &Array{
    32  				ElemType: &AttributeDefinition{Type: elemType},
    33  			}
    34  		})
    35  
    36  		It("returns a duplicate array type", func() {
    37  			Ω(dup).Should(Equal(dt))
    38  			Ω(dup == dt).Should(BeFalse())
    39  			Ω(dup.(*Array).ElemType == dt.(*Array).ElemType).Should(BeFalse())
    40  		})
    41  	})
    42  
    43  	Context("with a hash type", func() {
    44  		var keyType = String
    45  		var elemType = Integer
    46  
    47  		BeforeEach(func() {
    48  			dt = &Hash{
    49  				KeyType:  &AttributeDefinition{Type: keyType},
    50  				ElemType: &AttributeDefinition{Type: elemType},
    51  			}
    52  		})
    53  
    54  		It("returns a duplicate hash type", func() {
    55  			Ω(dup).Should(Equal(dt))
    56  			Ω(dup == dt).Should(BeFalse())
    57  			Ω(dup.(*Hash).KeyType == dt.(*Hash).KeyType).Should(BeFalse())
    58  			Ω(dup.(*Hash).ElemType == dt.(*Hash).ElemType).Should(BeFalse())
    59  		})
    60  	})
    61  
    62  	Context("with a user type", func() {
    63  		const typeName = "foo"
    64  		var att = &AttributeDefinition{Type: Integer}
    65  
    66  		BeforeEach(func() {
    67  			dt = &UserTypeDefinition{
    68  				TypeName:            typeName,
    69  				AttributeDefinition: att,
    70  			}
    71  		})
    72  
    73  		It("returns a duplicate user type", func() {
    74  			Ω(dup).Should(Equal(dt))
    75  			Ω(dup == dt).Should(BeFalse())
    76  			Ω(dup.(*UserTypeDefinition).AttributeDefinition == att).Should(BeFalse())
    77  		})
    78  	})
    79  
    80  	Context("with a media type", func() {
    81  		var obj = Object{"att": &AttributeDefinition{Type: Integer}}
    82  		var ut = &UserTypeDefinition{
    83  			TypeName:            "foo",
    84  			AttributeDefinition: &AttributeDefinition{Type: obj},
    85  		}
    86  		const identifier = "vnd.application/test"
    87  		var links = map[string]*LinkDefinition{
    88  			"link": {Name: "att", View: "default"},
    89  		}
    90  		var views = map[string]*ViewDefinition{
    91  			"default": {
    92  				Name:                "default",
    93  				AttributeDefinition: &AttributeDefinition{Type: obj},
    94  			},
    95  		}
    96  
    97  		BeforeEach(func() {
    98  			dt = &MediaTypeDefinition{
    99  				UserTypeDefinition: ut,
   100  				Identifier:         identifier,
   101  				Links:              links,
   102  				Views:              views,
   103  			}
   104  		})
   105  
   106  		It("returns a duplicate media type", func() {
   107  			Ω(dup).Should(Equal(dt))
   108  			Ω(dup == dt).Should(BeFalse())
   109  			Ω(dup.(*MediaTypeDefinition).UserTypeDefinition == ut).Should(BeFalse())
   110  		})
   111  	})
   112  
   113  	Context("with two media types referring to each other", func() {
   114  		var ut *UserTypeDefinition
   115  
   116  		BeforeEach(func() {
   117  			mt := &MediaTypeDefinition{Identifier: "application/mt1"}
   118  			mt2 := &MediaTypeDefinition{Identifier: "application/mt2"}
   119  			obj1 := Object{"att": &AttributeDefinition{Type: mt2}}
   120  			obj2 := Object{"att": &AttributeDefinition{Type: mt}}
   121  
   122  			att1 := &AttributeDefinition{Type: obj1}
   123  			ut = &UserTypeDefinition{AttributeDefinition: att1}
   124  			link1 := &LinkDefinition{Name: "att", View: "default"}
   125  			view1 := &ViewDefinition{AttributeDefinition: att1, Name: "default"}
   126  			mt.UserTypeDefinition = ut
   127  			mt.Links = map[string]*LinkDefinition{"att": link1}
   128  			mt.Views = map[string]*ViewDefinition{"default": view1}
   129  
   130  			att2 := &AttributeDefinition{Type: obj2}
   131  			ut2 := &UserTypeDefinition{AttributeDefinition: att2}
   132  			link2 := &LinkDefinition{Name: "att", View: "default"}
   133  			view2 := &ViewDefinition{AttributeDefinition: att2, Name: "default"}
   134  			mt2.UserTypeDefinition = ut2
   135  			mt2.Links = map[string]*LinkDefinition{"att": link2}
   136  			mt2.Views = map[string]*ViewDefinition{"default": view2}
   137  
   138  			dt = mt
   139  		})
   140  
   141  		It("duplicates without looping infinitly", func() {
   142  			Ω(dup).Should(Equal(dt))
   143  			Ω(dup == dt).Should(BeFalse())
   144  			Ω(dup.(*MediaTypeDefinition).UserTypeDefinition == ut).Should(BeFalse())
   145  		})
   146  	})
   147  })
   148  
   149  var _ = Describe("DupAtt", func() {
   150  	var att *AttributeDefinition
   151  	var dup *AttributeDefinition
   152  
   153  	JustBeforeEach(func() {
   154  		dup = DupAtt(att)
   155  	})
   156  
   157  	Context("with an attribute with a type which is a media type", func() {
   158  		BeforeEach(func() {
   159  			att = &AttributeDefinition{Type: &MediaTypeDefinition{}}
   160  		})
   161  
   162  		It("does not clone the type", func() {
   163  			Ω(dup == att).Should(BeFalse())
   164  			Ω(dup.Type == att.Type).Should(BeTrue())
   165  		})
   166  	})
   167  })