summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/stretchr/objx/mutations_test.go
blob: e20ee23bc4bb147fdac45c101791ccebbde0d71e (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
package objx

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

func TestExclude(t *testing.T) {

	d := make(Map)
	d["name"] = "Mat"
	d["age"] = 29
	d["secret"] = "ABC"

	excluded := d.Exclude([]string{"secret"})

	assert.Equal(t, d["name"], excluded["name"])
	assert.Equal(t, d["age"], excluded["age"])
	assert.False(t, excluded.Has("secret"), "secret should be excluded")

}

func TestCopy(t *testing.T) {

	d1 := make(map[string]interface{})
	d1["name"] = "Tyler"
	d1["location"] = "UT"

	d1Obj := New(d1)
	d2Obj := d1Obj.Copy()

	d2Obj["name"] = "Mat"

	assert.Equal(t, d1Obj.Get("name").Str(), "Tyler")
	assert.Equal(t, d2Obj.Get("name").Str(), "Mat")

}

func TestMerge(t *testing.T) {

	d := make(map[string]interface{})
	d["name"] = "Mat"

	d1 := make(map[string]interface{})
	d1["name"] = "Tyler"
	d1["location"] = "UT"

	dObj := New(d)
	d1Obj := New(d1)

	merged := dObj.Merge(d1Obj)

	assert.Equal(t, merged.Get("name").Str(), d1Obj.Get("name").Str())
	assert.Equal(t, merged.Get("location").Str(), d1Obj.Get("location").Str())
	assert.Empty(t, dObj.Get("location").Str())

}

func TestMergeHere(t *testing.T) {

	d := make(map[string]interface{})
	d["name"] = "Mat"

	d1 := make(map[string]interface{})
	d1["name"] = "Tyler"
	d1["location"] = "UT"

	dObj := New(d)
	d1Obj := New(d1)

	merged := dObj.MergeHere(d1Obj)

	assert.Equal(t, dObj, merged, "With MergeHere, it should return the first modified map")
	assert.Equal(t, merged.Get("name").Str(), d1Obj.Get("name").Str())
	assert.Equal(t, merged.Get("location").Str(), d1Obj.Get("location").Str())
	assert.Equal(t, merged.Get("location").Str(), dObj.Get("location").Str())
}