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