-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities_test.go
More file actions
107 lines (93 loc) · 2.77 KB
/
Copy pathutilities_test.go
File metadata and controls
107 lines (93 loc) · 2.77 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
package formvalidator
import (
"testing"
)
func Test_appendError(t *testing.T) {
var a []FormError
b := &FormError{"Hello", []interface{}{1, 2}}
c := &FormError{"Goodbye", []interface{}{3, 4}}
a = appendError(a, b, c)
if len(a) != 2 {
t.Errorf("appendError(): Should return a slice with length of 2!")
}
}
func Test_inSlice(t *testing.T) {
var list = []struct {
field string
expectation bool
}{
{"abc", true},
{"123", true},
{"Abc", false},
{"@bc", false},
{"ãbc", false},
{"314", false},
{"-3.14", false},
}
testList := []string{"abc", "def", "ghi", "jkl", "123", "3.14"}
for _, l := range list {
valid := inSlice(testList, l.field)
if l.expectation != valid {
t.Errorf("inSlice(list, %s): Valid[%t]. Expected: %t", l.field, valid, l.expectation)
}
}
}
func Test_hasDuplicates(t *testing.T) {
var list = []struct {
field []string
expectation bool
}{
{[]string{"abc"}, false},
{[]string{"abc", "Abc"}, false},
{[]string{"abc", "@bc"}, false},
{[]string{"abc", "ãbc"}, false},
{[]string{"abc", "def", "ghi", "jkl"}, false},
{[]string{"123", "1.23"}, false},
{[]string{"123", "-123"}, false},
{[]string{"abc", "abc"}, true},
}
for _, l := range list {
valid := hasDuplicates(l.field)
if l.expectation != valid {
t.Errorf("hasDuplicates(%v): Valid[%t]. Expected: %t", l.field, valid, l.expectation)
}
}
}
func Test_sliceDiff(t *testing.T) {
var list = []struct {
s1 []string
s2 []string
expectation []string
}{
{[]string{"abc"}, []string{"abc"}, []string{}},
{[]string{"abc"}, []string{"ãbc"}, []string{"abc"}},
{[]string{"Abc"}, []string{"abc"}, []string{"Abc"}},
{[]string{"ãbc"}, []string{"abc"}, []string{"ãbc"}},
{[]string{"abc", "def", "ghi"}, []string{"abc"}, []string{"def", "ghi"}},
{[]string{"abc"}, []string{"abc", "def", "ghi"}, []string{}}, // order matters for sliceDiff()
}
for _, l := range list {
result := sliceDiff(l.s1, l.s2)
if len(l.expectation) != len(result) {
t.Errorf("sliceDiff(%v, %v): Result: %v, Expected: %v", l.s1, l.s2, result, l.expectation)
}
}
}
func Test_mustFormatCSVBytes(t *testing.T) {
list1 := []byte("abc,def,ghi,jkl")
list2 := []string{"abc", "def", "ghi", "jkl"}
result := mustFormatCSVBytes(list1)
if len(result) != len(list2) {
t.Errorf("mustFormatCSVBytes(): Result is different from expectation!")
}
}
func Test_getFirstKey(t *testing.T) {
list1 := []string{"abc", "def", "ghi"}
list2 := []string{"123", "456", "789"}
if getFirstKey(list1) != "abc" {
t.Errorf("getFirstKey(): Did not return the first value of the provided slice! Return[%s]", getFirstKey(list1))
}
if getFirstKey(list2) != "123" {
t.Errorf("getFirstKey(): Did not return the first value of the provided slice! Return[%s]", getFirstKey(list1))
}
}