...
1 package matchers_test
2
3 import (
4 "reflect"
5
6 . "github.com/onsi/ginkgo/v2"
7 . "github.com/onsi/gomega"
8 )
9
10 type I interface {
11 M()
12 }
13
14 type S struct {
15 V int
16 }
17
18 func (s S) M() {}
19
20 var _ = Describe("HaveValue", func() {
21
22 It("should fail when passed nil", func() {
23 var p *struct{}
24 m := HaveValue(BeNil())
25 Expect(m.Match(p)).Error().To(MatchError(MatchRegexp("not to be <nil>$")))
26 })
27
28 It("should fail when passed nil indirectly", func() {
29 var p *struct{}
30 m := HaveValue(BeNil())
31 Expect(m.Match(&p)).Error().To(MatchError(MatchRegexp("not to be <nil>$")))
32 })
33
34 It("should use the matcher's failure message", func() {
35 m := HaveValue(Equal(42))
36 Expect(m.Match(666)).To(BeFalse())
37 Expect(m.FailureMessage(nil)).To(Equal("Expected\n <int>: 666\nto equal\n <int>: 42"))
38 Expect(m.NegatedFailureMessage(nil)).To(Equal("Expected\n <int>: 666\nnot to equal\n <int>: 42"))
39 })
40
41 It("should unwrap the value pointed to, even repeatedly", func() {
42 i := 1
43 Expect(&i).To(HaveValue(Equal(1)))
44 Expect(&i).NotTo(HaveValue(Equal(2)))
45
46 pi := &i
47 Expect(pi).To(HaveValue(Equal(1)))
48 Expect(pi).NotTo(HaveValue(Equal(2)))
49
50 Expect(&pi).To(HaveValue(Equal(1)))
51 Expect(&pi).NotTo(HaveValue(Equal(2)))
52 })
53
54 It("shouldn't endlessly star-gaze", func() {
55 dave := "It's full of stars!"
56 stargazer := reflect.ValueOf(dave)
57 for stars := 1; stars <= 31; stars++ {
58 p := reflect.New(stargazer.Type())
59 p.Elem().Set(stargazer)
60 stargazer = p
61 }
62 m := HaveValue(Equal(dave))
63 Expect(m.Match(stargazer.Interface())).Error().To(
64 MatchError(MatchRegexp(`too many indirections`)))
65 Expect(m.Match(stargazer.Elem().Interface())).To(BeTrue())
66 })
67
68 It("should unwrap the value of an interface", func() {
69 var i I = &S{V: 42}
70 Expect(i).To(HaveValue(Equal(S{V: 42})))
71 Expect(i).NotTo(HaveValue(Equal(S{})))
72 })
73
74 })
75
View as plain text