Completed
Push — master ( b483f1...082c99 )
by Valentin
02:29
created

util.TestFetchKeyword   A

Complexity

Conditions 4

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nop 1
dl 0
loc 19
rs 9.65
c 0
b 0
f 0
1
package util
2
3
import (
4
	"testing"
5
)
6
7
func TestPathsEqual(t *testing.T) {
8
	type check struct {
9
		a, b string
10
		exp  bool
11
	}
12
13
	set := []check{
14
		{"file\\with/different/slashes\\usage", "file/with\\different/slashes/usage", true},
15
		{"file\\with/different/slashes\\usage", "file/with/different/slashes/usage", true},
16
		{"file/with/different/slashes/usage", "file\\with\\different\\slashes\\usage", true},
17
		{"file\\with\\different\\slashes\\usage", "file\\with\\different\\slashes\\usage", true},
18
		{"file/with/different/slashes/usage", "file/with/different/slashes/usage", true},
19
		{"file/with/different/slashes/usage", "file/with/different/slashes/usage/", true},
20
		{"file/with/different/slashes/usage\\", "file/with/different/slashes/usage/", true},
21
		{"file/with/different/slashes/usage\\", "file/with/different/slashes/usage", true},
22
		{"file/with/different/slashes/usage\\", "file/with/different/slashes/usage/oops", false},
23
	}
24
25
	for i, s := range set {
26
		c := PathsEqual(s.a, s.b)
27
		if c != s.exp {
28
			t.Errorf("values compare failed (line `%d`):\ninput `%s`, `%s`\ngot `%t` \nexpected `%t`", i, s.a, s.b, c, s.exp)
29
		}
30
	}
31
}
32
33
func TestTokenizer(t *testing.T) {
34
	set := map[string][]string{
35
		"a,b,,c, d": {"a", "b", "c", "d"},
36
		"a,b,a":     {"a", "b", "a"},
37
		`a,"b,c",d`: {"a", "d", `b,c`},
38
		"a;b,c":     {"a", "b", "c"},
39
		`a;b,c"`:    {"a", "b", "c"},
40
	}
41
42
	for str, exp := range set {
43
		tokens := SplitKeywords(str)
44
		if !Equal(exp, tokens) {
45
			t.Errorf("tokens not equal:\ngot `%s`\nexpected `%s`", tokens, exp)
46
		}
47
	}
48
}
49
50
func TestFetchKeyword(t *testing.T) {
51
	type check struct {
52
		s, exp     string
53
		start, end int
54
	}
55
56
	set := []check{
57
		{"a,b,,c, d", "a,b,,c, d", 0, 100},
58
		{"a,b,,c, d", "a", 0, 1},
59
		{"a,b,,c, d", "a", 0, 2},
60
		{"a,b,,c, d", "a,b", 0, 5},
61
		{"a,b,,c, d", "", 3, 5},
62
		{"a,b,,c, d", "", 3, 3},
63
	}
64
65
	for i, s := range set {
66
		f := fetchKeyword(s.s, s.start, s.end)
67
		if f != s.exp {
68
			t.Errorf("keyword cut failed (line `%d`):\ngot `%s`\nexpected `%s`", i, f, s.exp)
69
		}
70
	}
71
}
72
73
func TestAdjustSize(t *testing.T) {
74
	type check struct {
75
		n, d, min, an, ad int
76
	}
77
	set := []check{
78
		{10, 3, 5, 2, 5},
79
		{10, 6, 5, 2, 5},
80
		{10, 3, 3, 3, 4},
81
		{10, 3, 2, 3, 4},
82
		{10, 3, 11, 1, 11},
83
		{10, 3, 9, 2, 9},
84
	}
85
86
	for i, v := range set {
87
		p, c := AdjustSizes(v.n, v.d, v.min)
88
		if p != v.an || c != v.ad {
89
			t.Errorf("values are not equal (line `%d`):\ninput `%d`, `%d` and `%d`\ngot `%d` and `%d`\nexpected `%d` and `%d`", i, v.n, v.d, v.min, p, c, v.an, v.ad)
90
		}
91
	}
92
}
93
94
func TestExtension(t *testing.T) {
95
	set := [][]string{
96
		{"filename.ext", "ext"},
97
		{".ext", "ext"},
98
		{"filename", ""},
99
		{"filename.", ""},
100
	}
101
102
	for i, str := range set {
103
		ext := Extension(str[0])
104
		if ext != str[1] {
105
			t.Errorf("extensions not equal (line `%d`):\ngot `%s`\nexpected `%s`", i, ext, str[1])
106
		}
107
	}
108
}
109
110
func TestEqual(t *testing.T) {
111
	type check struct {
112
		a, b []string
113
		exp  bool
114
	}
115
116
	set := []check{
117
		{[]string{"a", "b"}, []string{"b", "a"}, true},
118
		{[]string{"a", "b"}, []string{"b", "c"}, false},
119
		{[]string{"a", "b"}, []string{"b", "A"}, false},
120
		{[]string{"a", "b"}, []string{"b"}, false},
121
	}
122
123
	for i, v := range set {
124
		res := Equal(v.a, v.b)
125
		if res != v.exp {
126
			t.Errorf("equality failed (line `%d`):\ngot `%t` \nexpected `%t`", i, res, v.exp)
127
		}
128
	}
129
}
130
131
func TestUnique(t *testing.T) {
132
	type check struct {
133
		a, b []string
134
	}
135
136
	set := []check{
137
		{[]string{"a", "b", "c"}, []string{"a", "b", "c"}},
138
		{[]string{"a", "b", "a"}, []string{"b", "a"}},
139
		{[]string{"a", "B", "b"}, []string{"b", "a", "B"}},
140
	}
141
142
	for i, v := range set {
143
		res := UniqueValues(v.a)
144
		if !Equal(res, v.b) {
145
			t.Errorf("equality failed (line `%d`):\ngot `%s` \nexpected `%s`", i, res, v.b)
146
		}
147
	}
148
}
149