-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgocomply_test.go
109 lines (104 loc) · 2.87 KB
/
gocomply_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"testing"
)
func TestParseGoImport(t *testing.T) {
type row struct {
input string
expected GoImport
expectedOK bool
}
tests := []row{
{
// ending in ">"
input: `<html><meta name="go-import" content="example.org/vanity git https://github.com/example/vanity"></html>`,
expected: GoImport{
ImportPrefix: "example.org/vanity",
Vcs: "git",
RepoRoot: "https://github.com/example/vanity",
},
expectedOK: true,
},
{
// ending in "/>"
input: `<html><meta name="go-import" content="example.org/vanity git https://github.com/example/vanity" /></html>`,
expected: GoImport{
ImportPrefix: "example.org/vanity",
Vcs: "git",
RepoRoot: "https://github.com/example/vanity",
},
expectedOK: true,
},
{
// unicode
input: `<html><meta name="go-import" content="example.org/本 git https://github.com/example/本" /></html>`,
expected: GoImport{
ImportPrefix: "example.org/本",
Vcs: "git",
RepoRoot: "https://github.com/example/本",
},
expectedOK: true,
},
{
// awkward whitespace but still valid
input: `<html>
< MeTa NaMe = "go-import"
CoNtEnT = "example.org/foo git https://github.com/example/foo" /></html>`,
expected: GoImport{
ImportPrefix: "example.org/foo",
Vcs: "git",
RepoRoot: "https://github.com/example/foo",
},
expectedOK: true,
},
}
for i, test := range tests {
gi, ok := parseGoImport(test.input)
if ok != test.expectedOK {
t.Errorf("test %d failed: parse error", i)
} else if gi != test.expected {
t.Errorf("test %d failed: expected %+v but got %+v",
i, test.expected, gi)
}
}
}
func TestParseGoSource(t *testing.T) {
type row struct {
input string
expected GoSource
expectedOK bool
}
tests := []row{
{
// real-world example
input: `<html><meta name="go-source" content="a b c d"></html>`,
expected: GoSource{
ImportPrefix: "a",
Home: "b",
Directory: "c",
File: "d",
},
expectedOK: true,
},
{
// real-world example
input: `<html><meta name="go-source" content="gopkg.in/natefinch/lumberjack.v2 _ https://github.com/natefinch/lumberjack/tree/v2.1{/dir} https://github.com/natefinch/lumberjack/blob/v2.1{/dir}/{file}#L{line}"></html>`,
expected: GoSource{
ImportPrefix: "gopkg.in/natefinch/lumberjack.v2",
Home: "_",
Directory: "https://github.com/natefinch/lumberjack/tree/v2.1{/dir}",
File: "https://github.com/natefinch/lumberjack/blob/v2.1{/dir}/{file}#L{line}",
},
expectedOK: true,
},
}
for i, test := range tests {
gs, ok := parseGoSource(test.input)
if ok != test.expectedOK {
t.Errorf("test %d failed: parse error", i)
} else if gs != test.expected {
t.Errorf("test %d failed: expected %+v but got %+v",
i, test.expected, gs)
}
}
}