Skip to content

Commit

Permalink
Merge pull request #137 from glefloch/skip-message
Browse files Browse the repository at this point in the history
Skip message
  • Loading branch information
dnephin authored Dec 23, 2018
2 parents b2aa35c + 8f2da87 commit d9e91eb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
28 changes: 23 additions & 5 deletions skip/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,29 @@ type skipT interface {
Log(args ...interface{})
}

// Result of skip function
type Result interface {
Skip() bool
Message() string
}

type helperT interface {
Helper()
}

// BoolOrCheckFunc can be a bool or func() bool, other types will panic
// BoolOrCheckFunc can be a bool, func() bool, or func() Result. Other types will panic
type BoolOrCheckFunc interface{}

// If the condition expression evaluates to true, or the condition function returns
// true, skip the test.
// If the condition expression evaluates to true, skip the test.
//
// The condition argument may be one of three types: bool, func() bool, or
// func() SkipResult.
// When called with a bool, the test will be skip if the condition evaluates to true.
// When called with a func() bool, the test will be skip if the function returns true.
// When called with a func() Result, the test will be skip if the Skip method
// of the result returns true.
// The skip message will contain the source code of the expression.
// Extra message text can be passed as a format string with args
// Extra message text can be passed as a format string with args.
func If(t skipT, condition BoolOrCheckFunc, msgAndArgs ...interface{}) {
if ht, ok := t.(helperT); ok {
ht.Helper()
Expand All @@ -41,12 +53,18 @@ func If(t skipT, condition BoolOrCheckFunc, msgAndArgs ...interface{}) {
if check() {
t.Skip(format.WithCustomMessage(getFunctionName(check), msgAndArgs...))
}
case func() Result:
result := check()
if result.Skip() {
msg := getFunctionName(check) + ": " + result.Message()
t.Skip(format.WithCustomMessage(msg, msgAndArgs...))
}
default:
panic(fmt.Sprintf("invalid type for condition arg: %T", check))
}
}

func getFunctionName(function func() bool) string {
func getFunctionName(function interface{}) string {
funcPath := runtime.FuncForPC(reflect.ValueOf(function).Pointer()).Name()
return strings.SplitN(path.Base(funcPath), ".", 2)[1]
}
Expand Down
29 changes: 29 additions & 0 deletions skip/skip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,32 @@ func TestIf_InvalidCondition(t *testing.T) {
If(skipT, "just a string")
}))
}

func TestIfWithSkipResultFunc(t *testing.T) {
t.Run("no extra message", func(t *testing.T) {
skipT := &fakeSkipT{}
If(skipT, alwaysSkipWithMessage)

assert.Equal(t, "alwaysSkipWithMessage: skip because I said so!", skipT.reason)
})
t.Run("with extra message", func(t *testing.T) {
skipT := &fakeSkipT{}
If(skipT, alwaysSkipWithMessage, "also %v", 4)

assert.Equal(t, "alwaysSkipWithMessage: skip because I said so!: also 4", skipT.reason)
})
}

func alwaysSkipWithMessage() Result {
return skipResult{}
}

type skipResult struct{}

func (s skipResult) Skip() bool {
return true
}

func (s skipResult) Message() string {
return "skip because I said so!"
}

0 comments on commit d9e91eb

Please sign in to comment.