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