github.com/onsi/gomega@v1.32.0/matchers/match_regexp_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 . "github.com/onsi/ginkgo/v2" 5 . "github.com/onsi/gomega" 6 . "github.com/onsi/gomega/matchers" 7 ) 8 9 var _ = Describe("MatchRegexp", func() { 10 When("actual is a string", func() { 11 It("should match against the string", func() { 12 Expect(" a2!bla").Should(MatchRegexp(`\d!`)) 13 Expect(" a2!bla").ShouldNot(MatchRegexp(`[A-Z]`)) 14 }) 15 }) 16 17 When("actual is a stringer", func() { 18 It("should call the stringer and match agains the returned string", func() { 19 Expect(&myStringer{a: "Abc3"}).Should(MatchRegexp(`[A-Z][a-z]+\d`)) 20 }) 21 }) 22 23 When("the matcher is called with multiple arguments", func() { 24 It("should pass the string and arguments to sprintf", func() { 25 Expect(" a23!bla").Should(MatchRegexp(`\d%d!`, 3)) 26 }) 27 }) 28 29 When("actual is neither a string nor a stringer", func() { 30 It("should error", func() { 31 success, err := (&MatchRegexpMatcher{Regexp: `\d`}).Match(2) 32 Expect(success).Should(BeFalse()) 33 Expect(err).Should(HaveOccurred()) 34 }) 35 }) 36 37 When("the passed in regexp fails to compile", func() { 38 It("should error", func() { 39 success, err := (&MatchRegexpMatcher{Regexp: "("}).Match("Foo") 40 Expect(success).Should(BeFalse()) 41 Expect(err).Should(HaveOccurred()) 42 }) 43 }) 44 45 It("shows failure message", func() { 46 failuresMessages := InterceptGomegaFailures(func() { 47 Expect("foo").To(MatchRegexp("bar")) 48 }) 49 Expect(failuresMessages).To(Equal([]string{"Expected\n <string>: foo\nto match regular expression\n <string>: bar"})) 50 }) 51 52 It("shows negated failure message", func() { 53 failuresMessages := InterceptGomegaFailures(func() { 54 Expect("foo").ToNot(MatchRegexp("foo")) 55 }) 56 Expect(failuresMessages).To(Equal([]string{"Expected\n <string>: foo\nnot to match regular expression\n <string>: foo"})) 57 }) 58 })