Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 1 | // 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 | |
| 15 | package openapi |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "encoding/json" |
| 20 | "flag" |
| 21 | "io/ioutil" |
| 22 | "path/filepath" |
Marcel van Lohuizen | 4de93e1 | 2019-07-02 20:04:10 +0200 | [diff] [blame] | 23 | "strings" |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 24 | "testing" |
| 25 | |
| 26 | "cuelang.org/go/cue" |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 27 | "cuelang.org/go/cue/load" |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 28 | "github.com/kylelemons/godebug/diff" |
| 29 | ) |
| 30 | |
| 31 | var update *bool = flag.Bool("update", false, "update the test output") |
| 32 | |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 33 | func TestParseDefinitions(t *testing.T) { |
Marcel van Lohuizen | c89f5a6 | 2019-07-04 10:41:18 +0200 | [diff] [blame] | 34 | info := OrderedMap{kvs: []KeyValue{{"title", "test"}, {"version", "v1"}}} |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 35 | defaultConfig := &Config{} |
Marcel van Lohuizen | 4de93e1 | 2019-07-02 20:04:10 +0200 | [diff] [blame] | 36 | resolveRefs := &Config{Info: info, ExpandReferences: true} |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 37 | |
| 38 | testCases := []struct { |
| 39 | in, out string |
| 40 | config *Config |
| 41 | }{{ |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 42 | "structural.cue", |
| 43 | "structural.json", |
| 44 | resolveRefs, |
| 45 | }, { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 46 | "simple.cue", |
| 47 | "simple.json", |
| 48 | resolveRefs, |
| 49 | }, { |
Marcel van Lohuizen | 7b7ccdd | 2019-07-03 13:33:03 +0200 | [diff] [blame] | 50 | "simple.cue", |
| 51 | "simple-filter.json", |
| 52 | &Config{Info: info, FieldFilter: "min.*|max.*"}, |
| 53 | }, { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 54 | "array.cue", |
| 55 | "array.json", |
| 56 | defaultConfig, |
| 57 | }, { |
Marcel van Lohuizen | 3086ea6 | 2019-08-01 18:05:40 +0200 | [diff] [blame] | 58 | "struct.cue", |
| 59 | "struct.json", |
| 60 | defaultConfig, |
| 61 | }, { |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 62 | "strings.cue", |
| 63 | "strings.json", |
| 64 | defaultConfig, |
| 65 | }, { |
Marcel van Lohuizen | b05c768 | 2019-07-25 17:51:24 +0200 | [diff] [blame] | 66 | "nums.cue", |
| 67 | "nums.json", |
| 68 | defaultConfig, |
| 69 | }, { |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 70 | "builtins.cue", |
| 71 | "builtins.json", |
| 72 | defaultConfig, |
| 73 | }, { |
Marcel van Lohuizen | 95fcc28 | 2019-06-25 17:37:06 +0200 | [diff] [blame] | 74 | "oneof.cue", |
| 75 | "oneof.json", |
| 76 | defaultConfig, |
| 77 | }, { |
Marcel van Lohuizen | 73e3f6b | 2019-08-01 16:10:57 +0200 | [diff] [blame] | 78 | "oneof.cue", |
| 79 | "oneof-resolve.json", |
| 80 | resolveRefs, |
| 81 | }, { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 82 | "openapi.cue", |
| 83 | "openapi.json", |
| 84 | defaultConfig, |
| 85 | }, { |
| 86 | "openapi.cue", |
| 87 | "openapi-norefs.json", |
| 88 | resolveRefs, |
Marcel van Lohuizen | 4de93e1 | 2019-07-02 20:04:10 +0200 | [diff] [blame] | 89 | }, { |
| 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 Lohuizen | ceb003d | 2019-08-08 13:45:10 +0200 | [diff] [blame] | 101 | }, { |
| 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 Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 114 | }} |
| 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 Lohuizen | ad3fde1 | 2019-06-26 15:23:57 +0200 | [diff] [blame] | 119 | inst := cue.Build(load.Instances([]string{filename}, nil))[0] |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 120 | |
| 121 | b, err := Gen(inst, tc.config) |
Marcel van Lohuizen | ce195d2 | 2019-07-25 16:03:33 +0200 | [diff] [blame] | 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 125 | var out = &bytes.Buffer{} |
Marcel van Lohuizen | 153909e | 2019-06-18 20:39:47 +0200 | [diff] [blame] | 126 | _ = json.Indent(out, b, "", " ") |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 127 | |
| 128 | wantFile := filepath.Join("testdata", tc.out) |
| 129 | if *update { |
Marcel van Lohuizen | 153909e | 2019-06-18 20:39:47 +0200 | [diff] [blame] | 130 | _ = ioutil.WriteFile(wantFile, out.Bytes(), 0644) |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 131 | return |
| 132 | } |
| 133 | |
| 134 | b, err = ioutil.ReadFile(wantFile) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | |
Marcel van Lohuizen | 73e3f6b | 2019-08-01 16:10:57 +0200 | [diff] [blame] | 139 | if d := diff.Diff(out.String(), string(b)); d != "" { |
Marcel van Lohuizen | f4d483e | 2019-05-20 08:03:10 -0400 | [diff] [blame] | 140 | t.Errorf("files differ:\n%v", d) |
| 141 | } |
| 142 | }) |
| 143 | } |
| 144 | } |
Marcel van Lohuizen | 9fad62f | 2019-08-13 01:39:10 +0200 | [diff] [blame] | 145 | |
| 146 | // This is for debugging purposes. Do not remove. |
| 147 | func 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 | } |