|
1
|
|
|
package utils |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"testing" |
|
5
|
|
|
) |
|
6
|
|
|
|
|
7
|
|
|
func TestContains(t *testing.T) { |
|
8
|
|
|
t.Run("must return true", func(t *testing.T) { |
|
9
|
|
|
slice := []string{"hello", "many", "also", "hook"} |
|
10
|
|
|
|
|
11
|
|
|
if !Contains(slice, "hello") { |
|
12
|
|
|
t.Error("slice must return true instead of false") |
|
13
|
|
|
} |
|
14
|
|
|
}) |
|
15
|
|
|
|
|
16
|
|
|
t.Run("must return false", func(t *testing.T) { |
|
17
|
|
|
slice := []string{"hello", "many", "also", "hook"} |
|
18
|
|
|
|
|
19
|
|
|
if Contains(slice, "anton") { |
|
20
|
|
|
t.Error("slice must return false instead of true") |
|
21
|
|
|
} |
|
22
|
|
|
}) |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
func TestGetUniqueItem(t *testing.T) { |
|
26
|
|
|
slice1 := []string{"hello", "another", "nice", "cool"} |
|
27
|
|
|
slice2 := []string{"hello", "another", "nice", "cool", "unique"} |
|
28
|
|
|
expect := []string{"unique"} |
|
29
|
|
|
|
|
30
|
|
|
t.Run("returns unique item", func(t *testing.T) { |
|
31
|
|
|
result := GetUniqueItem(slice2, slice1) |
|
32
|
|
|
|
|
33
|
|
|
if result[0] != expect[0] { |
|
34
|
|
|
t.Errorf("result must be %v", expect) |
|
35
|
|
|
} |
|
36
|
|
|
}) |
|
37
|
|
|
|
|
38
|
|
|
t.Run("returns unique item with different order of args", func(t *testing.T) { |
|
39
|
|
|
result := GetUniqueItem(slice1, slice2) |
|
40
|
|
|
|
|
41
|
|
|
if result[0] != expect[0] { |
|
42
|
|
|
t.Errorf("result must be %v", expect) |
|
43
|
|
|
} |
|
44
|
|
|
}) |
|
45
|
|
|
|
|
46
|
|
|
t.Run("returns nil if no diff", func(t *testing.T) { |
|
47
|
|
|
sameSlice := []string{"hello", "another", "nice", "cool"} |
|
48
|
|
|
result := GetUniqueItem(slice1, sameSlice) |
|
49
|
|
|
|
|
50
|
|
|
if result != nil { |
|
51
|
|
|
t.Error("result must return nil") |
|
52
|
|
|
} |
|
53
|
|
|
}) |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
func TestGetIndexOfSliceItem(t *testing.T) { |
|
57
|
|
|
slice := []string{"first", "second", "third"} |
|
58
|
|
|
cases := []struct { |
|
59
|
|
|
Title string |
|
60
|
|
|
Expect int |
|
61
|
|
|
Input string |
|
62
|
|
|
}{ |
|
63
|
|
|
{"returns -1 if value is not in a slice", -1, "forth"}, |
|
64
|
|
|
{"returns 1 if value has index 1", 0, "first"}, |
|
65
|
|
|
{"returns 2 if value has index 2", 1, "second"}, |
|
66
|
|
|
{"returns 3 if value has index 3", 2, "third"}, |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
for _, singleCase := range cases { |
|
70
|
|
|
t.Run(singleCase.Title, func(t *testing.T) { |
|
71
|
|
|
t.Parallel() |
|
72
|
|
|
result := GetIndexOfSliceItem(slice, singleCase.Input) |
|
73
|
|
|
|
|
74
|
|
|
if result != singleCase.Expect { |
|
75
|
|
|
t.Errorf("resulted index must be %d but %d returned", singleCase.Expect, result) |
|
76
|
|
|
} |
|
77
|
|
|
}) |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|