Skip to content

Commit

Permalink
Add compatible test method for different checkers (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: plar <[email protected]>
  • Loading branch information
plar authored Mar 19, 2024
1 parent 9dae17c commit 35a80bf
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions test/arg_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ type ArgumentTest struct {
ExpectedErrMsg string
}

// CheckCommentString implements check.CommentInterface
func (t *ArgumentTest) CheckCommentString() string {
return t.Name
// comment provides a test name for the test during assertions and checks.
// This method is compatible with different checkers.
func (t *ArgumentTest) comment() any {
return check.Commentf("%s", t.Name)
}

// setDefaultExpectedLog sets the default value for ExpectedLog based on ExpectedCLI.
Expand All @@ -63,34 +64,34 @@ func (t *ArgumentTest) setDefaultExpectedLog() {

// assertNoError makes sure there is no error.
func (t *ArgumentTest) assertNoError(c *check.C, err error) {
c.Assert(err, check.IsNil)
c.Assert(err, check.IsNil, t.comment())
}

// assertError checks the error against ExpectedErr.
func (t *ArgumentTest) assertError(c *check.C, err error) {
actualErr := errors.Cause(err)
c.Assert(actualErr, check.Equals, t.ExpectedErr)
c.Assert(actualErr, check.Equals, t.ExpectedErr, t.comment())
}

// assertErrorMsg checks the error message against ExpectedErrMsg.
func (t *ArgumentTest) assertErrorMsg(c *check.C, err error) {
if t.ExpectedErrMsg != "" {
c.Assert(err.Error(), check.Equals, t.ExpectedErrMsg)
c.Assert(err.Error(), check.Equals, t.ExpectedErrMsg, t.comment())
}
}

// assertCLI asserts the builder's CLI output against ExpectedCLI.
func (t *ArgumentTest) assertCLI(c *check.C, b *safecli.Builder) {
if t.ExpectedCLI != nil {
c.Check(b.Build(), check.DeepEquals, t.ExpectedCLI)
c.Check(b.Build(), check.DeepEquals, t.ExpectedCLI, t.comment())
}
}

// assertLog asserts the builder's log output against ExpectedLog.
func (t *ArgumentTest) assertLog(c *check.C, b *safecli.Builder) {
if t.ExpectedCLI != nil {
t.setDefaultExpectedLog()
c.Check(b.String(), check.Equals, t.ExpectedLog)
c.Check(b.String(), check.Equals, t.ExpectedLog, t.comment())
}
}

Expand All @@ -99,7 +100,6 @@ func (t *ArgumentTest) Test(c *check.C, cmdName string) {
if t.Name == "" {
c.Fatal("Name is required")
}
c.Log(t.Name)
cmd, err := command.New(cmdName, t.Argument)
if t.ExpectedErr == nil {
t.assertNoError(c, err)
Expand Down

0 comments on commit 35a80bf

Please sign in to comment.