Unverified Commit fab57f01 authored by Hanzei's avatar Hanzei Committed by GitHub
Browse files

Add tests for mutations (#43)

parent 4b1ee21f
......@@ -5,14 +5,7 @@ package objx
func (m Map) Exclude(exclude []string) Map {
excluded := make(Map)
for k, v := range m {
var shouldInclude = true
for _, toExclude := range exclude {
if k == toExclude {
shouldInclude = false
break
}
}
if shouldInclude {
if !contains(exclude, k) {
excluded[k] = v
}
}
......@@ -72,3 +65,13 @@ func (m Map) TransformKeys(mapping map[string]string) Map {
return key, value
})
}
// Checks if a string slice contains a string
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
package objx
package objx_test
import (
"strings"
"testing"
"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)
func TestExclude(t *testing.T) {
d := make(Map)
d["name"] = "Mat"
d["age"] = 29
d["secret"] = "ABC"
m := objx.Map{
"name": "Mat",
"age": 29,
"secret": "ABC",
}
excluded := d.Exclude([]string{"secret"})
excluded := m.Exclude([]string{"secret"})
assert.Equal(t, d["name"], excluded["name"])
assert.Equal(t, d["age"], excluded["age"])
assert.Equal(t, m["name"], excluded["name"])
assert.Equal(t, m["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"
m1 := objx.Map{
"name": "Tyler",
"location": "UT",
}
d1Obj := New(d1)
d2Obj := d1Obj.Copy()
m2 := m1.Copy()
m2["name"] = "Mat"
d2Obj["name"] = "Mat"
assert.Equal(t, d1Obj.Get("name").Str(), "Tyler")
assert.Equal(t, d2Obj.Get("name").Str(), "Mat")
assert.Equal(t, m1.Get("name").Str(), "Tyler")
assert.Equal(t, m2.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())
m1 := objx.Map{
"name": "Mat",
}
m2 := objx.Map{
"name": "Tyler",
"location": "UT",
}
merged := m1.Merge(m2)
assert.Equal(t, merged.Get("name").Str(), m2.Get("name").Str())
assert.Equal(t, merged.Get("location").Str(), m2.Get("location").Str())
assert.Empty(t, m1.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"
m1 := objx.Map{
"name": "Mat",
}
m2 := objx.Map{
"name": "Tyler",
"location": "UT",
}
merged := m1.MergeHere(m2)
assert.Equal(t, m1, merged, "With MergeHere, it should return the first modified map")
assert.Equal(t, merged.Get("name").Str(), m2.Get("name").Str())
assert.Equal(t, merged.Get("location").Str(), m2.Get("location").Str())
assert.Equal(t, merged.Get("location").Str(), m1.Get("location").Str())
}
dObj := New(d)
d1Obj := New(d1)
func TestTransform(t *testing.T) {
m := objx.Map{
"name": "Mat",
"location": "UK",
}
r := m.Transform(keyToUpper)
assert.Equal(t, objx.Map{
"NAME": "Mat",
"LOCATION": "UK",
}, r)
}
merged := dObj.MergeHere(d1Obj)
func TestTransformKeys(t *testing.T) {
m := objx.Map{
"a": "1",
"b": "2",
"c": "3",
}
mapping := map[string]string{
"a": "d",
"b": "e",
}
r := m.TransformKeys(mapping)
assert.Equal(t, objx.Map{
"c": "3",
"d": "1",
"e": "2",
}, r)
}
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())
func keyToUpper(s string, v interface{}) (string, interface{}) {
return strings.ToUpper(s), v
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment