-
Notifications
You must be signed in to change notification settings - Fork 3
/
import_test.go
131 lines (114 loc) · 3.81 KB
/
import_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package humanize
import (
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var imprt1 = `
package test
// Doc
import (
"testing"
onn "github.com/fzerorubigd/humanize"
_ "github.com/fzerorubigd/humanize/fixture/imprt"
// Other
. "github.com/smartystreets/goconvey/convey"
"github.com/fzerorubigd/humanize/fixture/imprt"
// Not a valid go package
"github.com/fzerorubigd/humanize/fixture/notvalid"
"invalid/package/name/haha"
)
`
func TestImport(t *testing.T) {
Convey("Import test ", t, func() {
var p = &Package{}
f, err := ParseFile(imprt1, p)
So(err, ShouldBeNil)
p.Files = append(p.Files, f)
So(p.Bind(), ShouldBeNil)
Convey("Import testing", func() {
i, err := p.FindImport("testing")
So(err, ShouldBeNil)
So(i.Package, ShouldEqual, "testing")
So(i.Canonical, ShouldEqual, "testing")
So(i.Path, ShouldEqual, "testing")
So(len(i.Docs), ShouldEqual, 1)
So(i.Docs[0], ShouldEqual, "// Doc")
So(i.String(), ShouldEqual, `"testing"`)
})
Convey("Import canonical", func() {
i, err := p.FindImport("onn")
So(err, ShouldBeNil)
i2, err := p.FindImport("github.com/fzerorubigd/humanize")
So(err, ShouldBeNil)
So(reflect.DeepEqual(i, i2), ShouldBeTrue)
So(i.Package, ShouldEqual, "humanize")
So(i.Canonical, ShouldEqual, "onn")
So(i.Path, ShouldEqual, "github.com/fzerorubigd/humanize")
So(len(i.Docs), ShouldEqual, 1)
So(i.Docs[0], ShouldEqual, "// Doc")
pk, err := i.LoadPackage()
So(err, ShouldBeNil)
So(pk.Path, ShouldEqual, "github.com/fzerorubigd/humanize")
So(i.String(), ShouldEqual, `onn "github.com/fzerorubigd/humanize"`)
})
Convey("Import pq", func() {
i, err := p.FindImport("github.com/fzerorubigd/humanize/fixture/imprt")
So(err, ShouldBeNil)
_, err = p.FindImport("_")
So(err, ShouldNotBeNil)
So(i.Canonical, ShouldEqual, "_")
So(i.Package, ShouldEqual, "imprt")
So(i.Path, ShouldEqual, "github.com/fzerorubigd/humanize/fixture/imprt")
So(len(i.Docs), ShouldEqual, 1)
So(i.Docs[0], ShouldEqual, "// Doc")
})
Convey("Import convey", func() {
i, err := p.FindImport("github.com/smartystreets/goconvey/convey")
So(err, ShouldBeNil)
So(i.Canonical, ShouldEqual, ".")
So(i.Package, ShouldEqual, "convey")
So(i.Path, ShouldEqual, "github.com/smartystreets/goconvey/convey")
So(len(i.Docs), ShouldEqual, 2)
So(i.Docs[0], ShouldEqual, "// Doc")
So(i.Docs[1], ShouldEqual, "// Other")
})
Convey("Import test", func() {
i, err := p.FindImport("imprt")
So(err, ShouldBeNil)
i2, err := p.FindImport("github.com/fzerorubigd/humanize/fixture/imprt")
So(err, ShouldBeNil)
So(reflect.DeepEqual(i, i2), ShouldBeTrue)
So(i.Package, ShouldEqual, "imprt")
So(i.Canonical, ShouldEqual, "_")
So(i.Path, ShouldEqual, "github.com/fzerorubigd/humanize/fixture/imprt")
So(len(i.Docs), ShouldEqual, 1)
So(i.Docs[0], ShouldEqual, "// Doc")
So(i.Folder, ShouldEndWith, "github.com/fzerorubigd/humanize/fixture/imprt")
})
Convey("Import invalid folder", func() {
i, err := p.FindImport("github.com/fzerorubigd/humanize/fixture/notvalid")
So(err, ShouldBeNil)
So(i.Package, ShouldEqual, "notvalid")
So(i.Canonical, ShouldEqual, "notvalid")
So(i.Path, ShouldEqual, "github.com/fzerorubigd/humanize/fixture/notvalid")
So(i.Folder, ShouldEqual, "")
pk, err := i.LoadPackage()
So(err, ShouldNotBeNil)
So(pk, ShouldBeNil)
})
Convey("Import not exist folder", func() {
i, err := p.FindImport("invalid/package/name/haha")
So(err, ShouldBeNil)
So(i.Package, ShouldEqual, "haha")
So(i.Canonical, ShouldEqual, "haha")
So(i.Path, ShouldEqual, "invalid/package/name/haha")
So(i.Folder, ShouldEqual, "")
})
Convey("Import incorrect", func() {
i, err := p.FindImport("i/am/not/in/the/source")
So(err, ShouldNotBeNil)
So(i, ShouldBeNil)
})
})
}