You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

142 lines
3.4 KiB

  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. package gomock
  15. import (
  16. "fmt"
  17. "reflect"
  18. )
  19. // A Matcher is a representation of a class of values.
  20. // It is used to represent the valid or expected arguments to a mocked method.
  21. type Matcher interface {
  22. // Matches returns whether x is a match.
  23. Matches(x interface{}) bool
  24. // String describes what the matcher matches.
  25. String() string
  26. }
  27. type anyMatcher struct{}
  28. func (anyMatcher) Matches(x interface{}) bool {
  29. return true
  30. }
  31. func (anyMatcher) String() string {
  32. return "is anything"
  33. }
  34. type eqMatcher struct {
  35. x interface{}
  36. }
  37. func (e eqMatcher) Matches(x interface{}) bool {
  38. return reflect.DeepEqual(e.x, x)
  39. }
  40. func (e eqMatcher) String() string {
  41. return fmt.Sprintf("is equal to %v", e.x)
  42. }
  43. type nilMatcher struct{}
  44. func (nilMatcher) Matches(x interface{}) bool {
  45. if x == nil {
  46. return true
  47. }
  48. v := reflect.ValueOf(x)
  49. switch v.Kind() {
  50. case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
  51. reflect.Ptr, reflect.Slice:
  52. return v.IsNil()
  53. }
  54. return false
  55. }
  56. func (nilMatcher) String() string {
  57. return "is nil"
  58. }
  59. type notMatcher struct {
  60. m Matcher
  61. }
  62. func (n notMatcher) Matches(x interface{}) bool {
  63. return !n.m.Matches(x)
  64. }
  65. func (n notMatcher) String() string {
  66. // TODO: Improve this if we add a NotString method to the Matcher interface.
  67. return "not(" + n.m.String() + ")"
  68. }
  69. type assignableToTypeOfMatcher struct {
  70. targetType reflect.Type
  71. }
  72. func (m assignableToTypeOfMatcher) Matches(x interface{}) bool {
  73. return reflect.TypeOf(x).AssignableTo(m.targetType)
  74. }
  75. func (m assignableToTypeOfMatcher) String() string {
  76. return "is assignable to " + m.targetType.Name()
  77. }
  78. // Constructors
  79. // Any returns a matcher that always matches.
  80. func Any() Matcher { return anyMatcher{} }
  81. // Eq returns a matcher that matches on equality.
  82. //
  83. // Example usage:
  84. // Eq(5).Matches(5) // returns true
  85. // Eq(5).Matches(4) // returns false
  86. func Eq(x interface{}) Matcher { return eqMatcher{x} }
  87. // Nil returns a matcher that matches if the received value is nil.
  88. //
  89. // Example usage:
  90. // var x *bytes.Buffer
  91. // Nil().Matches(x) // returns true
  92. // x = &bytes.Buffer{}
  93. // Nil().Matches(x) // returns false
  94. func Nil() Matcher { return nilMatcher{} }
  95. // Not reverses the results of its given child matcher.
  96. //
  97. // Example usage:
  98. // Not(Eq(5)).Matches(4) // returns true
  99. // Not(Eq(5)).Matches(5) // returns false
  100. func Not(x interface{}) Matcher {
  101. if m, ok := x.(Matcher); ok {
  102. return notMatcher{m}
  103. }
  104. return notMatcher{Eq(x)}
  105. }
  106. // AssignableToTypeOf is a Matcher that matches if the parameter to the mock
  107. // function is assignable to the type of the parameter to this function.
  108. //
  109. // Example usage:
  110. // var s fmt.Stringer = &bytes.Buffer{}
  111. // AssignableToTypeOf(s).Matches(time.Second) // returns true
  112. // AssignableToTypeOf(s).Matches(99) // returns false
  113. func AssignableToTypeOf(x interface{}) Matcher {
  114. return assignableToTypeOfMatcher{reflect.TypeOf(x)}
  115. }