diff options
Diffstat (limited to 'go/packages/packages_test.go')
-rw-r--r-- | go/packages/packages_test.go | 124 |
1 files changed, 64 insertions, 60 deletions
diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 9e12c20a6..4e1efb69b 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -1025,78 +1025,82 @@ func testNewPackagesInOverlay(t *testing.T, exporter packagestest.Exporter) { dir := filepath.Dir(filepath.Dir(exported.File("golang.org/fake", "a/a.go"))) - for i, test := range []struct { + for _, test := range []struct { + name string overlay map[string][]byte want string // expected value of e.E }{ - // Overlay with one file. - {map[string][]byte{ - filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/a"; const E = "e" + a.A`)}, + {"one_file", + map[string][]byte{ + filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/a"; const E = "e" + a.A`)}, `"eabc"`}, - // Overlay with multiple files in the same package. - {map[string][]byte{ - filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/a"; const E = "e" + a.A + underscore`), - filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), - }, + {"multiple_files_same_package", + map[string][]byte{ + filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/a"; const E = "e" + a.A + underscore`), + filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), + }, `"eabc_"`}, - // Overlay with multiple files in different packages. - {map[string][]byte{ - filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/f"; const E = "e" + f.F + underscore`), - filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), - filepath.Join(dir, "f", "f.go"): []byte(`package f; const F = "f"`), - }, + {"multiple_files_two_packages", + map[string][]byte{ + filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/f"; const E = "e" + f.F + underscore`), + filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), + filepath.Join(dir, "f", "f.go"): []byte(`package f; const F = "f"`), + }, `"ef_"`}, - {map[string][]byte{ - filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/f"; const E = "e" + f.F + underscore`), - filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), - filepath.Join(dir, "f", "f.go"): []byte(`package f; import "golang.org/fake/g"; const F = "f" + g.G`), - filepath.Join(dir, "g", "g.go"): []byte(`package g; const G = "g"`), - }, + {"multiple_files_three_packages", + map[string][]byte{ + filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/f"; const E = "e" + f.F + underscore`), + filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), + filepath.Join(dir, "f", "f.go"): []byte(`package f; import "golang.org/fake/g"; const F = "f" + g.G`), + filepath.Join(dir, "g", "g.go"): []byte(`package g; const G = "g"`), + }, `"efg_"`}, - {map[string][]byte{ - filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/f"; import "golang.org/fake/h"; const E = "e" + f.F + h.H + underscore`), - filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), - filepath.Join(dir, "f", "f.go"): []byte(`package f; import "golang.org/fake/g"; const F = "f" + g.G`), - filepath.Join(dir, "g", "g.go"): []byte(`package g; const G = "g"`), - filepath.Join(dir, "h", "h.go"): []byte(`package h; const H = "h"`), - }, + {"multiple_files_four_packages", + map[string][]byte{ + filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/f"; import "golang.org/fake/h"; const E = "e" + f.F + h.H + underscore`), + filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), + filepath.Join(dir, "f", "f.go"): []byte(`package f; import "golang.org/fake/g"; const F = "f" + g.G`), + filepath.Join(dir, "g", "g.go"): []byte(`package g; const G = "g"`), + filepath.Join(dir, "h", "h.go"): []byte(`package h; const H = "h"`), + }, `"efgh_"`}, - {map[string][]byte{ - filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/f"; const E = "e" + f.F + underscore`), - filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), - filepath.Join(dir, "f", "f.go"): []byte(`package f; import "golang.org/fake/g"; const F = "f" + g.G`), - filepath.Join(dir, "g", "g.go"): []byte(`package g; import "golang.org/fake/h"; const G = "g" + h.H`), - filepath.Join(dir, "h", "h.go"): []byte(`package h; const H = "h"`), - }, + {"multiple_files_four_packages_again", + map[string][]byte{ + filepath.Join(dir, "e", "e.go"): []byte(`package e; import "golang.org/fake/f"; const E = "e" + f.F + underscore`), + filepath.Join(dir, "e", "e_util.go"): []byte(`package e; const underscore = "_"`), + filepath.Join(dir, "f", "f.go"): []byte(`package f; import "golang.org/fake/g"; const F = "f" + g.G`), + filepath.Join(dir, "g", "g.go"): []byte(`package g; import "golang.org/fake/h"; const G = "g" + h.H`), + filepath.Join(dir, "h", "h.go"): []byte(`package h; const H = "h"`), + }, `"efgh_"`}, - // Overlay with package main. - {map[string][]byte{ - filepath.Join(dir, "e", "main.go"): []byte(`package main; import "golang.org/fake/a"; const E = "e" + a.A; func main(){}`)}, + {"main_overlay", + map[string][]byte{ + filepath.Join(dir, "e", "main.go"): []byte(`package main; import "golang.org/fake/a"; const E = "e" + a.A; func main(){}`)}, `"eabc"`}, } { - exported.Config.Overlay = test.overlay - exported.Config.Mode = packages.LoadAllSyntax - exported.Config.Logf = t.Logf - - // With an overlay, we don't know the expected import path, - // so load with the absolute path of the directory. - initial, err := packages.Load(exported.Config, filepath.Join(dir, "e")) - if err != nil { - t.Error(err) - continue - } + t.Run(test.name, func(t *testing.T) { + exported.Config.Overlay = test.overlay + exported.Config.Mode = packages.LoadAllSyntax + exported.Config.Logf = t.Logf + + // With an overlay, we don't know the expected import path, + // so load with the absolute path of the directory. + initial, err := packages.Load(exported.Config, filepath.Join(dir, "e")) + if err != nil { + t.Fatal(err) + } - // Check value of e.E. - e := initial[0] - eE := constant(e, "E") - if eE == nil { - t.Errorf("%d. e.E: got nil", i) - continue - } - got := eE.Val().String() - if got != test.want { - t.Errorf("%d. e.E: got %s, want %s", i, got, test.want) - } + // Check value of e.E. + e := initial[0] + eE := constant(e, "E") + if eE == nil { + t.Fatalf("e.E: was nil in %#v", e) + } + got := eE.Val().String() + if got != test.want { + t.Fatalf("e.E: got %s, want %s", got, test.want) + } + }) } } |