diff --git a/connection.go b/connection.go index c4d51d7..0253d59 100644 --- a/connection.go +++ b/connection.go @@ -116,4 +116,3 @@ func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) { return eq.rows, nil } - diff --git a/expectations.go b/expectations.go index 7d5c078..1fcec35 100644 --- a/expectations.go +++ b/expectations.go @@ -101,4 +101,3 @@ type expectedExec struct { result driver.Result } - diff --git a/util.go b/util.go index 3b43ecd..070e8b4 100644 --- a/util.go +++ b/util.go @@ -1,11 +1,17 @@ package sqlmock -import "strings" +import ( + "regexp" + "strings" +) + +var re *regexp.Regexp + +func init() { + re = regexp.MustCompile("\\s+") +} // strip out new lines and trim spaces func stripQuery(q string) (s string) { - s = strings.Replace(q, "\n", " ", -1) - s = strings.Replace(s, "\r", "", -1) - s = strings.TrimSpace(s) - return + return strings.TrimSpace(re.ReplaceAllString(q, " ")) } diff --git a/util_test.go b/util_test.go new file mode 100644 index 0000000..c4b3974 --- /dev/null +++ b/util_test.go @@ -0,0 +1,21 @@ +package sqlmock + +import ( + "testing" +) + +func TestQueryStringStripping(t *testing.T) { + assert := func(actual, expected string) { + if res := stripQuery(actual); res != expected { + t.Errorf("Expected '%s' to be '%s', but got '%s'", actual, expected, res) + } + } + + assert(" SELECT 1", "SELECT 1") + assert("SELECT 1 FROM d", "SELECT 1 FROM d") + assert(` + SELECT c + FROM D +`, "SELECT c FROM D") + assert("UPDATE (.+) SET ", "UPDATE (.+) SET") +}