diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 51341e5..a3d31ef 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/apitest/testing.go b/apitest/testing.go index 1380061..59f0a46 100644 --- a/apitest/testing.go +++ b/apitest/testing.go @@ -6,4 +6,5 @@ type TestingT interface { Error(args ...interface{}) Errorf(format string, args ...interface{}) Log(args ...interface{}) + Failed() bool } diff --git a/assertions/testing.go b/assertions/testing.go index 9c3e590..6ede03a 100644 --- a/assertions/testing.go +++ b/assertions/testing.go @@ -6,4 +6,5 @@ type TestingT interface { Error(args ...interface{}) Errorf(format string, args ...interface{}) Log(args ...interface{}) + Failed() bool } diff --git a/assertjson/assertjson.go b/assertjson/assertjson.go index 3b2a136..389ad9a 100644 --- a/assertjson/assertjson.go +++ b/assertjson/assertjson.go @@ -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. @@ -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. diff --git a/assertjson/assertjson_test.go b/assertjson/assertjson_test.go index a9792bc..12ad5f4 100644 --- a/assertjson/assertjson_test.go +++ b/assertjson/assertjson_test.go @@ -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", @@ -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{} @@ -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) }) } } diff --git a/assertxml/testing.go b/assertxml/testing.go index 5632320..c039372 100644 --- a/assertxml/testing.go +++ b/assertxml/testing.go @@ -6,4 +6,5 @@ type TestingT interface { Error(args ...interface{}) Errorf(format string, args ...interface{}) Log(args ...interface{}) + Failed() bool } diff --git a/internal/mock/tester.go b/internal/mock/tester.go index 1b958ec..4c23210 100644 --- a/internal/mock/tester.go +++ b/internal/mock/tester.go @@ -7,20 +7,24 @@ 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...)) } @@ -28,6 +32,10 @@ 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) {