Skip to content

Commit

Permalink
Refine custom matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Apr 29, 2021
1 parent db61cf8 commit c410005
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
11 changes: 10 additions & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func formatValueInline(v interface{}) string {
string:
return formatValue(v)

case *CallbackMatch:
return formatValue(m.matcher())

case Matcher:
return fmt.Sprintf("%T(%q)", v, m.Expected())

Expand All @@ -232,12 +235,15 @@ func formatType(v interface{}) string {
return ""
}

switch v.(type) {
switch m := v.(type) {
case *ExactMatch,
[]byte,
string:
return ""

case *CallbackMatch:
return formatType(m.matcher())

default:
return fmt.Sprintf(" using %T", v)
}
Expand All @@ -249,6 +255,9 @@ func formatValue(v interface{}) string {
}

switch m := v.(type) {
case *CallbackMatch:
return formatValue(m.matcher())

case Matcher:
return m.Expected()

Expand Down
21 changes: 21 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ func TestFormatValueInline(t *testing.T) {
value: "expected",
expected: "expected",
},
{
scenario: "Callback",
value: Match(func() Matcher {
return Exact("expected")
}),
expected: "expected",
},
{
scenario: "Matcher",
value: JSON("{}"),
Expand Down Expand Up @@ -158,6 +165,13 @@ func TestFormatType(t *testing.T) {
value: "expected",
expected: "",
},
{
scenario: "Callback",
value: Match(func() Matcher {
return Exact("expected")
}),
expected: "",
},
{
scenario: "Matcher",
value: JSON("{}"),
Expand Down Expand Up @@ -197,6 +211,13 @@ func TestFormatValue(t *testing.T) {
value: "expected",
expected: "expected",
},
{
scenario: "Callback",
value: Match(func() Matcher {
return Exact("expected")
}),
expected: "expected",
},
{
scenario: "ExactMatch",
value: Exact("expected"),
Expand Down
24 changes: 17 additions & 7 deletions matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,32 @@ func (m *RegexMatch) Match(actual string) bool {

// CallbackMatch matches by calling a function.
type CallbackMatch struct {
expect func() string
match func(actual string) bool
callback func() Matcher
upstream Matcher
}

func (m *CallbackMatch) matcher() Matcher {
if m.upstream == nil {
m.upstream = m.callback()
}

return m.upstream
}

// Expected returns the expectation.
func (m *CallbackMatch) Expected() string {
return m.expect()
return m.matcher().Expected()
}

// Match determines if the actual is expected.
func (m *CallbackMatch) Match(actual string) bool {
return m.match(actual)
return m.matcher().Match(actual)
}

// Match creates a callback matcher.
func Match(expect func() string, match func(actual string) bool) Matcher {
func Match(callback func() Matcher) Matcher {
return &CallbackMatch{
expect: expect,
match: match,
callback: callback,
}
}

Expand Down Expand Up @@ -110,6 +117,9 @@ func ValueMatcher(v interface{}) Matcher {
case Matcher:
return val

case func() Matcher:
return Match(val)

case []byte:
return Exact(string(val))

Expand Down
28 changes: 12 additions & 16 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,6 @@ func TestRegexMatch_Match(t *testing.T) {
}
}

func TestCallbackMatch(t *testing.T) {
t.Parallel()

m := Match(
func() string {
return "expected"
},
func(string) bool {
return false
},
)

assert.Equal(t, "expected", m.Expected())
assert.False(t, m.Match("actual"))
}

func TestValueMatcher(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -231,6 +215,18 @@ func TestValueMatcher(t *testing.T) {
}
}

func TestValueMatcher_Match(t *testing.T) {
t.Parallel()

m := ValueMatcher(func() Matcher {
return Exact("expected")
})

assert.Equal(t, "expected", m.Expected())
assert.True(t, m.Match("expected"))
assert.False(t, m.Match("Mismatch"))
}

func TestValueMatcher_Panic(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit c410005

Please sign in to comment.