...

Source file src/cuelang.org/go/encoding/protobuf/textproto/decoder_test.go

Documentation: cuelang.org/go/encoding/protobuf/textproto

     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  	r := cue.Runtime{}
    36  
    37  	d := textproto.NewDecoder()
    38  
    39  	test.Run(t, func(t *cuetxtar.Test) {
    40  		// TODO: use high-level API.
    41  
    42  		var schema cue.Value
    43  		var filename string
    44  		var b []byte
    45  
    46  		for _, f := range t.Archive.Files {
    47  			switch {
    48  			case strings.HasSuffix(f.Name, ".cue"):
    49  				inst, err := r.Compile(f.Name, f.Data)
    50  				if err != nil {
    51  					t.WriteErrors(errors.Promote(err, "test"))
    52  					return
    53  				}
    54  				schema = inst.Value()
    55  
    56  			case strings.HasSuffix(f.Name, ".textproto"):
    57  				filename = f.Name
    58  				b = f.Data
    59  			}
    60  		}
    61  
    62  		x, err := d.Parse(schema, filename, b)
    63  		if err != nil {
    64  			t.WriteErrors(errors.Promote(err, "test"))
    65  			return
    66  		}
    67  
    68  		f, err := astutil.ToFile(x)
    69  		if err != nil {
    70  			t.WriteErrors(errors.Promote(err, "test"))
    71  		}
    72  		b, err = format.Node(f)
    73  		if err != nil {
    74  			t.WriteErrors(errors.Promote(err, "test"))
    75  		}
    76  		_, _ = t.Write(b)
    77  	})
    78  }
    79  

View as plain text