Reworked Required and OmitEmpty #8
|
@ -137,12 +137,12 @@ func performErrorTest(t *testing.T, testCase errorTestCase) {
|
|||
|
||||
errorHandler(testCase.errors, resp)
|
||||
|
||||
assert.EqualValues(t, resp.Code, testCase.expected.statusCode)
|
||||
assert.EqualValues(t, resp.Header().Get("Content-Type"), testCase.expected.contentType)
|
||||
assert.EqualValues(t, testCase.expected.statusCode, resp.Code)
|
||||
assert.EqualValues(t, testCase.expected.contentType, resp.Header().Get("Content-Type"))
|
||||
|
||||
actualBody, err := ioutil.ReadAll(resp.Body)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, string(actualBody), testCase.expected.body)
|
||||
assert.EqualValues(t, testCase.expected.body, string(actualBody))
|
||||
}
|
||||
|
||||
type (
|
||||
|
|
|
@ -34,12 +34,12 @@ func Test_ErrorsAdd(t *testing.T) {
|
|||
|
||||
actual.Add(expected[0].FieldNames, expected[0].Classification, expected[0].Message)
|
||||
|
||||
assert.EqualValues(t, len(actual), 1)
|
||||
assert.EqualValues(t, fmt.Sprintf("%#v", actual), fmt.Sprintf("%#v", expected))
|
||||
assert.Len(t, actual, 1)
|
||||
assert.EqualValues(t, fmt.Sprintf("%#v", expected), fmt.Sprintf("%#v", actual))
|
||||
}
|
||||
|
||||
func Test_ErrorsLen(t *testing.T) {
|
||||
assert.EqualValues(t, errorsTestSet.Len(), len(errorsTestSet))
|
||||
assert.EqualValues(t, len(errorsTestSet), errorsTestSet.Len())
|
||||
}
|
||||
|
||||
func Test_ErrorsHas(t *testing.T) {
|
||||
|
@ -57,12 +57,12 @@ func Test_ErrorGetters(t *testing.T) {
|
|||
|
||||
fieldsActual := err.Fields()
|
||||
|
||||
assert.EqualValues(t, len(fieldsActual), 2)
|
||||
assert.EqualValues(t, fieldsActual[0], "field1")
|
||||
assert.EqualValues(t, fieldsActual[1], "field2")
|
||||
assert.Len(t, fieldsActual, 2)
|
||||
assert.EqualValues(t, "field1", fieldsActual[0])
|
||||
assert.EqualValues(t, "field2", fieldsActual[1])
|
||||
|
||||
assert.EqualValues(t, err.Kind(), "ErrorClass")
|
||||
assert.EqualValues(t, err.Error(), "The message")
|
||||
assert.EqualValues(t, "ErrorClass", err.Kind())
|
||||
assert.EqualValues(t, "The message", err.Error())
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ func performFileTest(t *testing.T, binder handlerFunc, testCase fileTestCase) {
|
|||
|
||||
fileTestHandler := func(actual BlogPost, errs Errors) {
|
||||
assertFileAsExpected(t, testCase, actual.HeaderImage, testCase.singleFile)
|
||||
assert.EqualValues(t, len(testCase.multipleFiles), len(actual.Pictures))
|
||||
assert.Len(t, actual.Pictures, len(testCase.multipleFiles))
|
||||
|
||||
for i, expectedFile := range testCase.multipleFiles {
|
||||
if i >= len(actual.Pictures) {
|
||||
|
@ -117,8 +117,8 @@ func assertFileAsExpected(t *testing.T, testCase fileTestCase, actual *multipart
|
|||
return
|
||||
}
|
||||
|
||||
assert.EqualValues(t, actual.Filename, expected.fileName)
|
||||
assert.EqualValues(t, unpackFileHeaderData(actual), expected.data)
|
||||
assert.EqualValues(t, expected.fileName, actual.Filename)
|
||||
assert.EqualValues(t, expected.data, unpackFileHeaderData(actual))
|
||||
}
|
||||
|
||||
func buildRequestWithFile(testCase fileTestCase) *http.Request {
|
||||
|
|
16
form_test.go
16
form_test.go
|
@ -176,15 +176,15 @@ func performFormTest(t *testing.T, binder handlerFunc, testCase formTestCase) {
|
|||
m := chi.NewRouter()
|
||||
|
||||
formTestHandler := func(actual interface{}, errs Errors) {
|
||||
if testCase.shouldSucceed && len(errs) > 0 {
|
||||
assert.EqualValues(t, 0, len(errs))
|
||||
} else if !testCase.shouldSucceed && len(errs) == 0 {
|
||||
assert.NotEqual(t, 0, len(errs))
|
||||
if testCase.shouldSucceed {
|
||||
assert.Empty(t, errs)
|
||||
} else if !testCase.shouldSucceed {
|
||||
assert.NotEmpty(t, errs)
|
||||
}
|
||||
expString := fmt.Sprintf("%+v", testCase.expected)
|
||||
actString := fmt.Sprintf("%+v", actual)
|
||||
if actString != expString && !(testCase.deepEqual && reflect.DeepEqual(testCase.expected, actual)) {
|
||||
assert.EqualValues(t, actString, expString)
|
||||
assert.EqualValues(t, expString, actString)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -195,7 +195,7 @@ func performFormTest(t *testing.T, binder handlerFunc, testCase formTestCase) {
|
|||
var actual Post
|
||||
errs := binder(req, &actual)
|
||||
p := testCase.expected.(Post)
|
||||
assert.EqualValues(t, actual.Title, p.Title)
|
||||
assert.EqualValues(t, p.Title, actual.Title)
|
||||
formTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
|
@ -217,7 +217,7 @@ func performFormTest(t *testing.T, binder handlerFunc, testCase formTestCase) {
|
|||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
p := testCase.expected.(BlogPost)
|
||||
assert.EqualValues(t, actual.Title, p.Title)
|
||||
assert.EqualValues(t, p.Title, actual.Title)
|
||||
formTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
|
@ -290,7 +290,7 @@ func Test_Default(t *testing.T) {
|
|||
m.Get("/", func(resp http.ResponseWriter, req *http.Request) {
|
||||
var f defaultForm
|
||||
Bind(req, &f)
|
||||
assert.EqualValues(t, f.Default, "hello world")
|
||||
assert.EqualValues(t, "hello world", f.Default)
|
||||
})
|
||||
resp := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
|
|
16
json_test.go
16
json_test.go
|
@ -143,16 +143,16 @@ func performJsonTest(t *testing.T, binder handlerFunc, testCase jsonTestCase) {
|
|||
jsonTestHandler := func(actual interface{}, errs Errors) {
|
||||
if fnName == "JSON" {
|
||||
if testCase.shouldSucceedOnJson {
|
||||
assert.EqualValues(t, 0, len(errs), errs)
|
||||
assert.Empty(t, errs, errs)
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", testCase.expected), fmt.Sprintf("%+v", actual))
|
||||
} else {
|
||||
assert.NotEqual(t, 0, len(errs))
|
||||
assert.NotEmpty(t, errs)
|
||||
}
|
||||
} else if fnName == "Bind" {
|
||||
if !testCase.shouldFailOnBind {
|
||||
assert.EqualValues(t, 0, len(errs), errs)
|
||||
assert.Empty(t, errs, errs)
|
||||
} else {
|
||||
assert.NotEqual(t, 0, len(errs))
|
||||
assert.NotEmpty(t, errs)
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", testCase.expected), fmt.Sprintf("%+v", actual))
|
||||
}
|
||||
}
|
||||
|
@ -182,7 +182,7 @@ func performJsonTest(t *testing.T, binder handlerFunc, testCase jsonTestCase) {
|
|||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Post
|
||||
errs := binder(req, &actual)
|
||||
assert.EqualValues(t, actual.Title, testCase.expected.(Post).Title)
|
||||
assert.EqualValues(t, testCase.expected.(Post).Title, actual.Title)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
|
@ -198,7 +198,7 @@ func performJsonTest(t *testing.T, binder handlerFunc, testCase jsonTestCase) {
|
|||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
assert.EqualValues(t, actual.Title, testCase.expected.(BlogPost).Title)
|
||||
assert.EqualValues(t, testCase.expected.(BlogPost).Title, actual.Title)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
|
@ -213,7 +213,7 @@ func performJsonTest(t *testing.T, binder handlerFunc, testCase jsonTestCase) {
|
|||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Group
|
||||
errs := binder(req, &actual)
|
||||
assert.EqualValues(t, actual.Name, testCase.expected.(Group).Name)
|
||||
assert.EqualValues(t, testCase.expected.(Group).Name, actual.Name)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
|
@ -248,7 +248,7 @@ func performJsonTest(t *testing.T, binder handlerFunc, testCase jsonTestCase) {
|
|||
if testCase.shouldSucceedOnJson &&
|
||||
httpRecorder.Code != http.StatusOK &&
|
||||
!testCase.shouldFailOnBind {
|
||||
assert.EqualValues(t, httpRecorder.Code, http.StatusOK)
|
||||
assert.EqualValues(t, http.StatusOK, httpRecorder.Code)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -62,11 +62,11 @@ func Test_SetWithProperType(t *testing.T) {
|
|||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Everything
|
||||
errs := Form(req, &actual)
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", actual), fmt.Sprintf("%+v", expectedOutputs[key]))
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", expectedOutputs[key]), fmt.Sprintf("%+v", actual))
|
||||
if key == "errorful" {
|
||||
assert.EqualValues(t, len(errs), 10)
|
||||
assert.Len(t, errs, 10)
|
||||
} else {
|
||||
assert.EqualValues(t, len(errs), 0)
|
||||
assert.Empty(t, errs)
|
||||
}
|
||||
})
|
||||
req, err := http.NewRequest("POST", testRoute, strings.NewReader(testCase))
|
||||
|
|
|
@ -85,12 +85,12 @@ func performMultipartFormTest(t *testing.T, binder handlerFunc, testCase multipa
|
|||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
if testCase.shouldSucceed && len(errs) > 0 {
|
||||
assert.EqualValues(t, 0, len(errs))
|
||||
} else if !testCase.shouldSucceed && len(errs) == 0 {
|
||||
assert.NotEqual(t, 0, len(errs))
|
||||
if testCase.shouldSucceed {
|
||||
assert.Empty(t, errs)
|
||||
} else if !testCase.shouldSucceed {
|
||||
assert.NotEmpty(t, errs)
|
||||
}
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", actual), fmt.Sprintf("%+v", testCase.inputAndExpected))
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", testCase.inputAndExpected), fmt.Sprintf("%+v", actual))
|
||||
})
|
||||
|
||||
multipartPayload, mpWriter := makeMultipartPayload(testCase)
|
||||
|
|
|
@ -492,7 +492,7 @@ func performValidationTest(t *testing.T, testCase validationTestCase) {
|
|||
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
actual := Validate(req, testCase.data)
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", actual), fmt.Sprintf("%+v", testCase.expectedErrors), testCase.description)
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", testCase.expectedErrors), fmt.Sprintf("%+v", actual), testCase.description)
|
||||
})
|
||||
|
||||
req, err := http.NewRequest("POST", testRoute, nil)
|
||||
|
|
Loading…
Reference in New Issue
Block a user