1// Copyright 2010 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package gomock
16
17import (
18	"fmt"
19	"reflect"
20	"strings"
21)
22
23// A Matcher is a representation of a class of values.
24// It is used to represent the valid or expected arguments to a mocked method.
25type Matcher interface {
26	// Matches returns whether x is a match.
27	Matches(x interface{}) bool
28
29	// String describes what the matcher matches.
30	String() string
31}
32
33// WantFormatter modifies the given Matcher's String() method to the given
34// Stringer. This allows for control on how the "Want" is formatted when
35// printing .
36func WantFormatter(s fmt.Stringer, m Matcher) Matcher {
37	type matcher interface {
38		Matches(x interface{}) bool
39	}
40
41	return struct {
42		matcher
43		fmt.Stringer
44	}{
45		matcher:  m,
46		Stringer: s,
47	}
48}
49
50// StringerFunc type is an adapter to allow the use of ordinary functions as
51// a Stringer. If f is a function with the appropriate signature,
52// StringerFunc(f) is a Stringer that calls f.
53type StringerFunc func() string
54
55// String implements fmt.Stringer.
56func (f StringerFunc) String() string {
57	return f()
58}
59
60// GotFormatter is used to better print failure messages. If a matcher
61// implements GotFormatter, it will use the result from Got when printing
62// the failure message.
63type GotFormatter interface {
64	// Got is invoked with the received value. The result is used when
65	// printing the failure message.
66	Got(got interface{}) string
67}
68
69// GotFormatterFunc type is an adapter to allow the use of ordinary
70// functions as a GotFormatter. If f is a function with the appropriate
71// signature, GotFormatterFunc(f) is a GotFormatter that calls f.
72type GotFormatterFunc func(got interface{}) string
73
74// Got implements GotFormatter.
75func (f GotFormatterFunc) Got(got interface{}) string {
76	return f(got)
77}
78
79// GotFormatterAdapter attaches a GotFormatter to a Matcher.
80func GotFormatterAdapter(s GotFormatter, m Matcher) Matcher {
81	return struct {
82		GotFormatter
83		Matcher
84	}{
85		GotFormatter: s,
86		Matcher:      m,
87	}
88}
89
90type anyMatcher struct{}
91
92func (anyMatcher) Matches(interface{}) bool {
93	return true
94}
95
96func (anyMatcher) String() string {
97	return "is anything"
98}
99
100type eqMatcher struct {
101	x interface{}
102}
103
104func (e eqMatcher) Matches(x interface{}) bool {
105	return reflect.DeepEqual(e.x, x)
106}
107
108func (e eqMatcher) String() string {
109	return fmt.Sprintf("is equal to %v", e.x)
110}
111
112type nilMatcher struct{}
113
114func (nilMatcher) Matches(x interface{}) bool {
115	if x == nil {
116		return true
117	}
118
119	v := reflect.ValueOf(x)
120	switch v.Kind() {
121	case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
122		reflect.Ptr, reflect.Slice:
123		return v.IsNil()
124	}
125
126	return false
127}
128
129func (nilMatcher) String() string {
130	return "is nil"
131}
132
133type notMatcher struct {
134	m Matcher
135}
136
137func (n notMatcher) Matches(x interface{}) bool {
138	return !n.m.Matches(x)
139}
140
141func (n notMatcher) String() string {
142	// TODO: Improve this if we add a NotString method to the Matcher interface.
143	return "not(" + n.m.String() + ")"
144}
145
146type assignableToTypeOfMatcher struct {
147	targetType reflect.Type
148}
149
150func (m assignableToTypeOfMatcher) Matches(x interface{}) bool {
151	return reflect.TypeOf(x).AssignableTo(m.targetType)
152}
153
154func (m assignableToTypeOfMatcher) String() string {
155	return "is assignable to " + m.targetType.Name()
156}
157
158type allMatcher struct {
159	matchers []Matcher
160}
161
162func (am allMatcher) Matches(x interface{}) bool {
163	for _, m := range am.matchers {
164		if !m.Matches(x) {
165			return false
166		}
167	}
168	return true
169}
170
171func (am allMatcher) String() string {
172	ss := make([]string, 0, len(am.matchers))
173	for _, matcher := range am.matchers {
174		ss = append(ss, matcher.String())
175	}
176	return strings.Join(ss, "; ")
177}
178
179type lenMatcher struct {
180	i int
181}
182
183func (m lenMatcher) Matches(x interface{}) bool {
184	v := reflect.ValueOf(x)
185	switch v.Kind() {
186	case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
187		return v.Len() == m.i
188	default:
189		return false
190	}
191}
192
193func (m lenMatcher) String() string {
194	return fmt.Sprintf("has length %d", m.i)
195}
196
197// Constructors
198
199// All returns a composite Matcher that returns true if and only all of the
200// matchers return true.
201func All(ms ...Matcher) Matcher { return allMatcher{ms} }
202
203// Any returns a matcher that always matches.
204func Any() Matcher { return anyMatcher{} }
205
206// Eq returns a matcher that matches on equality.
207//
208// Example usage:
209//   Eq(5).Matches(5) // returns true
210//   Eq(5).Matches(4) // returns false
211func Eq(x interface{}) Matcher { return eqMatcher{x} }
212
213// Len returns a matcher that matches on length. This matcher returns false if
214// is compared to a type that is not an array, chan, map, slice, or string.
215func Len(i int) Matcher {
216	return lenMatcher{i}
217}
218
219// Nil returns a matcher that matches if the received value is nil.
220//
221// Example usage:
222//   var x *bytes.Buffer
223//   Nil().Matches(x) // returns true
224//   x = &bytes.Buffer{}
225//   Nil().Matches(x) // returns false
226func Nil() Matcher { return nilMatcher{} }
227
228// Not reverses the results of its given child matcher.
229//
230// Example usage:
231//   Not(Eq(5)).Matches(4) // returns true
232//   Not(Eq(5)).Matches(5) // returns false
233func Not(x interface{}) Matcher {
234	if m, ok := x.(Matcher); ok {
235		return notMatcher{m}
236	}
237	return notMatcher{Eq(x)}
238}
239
240// AssignableToTypeOf is a Matcher that matches if the parameter to the mock
241// function is assignable to the type of the parameter to this function.
242//
243// Example usage:
244//   var s fmt.Stringer = &bytes.Buffer{}
245//   AssignableToTypeOf(s).Matches(time.Second) // returns true
246//   AssignableToTypeOf(s).Matches(99) // returns false
247//
248//   var ctx = reflect.TypeOf((*context.Context)).Elem()
249//   AssignableToTypeOf(ctx).Matches(context.Background()) // returns true
250func AssignableToTypeOf(x interface{}) Matcher {
251	if xt, ok := x.(reflect.Type); ok {
252		return assignableToTypeOfMatcher{xt}
253	}
254	return assignableToTypeOfMatcher{reflect.TypeOf(x)}
255}
256