github.com/solo-io/cue@v0.4.7/encoding/protobuf/textproto/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 textproto_test 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/solo-io/cue/cue" 22 "github.com/solo-io/cue/cue/ast/astutil" 23 "github.com/solo-io/cue/cue/errors" 24 "github.com/solo-io/cue/cue/format" 25 "github.com/solo-io/cue/encoding/protobuf/textproto" 26 "github.com/solo-io/cue/internal/cuetest" 27 "github.com/solo-io/cue/internal/cuetxtar" 28 ) 29 30 func TestParse(t *testing.T) { 31 test := cuetxtar.TxTarTest{ 32 Root: "./testdata/decoder", 33 Name: "decode", 34 Update: cuetest.UpdateGoldenFiles, 35 } 36 37 r := cue.Runtime{} 38 39 d := textproto.NewDecoder() 40 41 test.Run(t, func(t *cuetxtar.Test) { 42 // TODO: use high-level API. 43 44 var schema cue.Value 45 var filename string 46 var b []byte 47 48 for _, f := range t.Archive.Files { 49 switch { 50 case strings.HasSuffix(f.Name, ".cue"): 51 inst, err := r.Compile(f.Name, f.Data) 52 if err != nil { 53 t.WriteErrors(errors.Promote(err, "test")) 54 return 55 } 56 schema = inst.Value() 57 58 case strings.HasSuffix(f.Name, ".textproto"): 59 filename = f.Name 60 b = f.Data 61 } 62 } 63 64 x, err := d.Parse(schema, filename, b) 65 if err != nil { 66 t.WriteErrors(errors.Promote(err, "test")) 67 return 68 } 69 70 f, err := astutil.ToFile(x) 71 if err != nil { 72 t.WriteErrors(errors.Promote(err, "test")) 73 } 74 b, err = format.Node(f) 75 if err != nil { 76 t.WriteErrors(errors.Promote(err, "test")) 77 } 78 _, _ = t.Write(b) 79 }) 80 }