github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/goagen/gen_app/encoding_test.go (about)

     1  package genapp_test
     2  
     3  import (
     4  	"github.com/goadesign/goa/design"
     5  	"github.com/goadesign/goa/goagen/gen_app"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("BuildEncoders", func() {
    11  	var info []*design.EncodingDefinition
    12  	var encoder bool
    13  
    14  	var data []*genapp.EncoderTemplateData
    15  	var resErr error
    16  
    17  	BeforeEach(func() {
    18  		info = nil
    19  		encoder = false
    20  	})
    21  
    22  	JustBeforeEach(func() {
    23  		data, resErr = genapp.BuildEncoders(info, encoder)
    24  	})
    25  
    26  	Context("with a single definition using a single known MIME type for encoding", func() {
    27  		BeforeEach(func() {
    28  			simple := &design.EncodingDefinition{
    29  				MIMETypes: []string{"application/json"},
    30  				Encoder:   true,
    31  			}
    32  			info = append(info, simple)
    33  			encoder = true
    34  		})
    35  
    36  		It("generates a map with a single entry", func() {
    37  			Ω(resErr).ShouldNot(HaveOccurred())
    38  			Ω(data).Should(HaveLen(1))
    39  			jd := data[0]
    40  			Ω(jd).ShouldNot(BeNil())
    41  			Ω(jd.PackagePath).Should(Equal("github.com/goadesign/goa"))
    42  			Ω(jd.PackageName).Should(Equal("goa"))
    43  			Ω(jd.Function).Should(Equal("NewJSONEncoder"))
    44  			Ω(jd.MIMETypes).Should(HaveLen(1))
    45  			Ω(jd.MIMETypes[0]).Should(Equal("application/json"))
    46  		})
    47  	})
    48  
    49  	Context("with a single definition using a single known MIME type for decoding", func() {
    50  		BeforeEach(func() {
    51  			simple := &design.EncodingDefinition{
    52  				MIMETypes: []string{"application/json"},
    53  			}
    54  			info = append(info, simple)
    55  			encoder = false
    56  		})
    57  
    58  		It("generates a map with a single entry", func() {
    59  			Ω(resErr).ShouldNot(HaveOccurred())
    60  			Ω(data).Should(HaveLen(1))
    61  			jd := data[0]
    62  			Ω(jd).ShouldNot(BeNil())
    63  			Ω(jd.PackagePath).Should(Equal("github.com/goadesign/goa"))
    64  			Ω(jd.PackageName).Should(Equal("goa"))
    65  			Ω(jd.Function).Should(Equal("NewJSONDecoder"))
    66  			Ω(jd.MIMETypes).Should(HaveLen(1))
    67  			Ω(jd.MIMETypes[0]).Should(Equal("application/json"))
    68  		})
    69  	})
    70  
    71  	Context("with a definition using a custom decoding package", func() {
    72  		const packagePath = "github.com/goadesign/goa/design" // Just to pick something always available
    73  		var mimeTypes = []string{"application/vnd.custom", "application/vnd.custom2"}
    74  
    75  		BeforeEach(func() {
    76  			simple := &design.EncodingDefinition{
    77  				PackagePath: packagePath,
    78  				Function:    "NewDecoder",
    79  				MIMETypes:   mimeTypes,
    80  			}
    81  			info = append(info, simple)
    82  		})
    83  
    84  		It("generates a map with a single entry", func() {
    85  			Ω(resErr).ShouldNot(HaveOccurred())
    86  			Ω(data).Should(HaveLen(1))
    87  			jd := data[0]
    88  			Ω(jd).ShouldNot(BeNil())
    89  			Ω(jd.PackagePath).Should(Equal(packagePath))
    90  			Ω(jd.PackageName).Should(Equal("design"))
    91  			Ω(jd.Function).Should(Equal("NewDecoder"))
    92  			Ω(jd.MIMETypes).Should(ConsistOf(interface{}(mimeTypes[0]), interface{}(mimeTypes[1])))
    93  		})
    94  	})
    95  
    96  	Context("with a definition using a custom decoding package for a known encoding", func() {
    97  		const packagePath = "github.com/goadesign/goa/design" // Just to pick something always available
    98  		var mimeTypes = []string{"application/json"}
    99  
   100  		BeforeEach(func() {
   101  			simple := &design.EncodingDefinition{
   102  				PackagePath: packagePath,
   103  				Function:    "NewDecoder",
   104  				MIMETypes:   mimeTypes,
   105  			}
   106  			info = append(info, simple)
   107  		})
   108  
   109  		It("generates a map with a single entry using the generic decoder factory name", func() {
   110  			Ω(resErr).ShouldNot(HaveOccurred())
   111  			Ω(data).Should(HaveLen(1))
   112  			jd := data[0]
   113  			Ω(jd).ShouldNot(BeNil())
   114  			Ω(jd.PackagePath).Should(Equal(packagePath))
   115  			Ω(jd.PackageName).Should(Equal("design"))
   116  			Ω(jd.Function).Should(Equal("NewDecoder"))
   117  		})
   118  	})
   119  
   120  	Context("with a definition using a custom decoding package from goadesign", func() {
   121  		const packagePath = "github.com/goadesign/goa/encoding/gogoprotobuf"
   122  		var mimeTypes = []string{"application/x-protobuf"}
   123  
   124  		BeforeEach(func() {
   125  			simple := &design.EncodingDefinition{
   126  				PackagePath: packagePath,
   127  				MIMETypes:   mimeTypes,
   128  			}
   129  			info = append(info, simple)
   130  		})
   131  
   132  		It("generates a map with a single entry using the generic decoder factory name", func() {
   133  			Ω(resErr).ShouldNot(HaveOccurred())
   134  			Ω(data).Should(HaveLen(1))
   135  			jd := data[0]
   136  			Ω(jd).ShouldNot(BeNil())
   137  			Ω(jd.PackagePath).Should(Equal(packagePath))
   138  			Ω(jd.PackageName).Should(Equal("gogoprotobuf"))
   139  			Ω(jd.Function).Should(Equal("NewDecoder"))
   140  		})
   141  	})
   142  })