Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

62 lignes
1.8 KiB

  1. // Copyright 2017 Google LLC
  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 testutil
  15. import (
  16. "math"
  17. "math/big"
  18. "github.com/golang/protobuf/proto"
  19. "github.com/google/go-cmp/cmp"
  20. )
  21. var (
  22. alwaysEqual = cmp.Comparer(func(_, _ interface{}) bool { return true })
  23. defaultCmpOptions = []cmp.Option{
  24. // Use proto.Equal for protobufs
  25. cmp.Comparer(proto.Equal),
  26. // Use big.Rat.Cmp for big.Rats
  27. cmp.Comparer(func(x, y *big.Rat) bool {
  28. if x == nil || y == nil {
  29. return x == y
  30. }
  31. return x.Cmp(y) == 0
  32. }),
  33. // NaNs compare equal
  34. cmp.FilterValues(func(x, y float64) bool {
  35. return math.IsNaN(x) && math.IsNaN(y)
  36. }, alwaysEqual),
  37. cmp.FilterValues(func(x, y float32) bool {
  38. return math.IsNaN(float64(x)) && math.IsNaN(float64(y))
  39. }, alwaysEqual),
  40. }
  41. )
  42. // Equal tests two values for equality.
  43. func Equal(x, y interface{}, opts ...cmp.Option) bool {
  44. // Put default options at the end. Order doesn't matter.
  45. opts = append(opts[:len(opts):len(opts)], defaultCmpOptions...)
  46. return cmp.Equal(x, y, opts...)
  47. }
  48. // Diff reports the differences between two values.
  49. // Diff(x, y) == "" iff Equal(x, y).
  50. func Diff(x, y interface{}, opts ...cmp.Option) string {
  51. // Put default options at the end. Order doesn't matter.
  52. opts = append(opts[:len(opts):len(opts)], defaultCmpOptions...)
  53. return cmp.Diff(x, y, opts...)
  54. }