github.com/shogo82148/goa-v1@v1.6.2/goagen/codegen/finalizer_test.go (about)

     1  package codegen_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/dslengine"
     8  	"github.com/shogo82148/goa-v1/goagen/codegen"
     9  )
    10  
    11  var _ = Describe("Struct finalize code generation", func() {
    12  	var (
    13  		att       *design.AttributeDefinition
    14  		target    string
    15  		finalizer *codegen.Finalizer
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		finalizer = codegen.NewFinalizer()
    20  	})
    21  
    22  	Context("given an object with a primitive field", func() {
    23  		BeforeEach(func() {
    24  			att = &design.AttributeDefinition{
    25  				Type: &design.Object{
    26  					"foo": &design.AttributeDefinition{
    27  						Type:         design.String,
    28  						DefaultValue: "bar",
    29  					},
    30  				},
    31  			}
    32  			target = "ut"
    33  		})
    34  		It("finalizes the fields", func() {
    35  			code := finalizer.Code(att, target, 0)
    36  			Ω(code).Should(Equal(primitiveAssignmentCode))
    37  		})
    38  	})
    39  
    40  	Context("given an object with a primitive Number field", func() {
    41  		BeforeEach(func() {
    42  			att = &design.AttributeDefinition{
    43  				Type: &design.Object{
    44  					"foo": &design.AttributeDefinition{
    45  						Type:         design.Number,
    46  						DefaultValue: 0.0,
    47  					},
    48  				},
    49  			}
    50  			target = "ut"
    51  		})
    52  		It("finalizes the fields", func() {
    53  			code := finalizer.Code(att, target, 0)
    54  			Ω(code).Should(Equal(numberAssignmentCode))
    55  		})
    56  	})
    57  
    58  	Context("given an object with a primitive Number field with a int default value", func() {
    59  		BeforeEach(func() {
    60  			att = &design.AttributeDefinition{
    61  				Type: &design.Object{
    62  					"foo": &design.AttributeDefinition{
    63  						Type:         design.Number,
    64  						DefaultValue: 50,
    65  					},
    66  				},
    67  			}
    68  			target = "ut"
    69  		})
    70  		It("finalizes the fields", func() {
    71  			code := finalizer.Code(att, target, 0)
    72  			Ω(code).Should(Equal(numberAssignmentCodeIntDefault))
    73  		})
    74  	})
    75  
    76  	Context("given an array field", func() {
    77  		BeforeEach(func() {
    78  			att = &design.AttributeDefinition{
    79  				Type: &design.Object{
    80  					"foo": &design.AttributeDefinition{
    81  						Type: &design.Array{
    82  							ElemType: &design.AttributeDefinition{
    83  								Type: design.String,
    84  							},
    85  						},
    86  						DefaultValue: []interface{}{"bar", "baz"},
    87  					},
    88  				},
    89  			}
    90  			target = "ut"
    91  		})
    92  		It("finalizes the array fields", func() {
    93  			code := finalizer.Code(att, target, 0)
    94  			Ω(code).Should(Equal(arrayAssignmentCode))
    95  		})
    96  	})
    97  
    98  	Context("given a hash field", func() {
    99  		BeforeEach(func() {
   100  			att = &design.AttributeDefinition{
   101  				Type: &design.Object{
   102  					"foo": &design.AttributeDefinition{
   103  						Type: &design.Hash{
   104  							KeyType: &design.AttributeDefinition{
   105  								Type: design.String,
   106  							},
   107  							ElemType: &design.AttributeDefinition{
   108  								Type: design.String,
   109  							},
   110  						},
   111  						DefaultValue: map[interface{}]interface{}{"bar": "baz"},
   112  					},
   113  				},
   114  			}
   115  			target = "ut"
   116  		})
   117  		Context("given a datetime field", func() {
   118  			BeforeEach(func() {
   119  				att = &design.AttributeDefinition{
   120  					Type: &design.Object{
   121  						"foo": &design.AttributeDefinition{
   122  							Type:         design.DateTime,
   123  							DefaultValue: interface{}("1978-06-30T10:00:00+09:00"),
   124  						},
   125  					},
   126  				}
   127  				target = "ut"
   128  			})
   129  			It("finalizes the hash fields", func() {
   130  				code := finalizer.Code(att, target, 0)
   131  				Ω(code).Should(Equal(datetimeAssignmentCode))
   132  			})
   133  		})
   134  
   135  		It("finalizes the recursive type fields", func() {
   136  			code := finalizer.Code(att, target, 0)
   137  			Ω(code).Should(Equal(recursiveAssignmentCodeA))
   138  		})
   139  	})
   140  
   141  	Context("given a recursive user type with an array attribute", func() {
   142  		BeforeEach(func() {
   143  			var (
   144  				rt  = &design.UserTypeDefinition{TypeName: "recursive"}
   145  				ar  = &design.Array{ElemType: &design.AttributeDefinition{Type: rt}}
   146  				obj = &design.Object{
   147  					"elems": &design.AttributeDefinition{Type: ar},
   148  					"other": &design.AttributeDefinition{
   149  						Type:         design.String,
   150  						DefaultValue: "foo",
   151  					},
   152  				}
   153  			)
   154  			rt.AttributeDefinition = &design.AttributeDefinition{Type: obj}
   155  
   156  			att = &design.AttributeDefinition{Type: rt}
   157  			target = "ut"
   158  		})
   159  		It("finalizes the recursive type fields", func() {
   160  			code := finalizer.Code(att, target, 0)
   161  			Ω(code).Should(Equal(recursiveAssignmentCodeB))
   162  		})
   163  	})
   164  
   165  	Context("given an object with a user definition type", func() {
   166  		BeforeEach(func() {
   167  			att = &design.AttributeDefinition{
   168  				Type: &design.Object{
   169  					"foo": &design.AttributeDefinition{
   170  						Type: design.String,
   171  						Metadata: dslengine.MetadataDefinition{
   172  							"struct:field:type": []string{"UserDefinitionType", "github.com/shogo82148/goa-v1/goagen/codegen_test"},
   173  						},
   174  						DefaultValue: UserDefinitionType("bar"),
   175  					},
   176  				},
   177  			}
   178  			target = "ut"
   179  		})
   180  		It("finalizes the fields", func() {
   181  			code := finalizer.Code(att, target, 0)
   182  			Ω(code).Should(Equal(userTypeAssignmentCode))
   183  		})
   184  	})
   185  })
   186  
   187  // UserDefinitionType is a user defined type used in the unit tests.
   188  type UserDefinitionType string
   189  
   190  const (
   191  	primitiveAssignmentCode = `var defaultFoo string = "bar"
   192  if ut.Foo == nil {
   193  	ut.Foo = &defaultFoo
   194  }`
   195  
   196  	numberAssignmentCodeIntDefault = `var defaultFoo float64 = 50.000000
   197  if ut.Foo == nil {
   198  	ut.Foo = &defaultFoo
   199  }`
   200  
   201  	numberAssignmentCode = `var defaultFoo float64 = 0.000000
   202  if ut.Foo == nil {
   203  	ut.Foo = &defaultFoo
   204  }`
   205  
   206  	arrayAssignmentCode = `if ut.Foo == nil {
   207  	ut.Foo = []string{"bar", "baz"}
   208  }`
   209  
   210  	datetimeAssignmentCode = `var defaultFoo, _ = time.Parse(time.RFC3339, "1978-06-30T10:00:00+09:00")
   211  if ut.Foo == nil {
   212  	ut.Foo = &defaultFoo
   213  }`
   214  
   215  	recursiveAssignmentCodeA = `if ut.Foo == nil {
   216  	ut.Foo = map[string]string{"bar": "baz"}
   217  }`
   218  
   219  	recursiveAssignmentCodeB = `	for _, e := range ut.Elems {
   220  		var defaultOther string = "foo"
   221  		if e.Other == nil {
   222  			e.Other = &defaultOther
   223  }
   224  	}
   225  var defaultOther string = "foo"
   226  if ut.Other == nil {
   227  	ut.Other = &defaultOther
   228  }`
   229  
   230  	userTypeAssignmentCode = `var defaultFoo UserDefinitionType = "bar"
   231  if ut.Foo == nil {
   232  	ut.Foo = &defaultFoo
   233  }`
   234  )