...

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

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

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

View as plain text