-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSlices+Fun_test.go
46 lines (41 loc) · 1.24 KB
/
Slices+Fun_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
package fun
import (
"testing"
)
func TestFirstAndLastWithStrings(t *testing.T) {
empty := []string{}
one := []string{"1"}
three := []string{"1", "2", "3"}
var nilSlice []string = nil
assert(t, First(empty) == "")
assert(t, First(one) == "1")
assert(t, First(three) == "1")
assert(t, Last(empty) == "")
assert(t, Last(one) == "1")
assert(t, Last(three) == "3")
assert(t, First(nilSlice) == "")
}
type testStruct struct{}
func TestFirstAndLastWithStructs(t *testing.T) {
first := testStruct{}
last := testStruct{}
one := []testStruct{first}
three := []testStruct{first, testStruct{}, last}
assert(t, First(one) == first)
assert(t, First(three) == first)
assert(t, Last(one) == first)
assert(t, Last(three) == last)
}
func TestFirstAndLastWithPtrs(t *testing.T) {
empty := []*testStruct{}
one := []*testStruct{&testStruct{}}
three := []*testStruct{&testStruct{}, &testStruct{}, &testStruct{}}
var nilSlice []*testStruct = nil
assert(t, First(empty).(*testStruct) == nil)
assert(t, First(one).(*testStruct) != nil)
assert(t, First(three).(*testStruct) != nil)
assert(t, Last(empty).(*testStruct) == nil)
assert(t, Last(one).(*testStruct) != nil)
assert(t, Last(three).(*testStruct) != nil)
assert(t, First(nilSlice).(*testStruct) == nil)
}