Skip to content

WIP: Adjusted outputs to increase readability #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
23 changes: 20 additions & 3 deletions expectations_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery {
}

func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
errMsgFmt := "\nwant\n %d\n got\n %d\n arguments"
if nil == e.args {
if e.noArgs && len(args) > 0 {
return fmt.Errorf("expected 0, but got %d arguments", len(args))
}
return nil
}
if len(args) != len(e.args) {
return fmt.Errorf("expected %d, but got %d arguments", len(e.args), len(args))
return fmt.Errorf(errMsgFmt, len(e.args), len(args))
}
// @TODO should we assert either all args are named or ordinal?
for k, v := range args {
Expand All @@ -54,7 +55,7 @@ func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
if named, isNamed := dval.(sql.NamedArg); isNamed {
dval = named.Value
if v.Name != named.Name {
return fmt.Errorf("named argument %d: name: \"%s\" does not match expected: \"%s\"", k, v.Name, named.Name)
return fmt.Errorf("named argument %d: name: '%s' does not match expected: '%s'", k, v.Name, named.Name)
}
} else if k+1 != v.Ordinal {
return fmt.Errorf("argument %d: ordinal position: %d does not match expected: %d", k, k+1, v.Ordinal)
Expand All @@ -67,12 +68,28 @@ func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
}

if !reflect.DeepEqual(darg, v.Value) {
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value)

return fmt.Errorf("Summary:\nargument %d \nwant: \n type '%T' \n value '%+v' \ngot: \n type '%T' \n value '%+v'",
k,
darg,
darg,
v.Value,
v.Value)
}
}
return nil
}

func errorFormat(argIndex int, expectedType interface{}, expectedValue interface{}, actualType interface{}, actualValue interface{}) error {
return fmt.Errorf("Argument %d \nExpected: \n\tType: '%T' \n\tValue: '%+v' \nReceived: \n\tType: '%T' \n\tValue: '%+v'",
argIndex,
expectedType,
expectedType,
actualType,
actualType)
}


func (e *queryBasedExpectation) attemptArgMatch(args []driver.NamedValue) (err error) {
// catch panic
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion sqlmock_before_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
}

if err := expected.argsMatches(args); err != nil {
return nil, fmt.Errorf("Query '%s', arguments do not match: %s", query, err)
return nil, fmt.Errorf("Query " + ErrMsgFmt, query, err)
}

expected.triggered = true
Expand Down
11 changes: 7 additions & 4 deletions sqlmock_go18.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// +build go1.8

package sqlmock

import (
Expand Down Expand Up @@ -242,7 +241,7 @@ func (c *sqlmock) query(query string, args []driver.NamedValue) (*ExpectedQuery,
}

if err := expected.argsMatches(args); err != nil {
return nil, fmt.Errorf("Query '%s', arguments do not match: %s", query, err)
return nil, fmt.Errorf("Query " + errMsgFmt, query, err)
}

expected.triggered = true
Expand Down Expand Up @@ -278,6 +277,10 @@ func (c *sqlmock) Exec(query string, args []driver.Value) (driver.Result, error)
return ex.result, nil
}

const (
errMsgFmt = "'%s', \n\narguments do not match: \n %s"
)

func (c *sqlmock) exec(query string, args []driver.NamedValue) (*ExpectedExec, error) {
var expected *ExpectedExec
var fulfilled int
Expand Down Expand Up @@ -324,7 +327,7 @@ func (c *sqlmock) exec(query string, args []driver.NamedValue) (*ExpectedExec, e
}

if err := expected.argsMatches(args); err != nil {
return nil, fmt.Errorf("ExecQuery '%s', arguments do not match: %s", query, err)
return nil, fmt.Errorf("ExecQuery: " + errMsgFmt, query, err)
}

expected.triggered = true
Expand All @@ -333,7 +336,7 @@ func (c *sqlmock) exec(query string, args []driver.NamedValue) (*ExpectedExec, e
}

if expected.result == nil {
return nil, fmt.Errorf("ExecQuery '%s' with args %+v, must return a database/sql/driver.Result, but it was not set for expectation %T as %+v", query, args, expected, expected)
return nil, fmt.Errorf("\nExecQuery '%s' with args %+v, must return a database/sql/driver.Result, but it was not set for expectation %T as %+v", query, args, expected, expected)
}

return expected, nil
Expand Down