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.
15 type readTest
struct {
16 // Test input contains ℙ where readImports should stop.
21 var readImportsTests
= []readTest
{
27 `package p; import "x"`,
31 `package p; import . "x"`,
35 `package p; import "x";ℙvar x = 1`,
53 ` + quote
+ `x` + quote
+ `
54 _ /*comment*/ ` + quote
+ `x` + quote
+ `
55 a ` + quote
+ `x` + quote
+ `
60 import()import()import()
61 import();import();import()
69 var readCommentsTests
= []readTest
{
75 `ℙpackage p; import "x"`,
79 `ℙpackage p; import . "x"`,
97 func testRead(t
*testing
.T
, tests
[]readTest
, read
func(io
.Reader
) ([]byte, error
)) {
98 for i
, tt
:= range tests
{
99 var in
, testOut
string
100 j
:= strings
.Index(tt
.in
, "ℙ")
105 in
= tt
.in
[:j
] + tt
.in
[j
+len("ℙ"):]
108 r
:= strings
.NewReader(in
)
112 t
.Errorf("#%d: err=%q, expected success (%q)", i
, err
, string(buf
))
115 if !strings
.Contains(err
.Error(), tt
.err
) {
116 t
.Errorf("#%d: err=%q, expected %q", i
, err
, tt
.err
)
121 if err
== nil && tt
.err
!= "" {
122 t
.Errorf("#%d: success, expected %q", i
, tt
.err
)
128 t
.Errorf("#%d: wrong output:\nhave %q\nwant %q\n", i
, out
, testOut
)
133 func TestReadImports(t
*testing
.T
) {
134 testRead(t
, readImportsTests
, func(r io
.Reader
) ([]byte, error
) { return readImports(r
, true, nil) })
137 func TestReadComments(t
*testing
.T
) {
138 testRead(t
, readCommentsTests
, readComments
)
141 var readFailuresTests
= []readTest
{
147 "package p\n\x00\nimport `math`\n",
148 "unexpected NUL in input",
155 `package p; import "`,
159 "package p; import ` \n\n",
163 `package p; import "x`,
167 `package p; import _`,
171 `package p; import _ "`,
175 `package p; import _ "x`,
179 `package p; import .`,
183 `package p; import . "`,
187 `package p; import . "x`,
191 `package p; import (`,
195 `package p; import ("`,
199 `package p; import ("x`,
203 `package p; import ("x"`,
208 func TestReadFailures(t
*testing
.T
) {
209 // Errors should be reported (true arg to readImports).
210 testRead(t
, readFailuresTests
, func(r io
.Reader
) ([]byte, error
) { return readImports(r
, true, nil) })
213 func TestReadFailuresIgnored(t
*testing
.T
) {
214 // Syntax errors should not be reported (false arg to readImports).
215 // Instead, entire file should be the output and no error.
216 // Convert tests not to return syntax errors.
217 tests
:= make([]readTest
, len(readFailuresTests
))
218 copy(tests
, readFailuresTests
)
219 for i
:= range tests
{
221 if !strings
.Contains(tt
.err
, "NUL") {
225 testRead(t
, tests
, func(r io
.Reader
) ([]byte, error
) { return readImports(r
, false, nil) })