-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_test.go
More file actions
110 lines (83 loc) · 1.96 KB
/
map_test.go
File metadata and controls
110 lines (83 loc) · 1.96 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
package collection
import (
"reflect"
"testing"
)
func TestMap_Ints(t *testing.T) {
c := New([]int{1, 2, 3})
mapped := c.Map(func(v int) int {
return v * 10
})
expected := []int{10, 20, 30}
if !reflect.DeepEqual(mapped.items, expected) {
t.Fatalf("expected %v, got %v", expected, mapped.items)
}
if mapped != c {
t.Fatalf("Map should return the same collection")
}
if !reflect.DeepEqual(c.Items(), expected) {
t.Fatalf("Map should mutate original collection")
}
}
func TestMap_Structs(t *testing.T) {
type User struct {
ID int
Name string
}
c := New([]User{
{1, "Chris"},
{2, "Van"},
})
mapped := c.Map(func(u User) User {
u.Name = u.Name + "!"
return u
})
expected := []User{
{1, "Chris!"},
{2, "Van!"},
}
if !reflect.DeepEqual(mapped.items, expected) {
t.Fatalf("expected %v, got %v", expected, mapped.items)
}
if mapped != c {
t.Fatalf("Map should return the same collection")
}
if !reflect.DeepEqual(c.Items(), expected) {
t.Fatalf("Map should mutate original collection")
}
}
func TestMap_Empty(t *testing.T) {
c := New([]int{})
mapped := c.Map(func(v int) int {
return v * 2
})
if len(mapped.items) != 0 {
t.Fatalf("expected empty slice, got %v", mapped.items)
}
if mapped != c {
t.Fatalf("Map should return the same collection")
}
}
func TestMap_PreservesNilSlice(t *testing.T) {
c := New([]int(nil))
c.Map(func(v int) int { return v * 2 })
if c.Items() != nil {
t.Fatalf("expected nil slice to remain nil, got %v", c.Items())
}
}
func TestMap_WritesThroughSourceSlice(t *testing.T) {
items := []int{1, 2, 3}
c := New(items)
c.Map(func(v int) int { return v * 2 })
want := []int{2, 4, 6}
if !reflect.DeepEqual(items, want) {
t.Fatalf("expected source slice %v, got %v", want, items)
}
}
func TestMap_LengthUnchanged(t *testing.T) {
c := New([]int{1, 2, 3})
c.Map(func(v int) int { return v + 1 })
if len(c.Items()) != 3 {
t.Fatalf("expected length 3, got %d", len(c.Items()))
}
}