Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a bool value from the assertjson and assertxml Has and FileHas functions #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
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
}
26 changes: 18 additions & 8 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) {
// Returns false if assertion has failed.
func FileHas(t TestingT, filename string, jsonAssert JSONAssertFunc) bool {
t.Helper()

data, err := ioutil.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) {
// Returns false if assertion has failed.
func Has(t TestingT, data []byte, jsonAssert JSONAssertFunc) bool {
t.Helper()
body := &AssertJSON{t: t}
body.assert(data, jsonAssert)
return body.assert(data, jsonAssert)
}

// Node searches for JSON node by JSON Path Syntax. Returns struct for asserting the node values.
Expand Down Expand Up @@ -108,14 +114,18 @@ func (j *AssertJSON) Atf(format string, a ...interface{}) *AssertJSON {
return j.At(fmt.Sprintf(format, a...))
}

func (j *AssertJSON) assert(data []byte, jsonAssert JSONAssertFunc) {
func (j *AssertJSON) assert(data []byte, jsonAssert JSONAssertFunc) bool {
j.t.Helper()
err := json.Unmarshal(data, &j.data)
if err != nil {
j.fail(fmt.Sprintf("data has invalid JSON: %s", err.Error()))
} else {
jsonAssert(j)

return false
}

jsonAssert(j)

return !j.t.Failed()
}

func (j *AssertJSON) fail(message string, msgAndArgs ...interface{}) {
Expand Down
10 changes: 7 additions & 3 deletions assertjson/assertjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func TestFileHas(t *testing.T) {
assertjson.FileHas(t, "./../test/testdata/object.json", func(json *assertjson.AssertJSON) {
res := assertjson.FileHas(t, "./../test/testdata/object.json", func(json *assertjson.AssertJSON) {
// common assertions
json.Node("nullNode").Exists()
json.Node("notExistingNode").DoesNotExist()
Expand Down Expand Up @@ -244,6 +244,8 @@ func TestFileHas(t *testing.T) {
// debug helpers
json.Node("bookstore", "books", 1).Print()
})

assert.True(t, res)
}

func TestHas(t *testing.T) {
Expand Down Expand Up @@ -2787,9 +2789,10 @@ func TestHas(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
tester := &mock.Tester{}

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

tester.AssertContains(t, test.wantMessages)
assert.False(t, res)
})
}
}
Expand All @@ -2807,11 +2810,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
20 changes: 14 additions & 6 deletions assertxml/assertxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,22 @@ type AssertNode struct {
type XMLAssertFunc func(xml *AssertXML)

// FileHas loads XML from file and runs user callback for testing its nodes.
func FileHas(t TestingT, filename string, xmlAssert XMLAssertFunc) {
// Returns false if assertion has failed.
func FileHas(t TestingT, filename string, xmlAssert XMLAssertFunc) bool {
t.Helper()
data, err := ioutil.ReadFile(filename)
if err != nil {
assert.Failf(t, "failed to read file '%s': %s", filename, err.Error())
} else {
Has(t, data, xmlAssert)

return false
}

return Has(t, data, xmlAssert)
}

// Has loads XML from byte slice and runs user callback for testing its nodes.
func Has(t TestingT, data []byte, xmlAssert XMLAssertFunc) {
// Returns false if assertion has failed.
func Has(t TestingT, data []byte, xmlAssert XMLAssertFunc) bool {
t.Helper()
xml, err := xmlpath.Parse(bytes.NewReader(data))
body := &AssertXML{
Expand All @@ -74,9 +78,13 @@ func Has(t TestingT, data []byte, xmlAssert XMLAssertFunc) {
}
if err != nil {
assert.Failf(t, "data has invalid XML: %s", err.Error())
} else {
xmlAssert(body)

return false
}

xmlAssert(body)

return !t.Failed()
}

// Node searches for XML node by XML Path Syntax. Returns struct for asserting the node values.
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