requirement.go 809 B

12345678910111213141516171819202122232425262728293031323334
  1. package requirement
  2. import (
  3. "fmt"
  4. "path"
  5. "reflect"
  6. "runtime"
  7. "strings"
  8. )
  9. // SkipT is the interface required to skip tests
  10. type SkipT interface {
  11. Skip(reason string)
  12. }
  13. // Test represent a function that can be used as a requirement validation.
  14. type Test func() bool
  15. // Is checks if the environment satisfies the requirements
  16. // for the test to run or skips the tests.
  17. func Is(s SkipT, requirements ...Test) {
  18. for _, r := range requirements {
  19. isValid := r()
  20. if !isValid {
  21. requirementFunc := runtime.FuncForPC(reflect.ValueOf(r).Pointer()).Name()
  22. s.Skip(fmt.Sprintf("unmatched requirement %s", extractRequirement(requirementFunc)))
  23. }
  24. }
  25. }
  26. func extractRequirement(requirementFunc string) string {
  27. requirement := path.Base(requirementFunc)
  28. return strings.SplitN(requirement, ".", 2)[1]
  29. }