matcher.go 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2011 Aaron Jacobs. All Rights Reserved.
  2. // Author: aaronjjacobs@gmail.com (Aaron Jacobs)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // Package oglematchers provides a set of matchers useful in a testing or
  16. // mocking framework. These matchers are inspired by and mostly compatible with
  17. // Google Test for C++ and Google JS Test.
  18. //
  19. // This package is used by github.com/smartystreets/assertions/internal/ogletest and
  20. // github.com/smartystreets/assertions/internal/oglemock, which may be more directly useful if you're not
  21. // writing your own testing package or defining your own matchers.
  22. package oglematchers
  23. // A Matcher is some predicate implicitly defining a set of values that it
  24. // matches. For example, GreaterThan(17) matches all numeric values greater
  25. // than 17, and HasSubstr("taco") matches all strings with the substring
  26. // "taco".
  27. //
  28. // Matchers are typically exposed to tests via constructor functions like
  29. // HasSubstr. In order to implement such a function you can either define your
  30. // own matcher type or use NewMatcher.
  31. type Matcher interface {
  32. // Check whether the supplied value belongs to the the set defined by the
  33. // matcher. Return a non-nil error if and only if it does not.
  34. //
  35. // The error describes why the value doesn't match. The error text is a
  36. // relative clause that is suitable for being placed after the value. For
  37. // example, a predicate that matches strings with a particular substring may,
  38. // when presented with a numerical value, return the following error text:
  39. //
  40. // "which is not a string"
  41. //
  42. // Then the failure message may look like:
  43. //
  44. // Expected: has substring "taco"
  45. // Actual: 17, which is not a string
  46. //
  47. // If the error is self-apparent based on the description of the matcher, the
  48. // error text may be empty (but the error still non-nil). For example:
  49. //
  50. // Expected: 17
  51. // Actual: 19
  52. //
  53. // If you are implementing a new matcher, see also the documentation on
  54. // FatalError.
  55. Matches(candidate interface{}) error
  56. // Description returns a string describing the property that values matching
  57. // this matcher have, as a verb phrase where the subject is the value. For
  58. // example, "is greather than 17" or "has substring "taco"".
  59. Description() string
  60. }
  61. // FatalError is an implementation of the error interface that may be returned
  62. // from matchers, indicating the error should be propagated. Returning a
  63. // *FatalError indicates that the matcher doesn't process values of the
  64. // supplied type, or otherwise doesn't know how to handle the value.
  65. //
  66. // For example, if GreaterThan(17) returned false for the value "taco" without
  67. // a fatal error, then Not(GreaterThan(17)) would return true. This is
  68. // technically correct, but is surprising and may mask failures where the wrong
  69. // sort of matcher is accidentally used. Instead, GreaterThan(17) can return a
  70. // fatal error, which will be propagated by Not().
  71. type FatalError struct {
  72. errorText string
  73. }
  74. // NewFatalError creates a FatalError struct with the supplied error text.
  75. func NewFatalError(s string) *FatalError {
  76. return &FatalError{s}
  77. }
  78. func (e *FatalError) Error() string {
  79. return e.errorText
  80. }