-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpluck_test.go
81 lines (72 loc) · 1.6 KB
/
pluck_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
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
package grules
import (
"testing"
)
func TestPluck(t *testing.T) {
t.Run("key does not exist", func(t *testing.T) {
props := map[string]interface{}{}
val := pluck(props, "email")
if val != nil {
t.Fatal("expected value to be nil")
}
})
t.Run("1 level", func(t *testing.T) {
props := map[string]interface{}{
"email": "test@test.com",
}
val := pluck(props, "email")
if val.(string) != "test@test.com" {
t.Fatal("expected value to match the given")
}
})
t.Run("2 levels", func(t *testing.T) {
props := map[string]interface{}{
"user": map[string]interface{}{
"name": "Trevor",
},
}
val := pluck(props, "user.name")
if val.(string) != "Trevor" {
t.Fatal("expected value to match the given")
}
})
t.Run("2 levels, key does not exist", func(t *testing.T) {
props := map[string]interface{}{
"user": map[string]interface{}{
"name": "Trevor",
},
}
val := pluck(props, "user.last_name")
if val != nil {
t.Fatal("expected value to be nil")
}
})
}
func BenchmarkPluckShallow(b *testing.B) {
props := map[string]interface{}{
"username": "huttotw",
}
for i := 0; i < b.N; i++ {
pluck(props, "username")
}
}
func BenchmarkPluckDeep(b *testing.B) {
props := map[string]interface{}{
"this": map[string]interface{}{
"is": map[string]interface{}{
"a": map[string]interface{}{
"super": map[string]interface{}{
"deep": map[string]interface{}{
"map": map[string]interface{}{
"hello": "world",
},
},
},
},
},
},
}
for i := 0; i < b.N; i++ {
pluck(props, "this.is.a.super.deep.map.hello")
}
}