-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_test.go
More file actions
182 lines (156 loc) · 4.69 KB
/
start_test.go
File metadata and controls
182 lines (156 loc) · 4.69 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"os"
"path/filepath"
"testing"
)
func assertString(t *testing.T, desc string, expected string, result string) {
if expected != result {
t.Log("expected: %s, result: %s\n", expected, result)
t.Error(desc)
}
}
func Test_stringsToMap_Nothing(t *testing.T) {
m := stringsToMap("", ",")
if len(m) != 0 {
t.Error("Verify no map key-value found.")
}
}
func Test_stringsToMap_Simple(t *testing.T) {
m := stringsToMap("foo=bar", ",")
if m["foo"] != "bar" {
t.Error("Verify there is a key value pair.")
}
}
func Test_stringsToMap_KeyOnly(t *testing.T) {
m := stringsToMap("foo", ",")
if m["foo"] != "" {
t.Error("Verify no value is set")
}
m = stringsToMap("foo=", ",")
if m["foo"] != "" {
t.Error("Verify no value is set")
}
}
func Test_stringsToMap_MultiKeywords(t *testing.T) {
m := stringsToMap("foo=bar,bar=hoge", ",")
if len(m) != 2 {
t.Error("Verify there are two entries.")
}
if m["foo"] != "bar" {
t.Error("Verify there is a key value pair.")
}
if m["bar"] != "hoge" {
t.Error("Verify there is a key value pair.")
}
}
func Test_newSourceAccess_URL(t *testing.T) {
sa := newSourceAccess("https://github.com/hata/gorep")
if sa == nil {
t.Error("Verify https protocol should return SourceAccess for github")
}
}
func Test_newSourceAccess_File(t *testing.T) {
sa := newSourceAccess("/tmp")
if sa == nil {
t.Error("Verify a local file should return SourceAccess for github")
}
}
func Test_isMatchSuffixes_matched(t *testing.T) {
if !isMatchSuffixes([]string{".foo", ".txt"}, "/tmp/test.txt") {
t.Error("Verify .txt should be matched.")
}
}
func Test_isMatchSuffixes_unmatched(t *testing.T) {
if isMatchSuffixes([]string{".foo"}, "/tmp/test.txt") {
t.Error("Verify unmatched suffix")
}
}
func Test_isMatchSuffixes_wildcard(t *testing.T) {
if !isMatchSuffixes([]string{"*"}, "/tmp/test.txt") {
t.Error("Verify wildcard match everything")
}
}
func Test_isDirectory_not_found(t *testing.T) {
dir, err := isDirectory("/tmp_not_found")
if dir {
t.Error("Failed to validate there is a dir or not")
}
if err == nil {
t.Error("Error should be found %s", err)
}
}
func Test_isDirectory_found_dir(t *testing.T) {
dir, err := isDirectory("/tmp")
if !dir {
t.Error("/tmp should be found.")
}
if err != nil {
t.Error("/tmp should be found.")
}
}
func Test_isDirectory_found_file(t *testing.T) {
curDir, _ := os.Getwd()
curFile := curDir + "/file_test.go"
dir, err := isDirectory(curFile)
if dir {
t.Error("Current file should be exist. %s", curFile)
}
if err != nil {
t.Error("Current file should be found. %s", curFile)
}
}
func Test_normalizePath_dir(t *testing.T) {
path := normalizePath("/tmp", true)
assertString(t, "normalizePath should have / at the end of a directory path", "/tmp/", path)
}
func Test_normalizePath_file(t *testing.T) {
curDir, _ := os.Getwd()
path := normalizePath(curDir + "/file_test.go", false)
path = normalizePath(path, false)
assertString(t, "normalizePath should not add / at the end of a file path", "file_test.go", filepath.Base(path))
}
func Test_toSubPath(t *testing.T) {
path := toSubPath("/tmp/", "/tmp/foo")
if path != "foo" {
t.Error("Verify sub path is an expected value")
}
}
func Test_newReplaceFunc_contents(t *testing.T) {
rf := newReplaceFunc(map[string]string{"foo":"bar"})
subPath, contents, err := rf("subPath", "foo,foo,foo,bar,bar,bar")
if subPath != "subPath" {
t.Error("Verify subPath is returned.")
}
if contents != "bar,bar,bar,bar,bar,bar" {
t.Error("Verify replacing keywords work well.")
}
if err != nil {
t.Error("Verify no error found")
}
}
func Test_newReplaceFunc_subPath(t *testing.T) {
rf := newReplaceFunc(map[string]string{"foo":"bar"})
subPath, contents, err := rf("foo/foo", "foo,bar,fo")
if subPath != "bar/bar" {
t.Error("Verify bar/bar is returned.")
}
if contents != "bar,bar,fo" {
t.Error("Verify replacing keywords work well.")
}
if err != nil {
t.Error("Verify no error found")
}
}
func Test_toList(t *testing.T) {
list := toList("a,b,c", ",")
if !(list[0] == "a" && list[1] == "b" && list[2] == "c") {
t.Error("Verify list is separated correctly")
}
}
func Test_toList_Space(t *testing.T) {
list := toList(" a,b , c ", ",")
if !(list[0] == "a" && list[1] == "b" && list[2] == "c") {
t.Error("Verify list is separated correctly")
}
}