...
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("HavePrefixMatcher", func() {
10 When("actual is a string", func() {
11 It("should match a string prefix", func() {
12 Expect("Ab").Should(HavePrefix("A"))
13 Expect("A").ShouldNot(HavePrefix("Ab"))
14 })
15 })
16
17 When("the matcher is called with multiple arguments", func() {
18 It("should pass the string and arguments to sprintf", func() {
19 Expect("C3PO").Should(HavePrefix("C%dP", 3))
20 })
21 })
22
23 When("actual is a stringer", func() {
24 It("should call the stringer and match against the returned string", func() {
25 Expect(&myStringer{a: "Ab"}).Should(HavePrefix("A"))
26 })
27 })
28
29 When("actual is neither a string nor a stringer", func() {
30 It("should error", func() {
31 success, err := (&HavePrefixMatcher{Prefix: "2"}).Match(2)
32 Expect(success).Should(BeFalse())
33 Expect(err).Should(HaveOccurred())
34 })
35 })
36
37 It("shows failure message", func() {
38 failuresMessages := InterceptGomegaFailures(func() {
39 Expect("foo").To(HavePrefix("bar"))
40 })
41 Expect(failuresMessages[0]).To(Equal("Expected\n <string>: foo\nto have prefix\n <string>: bar"))
42 })
43
44 It("shows negated failure message", func() {
45 failuresMessages := InterceptGomegaFailures(func() {
46 Expect("foo").ToNot(HavePrefix("fo"))
47 })
48 Expect(failuresMessages[0]).To(Equal("Expected\n <string>: foo\nnot to have prefix\n <string>: fo"))
49 })
50 })
51
View as plain text