summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/stretchr/objx/accessors_test.go
blob: ce5d8e4aa1a3ad96c05414f88429dd5ec6ecbeb8 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package objx

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestAccessorsAccessGetSingleField(t *testing.T) {

	current := map[string]interface{}{"name": "Tyler"}
	assert.Equal(t, "Tyler", access(current, "name", nil, false, true))

}
func TestAccessorsAccessGetDeep(t *testing.T) {

	current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}
	assert.Equal(t, "Tyler", access(current, "name.first", nil, false, true))
	assert.Equal(t, "Bunnell", access(current, "name.last", nil, false, true))

}
func TestAccessorsAccessGetDeepDeep(t *testing.T) {

	current := map[string]interface{}{"one": map[string]interface{}{"two": map[string]interface{}{"three": map[string]interface{}{"four": 4}}}}
	assert.Equal(t, 4, access(current, "one.two.three.four", nil, false, true))

}
func TestAccessorsAccessGetInsideArray(t *testing.T) {

	current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}}
	assert.Equal(t, "Tyler", access(current, "names[0].first", nil, false, true))
	assert.Equal(t, "Bunnell", access(current, "names[0].last", nil, false, true))
	assert.Equal(t, "Capitol", access(current, "names[1].first", nil, false, true))
	assert.Equal(t, "Bollocks", access(current, "names[1].last", nil, false, true))

	assert.Panics(t, func() {
		access(current, "names[2]", nil, false, true)
	})
	assert.Nil(t, access(current, "names[2]", nil, false, false))

}

func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) {

	current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}
	one := access(current, 0, nil, false, false)
	two := access(current, 1, nil, false, false)
	three := access(current, 2, nil, false, false)

	assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
	assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
	assert.Nil(t, three)

}

func TestAccessorsGet(t *testing.T) {

	current := New(map[string]interface{}{"name": "Tyler"})
	assert.Equal(t, "Tyler", current.Get("name").data)

}

func TestAccessorsAccessSetSingleField(t *testing.T) {

	current := map[string]interface{}{"name": "Tyler"}
	access(current, "name", "Mat", true, false)
	assert.Equal(t, current["name"], "Mat")

	access(current, "age", 29, true, true)
	assert.Equal(t, current["age"], 29)

}

func TestAccessorsAccessSetSingleFieldNotExisting(t *testing.T) {

	current := map[string]interface{}{}
	access(current, "name", "Mat", true, false)
	assert.Equal(t, current["name"], "Mat")

}

func TestAccessorsAccessSetDeep(t *testing.T) {

	current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}

	access(current, "name.first", "Mat", true, true)
	access(current, "name.last", "Ryer", true, true)

	assert.Equal(t, "Mat", access(current, "name.first", nil, false, true))
	assert.Equal(t, "Ryer", access(current, "name.last", nil, false, true))

}
func TestAccessorsAccessSetDeepDeep(t *testing.T) {

	current := map[string]interface{}{"one": map[string]interface{}{"two": map[string]interface{}{"three": map[string]interface{}{"four": 4}}}}

	access(current, "one.two.three.four", 5, true, true)

	assert.Equal(t, 5, access(current, "one.two.three.four", nil, false, true))

}
func TestAccessorsAccessSetArray(t *testing.T) {

	current := map[string]interface{}{"names": []interface{}{"Tyler"}}

	access(current, "names[0]", "Mat", true, true)

	assert.Equal(t, "Mat", access(current, "names[0]", nil, false, true))

}
func TestAccessorsAccessSetInsideArray(t *testing.T) {

	current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}}

	access(current, "names[0].first", "Mat", true, true)
	access(current, "names[0].last", "Ryer", true, true)
	access(current, "names[1].first", "Captain", true, true)
	access(current, "names[1].last", "Underpants", true, true)

	assert.Equal(t, "Mat", access(current, "names[0].first", nil, false, true))
	assert.Equal(t, "Ryer", access(current, "names[0].last", nil, false, true))
	assert.Equal(t, "Captain", access(current, "names[1].first", nil, false, true))
	assert.Equal(t, "Underpants", access(current, "names[1].last", nil, false, true))

}

func TestAccessorsAccessSetFromArrayWithInt(t *testing.T) {

	current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}
	one := access(current, 0, nil, false, false)
	two := access(current, 1, nil, false, false)
	three := access(current, 2, nil, false, false)

	assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
	assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
	assert.Nil(t, three)

}

func TestAccessorsSet(t *testing.T) {

	current := New(map[string]interface{}{"name": "Tyler"})
	current.Set("name", "Mat")
	assert.Equal(t, "Mat", current.Get("name").data)

}