1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-04-19 11:52:35 +02:00

add custom argument matching

This commit is contained in:
Algirdas Matas 2014-05-29 16:43:37 +03:00
parent fa31f407df
commit 27fabfa23a
3 changed files with 196 additions and 171 deletions

View File

@ -6,6 +6,12 @@ import (
"regexp" "regexp"
) )
// Argument interface allows to match
// any argument in specific way
type Argument interface {
Match(driver.Value) bool
}
// an expectation interface // an expectation interface
type expectation interface { type expectation interface {
fulfilled() bool fulfilled() bool
@ -47,6 +53,13 @@ func (e *queryBasedExpectation) argsMatches(args []driver.Value) bool {
return false return false
} }
for k, v := range args { for k, v := range args {
matcher, ok := e.args[k].(Argument)
if ok {
if !matcher.Match(v) {
return false
}
continue
}
vi := reflect.ValueOf(v) vi := reflect.ValueOf(v)
ai := reflect.ValueOf(e.args[k]) ai := reflect.ValueOf(e.args[k])
switch vi.Kind() { switch vi.Kind() {

View File

@ -6,6 +6,13 @@ import (
"time" "time"
) )
type matcher struct {
}
func (m matcher) Match(driver.Value) bool {
return true
}
func TestQueryExpectationArgComparison(t *testing.T) { func TestQueryExpectationArgComparison(t *testing.T) {
e := &queryBasedExpectation{} e := &queryBasedExpectation{}
against := []driver.Value{5} against := []driver.Value{5}
@ -44,4 +51,9 @@ func TestQueryExpectationArgComparison(t *testing.T) {
if !e.argsMatches(against) { if !e.argsMatches(against) {
t.Error("arguments should match (time will be compared only by type), but it did not") t.Error("arguments should match (time will be compared only by type), but it did not")
} }
against = []driver.Value{5, matcher{}}
if !e.argsMatches(against) {
t.Error("arguments should match, but it did not")
}
} }