Skip to content

Commit

Permalink
tests: rewrite to drop assert package
Browse files Browse the repository at this point in the history
  • Loading branch information
jehiah committed Jun 6, 2023
1 parent 39a59b1 commit 8d176ea
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 147 deletions.
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/bitly/go-simplejson

go 1.13
go 1.17

require github.com/stretchr/testify v1.4.0
require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 12 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
54 changes: 33 additions & 21 deletions simplejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func (j *Json) Del(key string) {
// for `key` in its `map` representation
//
// useful for chaining operations (to traverse a nested JSON):
// js.Get("top_level").Get("dict").Get("value").Int()
//
// js.Get("top_level").Get("dict").Get("value").Int()
func (j *Json) Get(key string) *Json {
m, err := j.Map()
if err == nil {
Expand All @@ -129,7 +130,7 @@ func (j *Json) Get(key string) *Json {
// GetPath searches for the item as specified by the branch
// without the need to deep dive using Get()'s.
//
// js.GetPath("top_level", "dict")
// js.GetPath("top_level", "dict")
func (j *Json) GetPath(branch ...string) *Json {
jin := j
for _, p := range branch {
Expand All @@ -143,7 +144,8 @@ func (j *Json) GetPath(branch ...string) *Json {
//
// this is the analog to Get when accessing elements of
// a json array instead of a json object:
// js.Get("top_level").Get("array").GetIndex(1).Get("key").Int()
//
// js.Get("top_level").Get("array").GetIndex(1).Get("key").Int()
func (j *Json) GetIndex(index int) *Json {
a, err := j.Array()
if err == nil {
Expand All @@ -158,9 +160,10 @@ func (j *Json) GetIndex(index int) *Json {
// a `bool` identifying success or failure
//
// useful for chained operations when success is important:
// if data, ok := js.Get("top_level").CheckGet("inner"); ok {
// log.Println(data)
// }
//
// if data, ok := js.Get("top_level").CheckGet("inner"); ok {
// log.Println(data)
// }
func (j *Json) CheckGet(key string) (*Json, bool) {
m, err := j.Map()
if err == nil {
Expand Down Expand Up @@ -235,9 +238,10 @@ func (j *Json) StringArray() ([]string, error) {
// MustArray guarantees the return of a `[]interface{}` (with optional default)
//
// useful when you want to interate over array values in a succinct manner:
// for i, v := range js.Get("results").MustArray() {
// fmt.Println(i, v)
// }
//
// for i, v := range js.Get("results").MustArray() {
// fmt.Println(i, v)
// }
func (j *Json) MustArray(args ...[]interface{}) []interface{} {
var def []interface{}

Expand All @@ -260,9 +264,10 @@ func (j *Json) MustArray(args ...[]interface{}) []interface{} {
// MustMap guarantees the return of a `map[string]interface{}` (with optional default)
//
// useful when you want to interate over map values in a succinct manner:
// for k, v := range js.Get("dictionary").MustMap() {
// fmt.Println(k, v)
// }
//
// for k, v := range js.Get("dictionary").MustMap() {
// fmt.Println(k, v)
// }
func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} {
var def map[string]interface{}

Expand All @@ -285,7 +290,8 @@ func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} {
// MustString guarantees the return of a `string` (with optional default)
//
// useful when you explicitly want a `string` in a single value return context:
// myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default"))
//
// myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default"))
func (j *Json) MustString(args ...string) string {
var def string

Expand All @@ -308,9 +314,10 @@ func (j *Json) MustString(args ...string) string {
// MustStringArray guarantees the return of a `[]string` (with optional default)
//
// useful when you want to interate over array values in a succinct manner:
// for i, s := range js.Get("results").MustStringArray() {
// fmt.Println(i, s)
// }
//
// for i, s := range js.Get("results").MustStringArray() {
// fmt.Println(i, s)
// }
func (j *Json) MustStringArray(args ...[]string) []string {
var def []string

Expand All @@ -333,7 +340,8 @@ func (j *Json) MustStringArray(args ...[]string) []string {
// MustInt guarantees the return of an `int` (with optional default)
//
// useful when you explicitly want an `int` in a single value return context:
// myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150))
//
// myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150))
func (j *Json) MustInt(args ...int) int {
var def int

Expand All @@ -356,7 +364,8 @@ func (j *Json) MustInt(args ...int) int {
// MustFloat64 guarantees the return of a `float64` (with optional default)
//
// useful when you explicitly want a `float64` in a single value return context:
// myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150))
//
// myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150))
func (j *Json) MustFloat64(args ...float64) float64 {
var def float64

Expand All @@ -379,7 +388,8 @@ func (j *Json) MustFloat64(args ...float64) float64 {
// MustBool guarantees the return of a `bool` (with optional default)
//
// useful when you explicitly want a `bool` in a single value return context:
// myFunc(js.Get("param1").MustBool(), js.Get("optional_param").MustBool(true))
//
// myFunc(js.Get("param1").MustBool(), js.Get("optional_param").MustBool(true))
func (j *Json) MustBool(args ...bool) bool {
var def bool

Expand All @@ -402,7 +412,8 @@ func (j *Json) MustBool(args ...bool) bool {
// MustInt64 guarantees the return of an `int64` (with optional default)
//
// useful when you explicitly want an `int64` in a single value return context:
// myFunc(js.Get("param1").MustInt64(), js.Get("optional_param").MustInt64(5150))
//
// myFunc(js.Get("param1").MustInt64(), js.Get("optional_param").MustInt64(5150))
func (j *Json) MustInt64(args ...int64) int64 {
var def int64

Expand All @@ -425,7 +436,8 @@ func (j *Json) MustInt64(args ...int64) int64 {
// MustUInt64 guarantees the return of an `uint64` (with optional default)
//
// useful when you explicitly want an `uint64` in a single value return context:
// myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150))
//
// myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150))
func (j *Json) MustUint64(args ...uint64) uint64 {
var def uint64

Expand Down
3 changes: 2 additions & 1 deletion simplejson_go10_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package simplejson

import (
"bytes"
"github.com/stretchr/testify/assert"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewFromReader(t *testing.T) {
Expand Down
77 changes: 55 additions & 22 deletions simplejson_go11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package simplejson
import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"reflect"
"strconv"
"testing"
)
Expand All @@ -26,32 +26,49 @@ func TestNewFromReader(t *testing.T) {
js, err := NewFromReader(buf)

//Standard Test Case
assert.NotEqual(t, nil, js)
assert.Equal(t, nil, err)
if js == nil {
t.Fatal("got nil")
}
if err != nil {
t.Fatalf("got err %#v", err)
}

arr, _ := js.Get("test").Get("array").Array()
assert.NotEqual(t, nil, arr)
if arr == nil {
t.Fatal("got nil")
}
for i, v := range arr {
var iv int
switch v.(type) {
case json.Number:
i64, err := v.(json.Number).Int64()
assert.Equal(t, nil, err)
if err != nil {
t.Fatalf("got err %#v", err)
}
iv = int(i64)
case string:
iv, _ = strconv.Atoi(v.(string))
}
assert.Equal(t, i+1, iv)
if iv != i+1 {
t.Errorf("got %#v expected %#v", iv, i+1)
}
}

ma := js.Get("test").Get("array").MustArray()
assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")})
if ma := js.Get("test").Get("array").MustArray(); !reflect.DeepEqual(ma, []interface{}{json.Number("1"), "2", json.Number("3")}) {
t.Errorf("got %#v", ma)
}

mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap()
assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")})
if !reflect.DeepEqual(mm, map[string]interface{}{"subkeyone": json.Number("1")}) {
t.Errorf("got %#v", mm)
}

assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807))
assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615))
if n := js.Get("test").Get("bignum").MustInt64(); n != int64(9223372036854775807) {
t.Errorf("got %#v", n)
}
if n := js.Get("test").Get("uint64").MustUint64(); n != uint64(18446744073709551615) {
t.Errorf("got %#v", n)
}
}

func TestSimplejsonGo11(t *testing.T) {
Expand All @@ -67,30 +84,46 @@ func TestSimplejsonGo11(t *testing.T) {
}
}`))

assert.NotEqual(t, nil, js)
assert.Equal(t, nil, err)
if js == nil {
t.Fatal("got nil")
}
if err != nil {
t.Fatalf("got err %#v", err)
}

arr, _ := js.Get("test").Get("array").Array()
assert.NotEqual(t, nil, arr)
if arr == nil {
t.Fatal("got nil")
}
for i, v := range arr {
var iv int
switch v.(type) {
case json.Number:
i64, err := v.(json.Number).Int64()
assert.Equal(t, nil, err)
if err != nil {
t.Fatalf("got err %#v", err)
}
iv = int(i64)
case string:
iv, _ = strconv.Atoi(v.(string))
}
assert.Equal(t, i+1, iv)
if iv != i+1 {
t.Errorf("got %#v expected %#v", iv, i+1)
}
}

ma := js.Get("test").Get("array").MustArray()
assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")})
if ma := js.Get("test").Get("array").MustArray(); !reflect.DeepEqual(ma, []interface{}{json.Number("1"), "2", json.Number("3")}) {
t.Errorf("got %#v", ma)
}

mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap()
assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")})

assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807))
assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615))
if !reflect.DeepEqual(mm, map[string]interface{}{"subkeyone": json.Number("1")}) {
t.Errorf("got %#v", mm)
}
if n := js.Get("test").Get("bignum").MustInt64(); n != int64(9223372036854775807) {
t.Errorf("got %#v", n)
}
if n := js.Get("test").Get("uint64").MustUint64(); n != uint64(18446744073709551615) {
t.Errorf("got %#v", n)
}
}
Loading

0 comments on commit 8d176ea

Please sign in to comment.