Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatible test method for different checkers #7

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading