boolean.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package httpexpect
  2. // Boolean provides methods to inspect attached bool value
  3. // (Go representation of JSON boolean).
  4. type Boolean struct {
  5. chain chain
  6. value bool
  7. }
  8. // NewBoolean returns a new Boolean given a reporter used to report
  9. // failures and value to be inspected.
  10. //
  11. // reporter should not be nil.
  12. //
  13. // Example:
  14. // boolean := NewBoolean(t, true)
  15. func NewBoolean(reporter Reporter, value bool) *Boolean {
  16. return &Boolean{makeChain(reporter), value}
  17. }
  18. // Raw returns underlying value attached to Boolean.
  19. // This is the value originally passed to NewBoolean.
  20. //
  21. // Example:
  22. // boolean := NewBoolean(t, true)
  23. // assert.Equal(t, true, boolean.Raw())
  24. func (b *Boolean) Raw() bool {
  25. return b.value
  26. }
  27. // Path is similar to Value.Path.
  28. func (b *Boolean) Path(path string) *Value {
  29. return getPath(&b.chain, b.value, path)
  30. }
  31. // Schema is similar to Value.Schema.
  32. func (b *Boolean) Schema(schema interface{}) *Boolean {
  33. checkSchema(&b.chain, b.value, schema)
  34. return b
  35. }
  36. // Equal succeeds if boolean is equal to given value.
  37. //
  38. // Example:
  39. // boolean := NewBoolean(t, true)
  40. // boolean.Equal(true)
  41. func (b *Boolean) Equal(value bool) *Boolean {
  42. if !(b.value == value) {
  43. b.chain.fail("expected boolean == %v, but got %v", value, b.value)
  44. }
  45. return b
  46. }
  47. // NotEqual succeeds if boolean is not equal to given value.
  48. //
  49. // Example:
  50. // boolean := NewBoolean(t, true)
  51. // boolean.NotEqual(false)
  52. func (b *Boolean) NotEqual(value bool) *Boolean {
  53. if !(b.value != value) {
  54. b.chain.fail("expected boolean != %v, but got %v", value, b.value)
  55. }
  56. return b
  57. }
  58. // True succeeds if boolean is true.
  59. //
  60. // Example:
  61. // boolean := NewBoolean(t, true)
  62. // boolean.True()
  63. func (b *Boolean) True() *Boolean {
  64. return b.Equal(true)
  65. }
  66. // False succeeds if boolean is false.
  67. //
  68. // Example:
  69. // boolean := NewBoolean(t, false)
  70. // boolean.False()
  71. func (b *Boolean) False() *Boolean {
  72. return b.Equal(false)
  73. }