github.com/aca02djr/gb@v0.4.1/cmd/gb/import_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "path/filepath" 6 "reflect" 7 "testing" 8 ) 9 10 type context struct { 11 projectdir string 12 allpkgs []string 13 } 14 15 func (c *context) Projectdir() string { return c.projectdir } 16 17 func (c *context) AllPackages(pattern string) ([]string, error) { 18 return c.allpkgs, nil 19 } 20 21 func testdata(args ...string) string { 22 cwd, _ := os.Getwd() 23 return filepath.Join(append([]string{cwd, "testdata"}, args...)...) 24 } 25 26 // l constructs a []string 27 func l(args ...string) []string { 28 return args 29 } 30 31 // p constructs a path 32 func p(args ...string) string { 33 return filepath.Join(args...) 34 } 35 36 func TestImportPaths(t *testing.T) { 37 var tests = []struct { 38 ctx context 39 cwd string 40 args []string 41 want []string 42 }{ 43 { 44 ctx: context{ 45 allpkgs: l("a", "b", "c", p("c", "d")), 46 }, 47 cwd: testdata("src"), 48 args: nil, 49 want: l("a", "b", "c", p("c", "d")), 50 }, { 51 ctx: context{ 52 allpkgs: l("a", "b", "c", p("c", "d")), 53 }, 54 cwd: testdata("src"), 55 args: l("..."), 56 want: l("a", "b", "c", p("c", "d")), 57 }, { 58 ctx: context{ 59 allpkgs: l("c", p("c", "d")), 60 }, 61 cwd: testdata("src", "c"), 62 args: nil, 63 want: l("c", p("c", "d")), 64 }, { 65 ctx: context{ 66 allpkgs: l("a", "b", "c", p("c", "d")), 67 }, 68 cwd: testdata("src"), 69 args: l("c"), 70 want: l("c"), 71 }, { 72 ctx: context{ 73 allpkgs: l("a", "b", "c", p("c", "d")), 74 }, 75 cwd: testdata("src"), 76 args: l("c", "b"), 77 want: l("c", "b"), 78 }, { 79 ctx: context{ 80 allpkgs: l("c", p("c", "d")), 81 }, 82 cwd: testdata("src"), 83 args: l("c/..."), 84 want: l("c", p("c", "d")), 85 }, 86 } 87 for _, tt := range tests { 88 got := importPaths(&tt.ctx, tt.cwd, tt.args) 89 if !reflect.DeepEqual(got, tt.want) { 90 t.Errorf("ImportPaths(%v): got %v, want %v", tt.args, got, tt.want) 91 } 92 } 93 }