github.com/solo-io/cue@v0.4.7/encoding/protobuf/textproto/encoder_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/errors" 23 "github.com/solo-io/cue/encoding/protobuf/textproto" 24 "github.com/solo-io/cue/internal/cuetest" 25 "github.com/solo-io/cue/internal/cuetxtar" 26 ) 27 28 func TestEncode(t *testing.T) { 29 test := cuetxtar.TxTarTest{ 30 Root: "./testdata/encoder", 31 Name: "encode", 32 Update: cuetest.UpdateGoldenFiles, 33 } 34 35 r := cue.Runtime{} 36 37 test.Run(t, func(t *cuetxtar.Test) { 38 // TODO: use high-level API. 39 40 var schema, value cue.Value 41 42 for _, f := range t.Archive.Files { 43 switch { 44 case strings.HasSuffix(f.Name, ".cue"): 45 inst, err := r.Compile(f.Name, f.Data) 46 if err != nil { 47 t.WriteErrors(errors.Promote(err, "test")) 48 return 49 } 50 switch f.Name { 51 case "schema.cue": 52 schema = inst.Value() 53 case "value.cue": 54 value = inst.Value() 55 } 56 } 57 } 58 59 v := schema.Unify(value) 60 if err := v.Err(); err != nil { 61 t.WriteErrors(errors.Promote(err, "test")) 62 return 63 } 64 65 b, err := textproto.NewEncoder().Encode(v) 66 if err != nil { 67 t.WriteErrors(errors.Promote(err, "test")) 68 return 69 } 70 _, _ = t.Write(b) 71 72 }) 73 }