Improve error handling

This commit is contained in:
Lena Fuhrimann 2017-05-28 19:38:59 +02:00
parent b3ed844ef3
commit a5b9d2ff34
13 changed files with 427 additions and 85 deletions

2
Gopkg.lock generated
View file

@ -60,4 +60,4 @@ memo = "3aa439f7fd3be3f2266640afdb7c86c6225490d5e1e14404bef5be1eaab6f640"
branch = "master" branch = "master"
name = "github.com/stretchr/testify" name = "github.com/stretchr/testify"
packages = ["assert"] packages = ["assert"]
revision = "4d4bfba8f1d1027c4fdbe371823030df51419987" revision = "b8c9b4ef3dade3a13f662c95942dcda39e962cd4"

View file

@ -1,9 +1,10 @@
package s3manager package s3manager
import ( import (
"encoding/json"
"io"
"log" "log"
"net/http" "net/http"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -25,15 +26,19 @@ var (
// handleHTTPError handles HTTP errors. // handleHTTPError handles HTTP errors.
func handleHTTPError(w http.ResponseWriter, err error) { func handleHTTPError(w http.ResponseWriter, err error) {
code := http.StatusInternalServerError var code int
if errors.Cause(err) == ErrBucketDoesNotExist { switch err := errors.Cause(err).(type) {
code = http.StatusNotFound case *json.SyntaxError:
} else if errors.Cause(err) == ErrKeyDoesNotExist {
code = http.StatusNotFound
} else if strings.Contains(err.Error(), errDecodingBody) {
code = http.StatusUnprocessableEntity
} else if strings.Contains(err.Error(), errParsingForm) {
code = http.StatusUnprocessableEntity code = http.StatusUnprocessableEntity
default:
if err == io.EOF {
code = http.StatusUnprocessableEntity
} else if err == ErrBucketDoesNotExist ||
err == ErrKeyDoesNotExist {
code = http.StatusNotFound
} else {
code = http.StatusInternalServerError
}
} }
http.Error(w, http.StatusText(code), code) http.Error(w, http.StatusText(code), code)

7
vendor/github.com/stretchr/testify/.travis.gofmt.sh generated vendored Executable file
View file

@ -0,0 +1,7 @@
#!/bin/bash
if [ -n "$(gofmt -l .)" ]; then
echo "Go code is not formatted:"
gofmt -d .
exit 1
fi

View file

@ -3,9 +3,6 @@ language: go
sudo: false sudo: false
go: go:
- 1.1
- 1.2
- 1.3
- 1.4 - 1.4
- 1.5 - 1.5
- 1.6 - 1.6
@ -13,4 +10,5 @@ go:
- tip - tip
script: script:
- ./.travis.gofmt.sh
- go test -v ./... - go test -v ./...

View file

@ -75,7 +75,7 @@ func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAn
// //
// actualObj, err := SomeFunction() // actualObj, err := SomeFunction()
// if a.Error(err, "An error was expected") { // if a.Error(err, "An error was expected") {
// assert.Equal(t, err, expectedError) // assert.Equal(t, expectedError, err)
// } // }
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
@ -228,7 +228,7 @@ func (a *Assertions) Nil(object interface{}, msgAndArgs ...interface{}) bool {
// //
// actualObj, err := SomeFunction() // actualObj, err := SomeFunction()
// if a.NoError(err) { // if a.NoError(err) {
// assert.Equal(t, actualObj, expectedObj) // assert.Equal(t, expectedObj, actualObj)
// } // }
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
@ -302,6 +302,16 @@ func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...in
return NotRegexp(a.t, rx, str, msgAndArgs...) return NotRegexp(a.t, rx, str, msgAndArgs...)
} }
// NotSubset asserts that the specified list(array, slice...) contains not all
// elements given in the specified subset(array, slice...).
//
// a.NotSubset([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func (a *Assertions) NotSubset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool {
return NotSubset(a.t, list, subset, msgAndArgs...)
}
// NotZero asserts that i is not the zero value for its type and returns the truth. // NotZero asserts that i is not the zero value for its type and returns the truth.
func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) bool { func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) bool {
return NotZero(a.t, i, msgAndArgs...) return NotZero(a.t, i, msgAndArgs...)
@ -328,6 +338,16 @@ func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...inter
return Regexp(a.t, rx, str, msgAndArgs...) return Regexp(a.t, rx, str, msgAndArgs...)
} }
// Subset asserts that the specified list(array, slice...) contains all
// elements given in the specified subset(array, slice...).
//
// a.Subset([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func (a *Assertions) Subset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool {
return Subset(a.t, list, subset, msgAndArgs...)
}
// True asserts that the specified value is true. // True asserts that the specified value is true.
// //
// a.True(myBool, "myBool should be true") // a.True(myBool, "myBool should be true")

View file

@ -38,7 +38,15 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil { if expected == nil || actual == nil {
return expected == actual return expected == actual
} }
if exp, ok := expected.([]byte); ok {
act, ok := actual.([]byte)
if !ok {
return false
} else if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}
return reflect.DeepEqual(expected, actual) return reflect.DeepEqual(expected, actual)
} }
@ -108,10 +116,12 @@ func CallerInfo() []string {
} }
parts := strings.Split(file, "/") parts := strings.Split(file, "/")
dir := parts[len(parts)-2]
file = parts[len(parts)-1] file = parts[len(parts)-1]
if (dir != "assert" && dir != "mock" && dir != "require") || file == "mock_test.go" { if len(parts) > 1 {
callers = append(callers, fmt.Sprintf("%s:%d", file, line)) dir := parts[len(parts)-2]
if (dir != "assert" && dir != "mock" && dir != "require") || file == "mock_test.go" {
callers = append(callers, fmt.Sprintf("%s:%d", file, line))
}
} }
// Drop the package // Drop the package
@ -181,7 +191,7 @@ func indentMessageLines(message string, longestLabelLen int) string {
// no need to align first line because it starts at the correct location (after the label) // no need to align first line because it starts at the correct location (after the label)
if i != 0 { if i != 0 {
// append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab // append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab
outBuf.WriteString("\n\r\t" + strings.Repeat(" ", longestLabelLen +1) + "\t") outBuf.WriteString("\n\r\t" + strings.Repeat(" ", longestLabelLen+1) + "\t")
} }
outBuf.WriteString(scanner.Text()) outBuf.WriteString(scanner.Text())
} }
@ -223,13 +233,13 @@ func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool {
content = append(content, labeledContent{"Messages", message}) content = append(content, labeledContent{"Messages", message})
} }
t.Errorf("\r" + getWhitespaceString() + labeledOutput(content...)) t.Errorf("%s", "\r"+getWhitespaceString()+labeledOutput(content...))
return false return false
} }
type labeledContent struct { type labeledContent struct {
label string label string
content string content string
} }
@ -296,7 +306,7 @@ func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{})
expected, actual = formatUnequalValues(expected, actual) expected, actual = formatUnequalValues(expected, actual)
return Fail(t, fmt.Sprintf("Not equal: \n"+ return Fail(t, fmt.Sprintf("Not equal: \n"+
"expected: %s\n"+ "expected: %s\n"+
"received: %s%s", expected, actual, diff), msgAndArgs...) "actual: %s%s", expected, actual, diff), msgAndArgs...)
} }
return true return true
@ -332,7 +342,7 @@ func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interfa
expected, actual = formatUnequalValues(expected, actual) expected, actual = formatUnequalValues(expected, actual)
return Fail(t, fmt.Sprintf("Not equal: \n"+ return Fail(t, fmt.Sprintf("Not equal: \n"+
"expected: %s\n"+ "expected: %s\n"+
"received: %s%s", expected, actual, diff), msgAndArgs...) "actual: %s%s", expected, actual, diff), msgAndArgs...)
} }
return true return true
@ -654,6 +664,92 @@ func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{})
} }
// Subset asserts that the specified list(array, slice...) contains all
// elements given in the specified subset(array, slice...).
//
// assert.Subset(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) {
if subset == nil {
return true // we consider nil to be equal to the nil set
}
subsetValue := reflect.ValueOf(subset)
defer func() {
if e := recover(); e != nil {
ok = false
}
}()
listKind := reflect.TypeOf(list).Kind()
subsetKind := reflect.TypeOf(subset).Kind()
if listKind != reflect.Array && listKind != reflect.Slice {
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...)
}
if subsetKind != reflect.Array && subsetKind != reflect.Slice {
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...)
}
for i := 0; i < subsetValue.Len(); i++ {
element := subsetValue.Index(i).Interface()
ok, found := includeElement(list, element)
if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...)
}
if !found {
return Fail(t, fmt.Sprintf("\"%s\" does not contain \"%s\"", list, element), msgAndArgs...)
}
}
return true
}
// NotSubset asserts that the specified list(array, slice...) contains not all
// elements given in the specified subset(array, slice...).
//
// assert.NotSubset(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) {
if subset == nil {
return false // we consider nil to be equal to the nil set
}
subsetValue := reflect.ValueOf(subset)
defer func() {
if e := recover(); e != nil {
ok = false
}
}()
listKind := reflect.TypeOf(list).Kind()
subsetKind := reflect.TypeOf(subset).Kind()
if listKind != reflect.Array && listKind != reflect.Slice {
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...)
}
if subsetKind != reflect.Array && subsetKind != reflect.Slice {
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...)
}
for i := 0; i < subsetValue.Len(); i++ {
element := subsetValue.Index(i).Interface()
ok, found := includeElement(list, element)
if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...)
}
if !found {
return true
}
}
return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...)
}
// Condition uses a Comparison to assert a complex condition. // Condition uses a Comparison to assert a complex condition.
func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool { func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool {
result := comp() result := comp()
@ -812,7 +908,7 @@ func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAn
expectedSlice := reflect.ValueOf(expected) expectedSlice := reflect.ValueOf(expected)
for i := 0; i < actualSlice.Len(); i++ { for i := 0; i < actualSlice.Len(); i++ {
result := InDelta(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta) result := InDelta(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta, msgAndArgs...)
if !result { if !result {
return result return result
} }
@ -882,7 +978,7 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m
// //
// actualObj, err := SomeFunction() // actualObj, err := SomeFunction()
// if assert.NoError(t, err) { // if assert.NoError(t, err) {
// assert.Equal(t, actualObj, expectedObj) // assert.Equal(t, expectedObj, actualObj)
// } // }
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
@ -898,7 +994,7 @@ func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool {
// //
// actualObj, err := SomeFunction() // actualObj, err := SomeFunction()
// if assert.Error(t, err, "An error was expected") { // if assert.Error(t, err, "An error was expected") {
// assert.Equal(t, err, expectedError) // assert.Equal(t, expectedError, err)
// } // }
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
@ -928,7 +1024,7 @@ func EqualError(t TestingT, theError error, errString string, msgAndArgs ...inte
if expected != actual { if expected != actual {
return Fail(t, fmt.Sprintf("Error message not equal:\n"+ return Fail(t, fmt.Sprintf("Error message not equal:\n"+
"expected: %q\n"+ "expected: %q\n"+
"received: %q", expected, actual), msgAndArgs...) "actual: %q", expected, actual), msgAndArgs...)
} }
return true return true
} }

View file

@ -254,8 +254,8 @@ func TestEqualFormatting(t *testing.T) {
msgAndArgs []interface{} msgAndArgs []interface{}
want string want string
}{ }{
{equalWant: "want", equalGot: "got", want: "\tassertions.go:[0-9]+: \r \r\tError Trace:\t\n\t\t\r\tError: \tNot equal: \n\t\t\r\t \texpected: \"want\"\n\t\t\r\t \treceived: \"got\"\n"}, {equalWant: "want", equalGot: "got", want: "\tassertions.go:[0-9]+: \r \r\tError Trace:\t\n\t\t\r\tError: \tNot equal: \n\t\t\r\t \texpected: \"want\"\n\t\t\r\t \tactual: \"got\"\n"},
{equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{"hello, %v!", "world"}, want: "\tassertions.go:[0-9]+: \r \r\tError Trace:\t\n\t\t\r\tError: \tNot equal: \n\t\t\r\t \texpected: \"want\"\n\t\t\r\t \treceived: \"got\"\n\t\t\r\tMessages: \thello, world!\n"}, {equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{"hello, %v!", "world"}, want: "\tassertions.go:[0-9]+: \r \r\tError Trace:\t\n\t\t\r\tError: \tNot equal: \n\t\t\r\t \texpected: \"want\"\n\t\t\r\t \tactual: \"got\"\n\t\t\r\tMessages: \thello, world!\n"},
} { } {
mockT := &bufferT{} mockT := &bufferT{}
Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...) Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...)
@ -493,6 +493,66 @@ func TestNotContains(t *testing.T) {
} }
} }
func TestSubset(t *testing.T) {
mockT := new(testing.T)
if !Subset(mockT, []int{1, 2, 3}, nil) {
t.Error("Subset should return true: given subset is nil")
}
if !Subset(mockT, []int{1, 2, 3}, []int{}) {
t.Error("Subset should return true: any set contains the nil set")
}
if !Subset(mockT, []int{1, 2, 3}, []int{1, 2}) {
t.Error("Subset should return true: [1, 2, 3] contains [1, 2]")
}
if !Subset(mockT, []int{1, 2, 3}, []int{1, 2, 3}) {
t.Error("Subset should return true: [1, 2, 3] contains [1, 2, 3]")
}
if !Subset(mockT, []string{"hello", "world"}, []string{"hello"}) {
t.Error("Subset should return true: [\"hello\", \"world\"] contains [\"hello\"]")
}
if Subset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) {
t.Error("Subset should return false: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]")
}
if Subset(mockT, []int{1, 2, 3}, []int{4, 5}) {
t.Error("Subset should return false: [1, 2, 3] does not contain [4, 5]")
}
if Subset(mockT, []int{1, 2, 3}, []int{1, 5}) {
t.Error("Subset should return false: [1, 2, 3] does not contain [1, 5]")
}
}
func TestNotSubset(t *testing.T) {
mockT := new(testing.T)
if NotSubset(mockT, []int{1, 2, 3}, nil) {
t.Error("NotSubset should return false: given subset is nil")
}
if NotSubset(mockT, []int{1, 2, 3}, []int{}) {
t.Error("NotSubset should return false: any set contains the nil set")
}
if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2}) {
t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2]")
}
if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2, 3}) {
t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2, 3]")
}
if NotSubset(mockT, []string{"hello", "world"}, []string{"hello"}) {
t.Error("NotSubset should return false: [\"hello\", \"world\"] contains [\"hello\"]")
}
if !NotSubset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) {
t.Error("NotSubset should return true: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]")
}
if !NotSubset(mockT, []int{1, 2, 3}, []int{4, 5}) {
t.Error("NotSubset should return true: [1, 2, 3] does not contain [4, 5]")
}
if !NotSubset(mockT, []int{1, 2, 3}, []int{1, 5}) {
t.Error("NotSubset should return true: [1, 2, 3] does not contain [1, 5]")
}
}
func Test_includeElement(t *testing.T) { func Test_includeElement(t *testing.T) {
list1 := []string{"Foo", "Bar"} list1 := []string{"Foo", "Bar"}
@ -630,7 +690,7 @@ func TestNoError(t *testing.T) {
}() }()
if err == nil { // err is not nil here! if err == nil { // err is not nil here!
t.Errorf("Error should be nil due to empty interface", err) t.Error("Error should be nil due to empty interface", err)
} }
False(t, NoError(mockT, err), "NoError should fail with empty error interface") False(t, NoError(mockT, err), "NoError should fail with empty error interface")
@ -664,7 +724,7 @@ func TestError(t *testing.T) {
}() }()
if err == nil { // err is not nil here! if err == nil { // err is not nil here!
t.Errorf("Error should be nil due to empty interface", err) t.Error("Error should be nil due to empty interface", err)
} }
True(t, Error(mockT, err), "Error should pass with empty error interface") True(t, Error(mockT, err), "Error should pass with empty error interface")
@ -1283,3 +1343,32 @@ func TestFailNowWithFullTestingT(t *testing.T) {
FailNow(mockT, "failed") FailNow(mockT, "failed")
}, "should call mockT.FailNow() rather than panicking") }, "should call mockT.FailNow() rather than panicking")
} }
func TestBytesEqual(t *testing.T) {
var cases = []struct {
a, b []byte
}{
{make([]byte, 2), make([]byte, 2)},
{make([]byte, 2), make([]byte, 2, 3)},
{nil, make([]byte, 0)},
}
for i, c := range cases {
Equal(t, reflect.DeepEqual(c.a, c.b), ObjectsAreEqual(c.a, c.b), "case %d failed", i+1)
}
}
func BenchmarkBytesEqual(b *testing.B) {
const size = 1024 * 8
s := make([]byte, size)
for i := range s {
s[i] = byte(i % 255)
}
s2 := make([]byte, size)
copy(s2, s)
mockT := &mockFailNowTestingT{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Equal(mockT, s, s2)
}
}

View file

@ -8,16 +8,16 @@ import (
"strings" "strings"
) )
// httpCode is a helper that returns HTTP code of the response. It returns -1 // httpCode is a helper that returns HTTP code of the response. It returns -1 and
// if building a new request fails. // an error if building a new request fails.
func httpCode(handler http.HandlerFunc, method, url string, values url.Values) int { func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
req, err := http.NewRequest(method, url+"?"+values.Encode(), nil) req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
if err != nil { if err != nil {
return -1 return -1, err
} }
handler(w, req) handler(w, req)
return w.Code return w.Code, nil
} }
// HTTPSuccess asserts that a specified handler returns a success status code. // HTTPSuccess asserts that a specified handler returns a success status code.
@ -26,11 +26,18 @@ func httpCode(handler http.HandlerFunc, method, url string, values url.Values) i
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool { func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
code := httpCode(handler, method, url, values) code, err := httpCode(handler, method, url, values)
if code == -1 { if err != nil {
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
return false return false
} }
return code >= http.StatusOK && code <= http.StatusPartialContent
isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent
if !isSuccessCode {
Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code))
}
return isSuccessCode
} }
// HTTPRedirect asserts that a specified handler returns a redirect status code. // HTTPRedirect asserts that a specified handler returns a redirect status code.
@ -39,11 +46,18 @@ func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, value
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool { func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
code := httpCode(handler, method, url, values) code, err := httpCode(handler, method, url, values)
if code == -1 { if err != nil {
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
return false return false
} }
return code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
if !isRedirectCode {
Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code))
}
return isRedirectCode
} }
// HTTPError asserts that a specified handler returns an error status code. // HTTPError asserts that a specified handler returns an error status code.
@ -52,11 +66,18 @@ func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, valu
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool { func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
code := httpCode(handler, method, url, values) code, err := httpCode(handler, method, url, values)
if code == -1 { if err != nil {
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
return false return false
} }
return code >= http.StatusBadRequest
isErrorCode := code >= http.StatusBadRequest
if !isErrorCode {
Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code))
}
return isErrorCode
} }
// HTTPBody is a helper that returns HTTP body of the response. It returns // HTTPBody is a helper that returns HTTP body of the response. It returns

View file

@ -19,21 +19,52 @@ func httpError(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
} }
func TestHTTPStatuses(t *testing.T) { func TestHTTPSuccess(t *testing.T) {
assert := New(t) assert := New(t)
mockT := new(testing.T)
assert.Equal(HTTPSuccess(mockT, httpOK, "GET", "/", nil), true) mockT1 := new(testing.T)
assert.Equal(HTTPSuccess(mockT, httpRedirect, "GET", "/", nil), false) assert.Equal(HTTPSuccess(mockT1, httpOK, "GET", "/", nil), true)
assert.Equal(HTTPSuccess(mockT, httpError, "GET", "/", nil), false) assert.False(mockT1.Failed())
assert.Equal(HTTPRedirect(mockT, httpOK, "GET", "/", nil), false) mockT2 := new(testing.T)
assert.Equal(HTTPRedirect(mockT, httpRedirect, "GET", "/", nil), true) assert.Equal(HTTPSuccess(mockT2, httpRedirect, "GET", "/", nil), false)
assert.Equal(HTTPRedirect(mockT, httpError, "GET", "/", nil), false) assert.True(mockT2.Failed())
assert.Equal(HTTPError(mockT, httpOK, "GET", "/", nil), false) mockT3 := new(testing.T)
assert.Equal(HTTPError(mockT, httpRedirect, "GET", "/", nil), false) assert.Equal(HTTPSuccess(mockT3, httpError, "GET", "/", nil), false)
assert.Equal(HTTPError(mockT, httpError, "GET", "/", nil), true) assert.True(mockT3.Failed())
}
func TestHTTPRedirect(t *testing.T) {
assert := New(t)
mockT1 := new(testing.T)
assert.Equal(HTTPRedirect(mockT1, httpOK, "GET", "/", nil), false)
assert.True(mockT1.Failed())
mockT2 := new(testing.T)
assert.Equal(HTTPRedirect(mockT2, httpRedirect, "GET", "/", nil), true)
assert.False(mockT2.Failed())
mockT3 := new(testing.T)
assert.Equal(HTTPRedirect(mockT3, httpError, "GET", "/", nil), false)
assert.True(mockT3.Failed())
}
func TestHTTPError(t *testing.T) {
assert := New(t)
mockT1 := new(testing.T)
assert.Equal(HTTPError(mockT1, httpOK, "GET", "/", nil), false)
assert.True(mockT1.Failed())
mockT2 := new(testing.T)
assert.Equal(HTTPError(mockT2, httpRedirect, "GET", "/", nil), false)
assert.True(mockT2.Failed())
mockT3 := new(testing.T)
assert.Equal(HTTPError(mockT3, httpError, "GET", "/", nil), true)
assert.False(mockT3.Failed())
} }
func TestHTTPStatusesWrapper(t *testing.T) { func TestHTTPStatusesWrapper(t *testing.T) {

View file

@ -141,7 +141,7 @@ func (c *Call) After(d time.Duration) *Call {
// arg := args.Get(0).(*map[string]interface{}) // arg := args.Get(0).(*map[string]interface{})
// arg["foo"] = "bar" // arg["foo"] = "bar"
// }) // })
func (c *Call) Run(fn func(Arguments)) *Call { func (c *Call) Run(fn func(args Arguments)) *Call {
c.lock() c.lock()
defer c.unlock() defer c.unlock()
c.RunFn = fn c.RunFn = fn
@ -214,8 +214,6 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
// */ // */
func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *Call) { func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *Call) {
m.mutex.Lock()
defer m.mutex.Unlock()
for i, call := range m.ExpectedCalls { for i, call := range m.ExpectedCalls {
if call.Method == method && call.Repeatability > -1 { if call.Method == method && call.Repeatability > -1 {
@ -288,6 +286,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
parts := strings.Split(functionPath, ".") parts := strings.Split(functionPath, ".")
functionName := parts[len(parts)-1] functionName := parts[len(parts)-1]
m.mutex.Lock()
found, call := m.findExpectedCall(functionName, arguments...) found, call := m.findExpectedCall(functionName, arguments...)
if found < 0 { if found < 0 {
@ -299,31 +298,29 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
// c) the developer has forgotten to add an accompanying On...Return pair. // c) the developer has forgotten to add an accompanying On...Return pair.
closestFound, closestCall := m.findClosestCall(functionName, arguments...) closestFound, closestCall := m.findClosestCall(functionName, arguments...)
m.mutex.Unlock()
if closestFound { if closestFound {
panic(fmt.Sprintf("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\n", callString(functionName, arguments, true), callString(functionName, closestCall.Arguments, true), diffArguments(arguments, closestCall.Arguments))) panic(fmt.Sprintf("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\n", callString(functionName, arguments, true), callString(functionName, closestCall.Arguments, true), diffArguments(arguments, closestCall.Arguments)))
} else { } else {
panic(fmt.Sprintf("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", functionName, functionName, callString(functionName, arguments, true), assert.CallerInfo())) panic(fmt.Sprintf("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", functionName, functionName, callString(functionName, arguments, true), assert.CallerInfo()))
} }
} else { }
m.mutex.Lock()
switch {
case call.Repeatability == 1:
call.Repeatability = -1
call.totalCalls++
case call.Repeatability > 1: switch {
call.Repeatability-- case call.Repeatability == 1:
call.totalCalls++ call.Repeatability = -1
call.totalCalls++
case call.Repeatability == 0: case call.Repeatability > 1:
call.totalCalls++ call.Repeatability--
} call.totalCalls++
m.mutex.Unlock()
case call.Repeatability == 0:
call.totalCalls++
} }
// add the call // add the call
m.mutex.Lock()
m.Calls = append(m.Calls, *newCall(m, functionName, arguments...)) m.Calls = append(m.Calls, *newCall(m, functionName, arguments...))
m.mutex.Unlock() m.mutex.Unlock()
@ -368,6 +365,8 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
// AssertExpectations asserts that everything specified with On and Return was // AssertExpectations asserts that everything specified with On and Return was
// in fact called as expected. Calls may have occurred in any order. // in fact called as expected. Calls may have occurred in any order.
func (m *Mock) AssertExpectations(t TestingT) bool { func (m *Mock) AssertExpectations(t TestingT) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
var somethingMissing bool var somethingMissing bool
var failedExpectations int var failedExpectations int
@ -379,14 +378,12 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
failedExpectations++ failedExpectations++
t.Logf("\u274C\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String()) t.Logf("\u274C\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
} else { } else {
m.mutex.Lock()
if expectedCall.Repeatability > 0 { if expectedCall.Repeatability > 0 {
somethingMissing = true somethingMissing = true
failedExpectations++ failedExpectations++
} else { } else {
t.Logf("\u2705\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String()) t.Logf("\u2705\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
} }
m.mutex.Unlock()
} }
} }
@ -399,6 +396,8 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
// AssertNumberOfCalls asserts that the method was called expectedCalls times. // AssertNumberOfCalls asserts that the method was called expectedCalls times.
func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool { func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
var actualCalls int var actualCalls int
for _, call := range m.calls() { for _, call := range m.calls() {
if call.Method == methodName { if call.Method == methodName {
@ -411,6 +410,8 @@ func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls
// AssertCalled asserts that the method was called. // AssertCalled asserts that the method was called.
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method. // It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool { func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
if !assert.True(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method should have been called with %d argument(s), but was not.", methodName, len(arguments))) { if !assert.True(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method should have been called with %d argument(s), but was not.", methodName, len(arguments))) {
t.Logf("%v", m.expectedCalls()) t.Logf("%v", m.expectedCalls())
return false return false
@ -421,6 +422,8 @@ func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interfac
// AssertNotCalled asserts that the method was not called. // AssertNotCalled asserts that the method was not called.
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method. // It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool { func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
if !assert.False(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method was called with %d argument(s), but should NOT have been.", methodName, len(arguments))) { if !assert.False(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method was called with %d argument(s), but should NOT have been.", methodName, len(arguments))) {
t.Logf("%v", m.expectedCalls()) t.Logf("%v", m.expectedCalls())
return false return false
@ -446,14 +449,10 @@ func (m *Mock) methodWasCalled(methodName string, expected []interface{}) bool {
} }
func (m *Mock) expectedCalls() []*Call { func (m *Mock) expectedCalls() []*Call {
m.mutex.Lock()
defer m.mutex.Unlock()
return append([]*Call{}, m.ExpectedCalls...) return append([]*Call{}, m.ExpectedCalls...)
} }
func (m *Mock) calls() []Call { func (m *Mock) calls() []Call {
m.mutex.Lock()
defer m.mutex.Unlock()
return append([]Call{}, m.Calls...) return append([]Call{}, m.Calls...)
} }
@ -514,7 +513,7 @@ func (f argumentMatcher) String() string {
// //
// |fn|, must be a function accepting a single argument (of the expected type) // |fn|, must be a function accepting a single argument (of the expected type)
// which returns a bool. If |fn| doesn't match the required signature, // which returns a bool. If |fn| doesn't match the required signature,
// MathedBy() panics. // MatchedBy() panics.
func MatchedBy(fn interface{}) argumentMatcher { func MatchedBy(fn interface{}) argumentMatcher {
fnType := reflect.TypeOf(fn) fnType := reflect.TypeOf(fn)
@ -715,6 +714,10 @@ func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
} }
func diffArguments(expected Arguments, actual Arguments) string { func diffArguments(expected Arguments, actual Arguments) string {
if len(expected) != len(actual) {
return fmt.Sprintf("Provided %v arguments, mocked for %v arguments", len(expected), len(actual))
}
for x := range expected { for x := range expected {
if diffString := diff(expected[x], actual[x]); diffString != "" { if diffString := diff(expected[x], actual[x]); diffString != "" {
return fmt.Sprintf("Difference found in argument %v:\n\n%s", x, diffString) return fmt.Sprintf("Difference found in argument %v:\n\n%s", x, diffString)

View file

@ -55,6 +55,11 @@ func (i *TestExampleImplementation) TheExampleMethodVariadicInterface(a ...inter
return args.Error(0) return args.Error(0)
} }
func (i *TestExampleImplementation) TheExampleMethodMixedVariadic(a int, b ...int) error {
args := i.Called(a, b)
return args.Error(0)
}
type ExampleFuncType func(string) error type ExampleFuncType func(string) error
func (i *TestExampleImplementation) TheExampleMethodFuncType(fn ExampleFuncType) error { func (i *TestExampleImplementation) TheExampleMethodFuncType(fn ExampleFuncType) error {
@ -226,6 +231,29 @@ func Test_Mock_On_WithVariadicFunc(t *testing.T) {
} }
func Test_Mock_On_WithMixedVariadicFunc(t *testing.T) {
// make a test impl object
var mockedService = new(TestExampleImplementation)
c := mockedService.
On("TheExampleMethodMixedVariadic", 1, []int{2, 3, 4}).
Return(nil)
assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
assert.Equal(t, 2, len(c.Arguments))
assert.Equal(t, 1, c.Arguments[0])
assert.Equal(t, []int{2, 3, 4}, c.Arguments[1])
assert.NotPanics(t, func() {
mockedService.TheExampleMethodMixedVariadic(1, 2, 3, 4)
})
assert.Panics(t, func() {
mockedService.TheExampleMethodMixedVariadic(1, 2, 3, 5)
})
}
func Test_Mock_On_WithVariadicFuncWithInterface(t *testing.T) { func Test_Mock_On_WithVariadicFuncWithInterface(t *testing.T) {
// make a test impl object // make a test impl object

View file

@ -88,7 +88,7 @@ func EqualValues(t TestingT, expected interface{}, actual interface{}, msgAndArg
// //
// actualObj, err := SomeFunction() // actualObj, err := SomeFunction()
// if assert.Error(t, err, "An error was expected") { // if assert.Error(t, err, "An error was expected") {
// assert.Equal(t, err, expectedError) // assert.Equal(t, expectedError, err)
// } // }
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
@ -279,7 +279,7 @@ func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) {
// //
// actualObj, err := SomeFunction() // actualObj, err := SomeFunction()
// if assert.NoError(t, err) { // if assert.NoError(t, err) {
// assert.Equal(t, actualObj, expectedObj) // assert.Equal(t, expectedObj, actualObj)
// } // }
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
@ -367,6 +367,18 @@ func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interf
} }
} }
// NotSubset asserts that the specified list(array, slice...) contains not all
// elements given in the specified subset(array, slice...).
//
// assert.NotSubset(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func NotSubset(t TestingT, list interface{}, subset interface{}, msgAndArgs ...interface{}) {
if !assert.NotSubset(t, list, subset, msgAndArgs...) {
t.FailNow()
}
}
// NotZero asserts that i is not the zero value for its type and returns the truth. // NotZero asserts that i is not the zero value for its type and returns the truth.
func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) { func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) {
if !assert.NotZero(t, i, msgAndArgs...) { if !assert.NotZero(t, i, msgAndArgs...) {
@ -399,6 +411,18 @@ func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface
} }
} }
// Subset asserts that the specified list(array, slice...) contains all
// elements given in the specified subset(array, slice...).
//
// assert.Subset(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func Subset(t TestingT, list interface{}, subset interface{}, msgAndArgs ...interface{}) {
if !assert.Subset(t, list, subset, msgAndArgs...) {
t.FailNow()
}
}
// True asserts that the specified value is true. // True asserts that the specified value is true.
// //
// assert.True(t, myBool, "myBool should be true") // assert.True(t, myBool, "myBool should be true")

View file

@ -76,7 +76,7 @@ func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAn
// //
// actualObj, err := SomeFunction() // actualObj, err := SomeFunction()
// if a.Error(err, "An error was expected") { // if a.Error(err, "An error was expected") {
// assert.Equal(t, err, expectedError) // assert.Equal(t, expectedError, err)
// } // }
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
@ -229,7 +229,7 @@ func (a *Assertions) Nil(object interface{}, msgAndArgs ...interface{}) {
// //
// actualObj, err := SomeFunction() // actualObj, err := SomeFunction()
// if a.NoError(err) { // if a.NoError(err) {
// assert.Equal(t, actualObj, expectedObj) // assert.Equal(t, expectedObj, actualObj)
// } // }
// //
// Returns whether the assertion was successful (true) or not (false). // Returns whether the assertion was successful (true) or not (false).
@ -303,6 +303,16 @@ func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...in
NotRegexp(a.t, rx, str, msgAndArgs...) NotRegexp(a.t, rx, str, msgAndArgs...)
} }
// NotSubset asserts that the specified list(array, slice...) contains not all
// elements given in the specified subset(array, slice...).
//
// a.NotSubset([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func (a *Assertions) NotSubset(list interface{}, subset interface{}, msgAndArgs ...interface{}) {
NotSubset(a.t, list, subset, msgAndArgs...)
}
// NotZero asserts that i is not the zero value for its type and returns the truth. // NotZero asserts that i is not the zero value for its type and returns the truth.
func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) { func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) {
NotZero(a.t, i, msgAndArgs...) NotZero(a.t, i, msgAndArgs...)
@ -329,6 +339,16 @@ func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...inter
Regexp(a.t, rx, str, msgAndArgs...) Regexp(a.t, rx, str, msgAndArgs...)
} }
// Subset asserts that the specified list(array, slice...) contains all
// elements given in the specified subset(array, slice...).
//
// a.Subset([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func (a *Assertions) Subset(list interface{}, subset interface{}, msgAndArgs ...interface{}) {
Subset(a.t, list, subset, msgAndArgs...)
}
// True asserts that the specified value is true. // True asserts that the specified value is true.
// //
// a.True(myBool, "myBool should be true") // a.True(myBool, "myBool should be true")