Skip to content

Commit

Permalink
refactor regexp test
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Jan 8, 2024
1 parent f372c0f commit 8685f37
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 60 deletions.
33 changes: 14 additions & 19 deletions obj/obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package obj

import (
"fmt"
"regexp"
"testing"

tu "github.com/bayashi/witness/testutil"
)

type Example struct {
Expand Down Expand Up @@ -69,59 +70,53 @@ 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)
}
}

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)
}
}

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)
}
}

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)
}
}

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)
}
}

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)
}
}

Expand Down
15 changes: 15 additions & 0 deletions testutil/regexp.go
Original file line number Diff line number Diff line change
@@ -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, ""
}
9 changes: 5 additions & 4 deletions trace/trace_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package trace

import (
"regexp"
"testing"

tu "github.com/bayashi/witness/testutil"
)

func TestInfo(t *testing.T) {
trace := Info()
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)
}
}

Expand Down
62 changes: 25 additions & 37 deletions witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,19 +35,16 @@ func TestNil(t *testing.T) {
// Expected: <nil>
// Actually got: <nil>

gotTypeRe := regexp.MustCompile(`Type:\s*\tExpect:<nil>, Got:<nil>`)
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:<nil>, Got:<nil>`, res); !ok {
t.Error(msg)
}

expectRe := regexp.MustCompile(`Expected:\s*\t<nil>`)
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<nil>`, res); !ok {
t.Error(msg)
}

gotRe := regexp.MustCompile(`Actually got:\s*\t<nil>`)
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<nil>`, res); !ok {
t.Error(msg)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

0 comments on commit 8685f37

Please sign in to comment.