示例#1
0
func TestProcessModes(t *testing.T) {
	data := []struct {
		in       string
		expected []checks.Mode
		err      error
	}{
		{"all", []checks.Mode{checks.ContinuousIntegration, checks.Lint}, nil},
		{"", nil, nil},
		{"pc", []checks.Mode{checks.PreCommit}, nil},
		{"fast", []checks.Mode{checks.PreCommit}, nil},
		{"pp", []checks.Mode{checks.PrePush}, nil},
		{"slow", []checks.Mode{checks.PrePush}, nil},
		{"ci", []checks.Mode{checks.ContinuousIntegration}, nil},
		{"full", []checks.Mode{checks.ContinuousIntegration}, nil},
		{"foo", nil, errors.New("invalid mode \"foo\"\n\n" + helpModes)},
	}
	for i, line := range data {
		actual, err := processModes(line.in)
		ut.AssertEqualIndex(t, i, line.expected, actual)
		ut.AssertEqualIndex(t, i, line.err, err)
	}
}
示例#2
0
func TestIsMainPackage(t *testing.T) {
	t.Parallel()
	data := []struct {
		expected string
		in       string
	}{
		{"foo", "// Hi\npackage foo\n"},
		{"main", "package main\n"},
		{"", ""},
	}
	for i, line := range data {
		ut.AssertEqualIndex(t, i, line.expected, getPackageName([]byte(line.in)))
	}
}
示例#3
0
func TestGetImports(t *testing.T) {
	t.Parallel()
	data := []struct {
		in      string
		pkg     string
		imports []string
	}{
		{
			"",
			"",
			nil,
		},
		{
			"package foo",
			"foo",
			nil,
		},
		{
			"package foo\nfunc bar() {}",
			"foo",
			nil,
		},
		{
			"package foo\nconst i = 0",
			"foo",
			nil,
		},
		{
			"const i = 0",
			"",
			nil,
		},
		{
			"package foo\nimport \"bar\"",
			"foo",
			[]string{"bar"},
		},
		{
			"package foo\nimport \"host/user/repo\"",
			"foo",
			[]string{"host/user/repo"},
		},
		{
			"//\npackage foo\n//\n\nimport \"bar\"",
			"foo",
			[]string{"bar"},
		},
		{
			"package foo\nimport \"bar\"\nconst i = 0",
			"foo",
			[]string{"bar"},
		},
		{
			"package foo\nimport (\n\"bar\"\n)",
			"foo",
			[]string{"bar"},
		},
		{
			"package foo\nimport (\n\"bér\"\n)",
			"foo",
			[]string{"bér"},
		},
		{
			"package foo\nimport (\n\"b\u00e8r\"\n)",
			"foo",
			[]string{"bèr"},
		},
		{
			// This is not legal Go.
			"package foo\nimport (\n\"bér\" \"ber\"\n)",
			"foo",
			[]string{"bér", "ber"},
		},
		{
			"package foo\nimport (\n\"bar\"\n)\nconst i = 0",
			"foo",
			[]string{"bar"},
		},
		{
			"package foo\nimport (\n\t\"bar\"\n\n\t// Yo\n\"baz\"\n  )",
			"foo",
			[]string{"bar", "baz"},
		},
		{
			"package foo\nimport (\n  . \"bar\"\n)",
			"foo",
			[]string{"bar"},
		},
		{
			"package foo\nimport (\n  _ \"bar\"\n)",
			"foo",
			[]string{"bar"},
		},
		{
			"package foo\nimport (\n  fakename \"bar\"\n)",
			"foo",
			[]string{"bar"},
		},
	}

	for i, line := range data {
		pkg, imports := getImports([]byte(line.in))
		ut.AssertEqualIndex(t, i, line.pkg, pkg)
		ut.AssertEqualIndex(t, i, line.imports, imports)
	}
}