...

Source file src/cuelang.org/go/cue/load/read_test.go

Documentation: cuelang.org/go/cue/load

     1  // Copyright 2018 The 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 load
    16  
    17  import (
    18  	"io"
    19  	"strings"
    20  	"testing"
    21  
    22  	"cuelang.org/go/cue/errors"
    23  )
    24  
    25  const quote = "`"
    26  
    27  type readTest struct {
    28  	// Test input contains ℙ where readImports should stop.
    29  	in  string
    30  	err string
    31  }
    32  
    33  var readImportsTests = []readTest{
    34  	{
    35  		`package p`,
    36  		"",
    37  	},
    38  	{
    39  		`package p; import "x"`,
    40  		"",
    41  	},
    42  	{
    43  		`package p; import . "x"`,
    44  		"",
    45  	},
    46  	{
    47  		`package p; import "x";ℙvar x = 1`,
    48  		"",
    49  	},
    50  	{
    51  		`package p
    52  
    53  		// comment
    54  
    55  		import "x"
    56  		import _ "x"
    57  		import a "x"
    58  
    59  		import (
    60  			"x"
    61  			_ "x"
    62  			a "x" // comment
    63  			` + quote + `x` + quote + `
    64  			_ ` + quote + `x` + quote + `
    65  			a ` + quote + `x` + quote + `
    66  		)
    67  		import (
    68  		)
    69  		import ()
    70  		import()import()import()
    71  		import();import();import()
    72  
    73  		ℙvar x = 1
    74  		`,
    75  		"",
    76  	},
    77  }
    78  
    79  func testRead(t *testing.T, tests []readTest, read func(io.Reader) ([]byte, errors.Error)) {
    80  	for i, tt := range tests {
    81  		var in, testOut string
    82  		j := strings.Index(tt.in, "ℙ")
    83  		if j < 0 {
    84  			in = tt.in
    85  			testOut = tt.in
    86  		} else {
    87  			in = tt.in[:j] + tt.in[j+len("ℙ"):]
    88  			testOut = tt.in[:j]
    89  		}
    90  		r := strings.NewReader(in)
    91  		buf, err := read(r)
    92  		if err != nil {
    93  			if tt.err == "" {
    94  				t.Errorf("#%d: err=%q, expected success (%q)", i, err, string(buf))
    95  				continue
    96  			}
    97  			if !strings.Contains(err.Error(), tt.err) {
    98  				t.Errorf("#%d: err=%q, expected %q", i, err, tt.err)
    99  				continue
   100  			}
   101  			continue
   102  		}
   103  		if tt.err != "" {
   104  			t.Errorf("#%d: success, expected %q", i, tt.err)
   105  			continue
   106  		}
   107  
   108  		out := string(buf)
   109  		if out != testOut {
   110  			t.Errorf("#%d: wrong output:\nhave %q\nwant %q\n", i, out, testOut)
   111  		}
   112  	}
   113  }
   114  
   115  func TestReadImports(t *testing.T) {
   116  	testRead(t, readImportsTests, func(r io.Reader) ([]byte, errors.Error) {
   117  		return readImports(r, true, nil)
   118  	})
   119  }
   120  
   121  var readFailuresTests = []readTest{
   122  	{
   123  		`package`,
   124  		"syntax error",
   125  	},
   126  	{
   127  		"package p\n\x00\nimport `math`\n",
   128  		"unexpected NUL in input",
   129  	},
   130  	{
   131  		`package p; import`,
   132  		"syntax error",
   133  	},
   134  	{
   135  		`package p; import "`,
   136  		"syntax error",
   137  	},
   138  	{
   139  		"package p; import ` \n\n",
   140  		"syntax error",
   141  	},
   142  	{
   143  		`package p; import "x`,
   144  		"syntax error",
   145  	},
   146  	{
   147  		`package p; import _`,
   148  		"syntax error",
   149  	},
   150  	{
   151  		`package p; import _ "`,
   152  		"syntax error",
   153  	},
   154  	{
   155  		`package p; import _ "x`,
   156  		"syntax error",
   157  	},
   158  	{
   159  		`package p; import .`,
   160  		"syntax error",
   161  	},
   162  	{
   163  		`package p; import . "`,
   164  		"syntax error",
   165  	},
   166  	{
   167  		`package p; import . "x`,
   168  		"syntax error",
   169  	},
   170  	{
   171  		`package p; import (`,
   172  		"syntax error",
   173  	},
   174  	{
   175  		`package p; import ("`,
   176  		"syntax error",
   177  	},
   178  	{
   179  		`package p; import ("x`,
   180  		"syntax error",
   181  	},
   182  	{
   183  		`package p; import ("x"`,
   184  		"syntax error",
   185  	},
   186  }
   187  
   188  func TestReadFailures(t *testing.T) {
   189  	// Errors should be reported (true arg to readImports).
   190  	testRead(t, readFailuresTests, func(r io.Reader) ([]byte, errors.Error) {
   191  		return readImports(r, true, nil)
   192  	})
   193  }
   194  
   195  func TestReadFailuresIgnored(t *testing.T) {
   196  	// Syntax errors should not be reported (false arg to readImports).
   197  	// Instead, entire file should be the output and no error.
   198  	// Convert tests not to return syntax errors.
   199  	tests := make([]readTest, len(readFailuresTests))
   200  	copy(tests, readFailuresTests)
   201  	for i := range tests {
   202  		tt := &tests[i]
   203  		if !strings.Contains(tt.err, "NUL") {
   204  			tt.err = ""
   205  		}
   206  	}
   207  	testRead(t, tests, func(r io.Reader) ([]byte, errors.Error) {
   208  		return readImports(r, false, nil)
   209  	})
   210  }
   211  

View as plain text