initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / with_transform_test.go
1 package matchers_test
2
3 import (
4         "errors"
5
6         . "github.com/onsi/ginkgo"
7         . "github.com/onsi/gomega"
8         . "github.com/onsi/gomega/matchers"
9 )
10
11 var _ = Describe("WithTransformMatcher", func() {
12
13         var plus1 = func(i int) int { return i + 1 }
14
15         Context("Panic if transform function invalid", func() {
16                 panicsWithTransformer := func(transform interface{}) {
17                         ExpectWithOffset(1, func() { WithTransform(transform, nil) }).To(Panic())
18                 }
19                 It("nil", func() {
20                         panicsWithTransformer(nil)
21                 })
22                 Context("Invalid number of args, but correct return value count", func() {
23                         It("zero", func() {
24                                 panicsWithTransformer(func() int { return 5 })
25                         })
26                         It("two", func() {
27                                 panicsWithTransformer(func(i, j int) int { return 5 })
28                         })
29                 })
30                 Context("Invalid number of return values, but correct number of arguments", func() {
31                         It("zero", func() {
32                                 panicsWithTransformer(func(i int) {})
33                         })
34                         It("two", func() {
35                                 panicsWithTransformer(func(i int) (int, int) { return 5, 6 })
36                         })
37                 })
38         })
39
40         It("works with positive cases", func() {
41                 Expect(1).To(WithTransform(plus1, Equal(2)))
42                 Expect(1).To(WithTransform(plus1, WithTransform(plus1, Equal(3))))
43                 Expect(1).To(WithTransform(plus1, And(Equal(2), BeNumerically(">", 1))))
44
45                 // transform expects custom type
46                 type S struct {
47                         A int
48                         B string
49                 }
50                 transformer := func(s S) string { return s.B }
51                 Expect(S{1, "hi"}).To(WithTransform(transformer, Equal("hi")))
52
53                 // transform expects interface
54                 errString := func(e error) string { return e.Error() }
55                 Expect(errors.New("abc")).To(WithTransform(errString, Equal("abc")))
56         })
57
58         It("works with negative cases", func() {
59                 Expect(1).ToNot(WithTransform(plus1, Equal(3)))
60                 Expect(1).ToNot(WithTransform(plus1, WithTransform(plus1, Equal(2))))
61         })
62
63         Context("failure messages", func() {
64                 Context("when match fails", func() {
65                         It("gives a descriptive message", func() {
66                                 m := WithTransform(plus1, Equal(3))
67                                 Expect(m.Match(1)).To(BeFalse())
68                                 Expect(m.FailureMessage(1)).To(Equal("Expected\n    <int>: 2\nto equal\n    <int>: 3"))
69                         })
70                 })
71
72                 Context("when match succeeds, but expected it to fail", func() {
73                         It("gives a descriptive message", func() {
74                                 m := Not(WithTransform(plus1, Equal(3)))
75                                 Expect(m.Match(2)).To(BeFalse())
76                                 Expect(m.FailureMessage(2)).To(Equal("Expected\n    <int>: 3\nnot to equal\n    <int>: 3"))
77                         })
78                 })
79
80                 Context("actual value is incompatible with transform function's argument type", func() {
81                         It("gracefully fails if transform cannot be performed", func() {
82                                 m := WithTransform(plus1, Equal(3))
83                                 result, err := m.Match("hi") // give it a string but transform expects int; doesn't panic
84                                 Expect(result).To(BeFalse())
85                                 Expect(err).To(MatchError("Transform function expects 'int' but we have 'string'"))
86                         })
87                 })
88         })
89
90         Context("MatchMayChangeInTheFuture()", func() {
91                 It("Propagates value from wrapped matcher on the transformed value", func() {
92                         m := WithTransform(plus1, Or()) // empty Or() always returns false, and indicates it cannot change
93                         Expect(m.Match(1)).To(BeFalse())
94                         Expect(m.(*WithTransformMatcher).MatchMayChangeInTheFuture(1)).To(BeFalse()) // empty Or() indicates cannot change
95                 })
96                 It("Defaults to true", func() {
97                         m := WithTransform(plus1, Equal(2)) // Equal does not have this method
98                         Expect(m.Match(1)).To(BeTrue())
99                         Expect(m.(*WithTransformMatcher).MatchMayChangeInTheFuture(1)).To(BeTrue()) // defaults to true
100                 })
101         })
102 })