github.com/shogo82148/goa-v1@v1.6.2/goagen/gen_schema/generator_test.go (about)

     1  package genschema_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	"github.com/shogo82148/goa-v1/design"
    12  	"github.com/shogo82148/goa-v1/design/apidsl"
    13  	"github.com/shogo82148/goa-v1/dslengine"
    14  	"github.com/shogo82148/goa-v1/goagen/codegen"
    15  	genschema "github.com/shogo82148/goa-v1/goagen/gen_schema"
    16  	"github.com/shogo82148/goa-v1/version"
    17  )
    18  
    19  var _ = Describe("Generate", func() {
    20  	var files []string
    21  	var genErr error
    22  	var workspace *codegen.Workspace
    23  	var testPkg *codegen.Package
    24  
    25  	BeforeEach(func() {
    26  		var err error
    27  		workspace, err = codegen.NewWorkspace("test")
    28  		Ω(err).ShouldNot(HaveOccurred())
    29  		testPkg, err = workspace.NewPackage("schematest")
    30  		Ω(err).ShouldNot(HaveOccurred())
    31  		os.Args = []string{"goagen", "--out=" + testPkg.Abs(), "--design=foo", "--version=" + version.String()}
    32  	})
    33  
    34  	JustBeforeEach(func() {
    35  		files, genErr = genschema.Generate()
    36  	})
    37  
    38  	AfterEach(func() {
    39  		workspace.Delete()
    40  	})
    41  
    42  	Context("with a dummy API", func() {
    43  		BeforeEach(func() {
    44  			dslengine.Reset()
    45  			apidsl.API("test api", func() {
    46  				apidsl.Title("dummy API with no resource")
    47  				apidsl.Description("I told you it's dummy")
    48  			})
    49  			dslengine.Run()
    50  		})
    51  
    52  		It("generates a dummy schema", func() {
    53  			Ω(genErr).Should(BeNil())
    54  			Ω(files).Should(HaveLen(2))
    55  			content, err := os.ReadFile(filepath.Join(testPkg.Abs(), "schema", "schema.json"))
    56  			Ω(err).ShouldNot(HaveOccurred())
    57  			Ω(len(strings.Split(string(content), "\n"))).Should(BeNumerically("==", 1))
    58  			var s genschema.JSONSchema
    59  			err = json.Unmarshal(content, &s)
    60  			Ω(err).ShouldNot(HaveOccurred())
    61  		})
    62  	})
    63  })
    64  
    65  var _ = Describe("NewGenerator", func() {
    66  	var generator *genschema.Generator
    67  
    68  	var args = struct {
    69  		api    *design.APIDefinition
    70  		outDir string
    71  	}{
    72  		api: &design.APIDefinition{
    73  			Name: "test api",
    74  		},
    75  		outDir: "out_dir",
    76  	}
    77  
    78  	Context("with options all options set", func() {
    79  		BeforeEach(func() {
    80  
    81  			generator = genschema.NewGenerator(
    82  				genschema.API(args.api),
    83  				genschema.OutDir(args.outDir),
    84  			)
    85  		})
    86  
    87  		It("has all public properties set with expected value", func() {
    88  			Ω(generator).ShouldNot(BeNil())
    89  			Ω(generator.API.Name).Should(Equal(args.api.Name))
    90  			Ω(generator.OutDir).Should(Equal(args.outDir))
    91  		})
    92  	})
    93  })