Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ^1.16
id: go

- name: Checkout code
Expand Down
1 change: 1 addition & 0 deletions apitest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type TestingT interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Log(args ...interface{})
Failed() bool
}
1 change: 1 addition & 0 deletions assertions/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type TestingT interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Log(args ...interface{})
Failed() bool
}
14 changes: 10 additions & 4 deletions assertjson/assertjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type TestingT interface {
Helper()
Errorf(format string, args ...interface{})
Log(args ...interface{})
Failed() bool
}

// AssertJSON - main structure that holds parsed JSON.
Expand All @@ -37,21 +38,26 @@ func NewAssertJSON(t TestingT, message string, data interface{}) *AssertJSON {
type JSONAssertFunc func(json *AssertJSON)

// FileHas loads JSON from file and runs user callback for testing its nodes.
func FileHas(t TestingT, filename string, jsonAssert JSONAssertFunc) {
func FileHas(t TestingT, filename string, jsonAssert JSONAssertFunc) bool {
t.Helper()

data, err := os.ReadFile(filename)
if err != nil {
assert.Fail(t, fmt.Sprintf(`failed to read file "%s": %s`, filename, err.Error()))
} else {
Has(t, data, jsonAssert)

return false
}

return Has(t, data, jsonAssert)
}

// Has - loads JSON from byte slice and runs user callback for testing its nodes.
func Has(t TestingT, data []byte, jsonAssert JSONAssertFunc) {
func Has(t TestingT, data []byte, jsonAssert JSONAssertFunc) bool {
t.Helper()
body := &AssertJSON{t: t}
body.assert(data, jsonAssert)

return !t.Failed()
}

// Node searches for JSON node by JSON Path Syntax. Returns struct for asserting the node values.
Expand Down
48 changes: 36 additions & 12 deletions assertjson/assertjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2732,17 +2732,6 @@ func TestHas(t *testing.T) {
`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 Expand Up @@ -2783,6 +2772,40 @@ func TestHas(t *testing.T) {
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tester := &mock.Tester{}

res := assertjson.Has(tester, []byte(test.json), test.assert)
wantRes := len(test.wantMessages) == 0

tester.AssertContains(t, test.wantMessages)

assert.Equal(t, wantRes, res)
})
}
}

func TestPrint(t *testing.T) {
tests := []struct {
name string
json string
assert assertjson.JSONAssertFunc
wantMessages []string
}{
// 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"`,
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tester := &mock.Tester{}
Expand All @@ -2807,11 +2830,12 @@ func TestAssertNode_Exists(t *testing.T) {
tester := &mock.Tester{}

var got bool
assertjson.Has(tester, []byte(test.json), func(json *assertjson.AssertJSON) {
res := assertjson.Has(tester, []byte(test.json), func(json *assertjson.AssertJSON) {
got = json.Node("key").Exists()
})

assert.Equal(t, test.want, got)
assert.Equal(t, test.want, res)
})
}
}
Expand Down
1 change: 1 addition & 0 deletions assertxml/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type TestingT interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Log(args ...interface{})
Failed() bool
}
8 changes: 8 additions & 0 deletions internal/mock/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,35 @@ import (
)

type Tester struct {
failed bool
messages []string
}

func (tester *Tester) Helper() {}

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

func (tester *Tester) Errorf(format string, args ...interface{}) {
tester.failed = true
tester.messages = append(tester.messages, fmt.Sprintf(format, args...))
}

func (tester *Tester) Fatal(args ...interface{}) {
tester.failed = true
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) Failed() bool {
return tester.failed
}

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