Skip to content
Merged
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: 0 additions & 1 deletion apitest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ type TestingT interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Log(args ...interface{})
Failed() bool
}
1 change: 0 additions & 1 deletion assertions/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ type TestingT interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Log(args ...interface{})
Failed() bool
}
26 changes: 8 additions & 18 deletions assertjson/assertjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type TestingT interface {
Helper()
Errorf(format string, args ...interface{})
Log(args ...interface{})
Failed() bool
}

// AssertJSON - main structure that holds parsed JSON.
Expand All @@ -38,26 +37,21 @@ 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.
// Returns false if assertion has failed.
func FileHas(t TestingT, filename string, jsonAssert JSONAssertFunc) bool {
func FileHas(t TestingT, filename string, jsonAssert JSONAssertFunc) {
t.Helper()

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

return false
} else {
Has(t, data, jsonAssert)
}

return Has(t, data, jsonAssert)
}

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

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

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

return false
} else {
jsonAssert(j)
}

jsonAssert(j)

return !j.t.Failed()
}

func (j *AssertJSON) fail(message string, msgAndArgs ...interface{}) {
Expand Down
10 changes: 3 additions & 7 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) {
res := assertjson.FileHas(t, "./../test/testdata/object.json", func(json *assertjson.AssertJSON) {
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,8 +244,6 @@ 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 @@ -2789,10 +2787,9 @@ func TestHas(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
tester := &mock.Tester{}

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

tester.AssertContains(t, test.wantMessages)
assert.False(t, res)
})
}
}
Expand All @@ -2810,12 +2807,11 @@ func TestAssertNode_Exists(t *testing.T) {
tester := &mock.Tester{}

var got bool
res := assertjson.Has(tester, []byte(test.json), func(json *assertjson.AssertJSON) {
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: 6 additions & 14 deletions assertxml/assertxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,18 @@ type AssertNode struct {
type XMLAssertFunc func(xml *AssertXML)

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

return false
} else {
Has(t, data, xmlAssert)
}

return Has(t, data, xmlAssert)
}

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

return false
} else {
xmlAssert(body)
}

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: 0 additions & 1 deletion assertxml/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ type TestingT interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Log(args ...interface{})
Failed() bool
}
8 changes: 0 additions & 8 deletions internal/mock/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,27 @@ 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