aboutsummaryrefslogtreecommitdiff
path: root/src/go/build/read_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/go/build/read_test.go')
-rw-r--r--src/go/build/read_test.go91
1 files changed, 75 insertions, 16 deletions
diff --git a/src/go/build/read_test.go b/src/go/build/read_test.go
index 8636533f69..9264d2606f 100644
--- a/src/go/build/read_test.go
+++ b/src/go/build/read_test.go
@@ -5,7 +5,9 @@
package build
import (
+ "go/token"
"io"
+ "reflect"
"strings"
"testing"
)
@@ -13,12 +15,12 @@ import (
const quote = "`"
type readTest struct {
- // Test input contains ℙ where readImports should stop.
+ // Test input contains ℙ where readGoInfo should stop.
in string
err string
}
-var readImportsTests = []readTest{
+var readGoInfoTests = []readTest{
{
`package p`,
"",
@@ -37,15 +39,15 @@ var readImportsTests = []readTest{
},
{
`package p
-
+
// comment
-
+
import "x"
import _ "x"
import a "x"
-
+
/* comment */
-
+
import (
"x" /* comment */
_ "x"
@@ -59,7 +61,7 @@ var readImportsTests = []readTest{
import ()
import()import()import()
import();import();import()
-
+
ℙvar x = 1
`,
"",
@@ -85,7 +87,7 @@ var readCommentsTests = []readTest{
/* bar */
/* quux */ // baz
-
+
/*/ zot */
// asdf
@@ -127,8 +129,12 @@ func testRead(t *testing.T, tests []readTest, read func(io.Reader) ([]byte, erro
}
}
-func TestReadImports(t *testing.T) {
- testRead(t, readImportsTests, func(r io.Reader) ([]byte, error) { return readImports(r, true, nil) })
+func TestReadGoInfo(t *testing.T) {
+ testRead(t, readGoInfoTests, func(r io.Reader) ([]byte, error) {
+ var info fileInfo
+ err := readGoInfo(r, &info)
+ return info.header, err
+ })
}
func TestReadComments(t *testing.T) {
@@ -202,11 +208,6 @@ var readFailuresTests = []readTest{
},
}
-func TestReadFailures(t *testing.T) {
- // Errors should be reported (true arg to readImports).
- testRead(t, readFailuresTests, func(r io.Reader) ([]byte, error) { return readImports(r, true, nil) })
-}
-
func TestReadFailuresIgnored(t *testing.T) {
// Syntax errors should not be reported (false arg to readImports).
// Instead, entire file should be the output and no error.
@@ -219,5 +220,63 @@ func TestReadFailuresIgnored(t *testing.T) {
tt.err = ""
}
}
- testRead(t, tests, func(r io.Reader) ([]byte, error) { return readImports(r, false, nil) })
+ testRead(t, tests, func(r io.Reader) ([]byte, error) {
+ var info fileInfo
+ err := readGoInfo(r, &info)
+ return info.header, err
+ })
+}
+
+var readEmbedTests = []struct {
+ in string
+ out []string
+}{
+ {
+ "package p\n",
+ nil,
+ },
+ {
+ "package p\nimport \"embed\"\nvar i int\n//go:embed x y z\nvar files embed.FS",
+ []string{"x", "y", "z"},
+ },
+ {
+ "package p\nimport \"embed\"\nvar i int\n//go:embed x \"\\x79\" `z`\nvar files embed.FS",
+ []string{"x", "y", "z"},
+ },
+ {
+ "package p\nimport \"embed\"\nvar i int\n//go:embed x y\n//go:embed z\nvar files embed.FS",
+ []string{"x", "y", "z"},
+ },
+ {
+ "package p\nimport \"embed\"\nvar i int\n\t //go:embed x y\n\t //go:embed z\n\t var files embed.FS",
+ []string{"x", "y", "z"},
+ },
+ {
+ "package p\nimport \"embed\"\n//go:embed x y z\nvar files embed.FS",
+ []string{"x", "y", "z"},
+ },
+ {
+ "package p\n//go:embed x y z\n", // no import, no scan
+ nil,
+ },
+ {
+ "package p\n//go:embed x y z\nvar files embed.FS", // no import, no scan
+ nil,
+ },
+}
+
+func TestReadEmbed(t *testing.T) {
+ fset := token.NewFileSet()
+ for i, tt := range readEmbedTests {
+ var info fileInfo
+ info.fset = fset
+ err := readGoInfo(strings.NewReader(tt.in), &info)
+ if err != nil {
+ t.Errorf("#%d: %v", i, err)
+ continue
+ }
+ if !reflect.DeepEqual(info.embeds, tt.out) {
+ t.Errorf("#%d: embeds=%v, want %v", i, info.embeds, tt.out)
+ }
+ }
}