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