less_or_equal.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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
  16. import (
  17. "fmt"
  18. "reflect"
  19. )
  20. // LessOrEqual returns a matcher that matches integer, floating point, or
  21. // strings values v such that v <= x. Comparison is not defined between numeric
  22. // and string types, but is defined between all integer and floating point
  23. // types.
  24. //
  25. // x must itself be an integer, floating point, or string type; otherwise,
  26. // LessOrEqual will panic.
  27. func LessOrEqual(x interface{}) Matcher {
  28. desc := fmt.Sprintf("less than or equal to %v", x)
  29. // Special case: make it clear that strings are strings.
  30. if reflect.TypeOf(x).Kind() == reflect.String {
  31. desc = fmt.Sprintf("less than or equal to \"%s\"", x)
  32. }
  33. // Put LessThan last so that its error messages will be used in the event of
  34. // failure.
  35. return transformDescription(AnyOf(Equals(x), LessThan(x)), desc)
  36. }