blob: ea634ace3f9fd5061d9b8b5460cb809e6b39cfda [file] [log] [blame]
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -04001// Copyright 2019 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package openapi
16
17import (
18 "bytes"
19 "encoding/json"
20 "flag"
21 "io/ioutil"
22 "path/filepath"
Marcel van Lohuizen4de93e12019-07-02 20:04:10 +020023 "strings"
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -040024 "testing"
25
26 "cuelang.org/go/cue"
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -040027 "cuelang.org/go/cue/load"
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -040028 "github.com/kylelemons/godebug/diff"
29)
30
31var update *bool = flag.Bool("update", false, "update the test output")
32
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -040033func TestParseDefinitions(t *testing.T) {
Marcel van Lohuizenc89f5a62019-07-04 10:41:18 +020034 info := OrderedMap{kvs: []KeyValue{{"title", "test"}, {"version", "v1"}}}
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -040035 defaultConfig := &Config{}
Marcel van Lohuizen4de93e12019-07-02 20:04:10 +020036 resolveRefs := &Config{Info: info, ExpandReferences: true}
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -040037
38 testCases := []struct {
39 in, out string
40 config *Config
41 }{{
Marcel van Lohuizen9fad62f2019-08-13 01:39:10 +020042 "structural.cue",
43 "structural.json",
44 resolveRefs,
45 }, {
Marcel van Lohuizena0d2a402019-06-29 14:07:41 +020046 "simple.cue",
47 "simple.json",
48 resolveRefs,
49 }, {
Marcel van Lohuizen7b7ccdd2019-07-03 13:33:03 +020050 "simple.cue",
51 "simple-filter.json",
52 &Config{Info: info, FieldFilter: "min.*|max.*"},
53 }, {
Marcel van Lohuizena0d2a402019-06-29 14:07:41 +020054 "array.cue",
55 "array.json",
56 defaultConfig,
57 }, {
Marcel van Lohuizen3086ea62019-08-01 18:05:40 +020058 "struct.cue",
59 "struct.json",
60 defaultConfig,
61 }, {
Marcel van Lohuizence195d22019-07-25 16:03:33 +020062 "strings.cue",
63 "strings.json",
64 defaultConfig,
65 }, {
Marcel van Lohuizenb05c7682019-07-25 17:51:24 +020066 "nums.cue",
67 "nums.json",
68 defaultConfig,
69 }, {
Marcel van Lohuizen6bf36972019-08-06 20:15:58 +020070 "builtins.cue",
71 "builtins.json",
72 defaultConfig,
73 }, {
Marcel van Lohuizen95fcc282019-06-25 17:37:06 +020074 "oneof.cue",
75 "oneof.json",
76 defaultConfig,
77 }, {
Marcel van Lohuizen73e3f6b2019-08-01 16:10:57 +020078 "oneof.cue",
79 "oneof-resolve.json",
80 resolveRefs,
81 }, {
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -040082 "openapi.cue",
83 "openapi.json",
84 defaultConfig,
85 }, {
86 "openapi.cue",
87 "openapi-norefs.json",
88 resolveRefs,
Marcel van Lohuizen4de93e12019-07-02 20:04:10 +020089 }, {
90 "oneof.cue",
91 "oneof-funcs.json",
92 &Generator{
93 Info: info,
94 ReferenceFunc: func(inst *cue.Instance, path []string) string {
95 return strings.ToUpper(strings.Join(path, "_"))
96 },
97 DescriptionFunc: func(v cue.Value) string {
98 return "Randomly picked description from a set of size one."
99 },
100 },
Marcel van Lohuizenceb003d2019-08-08 13:45:10 +0200101 }, {
102 "refs.cue",
103 "refs.json",
104 &Generator{
105 Info: info,
106 ReferenceFunc: func(inst *cue.Instance, path []string) string {
107 switch {
108 case strings.HasPrefix(path[0], "Excluded"):
109 return ""
110 }
111 return strings.Join(path, ".")
112 },
113 },
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -0400114 }}
115 for _, tc := range testCases {
116 t.Run(tc.out, func(t *testing.T) {
117 filename := filepath.Join("testdata", filepath.FromSlash(tc.in))
118
Marcel van Lohuizenad3fde12019-06-26 15:23:57 +0200119 inst := cue.Build(load.Instances([]string{filename}, nil))[0]
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -0400120
121 b, err := Gen(inst, tc.config)
Marcel van Lohuizence195d22019-07-25 16:03:33 +0200122 if err != nil {
123 t.Fatal(err)
124 }
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -0400125 var out = &bytes.Buffer{}
Marcel van Lohuizen153909e2019-06-18 20:39:47 +0200126 _ = json.Indent(out, b, "", " ")
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -0400127
128 wantFile := filepath.Join("testdata", tc.out)
129 if *update {
Marcel van Lohuizen153909e2019-06-18 20:39:47 +0200130 _ = ioutil.WriteFile(wantFile, out.Bytes(), 0644)
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -0400131 return
132 }
133
134 b, err = ioutil.ReadFile(wantFile)
135 if err != nil {
136 t.Fatal(err)
137 }
138
Marcel van Lohuizen73e3f6b2019-08-01 16:10:57 +0200139 if d := diff.Diff(out.String(), string(b)); d != "" {
Marcel van Lohuizenf4d483e2019-05-20 08:03:10 -0400140 t.Errorf("files differ:\n%v", d)
141 }
142 })
143 }
144}
Marcel van Lohuizen9fad62f2019-08-13 01:39:10 +0200145
146// This is for debugging purposes. Do not remove.
147func TestX(t *testing.T) {
148 t.Skip()
149
150 var r cue.Runtime
151 inst, err := r.Compile("test", `
152 AnyField: "any value"
153 `)
154 if err != nil {
155 t.Fatal(err)
156 }
157
158 b, err := Gen(inst, &Config{
159 ExpandReferences: true,
160 })
161 if err != nil {
162 t.Fatal(err)
163 }
164
165 var out = &bytes.Buffer{}
166 _ = json.Indent(out, b, "", " ")
167 t.Error(out.String())
168}