diff --git a/obj/obj_test.go b/obj/obj_test.go index 2a90bad..924d94b 100644 --- a/obj/obj_test.go +++ b/obj/obj_test.go @@ -2,8 +2,9 @@ package obj import ( "fmt" - "regexp" "testing" + + tu "github.com/bayashi/witness/testutil" ) type Example struct { @@ -69,9 +70,8 @@ func TestBooleanPointer(t *testing.T) { f := false o := NewObject(&f) - expectRe := regexp.MustCompile(`\(\*bool\)\([0-9a-fx]+\)\(false\)`) - if expectRe.FindStringSubmatch(o.AsString()) == nil { - t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString()) + if ok, msg := tu.Match(`\(\*bool\)\([0-9a-fx]+\)\(false\)`, o.AsString()); !ok { + t.Error(msg) } } @@ -79,9 +79,8 @@ func TestError(t *testing.T) { e := fmt.Errorf("foo error") o := NewObject(e) - expectRe := regexp.MustCompile(`\(\*errors\.errorString\)\([0-9a-fx]+\)\(foo error\)`) - if expectRe.FindStringSubmatch(o.AsString()) == nil { - t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString()) + if ok, msg := tu.Match(`\(\*errors\.errorString\)\([0-9a-fx]+\)\(foo error\)`, o.AsString()); !ok { + t.Error(msg) } } @@ -89,9 +88,8 @@ func TestPointerValueInt(t *testing.T) { i := 123 o := NewObject(&i) - expectRe := regexp.MustCompile(`\(\*int\)\([0-9a-fx]+\)\(123\)`) - if expectRe.FindStringSubmatch(o.AsString()) == nil { - t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString()) + if ok, msg := tu.Match(`\(\*int\)\([0-9a-fx]+\)\(123\)`, o.AsString()); !ok { + t.Error(msg) } } @@ -99,9 +97,8 @@ func TestPointerValueFloat(t *testing.T) { var i float64 = 0.123 o := NewObject(&i) - expectRe := regexp.MustCompile(`\(*float64\)\([0-9a-fx]+\)\(0\.123\)`) - if expectRe.FindStringSubmatch(o.AsString()) == nil { - t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString()) + if ok, msg := tu.Match(`\(*float64\)\([0-9a-fx]+\)\(0\.123\)`, o.AsString()); !ok { + t.Error(msg) } } @@ -109,9 +106,8 @@ func TestStructValue(t *testing.T) { i := struct{ ID int }{ ID: 123 } o := NewObject(i) - expectRe := regexp.MustCompile(`struct { ID int }{ID:123}`) - if expectRe.FindStringSubmatch(o.AsString()) == nil { - t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString()) + if ok, msg := tu.Match(`struct { ID int }{ID:123}`, o.AsString()); !ok { + t.Error(msg) } } @@ -119,9 +115,8 @@ func TestStructPointerValue(t *testing.T) { i := struct{ ID int }{ ID: 123 } o := NewObject(&i) - expectRe := regexp.MustCompile(`[0-9a-fx]+, &struct { ID int }{ID:123}`) - if expectRe.FindStringSubmatch(o.AsString()) == nil { - t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString()) + if ok, msg := tu.Match(`[0-9a-fx]+, &struct { ID int }{ID:123}`, o.AsString()); !ok { + t.Error(msg) } } diff --git a/testutil/regexp.go b/testutil/regexp.go new file mode 100644 index 0000000..1cae29b --- /dev/null +++ b/testutil/regexp.go @@ -0,0 +1,15 @@ +package testutil + +import ( + "fmt" + "regexp" +) + +func Match(re string, target string) (bool, string) { + reg := regexp.MustCompile(re) + if !reg.MatchString(target) { + return false, fmt.Sprintf("Not matched the regexp `%s` for %q", reg.String(), target) + } + + return true, "" +} diff --git a/trace/trace_test.go b/trace/trace_test.go index 80f6bfe..a0ec21d 100644 --- a/trace/trace_test.go +++ b/trace/trace_test.go @@ -1,8 +1,9 @@ package trace import ( - "regexp" "testing" + + tu "github.com/bayashi/witness/testutil" ) func TestInfo(t *testing.T) { @@ -10,9 +11,9 @@ func TestInfo(t *testing.T) { if len(trace) != 1 { t.Error("trace length should be 1.") } - var traceRegexp = regexp.MustCompile(`/witness/trace/trace_test\.go:\d+$`) - if !traceRegexp.MatchString(trace[0]) { - t.Errorf("trace was not match Regexp:`%s`, Got:`%s`", traceRegexp.String(), trace[0]) + + if ok, msg := tu.Match(`/witness/trace/trace_test\.go:\d+$`, trace[0]); !ok { + t.Error(msg) } } diff --git a/witness_test.go b/witness_test.go index c90221f..c36febd 100644 --- a/witness_test.go +++ b/witness_test.go @@ -2,11 +2,11 @@ package witness import ( "fmt" - "regexp" "strings" "testing" "github.com/bayashi/witness/report" + tu "github.com/bayashi/witness/testutil" ) // Global variables to check result @@ -35,19 +35,16 @@ func TestNil(t *testing.T) { // Expected: // Actually got: - gotTypeRe := regexp.MustCompile(`Type:\s*\tExpect:, Got:`) - if gotTypeRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", gotTypeRe.String(), res) + if ok, msg := tu.Match(`Type:\s*\tExpect:, Got:`, res); !ok { + t.Error(msg) } - expectRe := regexp.MustCompile(`Expected:\s*\t`) - if expectRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), res) + if ok, msg := tu.Match(`Expected:\s*\t`, res); !ok { + t.Error(msg) } - gotRe := regexp.MustCompile(`Actually got:\s*\t`) - if gotRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", gotRe.String(), res) + if ok, msg := tu.Match(`Actually got:\s*\t`, res); !ok { + t.Error(msg) } } @@ -61,14 +58,12 @@ func TestError(t *testing.T) { // Type: Got:*errors.errorString // Actually got: error example 123 - gotTypeRe := regexp.MustCompile(`Type:\s*\tGot:\*errors\.errorString`) - if gotTypeRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", gotTypeRe.String(), res) + if ok, msg := tu.Match(`Type:\s*\tGot:\*errors\.errorString`, res); !ok { + t.Error(msg) } - gotRe := regexp.MustCompile(`Actually got:\s*\t\(\*errors\.errorString\)\([0-9a-fx]+\)\(error example 123\)`) - if gotRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", gotRe.String(), res) + if ok, msg := tu.Match(`Actually got:\s*\t\(\*errors\.errorString\)\([0-9a-fx]+\)\(error example 123\)`, res); !ok { + t.Error(msg) } } @@ -106,9 +101,8 @@ func TestFailGot(t *testing.T) { t.Errorf("Expected to be contained type, but not: %q", res) } - gotRe := regexp.MustCompile(fmt.Sprintf("Actually got:\\s*\\t%q", got)) - if gotRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", gotRe.String(), res) + if ok, msg := tu.Match(fmt.Sprintf("Actually got:\\s*\\t%q", got), res); !ok { + t.Error(msg) } } @@ -144,14 +138,12 @@ func TestFailGotExpect(t *testing.T) { t.Errorf("Expected to be contained types, but not: %q", res) } - gotRe := regexp.MustCompile(fmt.Sprintf("Actually got:\\s*\\t%q", got)) - if gotRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", gotRe.String(), res) + if ok, msg := tu.Match(fmt.Sprintf("Actually got:\\s*\\t%q", got), res); !ok { + t.Error(msg) } - expectRe := regexp.MustCompile(fmt.Sprintf("Expected:\\s*\\t%q", expect)) - if expectRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), res) + if ok, msg := tu.Match(fmt.Sprintf("Expected:\\s*\\t%q", expect), res); !ok { + t.Error(msg) } } @@ -181,9 +173,8 @@ func TestFailWithDiff(t *testing.T) { t.Errorf("Expected to be contained the string `Diff details:`, but not: %q", res) } - diffRe := regexp.MustCompile("\\s*a\n\\s*-d\n\\s*\\+b\n\\s*c") - if diffRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", diffRe.String(), res) + if ok, msg := tu.Match("\\s*a\n\\s*-d\n\\s*\\+b\n\\s*c", res); !ok { + t.Error(msg) } } @@ -212,14 +203,12 @@ func TestFailWithRawData(t *testing.T) { // c // --- - rawExpectRe := regexp.MustCompile("Raw Expect:\\s*\t---\n\\s*a\n\\s*d") - if rawExpectRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", rawExpectRe.String(), res) + if ok, msg := tu.Match("Raw Expect:\\s*\t---\n\\s*a\n\\s*d", res); !ok { + t.Error(msg) } - rawGotRe := regexp.MustCompile("Raw Got:\\s*\t---\n\\s*a\n\\s*b") - if rawGotRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", rawGotRe.String(), res) + if ok, msg := tu.Match("Raw Got:\\s*\t---\n\\s*a\n\\s*b", res); !ok { + t.Error(msg) } } @@ -237,8 +226,7 @@ func TestFailWithAdditionalMessage(t *testing.T) { // Actually got: "a\nb\nc" // Example Label: Some info - infoRe := regexp.MustCompile("Example Label:\\s*\tSome info\n") - if infoRe.FindStringSubmatch(res) == nil { - t.Errorf("Not matched the regexp `%s` for %q", infoRe.String(), res) + if ok, msg := tu.Match("Example Label:\\s*\tSome info\n", res); !ok { + t.Error(msg) } }