// Copyright 2010 Google Inc.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.package gomockimport ()// A Matcher is a representation of a class of values.// It is used to represent the valid or expected arguments to a mocked method.typeMatcherinterface {// Matches returns whether x is a match.Matches(x interface{}) bool// String describes what the matcher matches.String() string}// WantFormatter modifies the given Matcher's String() method to the given// Stringer. This allows for control on how the "Want" is formatted when// printing .func ( fmt.Stringer, Matcher) Matcher {typeinterface { ( interface{}) bool }returnstruct {fmt.Stringer }{ : , : , }}// StringerFunc type is an adapter to allow the use of ordinary functions as// a Stringer. If f is a function with the appropriate signature,// StringerFunc(f) is a Stringer that calls f.typeStringerFuncfunc() string// String implements fmt.Stringer.func ( StringerFunc) () string {return ()}// GotFormatter is used to better print failure messages. If a matcher// implements GotFormatter, it will use the result from Got when printing// the failure message.typeGotFormatterinterface {// Got is invoked with the received value. The result is used when // printing the failure message.Got(got interface{}) string}// GotFormatterFunc type is an adapter to allow the use of ordinary// functions as a GotFormatter. If f is a function with the appropriate// signature, GotFormatterFunc(f) is a GotFormatter that calls f.typeGotFormatterFuncfunc(got interface{}) string// Got implements GotFormatter.func ( GotFormatterFunc) ( interface{}) string {return ()}// GotFormatterAdapter attaches a GotFormatter to a Matcher.func ( GotFormatter, Matcher) Matcher {returnstruct {GotFormatterMatcher }{ : , : , }}typeanyMatcherstruct{}func (anyMatcher) (interface{}) bool {returntrue}func (anyMatcher) () string {return"is anything"}typeeqMatcherstruct {xinterface{}}func ( eqMatcher) ( interface{}) bool {// In case, some value is nilif .x == nil || == nil {returnreflect.DeepEqual(.x, ) }// Check if types assignable and convert them to common type := reflect.ValueOf(.x) := reflect.ValueOf()if .Type().AssignableTo(.Type()) { := .Convert(.Type())returnreflect.DeepEqual(.Interface(), .Interface()) }returnfalse}func ( eqMatcher) () string {returnfmt.Sprintf("is equal to %v (%T)", .x, .x)}typenilMatcherstruct{}func (nilMatcher) ( interface{}) bool {if == nil {returntrue } := reflect.ValueOf()switch .Kind() {casereflect.Chan, reflect.Func, reflect.Interface, reflect.Map,reflect.Ptr, reflect.Slice:return .IsNil() }returnfalse}func (nilMatcher) () string {return"is nil"}typenotMatcherstruct {mMatcher}func ( notMatcher) ( interface{}) bool {return !.m.Matches()}func ( notMatcher) () string {return"not(" + .m.String() + ")"}typeassignableToTypeOfMatcherstruct {targetTypereflect.Type}func ( assignableToTypeOfMatcher) ( interface{}) bool {returnreflect.TypeOf().AssignableTo(.targetType)}func ( assignableToTypeOfMatcher) () string {return"is assignable to " + .targetType.Name()}typeallMatcherstruct {matchers []Matcher}func ( allMatcher) ( interface{}) bool {for , := range .matchers {if !.Matches() {returnfalse } }returntrue}func ( allMatcher) () string { := make([]string, 0, len(.matchers))for , := range .matchers { = append(, .String()) }returnstrings.Join(, "; ")}typelenMatcherstruct {iint}func ( lenMatcher) ( interface{}) bool { := reflect.ValueOf()switch .Kind() {casereflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:return .Len() == .idefault:returnfalse }}func ( lenMatcher) () string {returnfmt.Sprintf("has length %d", .i)}typeinAnyOrderMatcherstruct {xinterface{}}func ( inAnyOrderMatcher) ( interface{}) bool { , := .prepareValue()if ! {returnfalse } , := .prepareValue(.x)if ! {returnfalse }if .Len() != .Len() {returnfalse } := make([]bool, .Len()) := make([]bool, .Len())for := 0; < .Len(); ++ { := Eq(.Index().Interface())for := 0; < .Len(); ++ {if [] {continue }if .Matches(.Index().Interface()) { [] = true [] = truebreak } } } := 0for , := range {if ! { ++ } } := 0for , := range {if ! { ++ } }return == 0 && == 0}func ( inAnyOrderMatcher) ( interface{}) (reflect.Value, bool) { := reflect.ValueOf()switch .Kind() {casereflect.Slice, reflect.Array:return , truedefault:returnreflect.Value{}, false }}func ( inAnyOrderMatcher) () string {returnfmt.Sprintf("has the same elements as %v", .x)}// Constructors// All returns a composite Matcher that returns true if and only all of the// matchers return true.func ( ...Matcher) Matcher { returnallMatcher{} }// Any returns a matcher that always matches.func () Matcher { returnanyMatcher{} }// Eq returns a matcher that matches on equality.//// Example usage:// Eq(5).Matches(5) // returns true// Eq(5).Matches(4) // returns falsefunc ( interface{}) Matcher { returneqMatcher{} }// Len returns a matcher that matches on length. This matcher returns false if// is compared to a type that is not an array, chan, map, slice, or string.func ( int) Matcher {returnlenMatcher{}}// Nil returns a matcher that matches if the received value is nil.//// Example usage:// var x *bytes.Buffer// Nil().Matches(x) // returns true// x = &bytes.Buffer{}// Nil().Matches(x) // returns falsefunc () Matcher { returnnilMatcher{} }// Not reverses the results of its given child matcher.//// Example usage:// Not(Eq(5)).Matches(4) // returns true// Not(Eq(5)).Matches(5) // returns falsefunc ( interface{}) Matcher {if , := .(Matcher); {returnnotMatcher{} }returnnotMatcher{Eq()}}// AssignableToTypeOf is a Matcher that matches if the parameter to the mock// function is assignable to the type of the parameter to this function.//// Example usage:// var s fmt.Stringer = &bytes.Buffer{}// AssignableToTypeOf(s).Matches(time.Second) // returns true// AssignableToTypeOf(s).Matches(99) // returns false//// var ctx = reflect.TypeOf((*context.Context)(nil)).Elem()// AssignableToTypeOf(ctx).Matches(context.Background()) // returns truefunc ( interface{}) Matcher {if , := .(reflect.Type); {returnassignableToTypeOfMatcher{} }returnassignableToTypeOfMatcher{reflect.TypeOf()}}// InAnyOrder is a Matcher that returns true for collections of the same elements ignoring the order.//// Example usage:// InAnyOrder([]int{1, 2, 3}).Matches([]int{1, 3, 2}) // returns true// InAnyOrder([]int{1, 2, 3}).Matches([]int{1, 2}) // returns falsefunc ( interface{}) Matcher {returninAnyOrderMatcher{}}
The pages are generated with Goldsv0.4.9. (GOOS=linux GOARCH=amd64)