github.com/solo-io/cue@v0.4.7/encoding/protobuf/jsonpb/decoder_test.go (about) 1 // Copyright 2021 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 jsonpb_test 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/solo-io/cue/cue" 22 "github.com/solo-io/cue/cue/ast" 23 "github.com/solo-io/cue/cue/ast/astutil" 24 "github.com/solo-io/cue/cue/errors" 25 "github.com/solo-io/cue/cue/format" 26 "github.com/solo-io/cue/cue/parser" 27 "github.com/solo-io/cue/encoding/json" 28 "github.com/solo-io/cue/encoding/protobuf/jsonpb" 29 "github.com/solo-io/cue/encoding/yaml" 30 "github.com/solo-io/cue/internal/cuetest" 31 "github.com/solo-io/cue/internal/cuetxtar" 32 ) 33 34 func TestParse(t *testing.T) { 35 test := cuetxtar.TxTarTest{ 36 Root: "./testdata/decoder", 37 Name: "jsonpb", 38 Update: cuetest.UpdateGoldenFiles, 39 } 40 41 r := cue.Runtime{} 42 43 test.Run(t, func(t *cuetxtar.Test) { 44 // TODO: use high-level API. 45 46 var schema cue.Value 47 var file *ast.File 48 49 for _, f := range t.Archive.Files { 50 switch { 51 case f.Name == "schema.cue": 52 inst, err := r.Compile(f.Name, f.Data) 53 if err != nil { 54 t.WriteErrors(errors.Promote(err, "test")) 55 return 56 } 57 schema = inst.Value() 58 continue 59 60 case strings.HasPrefix(f.Name, "out/"): 61 continue 62 63 case strings.HasSuffix(f.Name, ".cue"): 64 f, err := parser.ParseFile(f.Name, f.Data, parser.ParseComments) 65 if err != nil { 66 t.Fatal(err) 67 } 68 file = f 69 70 case strings.HasSuffix(f.Name, ".json"): 71 x, err := json.Extract(f.Name, f.Data) 72 if err != nil { 73 t.Fatal(err) 74 } 75 file, err = astutil.ToFile(x) 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 case strings.HasSuffix(f.Name, ".yaml"): 81 f, err := yaml.Extract(f.Name, f.Data) 82 if err != nil { 83 t.Fatal(err) 84 } 85 file = f 86 } 87 88 w := t.Writer(f.Name) 89 err := jsonpb.NewDecoder(schema).RewriteFile(file) 90 if err != nil { 91 errors.Print(w, err, nil) 92 continue 93 } 94 95 b, err := format.Node(file) 96 if err != nil { 97 t.Fatal(err) 98 } 99 _, _ = w.Write(b) 100 } 101 }) 102 } 103 104 // For debugging purposes: DO NOT REMOVE. 105 func TestX(t *testing.T) { 106 const schema = ` 107 108 ` 109 const data = ` 110 ` 111 if strings.TrimSpace(data) == "" { 112 t.Skip() 113 } 114 var r cue.Runtime 115 inst, err := r.Compile("schema", schema) 116 if err != nil { 117 t.Fatal(err) 118 } 119 120 file, err := parser.ParseFile("data", data) 121 if err != nil { 122 t.Fatal(err) 123 } 124 125 if err := jsonpb.NewDecoder(inst.Value()).RewriteFile(file); err != nil { 126 t.Fatal(err) 127 } 128 129 b, err := format.Node(file) 130 if err != nil { 131 t.Fatal(err) 132 } 133 t.Error(string(b)) 134 }