pkger/here/parse_test.go

64 lines
1.5 KiB
Go
Raw Normal View History

2019-10-22 16:54:50 +03:00
package here_test
import (
2019-11-01 00:20:15 +03:00
"os"
2019-10-22 16:54:50 +03:00
"path/filepath"
"strings"
"testing"
"github.com/markbates/pkger/here"
2019-10-30 18:31:39 +03:00
"github.com/markbates/pkger/pkging/costello"
2019-10-22 16:54:50 +03:00
"github.com/stretchr/testify/require"
)
func Test_Info_Parse(t *testing.T) {
const name = "/public/index.html"
r := require.New(t)
2019-10-30 18:31:39 +03:00
app, err := costello.NewRef()
2019-10-22 16:54:50 +03:00
r.NoError(err)
2019-11-01 00:20:15 +03:00
defer os.RemoveAll(app.Dir)
2019-10-22 16:54:50 +03:00
ip := app.Info.ImportPath
ip2 := "another/app"
table := []struct {
in string
exp here.Path
err bool
}{
{in: name, exp: here.Path{Pkg: ip, Name: name}},
{in: "", exp: here.Path{Pkg: ip, Name: "/"}},
{in: "/", exp: here.Path{Pkg: ip, Name: "/"}},
{in: filepath.Join(app.Info.Dir, name), exp: here.Path{Pkg: ip, Name: name}},
{in: ":" + name, exp: here.Path{Pkg: ip, Name: name}},
{in: ip + ":" + name, exp: here.Path{Pkg: ip, Name: name}},
{in: ip, exp: here.Path{Pkg: ip, Name: "/"}},
{in: ":", exp: here.Path{Pkg: ip, Name: "/"}},
{in: ip2 + ":" + name, exp: here.Path{Pkg: ip2, Name: name}},
{in: ip2, exp: here.Path{Pkg: ip2, Name: "/"}},
{in: ip2 + ":", exp: here.Path{Pkg: ip2, Name: "/"}},
2019-10-24 23:22:15 +03:00
{in: filepath.Join(app.Info.Dir, "public"), exp: here.Path{Pkg: ip, Name: "/public"}},
2019-10-22 16:54:50 +03:00
}
for _, tt := range table {
for _, in := range []string{tt.in, strings.ReplaceAll(tt.in, "/", "\\")} {
t.Run(in, func(st *testing.T) {
r := require.New(st)
pt, err := app.Info.Parse(in)
if tt.err {
r.Error(err)
return
}
r.NoError(err)
r.Equal(tt.exp, pt)
})
}
}
}