2016-02-11 11:57:16 -08:00
|
|
|
package vain
|
2016-02-07 23:54:55 -08:00
|
|
|
|
|
|
|
import (
|
2016-04-11 20:43:18 -07:00
|
|
|
"errors"
|
2016-02-07 23:54:55 -08:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestString(t *testing.T) {
|
2016-02-08 00:14:30 -08:00
|
|
|
p := Package{
|
2016-02-23 22:09:29 -08:00
|
|
|
Vcs: "git",
|
2016-04-11 20:43:18 -07:00
|
|
|
Path: "mcquay.me/bps",
|
2016-02-07 23:54:55 -08:00
|
|
|
Repo: "https://s.mcquay.me/sm/bps",
|
|
|
|
}
|
|
|
|
got := fmt.Sprintf("%s", p)
|
|
|
|
want := `<meta name="go-import" content="mcquay.me/bps git https://s.mcquay.me/sm/bps">`
|
|
|
|
if got != want {
|
|
|
|
t.Errorf(
|
|
|
|
"incorrect converstion to meta; got %s, want %s",
|
|
|
|
got,
|
|
|
|
want,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 22:09:29 -08:00
|
|
|
func TestSupportedVcsStrings(t *testing.T) {
|
2016-02-07 23:54:55 -08:00
|
|
|
tests := []struct {
|
2016-02-23 22:09:29 -08:00
|
|
|
in string
|
|
|
|
want bool
|
2016-02-07 23:54:55 -08:00
|
|
|
}{
|
2016-02-23 22:09:29 -08:00
|
|
|
{"hg", true},
|
|
|
|
{"git", true},
|
|
|
|
{"bzr", true},
|
|
|
|
|
|
|
|
{"", false},
|
|
|
|
{"bazar", false},
|
|
|
|
{"mercurial", false},
|
2016-02-07 23:54:55 -08:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2016-02-23 22:09:29 -08:00
|
|
|
if got, want := valid(test.in), test.want; got != want {
|
|
|
|
t.Errorf("%s: %t is incorrect validity", test.in, got)
|
2016-02-07 23:54:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-13 10:44:41 -08:00
|
|
|
|
|
|
|
func TestValid(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
pkgs []Package
|
|
|
|
in string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pkgs: []Package{},
|
|
|
|
in: "bobo",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkgs: []Package{
|
2016-04-11 20:43:18 -07:00
|
|
|
{Path: "bobo"},
|
2016-02-13 10:44:41 -08:00
|
|
|
},
|
|
|
|
in: "bobo",
|
2016-03-02 22:06:17 -08:00
|
|
|
want: false,
|
2016-02-13 10:44:41 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
pkgs: []Package{
|
2016-04-11 20:43:18 -07:00
|
|
|
{Path: "a/b/c"},
|
2016-02-13 10:44:41 -08:00
|
|
|
},
|
2016-03-02 22:06:17 -08:00
|
|
|
in: "a/b/c",
|
2016-02-13 10:44:41 -08:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkgs: []Package{
|
2016-04-11 20:43:18 -07:00
|
|
|
{Path: "a/b/c"},
|
2016-02-13 10:44:41 -08:00
|
|
|
},
|
2016-03-02 22:06:17 -08:00
|
|
|
in: "a/b",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkgs: []Package{
|
2016-04-11 20:43:18 -07:00
|
|
|
{Path: "name/db"},
|
|
|
|
{Path: "name/lib"},
|
2016-03-02 22:06:17 -08:00
|
|
|
},
|
|
|
|
in: "name/foo",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkgs: []Package{
|
2016-04-11 20:43:18 -07:00
|
|
|
{Path: "a"},
|
2016-03-02 22:06:17 -08:00
|
|
|
},
|
|
|
|
in: "a/b",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkgs: []Package{
|
2016-04-11 20:43:18 -07:00
|
|
|
{Path: "foo"},
|
2016-03-02 22:06:17 -08:00
|
|
|
},
|
|
|
|
in: "foo/bar",
|
2016-02-13 10:44:41 -08:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkgs: []Package{
|
2016-04-11 20:43:18 -07:00
|
|
|
{Path: "foo/bar"},
|
|
|
|
{Path: "foo/baz"},
|
2016-02-13 10:44:41 -08:00
|
|
|
},
|
|
|
|
in: "foo",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkgs: []Package{
|
2016-04-11 20:43:18 -07:00
|
|
|
{Path: "bilbo"},
|
|
|
|
{Path: "frodo"},
|
2016-02-13 10:44:41 -08:00
|
|
|
},
|
|
|
|
in: "foo/bar/baz",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
got := Valid(test.in, test.pkgs)
|
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("Incorrect testing of %q against %#v; got %t, want %t", test.in, test.pkgs, got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
func TestNamespaceParsing(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
want string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "/sm/foo",
|
|
|
|
want: "sm",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "/a/b/c/d",
|
|
|
|
want: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "/dm/bar",
|
|
|
|
want: "dm",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "/ud",
|
|
|
|
want: "ud",
|
|
|
|
},
|
|
|
|
// test stripping
|
|
|
|
{
|
|
|
|
input: "ud",
|
|
|
|
want: "ud",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "/",
|
|
|
|
err: errors.New("should find no namespace"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "",
|
|
|
|
err: errors.New("should find no namespace"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
got, err := parseNamespace(test.input)
|
|
|
|
if err != nil && test.err == nil {
|
|
|
|
t.Errorf("unexpected error parsing %q; got %q, want %q, error: %v", test.input, got, test.want, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("parse failure: got %q, want %q", got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|