github.com/onsi/gomega@v1.32.0/internal/internal_suite_test.go (about) 1 package internal_test 2 3 import ( 4 "errors" 5 "fmt" 6 "runtime" 7 "strings" 8 "testing" 9 10 . "github.com/onsi/ginkgo/v2" 11 . "github.com/onsi/gomega" 12 "github.com/onsi/gomega/internal" 13 ) 14 15 func TestInternal(t *testing.T) { 16 RegisterFailHandler(Fail) 17 RunSpecs(t, "Internal Suite") 18 } 19 20 // InstrumentedGomega 21 type InstrumentedGomega struct { 22 G *internal.Gomega 23 FailureMessage string 24 FailureSkip []int 25 RegisteredHelpers []string 26 } 27 28 func NewInstrumentedGomega() *InstrumentedGomega { 29 out := &InstrumentedGomega{} 30 31 out.G = internal.NewGomega(internal.FetchDefaultDurationBundle()) 32 out.G.Fail = func(message string, skip ...int) { 33 out.FailureMessage = message 34 out.FailureSkip = skip 35 } 36 out.G.THelper = func() { 37 pc, _, _, _ := runtime.Caller(1) 38 f := runtime.FuncForPC(pc) 39 funcName := strings.TrimPrefix(f.Name(), "github.com/onsi/gomega/internal.") 40 out.RegisteredHelpers = append(out.RegisteredHelpers, funcName) 41 } 42 43 return out 44 } 45 46 // TestMatcher 47 var MATCH = "match" 48 var NO_MATCH = "no match" 49 var ERR_MATCH = "err match" 50 var TEST_MATCHER_ERR = errors.New("spec matcher error") 51 52 type SpecMatcher struct{} 53 54 func (matcher SpecMatcher) Match(actual interface{}) (bool, error) { 55 switch actual { 56 case MATCH: 57 return true, nil 58 case NO_MATCH: 59 return false, nil 60 case ERR_MATCH: 61 return false, TEST_MATCHER_ERR 62 } 63 return false, fmt.Errorf("unkown actual %v", actual) 64 } 65 66 func (matcher SpecMatcher) FailureMessage(actual interface{}) string { 67 return fmt.Sprintf("positive: %s", actual) 68 } 69 70 func (matcher SpecMatcher) NegatedFailureMessage(actual interface{}) string { 71 return fmt.Sprintf("negative: %s", actual) 72 } 73 74 func SpecMatch() SpecMatcher { 75 return SpecMatcher{} 76 } 77 78 //FakeGomegaTestingT 79 type FakeGomegaTestingT struct { 80 CalledHelper bool 81 CalledFatalf string 82 } 83 84 func (f *FakeGomegaTestingT) Helper() { 85 f.CalledHelper = true 86 } 87 88 func (f *FakeGomegaTestingT) Fatalf(s string, args ...interface{}) { 89 f.CalledFatalf = fmt.Sprintf(s, args...) 90 }