Codebase list golang-gomega / bee1065
pull oracle matcher into its own package Onsi Fakhouri 8 years ago
7 changed file(s) with 43 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
55 "reflect"
66 "time"
77
8 "github.com/onsi/gomega/internal/oraclematcher"
89 "github.com/onsi/gomega/types"
910 )
1011
8586 return assertion.actualInput, nil
8687 }
8788
88 type oracleMatcher interface {
89 MatchMayChangeInTheFuture(actual interface{}) bool
90 }
91
9289 func (assertion *AsyncAssertion) matcherMayChange(matcher types.GomegaMatcher, value interface{}) bool {
9390 if assertion.actualInputIsAFunction() {
9491 return true
9592 }
9693
97 return MatchMayChangeInTheFuture(matcher, value)
98 }
99
100 //MatchMayChangeInTheFuture is a helper to call MatchMayChangeInTheFuture on an unknown matcher.
101 //If matcher implements oracleMatcher, it will call the method. Otherwise just returns true.
102 func MatchMayChangeInTheFuture(matcher types.GomegaMatcher, value interface{}) bool {
103 oracleMatcher, ok := matcher.(oracleMatcher)
104 if !ok {
105 return true
106 }
107
108 return oracleMatcher.MatchMayChangeInTheFuture(value)
94 return oraclematcher.MatchMayChangeInTheFuture(matcher, value)
10995 }
11096
11197 func (assertion *AsyncAssertion) match(matcher types.GomegaMatcher, desiredMatch bool, optionalDescription ...interface{}) bool {
0 package oraclematcher
1
2 import "github.com/onsi/gomega/types"
3
4 /*
5 GomegaMatchers that also match the OracleMatcher interface can convey information about
6 whether or not their result will change upon future attempts.
7
8 This allows `Eventually` and `Consistently` to short circuit if success becomes impossible.
9
10 For example, a process' exit code can never change. So, gexec's Exit matcher returns `true`
11 for `MatchMayChangeInTheFuture` until the process exits, at which point it returns `false` forevermore.
12 */
13 type OracleMatcher interface {
14 MatchMayChangeInTheFuture(actual interface{}) bool
15 }
16
17 func MatchMayChangeInTheFuture(matcher types.GomegaMatcher, value interface{}) bool {
18 oracleMatcher, ok := matcher.(OracleMatcher)
19 if !ok {
20 return true
21 }
22
23 return oracleMatcher.MatchMayChangeInTheFuture(value)
24 }
11
22 import (
33 "fmt"
4
45 "github.com/onsi/gomega/format"
5 "github.com/onsi/gomega/internal/asyncassertion"
6 "github.com/onsi/gomega/internal/oraclematcher"
67 "github.com/onsi/gomega/types"
78 )
89
5051 if m.firstFailedMatcher == nil {
5152 // so all matchers succeeded.. Any one of them changing would change the result.
5253 for _, matcher := range m.Matchers {
53 if asyncassertion.MatchMayChangeInTheFuture(matcher, actual) {
54 if oraclematcher.MatchMayChangeInTheFuture(matcher, actual) {
5455 return true
5556 }
5657 }
5758 return false // none of were going to change
5859 } else {
5960 // one of the matchers failed.. it must be able to change in order to affect the result
60 return asyncassertion.MatchMayChangeInTheFuture(m.firstFailedMatcher, actual)
61 return oraclematcher.MatchMayChangeInTheFuture(m.firstFailedMatcher, actual)
6162 }
6263 }
11
22 import (
33 "testing"
4
45 . "github.com/onsi/ginkgo"
56 . "github.com/onsi/gomega"
67 )
2425
2526 func Test(t *testing.T) {
2627 RegisterFailHandler(Fail)
27 RunSpecs(t, "Gomega")
28 RunSpecs(t, "Gomega Matchers")
2829 }
00 package matchers
11
22 import (
3 "github.com/onsi/gomega/internal/asyncassertion"
3 "github.com/onsi/gomega/internal/oraclematcher"
44 "github.com/onsi/gomega/types"
55 )
66
2525 }
2626
2727 func (m *NotMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
28 return asyncassertion.MatchMayChangeInTheFuture(m.Matcher, actual) // just return m.Matcher's value
28 return oraclematcher.MatchMayChangeInTheFuture(m.Matcher, actual) // just return m.Matcher's value
2929 }
11
22 import (
33 "fmt"
4
45 "github.com/onsi/gomega/format"
5 "github.com/onsi/gomega/internal/asyncassertion"
6 "github.com/onsi/gomega/internal/oraclematcher"
67 "github.com/onsi/gomega/types"
78 )
89
5253
5354 if m.firstSuccessfulMatcher != nil {
5455 // one of the matchers succeeded.. it must be able to change in order to affect the result
55 return asyncassertion.MatchMayChangeInTheFuture(m.firstSuccessfulMatcher, actual)
56 return oraclematcher.MatchMayChangeInTheFuture(m.firstSuccessfulMatcher, actual)
5657 } else {
5758 // so all matchers failed.. Any one of them changing would change the result.
5859 for _, matcher := range m.Matchers {
59 if asyncassertion.MatchMayChangeInTheFuture(matcher, actual) {
60 if oraclematcher.MatchMayChangeInTheFuture(matcher, actual) {
6061 return true
6162 }
6263 }
11
22 import (
33 "fmt"
4 "github.com/onsi/gomega/internal/asyncassertion"
4 "reflect"
5
6 "github.com/onsi/gomega/internal/oraclematcher"
57 "github.com/onsi/gomega/types"
6 "reflect"
78 )
89
910 type WithTransformMatcher struct {
6667 // Querying the next matcher is fine if the transformer always will return the same value.
6768 // But if the transformer is non-deterministic and returns a different value each time, then there
6869 // is no point in querying the next matcher, since it can only comment on the last transformed value.
69 return asyncassertion.MatchMayChangeInTheFuture(m.Matcher, m.transformedValue)
70 return oraclematcher.MatchMayChangeInTheFuture(m.Matcher, m.transformedValue)
7071 }