github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/imports/read_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Copied from Go distribution src/go/build/read.go. 6 7 package imports 8 9 import ( 10 "io" 11 "strings" 12 "testing" 13 ) 14 15 const quote = "`" 16 17 type readTest struct { 18 // Test input contains ℙ where readImports should stop. 19 in string 20 err string 21 } 22 23 var readImportsTests = []readTest{ 24 { 25 `package p`, 26 "", 27 }, 28 { 29 `package p; import "x"`, 30 "", 31 }, 32 { 33 `package p; import . "x"`, 34 "", 35 }, 36 { 37 `package p; import "x";ℙvar x = 1`, 38 "", 39 }, 40 { 41 `package p 42 43 // comment 44 45 import "x" 46 import _ "x" 47 import a "x" 48 49 /* comment */ 50 51 import ( 52 "x" /* comment */ 53 _ "x" 54 a "x" // comment 55 ` + quote + `x` + quote + ` 56 _ /*comment*/ ` + quote + `x` + quote + ` 57 a ` + quote + `x` + quote + ` 58 ) 59 import ( 60 ) 61 import () 62 import()import()import() 63 import();import();import() 64 65 ℙvar x = 1 66 `, 67 "", 68 }, 69 } 70 71 var readCommentsTests = []readTest{ 72 { 73 `ℙpackage p`, 74 "", 75 }, 76 { 77 `ℙpackage p; import "x"`, 78 "", 79 }, 80 { 81 `ℙpackage p; import . "x"`, 82 "", 83 }, 84 { 85 `// foo 86 87 /* bar */ 88 89 /* quux */ // baz 90 91 /*/ zot */ 92 93 // asdf 94 ℙHello, world`, 95 "", 96 }, 97 } 98 99 func testRead(t *testing.T, tests []readTest, read func(io.Reader) ([]byte, error)) { 100 for i, tt := range tests { 101 var in, testOut string 102 j := strings.Index(tt.in, "ℙ") 103 if j < 0 { 104 in = tt.in 105 testOut = tt.in 106 } else { 107 in = tt.in[:j] + tt.in[j+len("ℙ"):] 108 testOut = tt.in[:j] 109 } 110 r := strings.NewReader(in) 111 buf, err := read(r) 112 if err != nil { 113 if tt.err == "" { 114 t.Errorf("#%d: err=%q, expected success (%q)", i, err, string(buf)) 115 continue 116 } 117 if !strings.Contains(err.Error(), tt.err) { 118 t.Errorf("#%d: err=%q, expected %q", i, err, tt.err) 119 continue 120 } 121 continue 122 } 123 if err == nil && tt.err != "" { 124 t.Errorf("#%d: success, expected %q", i, tt.err) 125 continue 126 } 127 128 out := string(buf) 129 if out != testOut { 130 t.Errorf("#%d: wrong output:\nhave %q\nwant %q\n", i, out, testOut) 131 } 132 } 133 } 134 135 func TestReadImports(t *testing.T) { 136 testRead(t, readImportsTests, func(r io.Reader) ([]byte, error) { return ReadImports(r, true, nil) }) 137 } 138 139 func TestReadComments(t *testing.T) { 140 testRead(t, readCommentsTests, ReadComments) 141 } 142 143 var readFailuresTests = []readTest{ 144 { 145 `package`, 146 "syntax error", 147 }, 148 { 149 "package p\n\x00\nimport `math`\n", 150 "unexpected NUL in input", 151 }, 152 { 153 `package p; import`, 154 "syntax error", 155 }, 156 { 157 `package p; import "`, 158 "syntax error", 159 }, 160 { 161 "package p; import ` \n\n", 162 "syntax error", 163 }, 164 { 165 `package p; import "x`, 166 "syntax error", 167 }, 168 { 169 `package p; import _`, 170 "syntax error", 171 }, 172 { 173 `package p; import _ "`, 174 "syntax error", 175 }, 176 { 177 `package p; import _ "x`, 178 "syntax error", 179 }, 180 { 181 `package p; import .`, 182 "syntax error", 183 }, 184 { 185 `package p; import . "`, 186 "syntax error", 187 }, 188 { 189 `package p; import . "x`, 190 "syntax error", 191 }, 192 { 193 `package p; import (`, 194 "syntax error", 195 }, 196 { 197 `package p; import ("`, 198 "syntax error", 199 }, 200 { 201 `package p; import ("x`, 202 "syntax error", 203 }, 204 { 205 `package p; import ("x"`, 206 "syntax error", 207 }, 208 } 209 210 func TestReadFailures(t *testing.T) { 211 // Errors should be reported (true arg to readImports). 212 testRead(t, readFailuresTests, func(r io.Reader) ([]byte, error) { return ReadImports(r, true, nil) }) 213 } 214 215 func TestReadFailuresIgnored(t *testing.T) { 216 // Syntax errors should not be reported (false arg to readImports). 217 // Instead, entire file should be the output and no error. 218 // Convert tests not to return syntax errors. 219 tests := make([]readTest, len(readFailuresTests)) 220 copy(tests, readFailuresTests) 221 for i := range tests { 222 tt := &tests[i] 223 if !strings.Contains(tt.err, "NUL") { 224 tt.err = "" 225 } 226 } 227 testRead(t, tests, func(r io.Reader) ([]byte, error) { return ReadImports(r, false, nil) }) 228 }