boolean.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package httpexpect
  2. import (
  3. "errors"
  4. )
  5. // Boolean provides methods to inspect attached bool value
  6. // (Go representation of JSON boolean).
  7. type Boolean struct {
  8. noCopy noCopy
  9. chain *chain
  10. value bool
  11. }
  12. // NewBoolean returns a new Boolean instance.
  13. //
  14. // If reporter is nil, the function panics.
  15. //
  16. // Example:
  17. //
  18. // boolean := NewBoolean(t, true)
  19. // boolean.IsTrue()
  20. func NewBoolean(reporter Reporter, value bool) *Boolean {
  21. return newBoolean(newChainWithDefaults("Boolean()", reporter), value)
  22. }
  23. // NewBooleanC returns a new Boolean instance with config.
  24. //
  25. // Requirements for config are same as for WithConfig function.
  26. //
  27. // Example:
  28. //
  29. // boolean := NewBooleanC(config, true)
  30. // boolean.IsTrue()
  31. func NewBooleanC(config Config, value bool) *Boolean {
  32. return newBoolean(newChainWithConfig("Boolean()", config.withDefaults()), value)
  33. }
  34. func newBoolean(parent *chain, val bool) *Boolean {
  35. return &Boolean{chain: parent.clone(), value: val}
  36. }
  37. // Raw returns underlying value attached to Boolean.
  38. // This is the value originally passed to NewBoolean.
  39. //
  40. // Example:
  41. //
  42. // boolean := NewBoolean(t, true)
  43. // assert.Equal(t, true, boolean.Raw())
  44. func (b *Boolean) Raw() bool {
  45. return b.value
  46. }
  47. // Decode unmarshals the underlying value attached to the Boolean to a target variable.
  48. // target should be one of these:
  49. //
  50. // - pointer to an empty interface
  51. // - pointer to a boolean
  52. //
  53. // Example:
  54. //
  55. // value := NewBoolean(t, true)
  56. //
  57. // var target bool
  58. // value.Decode(&target)
  59. //
  60. // assert.Equal(t, true, target)
  61. func (b *Boolean) Decode(target interface{}) *Boolean {
  62. opChain := b.chain.enter("Decode()")
  63. defer opChain.leave()
  64. if opChain.failed() {
  65. return b
  66. }
  67. canonDecode(opChain, b.value, target)
  68. return b
  69. }
  70. // Alias is similar to Value.Alias.
  71. func (b *Boolean) Alias(name string) *Boolean {
  72. opChain := b.chain.enter("Alias(%q)", name)
  73. defer opChain.leave()
  74. b.chain.setAlias(name)
  75. return b
  76. }
  77. // Path is similar to Value.Path.
  78. func (b *Boolean) Path(path string) *Value {
  79. opChain := b.chain.enter("Path(%q)", path)
  80. defer opChain.leave()
  81. return jsonPath(opChain, b.value, path)
  82. }
  83. // Schema is similar to Value.Schema.
  84. func (b *Boolean) Schema(schema interface{}) *Boolean {
  85. opChain := b.chain.enter("Schema()")
  86. defer opChain.leave()
  87. jsonSchema(opChain, b.value, schema)
  88. return b
  89. }
  90. // IsTrue succeeds if boolean is true.
  91. //
  92. // Example:
  93. //
  94. // boolean := NewBoolean(t, true)
  95. // boolean.IsTrue()
  96. func (b *Boolean) IsTrue() *Boolean {
  97. opChain := b.chain.enter("IsTrue()")
  98. defer opChain.leave()
  99. if opChain.failed() {
  100. return b
  101. }
  102. if !(b.value == true) {
  103. opChain.fail(AssertionFailure{
  104. Type: AssertEqual,
  105. Actual: &AssertionValue{b.value},
  106. Expected: &AssertionValue{true},
  107. Errors: []error{
  108. errors.New("expected: boolean is true"),
  109. },
  110. })
  111. }
  112. return b
  113. }
  114. // IsFalse succeeds if boolean is false.
  115. //
  116. // Example:
  117. //
  118. // boolean := NewBoolean(t, false)
  119. // boolean.IsFalse()
  120. func (b *Boolean) IsFalse() *Boolean {
  121. opChain := b.chain.enter("IsFalse()")
  122. defer opChain.leave()
  123. if opChain.failed() {
  124. return b
  125. }
  126. if !(b.value == false) {
  127. opChain.fail(AssertionFailure{
  128. Type: AssertEqual,
  129. Actual: &AssertionValue{b.value},
  130. Expected: &AssertionValue{false},
  131. Errors: []error{
  132. errors.New("expected: boolean is false"),
  133. },
  134. })
  135. }
  136. return b
  137. }
  138. // Deprecated: use IsTrue instead.
  139. func (b *Boolean) True() *Boolean {
  140. return b.IsTrue()
  141. }
  142. // Deprecated: use IsFalse instead.
  143. func (b *Boolean) False() *Boolean {
  144. return b.IsFalse()
  145. }
  146. // IsEqual succeeds if boolean is equal to given value.
  147. //
  148. // Example:
  149. //
  150. // boolean := NewBoolean(t, true)
  151. // boolean.IsEqual(true)
  152. func (b *Boolean) IsEqual(value bool) *Boolean {
  153. opChain := b.chain.enter("IsEqual()")
  154. defer opChain.leave()
  155. if opChain.failed() {
  156. return b
  157. }
  158. if !(b.value == value) {
  159. opChain.fail(AssertionFailure{
  160. Type: AssertEqual,
  161. Actual: &AssertionValue{b.value},
  162. Expected: &AssertionValue{value},
  163. Errors: []error{
  164. errors.New("expected: booleans are equal"),
  165. },
  166. })
  167. }
  168. return b
  169. }
  170. // NotEqual succeeds if boolean is not equal to given value.
  171. //
  172. // Example:
  173. //
  174. // boolean := NewBoolean(t, true)
  175. // boolean.NotEqual(false)
  176. func (b *Boolean) NotEqual(value bool) *Boolean {
  177. opChain := b.chain.enter("NotEqual()")
  178. defer opChain.leave()
  179. if opChain.failed() {
  180. return b
  181. }
  182. if b.value == value {
  183. opChain.fail(AssertionFailure{
  184. Type: AssertNotEqual,
  185. Actual: &AssertionValue{b.value},
  186. Expected: &AssertionValue{value},
  187. Errors: []error{
  188. errors.New("expected: booleans are non-equal"),
  189. },
  190. })
  191. }
  192. return b
  193. }
  194. // Deprecated: use IsEqual instead.
  195. func (b *Boolean) Equal(value bool) *Boolean {
  196. return b.IsEqual(value)
  197. }
  198. // InList succeeds if boolean is equal to one of the values from given
  199. // list of booleans.
  200. //
  201. // Example:
  202. //
  203. // boolean := NewBoolean(t, true)
  204. // boolean.InList(true, false)
  205. func (b *Boolean) InList(values ...bool) *Boolean {
  206. opChain := b.chain.enter("InList()")
  207. defer opChain.leave()
  208. if opChain.failed() {
  209. return b
  210. }
  211. if len(values) == 0 {
  212. opChain.fail(AssertionFailure{
  213. Type: AssertUsage,
  214. Errors: []error{
  215. errors.New("unexpected empty list argument"),
  216. },
  217. })
  218. return b
  219. }
  220. var isListed bool
  221. for _, v := range values {
  222. if b.value == v {
  223. isListed = true
  224. break
  225. }
  226. }
  227. if !isListed {
  228. valueList := make([]interface{}, 0, len(values))
  229. for _, v := range values {
  230. valueList = append(valueList, v)
  231. }
  232. opChain.fail(AssertionFailure{
  233. Type: AssertBelongs,
  234. Actual: &AssertionValue{b.value},
  235. Expected: &AssertionValue{AssertionList(valueList)},
  236. Errors: []error{
  237. errors.New("expected: boolean is equal to one of the values"),
  238. },
  239. })
  240. }
  241. return b
  242. }
  243. // NotInList succeeds if boolean is not equal to any of the values from
  244. // given list of booleans.
  245. //
  246. // Example:
  247. //
  248. // boolean := NewBoolean(t, true)
  249. // boolean.NotInList(true, false) // failure
  250. func (b *Boolean) NotInList(values ...bool) *Boolean {
  251. opChain := b.chain.enter("NotInList()")
  252. defer opChain.leave()
  253. if opChain.failed() {
  254. return b
  255. }
  256. if len(values) == 0 {
  257. opChain.fail(AssertionFailure{
  258. Type: AssertUsage,
  259. Errors: []error{
  260. errors.New("unexpected empty list argument"),
  261. },
  262. })
  263. return b
  264. }
  265. for _, v := range values {
  266. if b.value == v {
  267. valueList := make([]interface{}, 0, len(values))
  268. for _, v := range values {
  269. valueList = append(valueList, v)
  270. }
  271. opChain.fail(AssertionFailure{
  272. Type: AssertNotBelongs,
  273. Actual: &AssertionValue{b.value},
  274. Expected: &AssertionValue{AssertionList(valueList)},
  275. Errors: []error{
  276. errors.New("expected: boolean is not equal to any of the values"),
  277. },
  278. })
  279. return b
  280. }
  281. }
  282. return b
  283. }