blob: dddb127544f65fb54cbe26cf7d4f6685f6ebeb37 [file] [log] [blame]
Marcel van Lohuizen6cb08782020-01-13 18:03:27 +01001// 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
15package jsonschema
16
17import (
18 "bytes"
19 "flag"
20 "io/ioutil"
21 "os"
22 "path"
23 "path/filepath"
24 "strings"
25 "testing"
26
27 "cuelang.org/go/cue"
28 "cuelang.org/go/cue/errors"
29 "cuelang.org/go/cue/format"
30 "cuelang.org/go/encoding/json"
31 "cuelang.org/go/encoding/yaml"
32 "github.com/google/go-cmp/cmp"
33 "github.com/stretchr/testify/assert"
34 "golang.org/x/tools/txtar"
35)
36
37var update = flag.Bool("update", false, "update the test files")
38
39// TestDecode reads the testdata/*.txtar files, converts the contained
40// JSON schema to CUE and compares it against the output.
41//
42// Use the --update flag to update test files with the corresponding output.
43func TestDecode(t *testing.T) {
44 err := filepath.Walk("testdata", func(fullpath string, info os.FileInfo, err error) error {
45 _ = err
46 if !strings.HasSuffix(fullpath, ".txtar") {
47 return nil
48 }
49
50 t.Run(fullpath, func(t *testing.T) {
51 a, err := txtar.ParseFile(fullpath)
52 if err != nil {
53 t.Fatal(err)
54 }
55
56 r := &cue.Runtime{}
57 var in *cue.Instance
58 var out, errout []byte
59 outIndex := -1
60
61 for i, f := range a.Files {
62 switch path.Ext(f.Name) {
63 case ".json":
64 in, err = json.Decode(r, f.Name, f.Data)
65 case ".yaml":
66 in, err = yaml.Decode(r, f.Name, f.Data)
67 case ".cue":
68 out = f.Data
69 outIndex = i
70 case ".err":
71 errout = f.Data
72 }
73 }
74 if err != nil {
75 t.Fatal(err)
76 }
77
Marcel van Lohuizen47d98702020-01-17 21:51:39 +010078 expr, err := Extract(in, &Config{ID: fullpath})
Marcel van Lohuizen6cb08782020-01-13 18:03:27 +010079 if err != nil && errout == nil {
80 t.Fatal(errors.Details(err, nil))
81 }
82 got := []byte(nil)
83 if err != nil {
84 got = []byte(err.Error())
85 }
86 if !cmp.Equal(errout, got) {
87 t.Error(cmp.Diff(string(got), string(errout)))
88 }
89
90 if expr != nil {
91 b, err := format.Node(expr, format.Simplify())
92 if err != nil {
93 t.Fatal(err)
94 }
95
96 // verify the generated CUE.
97 if _, err = r.Compile(fullpath, b); err != nil {
98 t.Fatal(errors.Details(err, nil))
99 }
100
101 b = bytes.TrimSpace(b)
102 out = bytes.TrimSpace(out)
103
104 if !cmp.Equal(b, out) {
105 if *update {
106 a.Files[outIndex].Data = b
107 b = txtar.Format(a)
108 err = ioutil.WriteFile(fullpath, b, 0644)
109 if err != nil {
110 t.Fatal(err)
111 }
112 return
113 }
114 t.Error(cmp.Diff(string(out), string(b)))
115 }
116 }
117 })
118 return nil
119 })
120 assert.NoError(t, err)
121}