github.com/solo-io/cue@v0.4.7/internal/encoding/detect_test.go (about) 1 // Copyright 2020 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 encoding 16 17 import ( 18 "testing" 19 20 "github.com/solo-io/cue/cue" 21 "github.com/solo-io/cue/cue/build" 22 ) 23 24 func TestDetect(t *testing.T) { 25 testCases := []struct { 26 name string 27 in string 28 out build.Interpretation 29 }{{ 30 name: "validOpenAPI", 31 in: ` 32 openapi: "3.0.0" 33 info: title: "Foo" 34 info: version: "v1alpha1" 35 `, 36 out: build.OpenAPI, 37 }, { 38 name: "noOpenAPI", 39 in: ` 40 info: title: "Foo" 41 info: version: "v1alpha1" 42 `, 43 }, { 44 name: "noTitle", 45 in: ` 46 openapi: "3.0.0" 47 info: version: "v1alpha1" 48 `, 49 }, { 50 name: "noVersion", 51 in: ` 52 openapi: "3.0.0" 53 info: title: "Foo" 54 `, 55 }, { 56 name: "validJSONSchema", 57 in: ` 58 $schema: "https://json-schema.org/schema#" 59 `, 60 out: build.JSONSchema, 61 }, { 62 name: "validJSONSchema", 63 in: ` 64 $schema: "https://json-schema.org/draft-07/schema#" 65 `, 66 out: build.JSONSchema, 67 }, { 68 name: "noSchema", 69 in: ` 70 $id: "https://acme.com/schema#" 71 `, 72 }, { 73 name: "wrongHost", 74 in: ` 75 $schema: "https://acme.com/schema#" 76 `, 77 }, { 78 name: "invalidURL", 79 in: ` 80 $schema: "://json-schema.org/draft-07" 81 `, 82 }, { 83 name: "invalidPath", 84 in: ` 85 $schema: "https://json-schema.org/draft-07" 86 `, 87 }} 88 for _, tc := range testCases { 89 t.Run(tc.name, func(t *testing.T) { 90 var r cue.Runtime 91 inst, err := r.Compile(tc.name, tc.in) 92 if err != nil { 93 t.Fatal(err) 94 } 95 got := Detect(inst.Value()) 96 if got != tc.out { 97 t.Errorf("got %v; want %v", got, tc.out) 98 } 99 }) 100 } 101 }