checker.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Package checker provides helpers for gotest.tools/assert.
  2. // Please remove this package whenever possible.
  3. package checker // import "github.com/docker/docker/integration-cli/checker"
  4. import (
  5. "fmt"
  6. "gotest.tools/v3/assert"
  7. "gotest.tools/v3/assert/cmp"
  8. )
  9. // Compare defines the interface to compare values
  10. type Compare func(x interface{}) assert.BoolOrComparison
  11. // False checks if the value is false
  12. func False() Compare {
  13. return func(x interface{}) assert.BoolOrComparison {
  14. return !x.(bool)
  15. }
  16. }
  17. // True checks if the value is true
  18. func True() Compare {
  19. return func(x interface{}) assert.BoolOrComparison {
  20. return x
  21. }
  22. }
  23. // Equals checks if the value is equal to the given value
  24. func Equals(y interface{}) Compare {
  25. return func(x interface{}) assert.BoolOrComparison {
  26. return cmp.Equal(x, y)
  27. }
  28. }
  29. // Contains checks if the value contains the given value
  30. func Contains(y interface{}) Compare {
  31. return func(x interface{}) assert.BoolOrComparison {
  32. return cmp.Contains(x, y)
  33. }
  34. }
  35. // Not checks if two values are not
  36. func Not(c Compare) Compare {
  37. return func(x interface{}) assert.BoolOrComparison {
  38. r := c(x)
  39. switch r := r.(type) {
  40. case bool:
  41. return !r
  42. case cmp.Comparison:
  43. return !r().Success()
  44. default:
  45. panic(fmt.Sprintf("unexpected type %T", r))
  46. }
  47. }
  48. }
  49. // DeepEquals checks if two values are equal
  50. func DeepEquals(y interface{}) Compare {
  51. return func(x interface{}) assert.BoolOrComparison {
  52. return cmp.DeepEqual(x, y)
  53. }
  54. }
  55. // HasLen checks if the value has the expected number of elements
  56. func HasLen(y int) Compare {
  57. return func(x interface{}) assert.BoolOrComparison {
  58. return cmp.Len(x, y)
  59. }
  60. }
  61. // IsNil checks if the value is nil
  62. func IsNil() Compare {
  63. return func(x interface{}) assert.BoolOrComparison {
  64. return cmp.Nil(x)
  65. }
  66. }
  67. // GreaterThan checks if the value is greater than the given value
  68. func GreaterThan(y int) Compare {
  69. return func(x interface{}) assert.BoolOrComparison {
  70. return x.(int) > y
  71. }
  72. }