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

Refactor tests (#46)

* Make methode more compact

* Remove dupicate switch statement

* Remove duplicate test

* Ignore doc.go for codeclimate

* Remove unneeded linebreaks

* Add more test and refactor existing ones

* MSI(keyAndValuePairs ...interface{}) Map does not panik

* Upload github.com/stretchr/testify/require to vendor

* Replace map[string]interface{} with Map
parent c8018d91
......@@ -3,9 +3,6 @@ engines:
enabled: true
golint:
enabled: true
checks:
GoLint/Comments/PackageComments:
enabled: false
govet:
enabled: true
......@@ -13,3 +10,4 @@ exclude_patterns:
- ".github/"
- "vendor/"
- "codegen/"
- "doc.go"
......@@ -15,13 +15,16 @@
[[projects]]
name = "github.com/stretchr/testify"
packages = ["assert"]
packages = [
"assert",
"require"
]
revision = "b91bfb9ebec76498946beb6af7c0230c7cc7ba6c"
version = "v1.2.0"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "50e2495ec1af6e2f7ffb2f3551e4300d30357d7c7fe38ff6056469fa9cfb3673"
inputs-digest = "2d160a7dea4ffd13c6c31dab40373822f9d78c73beba016d662bef8f7a998876"
solver-name = "gps-cdcl"
solver-version = 1
package objx
import (
"fmt"
"regexp"
"strconv"
"strings"
......@@ -28,7 +27,7 @@ var arrayAccesRegex = regexp.MustCompile(arrayAccesRegexString)
//
// o.Get("books[1].chapters[2].title")
func (m Map) Get(selector string) *Value {
rawObj := access(m, selector, nil, false, false)
rawObj := access(m, selector, nil, false)
return &Value{data: rawObj}
}
......@@ -43,34 +42,25 @@ func (m Map) Get(selector string) *Value {
//
// o.Set("books[1].chapters[2].title","Time to Go")
func (m Map) Set(selector string, value interface{}) Map {
access(m, selector, value, true, false)
access(m, selector, value, true)
return m
}
// access accesses the object using the selector and performs the
// appropriate action.
func access(current, selector, value interface{}, isSet, panics bool) interface{} {
func access(current, selector, value interface{}, isSet bool) interface{} {
switch selector.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
if array, ok := current.([]interface{}); ok {
index := intFromInterface(selector)
if index >= len(array) {
if panics {
panic(fmt.Sprintf("objx: Index %d is out of range. Slice only contains %d items.", index, len(array)))
}
return nil
}
return array[index]
}
return nil
case string:
selStr := selector.(string)
selSegs := strings.SplitN(selStr, PathSeparator, 2)
thisSel := selSegs[0]
......@@ -79,7 +69,6 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
if strings.Contains(thisSel, "[") {
arrayMatches := arrayAccesRegex.FindStringSubmatch(thisSel)
if len(arrayMatches) > 0 {
// Get the key into the map
thisSel = arrayMatches[1]
......@@ -94,11 +83,9 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
}
}
}
if curMap, ok := current.(Map); ok {
current = map[string]interface{}(curMap)
}
// get the object in question
switch current.(type) {
case map[string]interface{}:
......@@ -111,29 +98,19 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
default:
current = nil
}
if current == nil && panics {
panic(fmt.Sprintf("objx: '%v' invalid on object.", selector))
}
// do we need to access the item of an array?
if index > -1 {
if array, ok := current.([]interface{}); ok {
if index < len(array) {
current = array[index]
} else {
if panics {
panic(fmt.Sprintf("objx: Index %d is out of range. Slice only contains %d items.", index, len(array)))
}
current = nil
}
}
}
if len(selSegs) > 1 {
current = access(current, selSegs[1], value, isSet, panics)
current = access(current, selSegs[1], value, isSet)
}
}
return current
}
......@@ -165,7 +142,7 @@ func intFromInterface(selector interface{}) int {
case uint64:
value = int(selector.(uint64))
default:
panic("objx: array access argument is not an integer type (this should never happen)")
return 0
}
return value
}
......@@ -7,125 +7,230 @@ import (
)
func TestAccessorsAccessGetSingleField(t *testing.T) {
current := map[string]interface{}{"name": "Tyler"}
current := Map{"name": "Tyler"}
assert.Equal(t, "Tyler", access(current, "name", nil, false, true))
assert.Equal(t, "Tyler", current.Get("name").Data())
}
func TestAccessorsAccessGetDeep(t *testing.T) {
current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}
func TestAccessorsAccessGetSingleFieldInt(t *testing.T) {
current := Map{"name": 10}
assert.Equal(t, "Tyler", access(current, "name.first", nil, false, true))
assert.Equal(t, "Bunnell", access(current, "name.last", nil, false, true))
assert.Equal(t, 10, current.Get("name").Data())
}
func TestAccessorsAccessGetDeepDeep(t *testing.T) {
current := map[string]interface{}{"one": map[string]interface{}{"two": map[string]interface{}{"three": map[string]interface{}{"four": 4}}}}
func TestAccessorsAccessGetDeep(t *testing.T) {
current := Map{
"name": Map{
"first": "Tyler",
"last": "Bunnell",
},
}
assert.Equal(t, "Tyler", current.Get("name.first").Data())
assert.Equal(t, "Bunnell", current.Get("name.last").Data())
}
assert.Equal(t, 4, access(current, "one.two.three.four", nil, false, true))
func TestAccessorsAccessGetDeepDeep(t *testing.T) {
current := Map{
"one": Map{
"two": Map{
"three": Map{
"four": 4,
},
},
},
}
assert.Equal(t, 4, current.Get("one.two.three.four").Data())
}
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))
current := Map{
"names": []interface{}{
Map{
"first": "Tyler",
"last": "Bunnell",
},
Map{
"first": "Capitol",
"last": "Bollocks",
},
},
}
assert.Equal(t, "Tyler", current.Get("names[0].first").Data())
assert.Equal(t, "Bunnell", current.Get("names[0].last").Data())
assert.Equal(t, "Capitol", current.Get("names[1].first").Data())
assert.Equal(t, "Bollocks", current.Get("names[1].last").Data())
assert.Nil(t, current.Get("names[2]").Data())
}
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)
current := []interface{}{
map[string]interface{}{
"first": "Tyler",
"last": "Bunnell",
},
map[string]interface{}{
"first": "Capitol",
"last": "Bollocks",
},
}
one := access(current, 0, nil, false)
two := access(current, 1, nil, false)
three := access(current, 2, nil, false)
assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
assert.Nil(t, three)
}
func TestAccessorsAccessGetFromArrayWithIntTypes(t *testing.T) {
current := []interface{}{
"abc",
"def",
}
assert.Equal(t, "abc", access(current, 0, nil, false))
assert.Equal(t, "def", access(current, 1, nil, false))
assert.Nil(t, access(current, 2, nil, false))
assert.Equal(t, "abc", access(current, int8(0), nil, false))
assert.Equal(t, "def", access(current, int8(1), nil, false))
assert.Nil(t, access(current, int8(2), nil, false))
assert.Equal(t, "abc", access(current, int16(0), nil, false))
assert.Equal(t, "def", access(current, int16(1), nil, false))
assert.Nil(t, access(current, int16(2), nil, false))
assert.Equal(t, "abc", access(current, int32(0), nil, false))
assert.Equal(t, "def", access(current, int32(1), nil, false))
assert.Nil(t, access(current, int32(2), nil, false))
assert.Equal(t, "abc", access(current, int64(0), nil, false))
assert.Equal(t, "def", access(current, int64(1), nil, false))
assert.Nil(t, access(current, int64(2), nil, false))
assert.Equal(t, "abc", access(current, uint(0), nil, false))
assert.Equal(t, "def", access(current, uint(1), nil, false))
assert.Nil(t, access(current, uint(2), nil, false))
assert.Equal(t, "abc", access(current, uint8(0), nil, false))
assert.Equal(t, "def", access(current, uint8(1), nil, false))
assert.Nil(t, access(current, uint8(2), nil, false))
assert.Equal(t, "abc", access(current, uint16(0), nil, false))
assert.Equal(t, "def", access(current, uint16(1), nil, false))
assert.Nil(t, access(current, uint16(2), nil, false))
assert.Equal(t, "abc", access(current, uint32(0), nil, false))
assert.Equal(t, "def", access(current, uint32(1), nil, false))
assert.Nil(t, access(current, uint32(2), nil, false))
assert.Equal(t, "abc", access(current, uint64(0), nil, false))
assert.Equal(t, "def", access(current, uint64(1), nil, false))
assert.Nil(t, access(current, uint64(2), nil, false))
}
func TestAccessorsAccessGetFromArrayWithIntError(t *testing.T) {
current := Map{"name": "Tyler"}
assert.Nil(t, access(current, 0, nil, false))
}
func TestAccessorsGet(t *testing.T) {
current := New(map[string]interface{}{"name": "Tyler"})
current := Map{"name": "Tyler"}
assert.Equal(t, "Tyler", current.Get("name").data)
assert.Equal(t, "Tyler", current.Get("name").Data())
}
func TestAccessorsAccessSetSingleField(t *testing.T) {
current := map[string]interface{}{"name": "Tyler"}
current := Map{"name": "Tyler"}
access(current, "name", "Mat", true, false)
access(current, "age", 29, true, true)
current.Set("name", "Mat")
current.Set("age", 29)
assert.Equal(t, current["name"], "Mat")
assert.Equal(t, current["age"], 29)
}
func TestAccessorsAccessSetSingleFieldNotExisting(t *testing.T) {
current := map[string]interface{}{}
current := Map{
"first": "Tyler",
"last": "Bunnell",
}
access(current, "name", "Mat", true, false)
current.Set("name", "Mat")
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))
current := Map{
"name": Map{
"first": "Tyler",
"last": "Bunnell",
},
}
current.Set("name.first", "Mat")
current.Set("name.last", "Ryer")
assert.Equal(t, "Mat", current.Get("name.first").Data())
assert.Equal(t, "Ryer", current.Get("name.last").Data())
}
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))
current := Map{
"one": Map{
"two": Map{
"three": Map{
"four": 4},
},
},
}
current.Set("one.two.three.four", 5)
assert.Equal(t, 5, current.Get("one.two.three.four").Data())
}
func TestAccessorsAccessSetArray(t *testing.T) {
current := map[string]interface{}{"names": []interface{}{"Tyler"}}
current := Map{
"names": []interface{}{"Tyler"},
}
current.Set("names[0]", "Mat")
access(current, "names[0]", "Mat", true, true)
assert.Equal(t, "Mat", access(current, "names[0]", nil, false, true))
assert.Equal(t, "Mat", current.Get("names[0]").Data())
}
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)
current := Map{
"names": []interface{}{
Map{
"first": "Tyler",
"last": "Bunnell",
},
Map{
"first": "Capitol",
"last": "Bollocks",
},
},
}
current.Set("names[0].first", "Mat")
current.Set("names[0].last", "Ryer")
current.Set("names[1].first", "Captain")
current.Set("names[1].last", "Underpants")
assert.Equal(t, "Mat", current.Get("names[0].first").Data())
assert.Equal(t, "Ryer", current.Get("names[0].last").Data())
assert.Equal(t, "Captain", current.Get("names[1].first").Data())
assert.Equal(t, "Underpants", current.Get("names[1].last").Data())
}
func TestAccessorsSet(t *testing.T) {
current := New(map[string]interface{}{"name": "Tyler"})
current := Map{"name": "Tyler"}
current.Set("name", "Mat")
......
package objx
package objx_test
import (
"testing"
"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConversionJSON(t *testing.T) {
jsonString := `{"name":"Mat"}`
o := MustFromJSON(jsonString)
o := objx.MustFromJSON(jsonString)
result, err := o.JSON()
if assert.NoError(t, err) {
assert.Equal(t, jsonString, result)
}
require.NoError(t, err)
assert.Equal(t, jsonString, result)
assert.Equal(t, jsonString, o.MustJSON())
}
func TestConversionJSONWithError(t *testing.T) {
o := MSI()
o := objx.MSI()
o["test"] = func() {}
assert.Panics(t, func() {
......@@ -32,18 +33,17 @@ func TestConversionJSONWithError(t *testing.T) {
}
func TestConversionBase64(t *testing.T) {
o := New(map[string]interface{}{"name": "Mat"})
o := objx.Map{"name": "Mat"}
result, err := o.Base64()
if assert.NoError(t, err) {
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", result)
}
require.NoError(t, err)
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", result)
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", o.MustBase64())
}
func TestConversionBase64WithError(t *testing.T) {
o := MSI()
o := objx.MSI()
o["test"] = func() {}
assert.Panics(t, func() {
......@@ -56,18 +56,17 @@ func TestConversionBase64WithError(t *testing.T) {
}
func TestConversionSignedBase64(t *testing.T) {
o := New(map[string]interface{}{"name": "Mat"})
o := objx.Map{"name": "Mat"}
result, err := o.SignedBase64("key")
if assert.NoError(t, err) {
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", result)
}
require.NoError(t, err)
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", result)
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", o.MustSignedBase64("key"))
}
func TestConversionSignedBase64WithError(t *testing.T) {
o := MSI()
o := objx.MSI()
o["test"] = func() {}
assert.Panics(t, func() {
......
package objx
package objx_test
import (
"testing"
"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)
......@@ -81,14 +82,14 @@ var fixtures = []struct {
func TestFixtures(t *testing.T) {
for _, fixture := range fixtures {
m := MustFromJSON(fixture.data)
m := objx.MustFromJSON(fixture.data)
// get the value
t.Logf("Running get fixture: \"%s\" (%v)", fixture.name, fixture)
value := m.Get(fixture.get.(string))
// make sure it matches
assert.Equal(t, fixture.output, value.data,
assert.Equal(t, fixture.output, value.Data(),
"Get fixture \"%s\" failed: %v", fixture.name, fixture,
)
}
......
......@@ -47,9 +47,8 @@ func New(data interface{}) Map {
//
// The arguments follow a key, value pattern.
//
// Panics
//
// Panics if any key argument is non-string or if there are an odd number of arguments.
// Returns nil if any key argument is non-string or if there are an odd number of arguments.
//
// Example
//
......@@ -58,14 +57,13 @@ func New(data interface{}) Map {
// m := objx.MSI("name", "Mat", "age", 29, "subobj", objx.MSI("active", true))
//
// // creates an Map equivalent to
// m := objx.New(map[string]interface{}{"name": "Mat", "age": 29, "subobj": map[string]interface{}{"active": true}})
// m := objx.Map{"name": "Mat", "age": 29, "subobj": objx.Map{"active": true}}
func MSI(keyAndValuePairs ...interface{}) Map {
newMap := make(map[string]interface{})
newMap := Map{}
keyAndValuePairsLen := len(keyAndValuePairs)
if keyAndValuePairsLen%2 != 0 {
panic("objx: MSI must have an even number of arguments following the 'key, value' pattern.")
return nil
}
for i := 0; i < keyAndValuePairsLen; i = i + 2 {
key := keyAndValuePairs[i]
value := keyAndValuePairs[i+1]
......@@ -73,11 +71,11 @@ func MSI(keyAndValuePairs ...interface{}) Map {
// make sure the key is a string
keyString, keyStringOK := key.(string)
if !keyStringOK {
panic("objx: MSI must follow 'string, interface{}' pattern. " + keyString + " is not a valid key.")
return nil
}
newMap[keyString] = value
}
return New(newMap)
return newMap