initial commit
[govpp.git] / vendor / github.com / onsi / gomega / internal / assertion / assertion.go
1 package assertion
2
3 import (
4         "fmt"
5         "reflect"
6
7         "github.com/onsi/gomega/types"
8 )
9
10 type Assertion struct {
11         actualInput interface{}
12         fail        types.GomegaFailHandler
13         offset      int
14         extra       []interface{}
15 }
16
17 func New(actualInput interface{}, fail types.GomegaFailHandler, offset int, extra ...interface{}) *Assertion {
18         return &Assertion{
19                 actualInput: actualInput,
20                 fail:        fail,
21                 offset:      offset,
22                 extra:       extra,
23         }
24 }
25
26 func (assertion *Assertion) Should(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
27         return assertion.vetExtras(optionalDescription...) && assertion.match(matcher, true, optionalDescription...)
28 }
29
30 func (assertion *Assertion) ShouldNot(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
31         return assertion.vetExtras(optionalDescription...) && assertion.match(matcher, false, optionalDescription...)
32 }
33
34 func (assertion *Assertion) To(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
35         return assertion.vetExtras(optionalDescription...) && assertion.match(matcher, true, optionalDescription...)
36 }
37
38 func (assertion *Assertion) ToNot(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
39         return assertion.vetExtras(optionalDescription...) && assertion.match(matcher, false, optionalDescription...)
40 }
41
42 func (assertion *Assertion) NotTo(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
43         return assertion.vetExtras(optionalDescription...) && assertion.match(matcher, false, optionalDescription...)
44 }
45
46 func (assertion *Assertion) buildDescription(optionalDescription ...interface{}) string {
47         switch len(optionalDescription) {
48         case 0:
49                 return ""
50         default:
51                 return fmt.Sprintf(optionalDescription[0].(string), optionalDescription[1:]...) + "\n"
52         }
53 }
54
55 func (assertion *Assertion) match(matcher types.GomegaMatcher, desiredMatch bool, optionalDescription ...interface{}) bool {
56         matches, err := matcher.Match(assertion.actualInput)
57         description := assertion.buildDescription(optionalDescription...)
58         if err != nil {
59                 assertion.fail(description+err.Error(), 2+assertion.offset)
60                 return false
61         }
62         if matches != desiredMatch {
63                 var message string
64                 if desiredMatch {
65                         message = matcher.FailureMessage(assertion.actualInput)
66                 } else {
67                         message = matcher.NegatedFailureMessage(assertion.actualInput)
68                 }
69                 assertion.fail(description+message, 2+assertion.offset)
70                 return false
71         }
72
73         return true
74 }
75
76 func (assertion *Assertion) vetExtras(optionalDescription ...interface{}) bool {
77         success, message := vetExtras(assertion.extra)
78         if success {
79                 return true
80         }
81
82         description := assertion.buildDescription(optionalDescription...)
83         assertion.fail(description+message, 2+assertion.offset)
84         return false
85 }
86
87 func vetExtras(extras []interface{}) (bool, string) {
88         for i, extra := range extras {
89                 if extra != nil {
90                         zeroValue := reflect.Zero(reflect.TypeOf(extra)).Interface()
91                         if !reflect.DeepEqual(zeroValue, extra) {
92                                 message := fmt.Sprintf("Unexpected non-nil/non-zero extra argument at index %d:\n\t<%T>: %#v", i+1, extra, extra)
93                                 return false, message
94                         }
95                 }
96         }
97         return true, ""
98 }