diff --git a/domtest/error.go b/domtest/error.go deleted file mode 100644 index 1c4b937..0000000 --- a/domtest/error.go +++ /dev/null @@ -1,47 +0,0 @@ -package domtest - -import ( - "net/http" - - "golang.org/x/net/html/atom" - - "github.com/crhntr/dom/spec" -) - -func DocumentResponseErrorMessage(t T, res *http.Response, httpStatusCode int, errMessageElementSelector string) spec.Element { - t.Helper() - if res.StatusCode != httpStatusCode { - t.Errorf("unexpected status code: %d", res.StatusCode) - } - - document := Response(t, res) - if document == nil { - return nil - } - - errEl := document.QuerySelector(errMessageElementSelector) - if errEl == nil { - t.Errorf("error message element not found for query: %s", errMessageElementSelector) - return nil - } - return errEl -} - -func DocumentFragmentResponseErrorMessage(t T, res *http.Response, httpStatusCode int, errMessageElementSelector string, parent atom.Atom) spec.Element { - t.Helper() - if res.StatusCode != httpStatusCode { - t.Errorf("unexpected status code: %d", res.StatusCode) - } - - document := DocumentFragmentResponse(t, res, parent) - if document == nil { - return nil - } - - errEl := document.QuerySelector(errMessageElementSelector) - if errEl == nil { - t.Errorf("error message element not found for query: %s", errMessageElementSelector) - return nil - } - return errEl -} diff --git a/domtest/error_test.go b/domtest/error_test.go deleted file mode 100644 index 65d496c..0000000 --- a/domtest/error_test.go +++ /dev/null @@ -1,133 +0,0 @@ -package domtest_test - -import ( - "errors" - "io" - "net/http" - "net/http/httptest" - "testing" - "testing/iotest" - - "github.com/stretchr/testify/assert" - "golang.org/x/net/html/atom" - - "github.com/crhntr/dom/domtest" - "github.com/crhntr/dom/internal/fakes" - "github.com/crhntr/dom/spec" -) - -func TestDocumentResponseErrorMessage(t *testing.T) { - for _, tt := range []struct { - Name string - ResCode int - ExpCode int - Body string - Query string - Assert func(t *testing.T, fakeT *fakes.T, el spec.Element) - }{ - { - Name: "wrong status code", - ExpCode: http.StatusBadRequest, - ResCode: http.StatusOK, - Query: "#error", - Assert: func(t *testing.T, fakeT *fakes.T, el spec.Element) { - assert.NotZero(t, errorMethodCallCount(fakeT)) - assert.Nil(t, el) - }, - }, - { - Name: "query succeeds ", - ExpCode: http.StatusBadRequest, - ResCode: http.StatusBadRequest, - Query: "#error", - Body: `

bad input value

`, - Assert: func(t *testing.T, fakeT *fakes.T, el spec.Element) { - assert.Zero(t, errorMethodCallCount(fakeT)) - assert.NotNil(t, el) - assert.Equal(t, "bad input value", el.TextContent()) - }, - }, - } { - t.Run(tt.Name, func(t *testing.T) { - rec := httptest.NewRecorder() - rec.WriteHeader(tt.ResCode) - _, _ = rec.WriteString(tt.Body) - res := rec.Result() - fakeT := new(fakes.T) - el := domtest.DocumentResponseErrorMessage(fakeT, res, tt.ExpCode, tt.Query) - tt.Assert(t, fakeT, el) - }) - } - - t.Run("fail to read document", func(t *testing.T) { - res := &http.Response{ - StatusCode: http.StatusOK, - Body: io.NopCloser(iotest.ErrReader(errors.New("banana"))), - } - fakeT := new(fakes.T) - el := domtest.DocumentResponseErrorMessage(fakeT, res, http.StatusOK, "#error") - - assert.NotZero(t, errorMethodCallCount(fakeT)) - assert.Nil(t, el) - }) -} - -func errorMethodCallCount(t *fakes.T) int { - return t.ErrorfCallCount() + t.ErrorCallCount() + t.FailNowCallCount() -} - -func TestDocumentFragmentResponseErrorMessage(t *testing.T) { - for _, tt := range []struct { - Name string - ResCode int - ExpCode int - Body string - Query string - Assert func(t *testing.T, fakeT *fakes.T, el spec.Element) - }{ - { - Name: "wrong status code", - ExpCode: http.StatusBadRequest, - ResCode: http.StatusOK, - Query: "#error", - Assert: func(t *testing.T, fakeT *fakes.T, el spec.Element) { - assert.NotZero(t, errorMethodCallCount(fakeT)) - assert.Nil(t, el) - }, - }, - { - Name: "query succeeds ", - ExpCode: http.StatusBadRequest, - ResCode: http.StatusBadRequest, - Query: "#error", - Body: `

bad input value

`, - Assert: func(t *testing.T, fakeT *fakes.T, el spec.Element) { - assert.Zero(t, errorMethodCallCount(fakeT)) - assert.NotNil(t, el) - assert.Equal(t, "bad input value", el.TextContent()) - }, - }, - } { - t.Run(tt.Name, func(t *testing.T) { - rec := httptest.NewRecorder() - rec.WriteHeader(tt.ResCode) - _, _ = rec.WriteString(tt.Body) - res := rec.Result() - fakeT := new(fakes.T) - el := domtest.DocumentFragmentResponseErrorMessage(fakeT, res, tt.ExpCode, tt.Query, atom.Body) - tt.Assert(t, fakeT, el) - }) - } - - t.Run("fail to read document", func(t *testing.T) { - res := &http.Response{ - StatusCode: http.StatusOK, - Body: io.NopCloser(iotest.ErrReader(errors.New("banana"))), - } - fakeT := new(fakes.T) - el := domtest.DocumentFragmentResponseErrorMessage(fakeT, res, http.StatusOK, "#error", atom.Body) - - assert.NotZero(t, errorMethodCallCount(fakeT)) - assert.Nil(t, el) - }) -} diff --git a/domtest/query.go b/domtest/query.go new file mode 100644 index 0000000..ed44e64 --- /dev/null +++ b/domtest/query.go @@ -0,0 +1,75 @@ +package domtest + +import ( + "net/http" + + "golang.org/x/net/html/atom" + + "github.com/crhntr/dom/spec" +) + +func DocumentResponseQuery(t T, res *http.Response, statusCode int, selector string) spec.Element { + t.Helper() + if res.StatusCode != statusCode { + t.Errorf("unexpected status code: %d", res.StatusCode) + } + + document := Response(t, res) + if document == nil { + return nil + } + + errEl := document.QuerySelector(selector) + if errEl == nil { + t.Errorf("error message element not found for query: %s", selector) + return nil + } + return errEl +} + +func DocumentFragmentQuery(t T, res *http.Response, statusCode int, selector string, parent atom.Atom) spec.Element { + t.Helper() + if res.StatusCode != statusCode { + t.Errorf("unexpected status code: %d", res.StatusCode) + } + + document := DocumentFragmentResponse(t, res, parent) + if document == nil { + return nil + } + + errEl := document.QuerySelector(selector) + if errEl == nil { + t.Errorf("error message element not found for query: %s", selector) + return nil + } + return errEl +} + +func DocumentResponseQueryAll(t T, res *http.Response, statusCode int, selector string) spec.NodeList[spec.Element] { + t.Helper() + if res.StatusCode != statusCode { + t.Errorf("unexpected status code: %d", res.StatusCode) + } + + document := Response(t, res) + if document == nil { + return nil + } + + return document.QuerySelectorAll(selector) +} + +func DocumentFragmentQueryAll(t T, res *http.Response, statusCode int, selector string, parent atom.Atom) spec.NodeList[spec.Element] { + t.Helper() + if res.StatusCode != statusCode { + t.Errorf("unexpected status code: %d", res.StatusCode) + } + + document := DocumentFragmentResponse(t, res, parent) + if document == nil { + return nil + } + + return document.QuerySelectorAll(selector) +} diff --git a/domtest/query_test.go b/domtest/query_test.go new file mode 100644 index 0000000..c61f511 --- /dev/null +++ b/domtest/query_test.go @@ -0,0 +1,246 @@ +package domtest_test + +import ( + "errors" + "io" + "net/http" + "net/http/httptest" + "testing" + "testing/iotest" + + "github.com/stretchr/testify/assert" + "golang.org/x/net/html/atom" + + "github.com/crhntr/dom/domtest" + "github.com/crhntr/dom/internal/fakes" + "github.com/crhntr/dom/spec" +) + +func TestDocumentResponseQuery(t *testing.T) { + for _, tt := range []struct { + Name string + ResCode int + ExpCode int + Body string + Query string + Assert func(t *testing.T, fakeT *fakes.T, el spec.Element) + }{ + { + Name: "wrong status code", + ExpCode: http.StatusBadRequest, + ResCode: http.StatusOK, + Query: "#error", + Assert: func(t *testing.T, fakeT *fakes.T, el spec.Element) { + assert.NotZero(t, errorMethodCallCount(fakeT)) + assert.Nil(t, el) + }, + }, + { + Name: "query succeeds ", + ExpCode: http.StatusBadRequest, + ResCode: http.StatusBadRequest, + Query: "#error", + Body: `

bad input value

`, + Assert: func(t *testing.T, fakeT *fakes.T, el spec.Element) { + assert.Zero(t, errorMethodCallCount(fakeT)) + assert.NotNil(t, el) + assert.Equal(t, "bad input value", el.TextContent()) + }, + }, + } { + t.Run(tt.Name, func(t *testing.T) { + rec := httptest.NewRecorder() + rec.WriteHeader(tt.ResCode) + _, _ = rec.WriteString(tt.Body) + res := rec.Result() + fakeT := new(fakes.T) + el := domtest.DocumentResponseQuery(fakeT, res, tt.ExpCode, tt.Query) + tt.Assert(t, fakeT, el) + }) + } + + t.Run("fail to read document", func(t *testing.T) { + res := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(iotest.ErrReader(errors.New("banana"))), + } + fakeT := new(fakes.T) + el := domtest.DocumentResponseQuery(fakeT, res, http.StatusOK, "#error") + + assert.NotZero(t, errorMethodCallCount(fakeT)) + assert.Nil(t, el) + }) +} + +func errorMethodCallCount(t *fakes.T) int { + return t.ErrorfCallCount() + t.ErrorCallCount() + t.FailNowCallCount() +} + +func TestDocumentFragmentQuery(t *testing.T) { + for _, tt := range []struct { + Name string + ResCode int + ExpCode int + Body string + Query string + Assert func(t *testing.T, fakeT *fakes.T, el spec.Element) + }{ + { + Name: "wrong status code", + ExpCode: http.StatusBadRequest, + ResCode: http.StatusOK, + Query: "#error", + Assert: func(t *testing.T, fakeT *fakes.T, el spec.Element) { + assert.NotZero(t, errorMethodCallCount(fakeT)) + assert.Nil(t, el) + }, + }, + { + Name: "query succeeds ", + ExpCode: http.StatusBadRequest, + ResCode: http.StatusBadRequest, + Query: "#error", + Body: `

bad input value

`, + Assert: func(t *testing.T, fakeT *fakes.T, el spec.Element) { + assert.Zero(t, errorMethodCallCount(fakeT)) + assert.NotNil(t, el) + assert.Equal(t, "bad input value", el.TextContent()) + }, + }, + } { + t.Run(tt.Name, func(t *testing.T) { + rec := httptest.NewRecorder() + rec.WriteHeader(tt.ResCode) + _, _ = rec.WriteString(tt.Body) + res := rec.Result() + fakeT := new(fakes.T) + el := domtest.DocumentFragmentQuery(fakeT, res, tt.ExpCode, tt.Query, atom.Body) + tt.Assert(t, fakeT, el) + }) + } + + t.Run("fail to read document", func(t *testing.T) { + res := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(iotest.ErrReader(errors.New("banana"))), + } + fakeT := new(fakes.T) + el := domtest.DocumentFragmentQuery(fakeT, res, http.StatusOK, "#error", atom.Body) + + assert.NotZero(t, errorMethodCallCount(fakeT)) + assert.Nil(t, el) + }) +} + +func TestDocumentResponseQueryAll(t *testing.T) { + for _, tt := range []struct { + Name string + ResCode int + ExpCode int + Body string + Query string + Assert func(t *testing.T, fakeT *fakes.T, result spec.NodeList[spec.Element]) + }{ + { + Name: "wrong status code", + ExpCode: http.StatusBadRequest, + ResCode: http.StatusOK, + Query: "#error", + Assert: func(t *testing.T, fakeT *fakes.T, result spec.NodeList[spec.Element]) { + assert.NotZero(t, errorMethodCallCount(fakeT)) + assert.Nil(t, result) + }, + }, + { + Name: "query succeeds ", + ExpCode: http.StatusBadRequest, + ResCode: http.StatusBadRequest, + Query: "#error", + Body: `

bad input value

`, + Assert: func(t *testing.T, fakeT *fakes.T, result spec.NodeList[spec.Element]) { + assert.Zero(t, errorMethodCallCount(fakeT)) + assert.NotNil(t, result) + assert.Equal(t, 1, result.Length()) + }, + }, + } { + t.Run(tt.Name, func(t *testing.T) { + rec := httptest.NewRecorder() + rec.WriteHeader(tt.ResCode) + _, _ = rec.WriteString(tt.Body) + res := rec.Result() + fakeT := new(fakes.T) + el := domtest.DocumentResponseQueryAll(fakeT, res, tt.ExpCode, tt.Query) + tt.Assert(t, fakeT, el) + }) + } + + t.Run("fail to read document", func(t *testing.T) { + res := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(iotest.ErrReader(errors.New("banana"))), + } + fakeT := new(fakes.T) + el := domtest.DocumentResponseQueryAll(fakeT, res, http.StatusOK, "#error") + + assert.NotZero(t, errorMethodCallCount(fakeT)) + assert.Nil(t, el) + }) +} + +func TestDocumentFragmentQueryAll(t *testing.T) { + for _, tt := range []struct { + Name string + ResCode int + ExpCode int + Body string + Query string + Assert func(t *testing.T, fakeT *fakes.T, result spec.NodeList[spec.Element]) + }{ + { + Name: "wrong status code", + ExpCode: http.StatusBadRequest, + ResCode: http.StatusOK, + Query: "#error", + Assert: func(t *testing.T, fakeT *fakes.T, result spec.NodeList[spec.Element]) { + assert.NotZero(t, errorMethodCallCount(fakeT)) + assert.Nil(t, result) + }, + }, + { + Name: "query succeeds ", + ExpCode: http.StatusBadRequest, + ResCode: http.StatusBadRequest, + Query: "#error", + Body: `

bad input value

`, + Assert: func(t *testing.T, fakeT *fakes.T, result spec.NodeList[spec.Element]) { + assert.Zero(t, errorMethodCallCount(fakeT)) + if assert.NotNil(t, result) { + assert.Equal(t, 1, result.Length()) + } + }, + }, + } { + t.Run(tt.Name, func(t *testing.T) { + rec := httptest.NewRecorder() + rec.WriteHeader(tt.ResCode) + _, _ = rec.WriteString(tt.Body) + res := rec.Result() + fakeT := new(fakes.T) + el := domtest.DocumentFragmentQueryAll(fakeT, res, tt.ExpCode, tt.Query, atom.Body) + tt.Assert(t, fakeT, el) + }) + } + + t.Run("fail to read document", func(t *testing.T) { + res := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(iotest.ErrReader(errors.New("banana"))), + } + fakeT := new(fakes.T) + el := domtest.DocumentFragmentQueryAll(fakeT, res, http.StatusOK, "#error", atom.Body) + + assert.NotZero(t, errorMethodCallCount(fakeT)) + assert.Nil(t, el) + }) +}