Skip to content

Commit

Permalink
Add Exactf
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Apr 29, 2021
1 parent c410005 commit 4c94f56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func Exact(expected string) *ExactMatch {
return &ExactMatch{expected: expected}
}

// Exactf matches two objects by the formatted expectation.
func Exactf(format string, args ...interface{}) *ExactMatch {
return &ExactMatch{expected: fmt.Sprintf(format, args...)}
}

// JSON matches two json objects with <ignore-diff> support.
func JSON(expected string) *JSONMatch {
return &JSONMatch{expected: expected}
Expand Down
37 changes: 37 additions & 0 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,43 @@ func TestExactMatch_Match(t *testing.T) {
}
}

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

testCases := []struct {
scenario string
format string
args []interface{}
actual string
expected bool
}{
{
scenario: "match",
format: "Bearer %s",
args: []interface{}{"token"},
actual: "Bearer token",
expected: true,
},
{
scenario: "no match",
format: "Bearer %s",
args: []interface{}{"token"},
actual: "Bearer unknown",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()

m := Exactf(tc.format, tc.args...)

assert.Equal(t, tc.expected, m.Match(tc.actual))
})
}
}

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

Expand Down

0 comments on commit 4c94f56

Please sign in to comment.