Skip to content

Commit

Permalink
Merge pull request #10 from muonsoft/fix-panic
Browse files Browse the repository at this point in the history
fix panic and add debug helper
  • Loading branch information
strider2038 authored Jan 16, 2023
2 parents 3b6bba3 + d6caa37 commit 917b119
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ linters:
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goimports
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func TestYourAPI(t *testing.T) {
assert.Equal(t, "2022-10-16T15:14:32+03:00", json.Node("time").Time().Format(time.RFC3339))
assert.Equal(t, "23e98a0c-26c8-410f-978f-d1d67228af87", json.Node("uuid").IsUUID().Value().String())
assert.Equal(t, "23e98a0c-26c8-410f-978f-d1d67228af87", json.Node("uuid").UUID().String())

// debug helpers
json.Node("bookstore", "books", 1).Print()
})
}
```
Expand Down
7 changes: 7 additions & 0 deletions assertjson/assertjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
type TestingT interface {
Helper()
Errorf(format string, args ...interface{})
Log(args ...interface{})
}

// AssertJSON - main structure that holds parsed JSON.
Expand Down Expand Up @@ -163,6 +164,12 @@ func pathFromJSONPointer(p string) []interface{} {
func getValueByPath(data interface{}, path ...interface{}) (interface{}, error) {
v := jsoniter.Wrap(data)
for _, e := range path {
if _, ok := e.(int); ok {
if _, ok := v.GetInterface().(map[string]interface{}); ok {
return nil, fmt.Errorf("value of type int is not assignable to type string")
}
}

v = v.Get(e)
if v.LastError() != nil {
return nil, v.LastError()
Expand Down
34 changes: 34 additions & 0 deletions assertjson/assertjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ func TestFileHas(t *testing.T) {
}).
Raw,
)

// debug helpers
json.Node("bookstore", "books", 1).Print()
})
}

Expand Down Expand Up @@ -2591,6 +2594,37 @@ func TestHas(t *testing.T) {
`failed asserting that JSON node "a.b.c[0]": equal to "expected", actual is "value"`,
},
},
{
name: "seek by json iterator",
json: `{"a": {"b": {"c": ["value"]}}}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("a", 0).IsString()
},
wantMessages: []string{
`failed to find JSON node "a[0]": value of type int is not assignable to type string`,
},
},
{
name: "seek by json iterator",
json: `{"a": [{"b": 1}]}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("a", "b").IsString()
},
wantMessages: []string{
`failed to find JSON node "a.b": [b] not found`,
},
},
// debug helpers
{
name: "print json node",
json: `{"a": {"b": {"c": ["value"]}}}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("a", "b").Print()
},
wantMessages: []string{
`JSON node at "a.b"`,
},
},
// deprecated behaviour: seek by json pointer path
{
name: "deprecated: json pointer path",
Expand Down
6 changes: 6 additions & 0 deletions assertjson/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ func (node *AssertNode) Assert(jsonAssert JSONAssertFunc) {
})
}

func (node *AssertNode) Print() {
node.t.Helper()
printableJSON, _ := json.MarshalIndent(node.value, "", "\t")
node.t.Log(fmt.Sprintf("JSON node at \"%s\":\n", node.path.String()), string(printableJSON))
}

func (node *AssertNode) fail(message string, msgAndArgs ...interface{}) {
node.t.Helper()
assert.Fail(node.t, node.message+message, msgAndArgs...)
Expand Down
4 changes: 4 additions & 0 deletions internal/mock/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (tester *Tester) Fatal(args ...interface{}) {
tester.messages = append(tester.messages, fmt.Sprint(args...))
}

func (tester *Tester) Log(args ...interface{}) {
tester.messages = append(tester.messages, fmt.Sprint(args...))
}

func (tester *Tester) AssertContains(t *testing.T, messages []string) {
t.Helper()
if len(tester.messages) != len(messages) {
Expand Down

0 comments on commit 917b119

Please sign in to comment.