cuelang.org/go@v0.13.0/encoding/openapi/openapi_test.go (about) 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_test 16 17 import ( 18 "bytes" 19 "encoding/json" 20 "strings" 21 "testing" 22 23 "cuelang.org/go/cue" 24 "cuelang.org/go/cue/cuecontext" 25 "cuelang.org/go/cue/errors" 26 "cuelang.org/go/encoding/openapi" 27 "cuelang.org/go/internal/cuetdtest" 28 "cuelang.org/go/internal/cuetxtar" 29 ) 30 31 var ( 32 matrix = cuetdtest.FullMatrix 33 ) 34 35 func TestGenerateOpenAPI(t *testing.T) { 36 test := cuetxtar.TxTarTest{ 37 Root: "./testdata", 38 Name: t.Name(), 39 Matrix: matrix, 40 } 41 42 nameFuncs := map[string]func(v cue.Value, path cue.Path) string{ 43 "toUpper": func(v cue.Value, path cue.Path) string { 44 var buf strings.Builder 45 for i, sel := range path.Selectors() { 46 if i > 0 { 47 buf.WriteByte('_') 48 } 49 s := sel.String() 50 s = strings.TrimPrefix(s, "#") 51 buf.WriteString(strings.ToUpper(s)) 52 } 53 return buf.String() 54 }, 55 "excludeExcluded": func(v cue.Value, path cue.Path) string { 56 switch { 57 case strings.HasPrefix(path.Selectors()[0].String(), "#Excluded"): 58 return "" 59 } 60 return strings.TrimPrefix(path.String(), "#") 61 }, 62 } 63 64 descFuncs := map[string]func(v cue.Value) string{ 65 "randomish": func(v cue.Value) string { 66 return "Randomly picked description from a set of size one." 67 }, 68 } 69 70 test.Run(t, func(t *cuetxtar.Test) { 71 a := t.Instance() 72 ctx := t.CueContext() 73 v := ctx.BuildInstance(a) 74 75 if err := v.Err(); err != nil { 76 t.Fatal(errors.Details(err, nil)) 77 } 78 79 config := openapi.Config{} 80 if t.HasTag("ExpandReferences") { 81 config.ExpandReferences = true 82 } 83 if filter, ok := t.Value("FieldFilter"); ok { 84 config.FieldFilter = filter 85 } 86 if version, ok := t.Value("Version"); ok { 87 config.Version = version 88 } 89 if name, ok := t.Value("NameFunc"); ok { 90 if fun, found := nameFuncs[name]; found { 91 config.NameFunc = fun 92 } else { 93 t.Fatal("Unknown NameFunc", name) 94 } 95 } 96 if desc, ok := t.Value("DescriptionFunc"); ok { 97 if fun, found := descFuncs[desc]; found { 98 config.DescriptionFunc = fun 99 } else { 100 t.Fatal("Unknown DescriptionFunc", desc) 101 } 102 } 103 104 expectedErr, shouldErr := t.Value("ExpectError") 105 b, err := openapi.Gen(v, &config) 106 if err != nil { 107 details := errors.Details(err, nil) 108 if !shouldErr || !strings.Contains(details, expectedErr) { 109 t.Fatal("unexpected error:", details) 110 } 111 return 112 } 113 114 if shouldErr { 115 t.Fatal("unexpected success") 116 } else { 117 _, err := openapi.Generate(v, &config) 118 if err != nil { 119 t.Fatal(err) 120 } 121 } 122 123 var out bytes.Buffer 124 err = json.Indent(&out, b, "", " ") 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 w := t.Writer("out.json") 130 _, err = w.Write(out.Bytes()) 131 if err != nil { 132 t.Fatal(err) 133 } 134 }) 135 } 136 137 // TODO: move OpenAPI testing to txtar and allow errors. 138 func TestIssue1234(t *testing.T) { 139 val := cuecontext.New().CompileString(` 140 #Test: or([]) 141 142 `) 143 if err := val.Err(); err != nil { 144 t.Fatal(err) 145 } 146 147 _, err := openapi.Gen(val, &openapi.Config{}) 148 if err == nil { 149 t.Fatal("expected error") 150 } 151 } 152 153 // This is for debugging purposes. Do not remove. 154 func TestX(t *testing.T) { 155 t.Skip() 156 157 val := cuecontext.New().CompileString(` 158 `) 159 if err := val.Err(); err != nil { 160 t.Fatal(err) 161 } 162 163 b, err := openapi.Gen(val, &openapi.Config{ 164 // ExpandReferences: true, 165 }) 166 if err != nil { 167 t.Fatal(errors.Details(err, nil)) 168 } 169 170 var out = &bytes.Buffer{} 171 _ = json.Indent(out, b, "", " ") 172 t.Error(out.String()) 173 }