2016-12-16 09:13:23 -05:00
|
|
|
package requirement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-26 13:20:27 -05:00
|
|
|
"path"
|
2016-12-16 09:13:23 -05:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2016-12-26 13:20:27 -05:00
|
|
|
"strings"
|
2016-12-16 09:13:23 -05:00
|
|
|
)
|
|
|
|
|
2017-08-25 18:48:36 -04:00
|
|
|
// SkipT is the interface required to skip tests
|
|
|
|
type SkipT interface {
|
2016-12-16 09:13:23 -05:00
|
|
|
Skip(reason string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test represent a function that can be used as a requirement validation.
|
|
|
|
type Test func() bool
|
|
|
|
|
|
|
|
// Is checks if the environment satisfies the requirements
|
|
|
|
// for the test to run or skips the tests.
|
2017-08-25 18:48:36 -04:00
|
|
|
func Is(s SkipT, requirements ...Test) {
|
2016-12-16 09:13:23 -05:00
|
|
|
for _, r := range requirements {
|
|
|
|
isValid := r()
|
|
|
|
if !isValid {
|
|
|
|
requirementFunc := runtime.FuncForPC(reflect.ValueOf(r).Pointer()).Name()
|
2016-12-26 13:20:27 -05:00
|
|
|
s.Skip(fmt.Sprintf("unmatched requirement %s", extractRequirement(requirementFunc)))
|
2016-12-16 09:13:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-26 13:20:27 -05:00
|
|
|
|
|
|
|
func extractRequirement(requirementFunc string) string {
|
|
|
|
requirement := path.Base(requirementFunc)
|
|
|
|
return strings.SplitN(requirement, ".", 2)[1]
|
|
|
|
}
|