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