value.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. package httpexpect
  2. import (
  3. "errors"
  4. "reflect"
  5. )
  6. // Value provides methods to inspect attached interface{} object
  7. // (Go representation of arbitrary JSON value) and cast it to
  8. // concrete type.
  9. type Value struct {
  10. chain *chain
  11. value interface{}
  12. }
  13. // NewValue returns a new Value instance.
  14. //
  15. // If reporter is nil, the function panics.
  16. // Value may be nil.
  17. //
  18. // Example:
  19. //
  20. // value := NewValue(t, map[string]interface{}{"foo": 123})
  21. // value.IsObject()
  22. //
  23. // value := NewValue(t, []interface{}{"foo", 123})
  24. // value.IsArray()
  25. //
  26. // value := NewValue(t, "foo")
  27. // value.IsString()
  28. //
  29. // value := NewValue(t, 123)
  30. // value.IsNumber()
  31. //
  32. // value := NewValue(t, true)
  33. // value.IsBoolean()
  34. //
  35. // value := NewValue(t, nil)
  36. // value.IsNull()
  37. func NewValue(reporter Reporter, value interface{}) *Value {
  38. return newValue(newChainWithDefaults("Value()", reporter), value)
  39. }
  40. // NewValueC returns a new Value instance with config.
  41. //
  42. // Requirements for config are same as for WithConfig function.
  43. // Value may be nil.
  44. //
  45. // See NewValue for usage example.
  46. func NewValueC(config Config, value interface{}) *Value {
  47. return newValue(newChainWithConfig("Value()", config.withDefaults()), value)
  48. }
  49. func newValue(parent *chain, val interface{}) *Value {
  50. v := &Value{parent.clone(), nil}
  51. opChain := v.chain.enter("")
  52. defer opChain.leave()
  53. if val != nil {
  54. v.value, _ = canonValue(opChain, val)
  55. }
  56. return v
  57. }
  58. // Raw returns underlying value attached to Value.
  59. // This is the value originally passed to NewValue, converted to canonical form.
  60. //
  61. // Example:
  62. //
  63. // value := NewValue(t, "foo")
  64. // assert.Equal(t, "foo", number.Raw().(string))
  65. func (v *Value) Raw() interface{} {
  66. return v.value
  67. }
  68. // Decode unmarshals the underlying value attached to the Object to a target variable
  69. // target should be pointer to any type.
  70. //
  71. // Example:
  72. //
  73. // type S struct {
  74. // Foo int `json:"foo"`
  75. // Bar []interface{} `json:"bar"`
  76. // Baz struct{ A int } `json:"baz"`
  77. // }
  78. //
  79. // m := map[string]interface{}{
  80. // "foo": 123,
  81. // "bar": []interface{}{"123", 456.0},
  82. // "baz": struct{ A int }{123},
  83. // }
  84. //
  85. // value = NewValue(reporter,m)
  86. //
  87. // var target S
  88. // value.Decode(&target)
  89. //
  90. // assert.Equal(t, S{123, []interface{}{"123", 456.0}, struct{ A int }{123}, target})
  91. func (v *Value) Decode(target interface{}) *Value {
  92. opChain := v.chain.enter("Decode()")
  93. defer opChain.leave()
  94. if opChain.failed() {
  95. return v
  96. }
  97. canonDecode(opChain, v.value, target)
  98. return v
  99. }
  100. // Alias returns a new Value object with alias.
  101. // When a test of Value object with alias is failed,
  102. // an assertion is displayed as a chain starting from the alias.
  103. //
  104. // Example:
  105. //
  106. // // In this example, GET /example responds "foo"
  107. // foo := e.GET("/example").Expect().Status(http.StatusOK).JSON().Object()
  108. //
  109. // // When a test is failed, an assertion without alias is
  110. // // Request("GET").Expect().JSON().Object().IsEqual()
  111. // foo.IsEqual("bar")
  112. //
  113. // // Set Alias
  114. // fooWithAlias := e.GET("/example").
  115. // Expect().
  116. // Status(http.StatusOK).JSON().Object().Alias("foo")
  117. //
  118. // // When a test is failed, an assertion with alias is
  119. // // foo.IsEqual()
  120. // fooWithAlias.IsEqual("bar")
  121. func (v *Value) Alias(name string) *Value {
  122. opChain := v.chain.enter("Alias(%q)", name)
  123. defer opChain.leave()
  124. v.chain.setAlias(name)
  125. return v
  126. }
  127. // Path returns a new Value object for child object(s) matching given
  128. // JSONPath expression.
  129. //
  130. // JSONPath is a simple XPath-like query language.
  131. // See http://goessner.net/articles/JsonPath/.
  132. //
  133. // We currently use https://github.com/yalp/jsonpath, which implements
  134. // only a subset of JSONPath, yet useful for simple queries. It doesn't
  135. // support filters and requires double quotes for strings.
  136. //
  137. // Example 1:
  138. //
  139. // json := `{"users": [{"name": "john"}, {"name": "bob"}]}`
  140. // value := NewValue(t, json)
  141. //
  142. // value.Path("$.users[0].name").String().IsEqual("john")
  143. // value.Path("$.users[1].name").String().IsEqual("bob")
  144. //
  145. // Example 2:
  146. //
  147. // json := `{"yfGH2a": {"user": "john"}, "f7GsDd": {"user": "john"}}`
  148. // value := NewValue(t, json)
  149. //
  150. // for _, user := range value.Path("$..user").Array().Iter() {
  151. // user.String().IsEqual("john")
  152. // }
  153. func (v *Value) Path(path string) *Value {
  154. opChain := v.chain.enter("Path(%q)", path)
  155. defer opChain.leave()
  156. return jsonPath(opChain, v.value, path)
  157. }
  158. // Schema succeeds if value matches given JSON Schema.
  159. //
  160. // JSON Schema specifies a JSON-based format to define the structure of
  161. // JSON data. See http://json-schema.org/.
  162. // We use https://github.com/xeipuuv/gojsonschema implementation.
  163. //
  164. // schema should be one of the following:
  165. // - go value that can be json.Marshal-ed to a valid schema
  166. // - type convertible to string containing valid schema
  167. // - type convertible to string containing valid http:// or file:// URI,
  168. // pointing to reachable and valid schema
  169. //
  170. // Example 1:
  171. //
  172. // schema := `{
  173. // "type": "object",
  174. // "properties": {
  175. // "foo": {
  176. // "type": "string"
  177. // },
  178. // "bar": {
  179. // "type": "integer"
  180. // }
  181. // },
  182. // "require": ["foo", "bar"]
  183. // }`
  184. //
  185. // value := NewValue(t, map[string]interface{}{
  186. // "foo": "a",
  187. // "bar": 1,
  188. // })
  189. //
  190. // value.Schema(schema)
  191. //
  192. // Example 2:
  193. //
  194. // value := NewValue(t, data)
  195. // value.Schema("http://example.com/schema.json")
  196. func (v *Value) Schema(schema interface{}) *Value {
  197. opChain := v.chain.enter("Schema()")
  198. defer opChain.leave()
  199. jsonSchema(opChain, v.value, schema)
  200. return v
  201. }
  202. // Object returns a new Object attached to underlying value.
  203. //
  204. // If underlying value is not an object (map[string]interface{}), failure is reported
  205. // and empty (but non-nil) value is returned.
  206. //
  207. // Example:
  208. //
  209. // value := NewValue(t, map[string]interface{}{"foo": 123})
  210. // value.Object().ContainsKey("foo")
  211. func (v *Value) Object() *Object {
  212. opChain := v.chain.enter("Object()")
  213. defer opChain.leave()
  214. if opChain.failed() {
  215. return newObject(opChain, nil)
  216. }
  217. data, ok := v.value.(map[string]interface{})
  218. if !ok {
  219. opChain.fail(AssertionFailure{
  220. Type: AssertValid,
  221. Actual: &AssertionValue{v.value},
  222. Errors: []error{
  223. errors.New("expected: value is object"),
  224. },
  225. })
  226. return newObject(opChain, nil)
  227. }
  228. return newObject(opChain, data)
  229. }
  230. // Array returns a new Array attached to underlying value.
  231. //
  232. // If underlying value is not an array ([]interface{}), failure is reported and empty
  233. // (but non-nil) value is returned.
  234. //
  235. // Example:
  236. //
  237. // value := NewValue(t, []interface{}{"foo", 123})
  238. // value.Array().ConsistsOf("foo", 123)
  239. func (v *Value) Array() *Array {
  240. opChain := v.chain.enter("Array()")
  241. defer opChain.leave()
  242. if opChain.failed() {
  243. return newArray(opChain, nil)
  244. }
  245. data, ok := v.value.([]interface{})
  246. if !ok {
  247. opChain.fail(AssertionFailure{
  248. Type: AssertValid,
  249. Actual: &AssertionValue{v.value},
  250. Errors: []error{
  251. errors.New("expected: value is array"),
  252. },
  253. })
  254. return newArray(opChain, nil)
  255. }
  256. return newArray(opChain, data)
  257. }
  258. // String returns a new String attached to underlying value.
  259. //
  260. // If underlying value is not a string, failure is reported and empty (but non-nil)
  261. // value is returned.
  262. //
  263. // Example:
  264. //
  265. // value := NewValue(t, "foo")
  266. // value.String().IsEqualFold("FOO")
  267. func (v *Value) String() *String {
  268. opChain := v.chain.enter("String()")
  269. defer opChain.leave()
  270. if opChain.failed() {
  271. return newString(opChain, "")
  272. }
  273. data, ok := v.value.(string)
  274. if !ok {
  275. opChain.fail(AssertionFailure{
  276. Type: AssertValid,
  277. Actual: &AssertionValue{v.value},
  278. Errors: []error{
  279. errors.New("expected: value is string"),
  280. },
  281. })
  282. return newString(opChain, "")
  283. }
  284. return newString(opChain, data)
  285. }
  286. // Number returns a new Number attached to underlying value.
  287. //
  288. // If underlying value is not a number (numeric type convertible to float64), failure
  289. // is reported and empty (but non-nil) value is returned.
  290. //
  291. // Example:
  292. //
  293. // value := NewValue(t, 123)
  294. // value.Number().InRange(100, 200)
  295. func (v *Value) Number() *Number {
  296. opChain := v.chain.enter("Number()")
  297. defer opChain.leave()
  298. if opChain.failed() {
  299. return newNumber(opChain, 0)
  300. }
  301. data, ok := v.value.(float64)
  302. if !ok {
  303. opChain.fail(AssertionFailure{
  304. Type: AssertValid,
  305. Actual: &AssertionValue{v.value},
  306. Errors: []error{
  307. errors.New("expected: value is number"),
  308. },
  309. })
  310. return newNumber(opChain, 0)
  311. }
  312. return newNumber(opChain, data)
  313. }
  314. // Boolean returns a new Boolean attached to underlying value.
  315. //
  316. // If underlying value is not a bool, failure is reported and empty (but non-nil)
  317. // value is returned.
  318. //
  319. // Example:
  320. //
  321. // value := NewValue(t, true)
  322. // value.Boolean().IsTrue()
  323. func (v *Value) Boolean() *Boolean {
  324. opChain := v.chain.enter("Boolean()")
  325. defer opChain.leave()
  326. if opChain.failed() {
  327. return newBoolean(opChain, false)
  328. }
  329. data, ok := v.value.(bool)
  330. if !ok {
  331. opChain.fail(AssertionFailure{
  332. Type: AssertValid,
  333. Actual: &AssertionValue{v.value},
  334. Errors: []error{
  335. errors.New("expected: value is boolean"),
  336. },
  337. })
  338. return newBoolean(opChain, false)
  339. }
  340. return newBoolean(opChain, data)
  341. }
  342. // IsNull succeeds if value is nil.
  343. //
  344. // Note that non-nil interface{} that points to nil value (e.g. nil slice or map)
  345. // is also treated as null value. Empty (non-nil) slice or map, empty string, and
  346. // zero number are not treated as null value.
  347. //
  348. // Example:
  349. //
  350. // value := NewValue(t, nil)
  351. // value.IsNull()
  352. //
  353. // value := NewValue(t, []interface{}(nil))
  354. // value.IsNull()
  355. func (v *Value) IsNull() *Value {
  356. opChain := v.chain.enter("IsNull()")
  357. defer opChain.leave()
  358. if opChain.failed() {
  359. return v
  360. }
  361. if !(v.value == nil) {
  362. opChain.fail(AssertionFailure{
  363. Type: AssertNil,
  364. Actual: &AssertionValue{v.value},
  365. Errors: []error{
  366. errors.New("expected: value is null"),
  367. },
  368. })
  369. }
  370. return v
  371. }
  372. // NotNull succeeds if value is not nil.
  373. //
  374. // Note that non-nil interface{} that points to nil value (e.g. nil slice or map)
  375. // is also treated as null value. Empty (non-nil) slice or map, empty string, and
  376. // zero number are not treated as null value.
  377. //
  378. // Example:
  379. //
  380. // value := NewValue(t, "")
  381. // value.NotNull()
  382. //
  383. // value := NewValue(t, make([]interface{}, 0))
  384. // value.NotNull()
  385. func (v *Value) NotNull() *Value {
  386. opChain := v.chain.enter("NotNull()")
  387. defer opChain.leave()
  388. if opChain.failed() {
  389. return v
  390. }
  391. if v.value == nil {
  392. opChain.fail(AssertionFailure{
  393. Type: AssertNotNil,
  394. Actual: &AssertionValue{v.value},
  395. Errors: []error{
  396. errors.New("expected: value is non-null"),
  397. },
  398. })
  399. }
  400. return v
  401. }
  402. // Deprecated: use IsNull instead.
  403. func (v *Value) Null() *Value {
  404. return v.IsNull()
  405. }
  406. // IsObject succeeds if the underlying value is an object.
  407. //
  408. // If underlying value is not an object (map[string]interface{}), failure is reported.
  409. //
  410. // Example:
  411. //
  412. // value := NewValue(t, map[string]interface{}{"foo": 123})
  413. // value.IsObject()
  414. func (v *Value) IsObject() *Value {
  415. opChain := v.chain.enter("IsObject()")
  416. defer opChain.leave()
  417. if opChain.failed() {
  418. return v
  419. }
  420. if _, ok := v.value.(map[string]interface{}); !ok {
  421. opChain.fail(AssertionFailure{
  422. Type: AssertValid,
  423. Actual: &AssertionValue{v.value},
  424. Errors: []error{
  425. errors.New("expected: value is object"),
  426. },
  427. })
  428. }
  429. return v
  430. }
  431. // NotObject succeeds if the underlying value is not an object.
  432. //
  433. // If underlying value is an object (map[string]interface{}), failure is reported.
  434. //
  435. // Example:
  436. //
  437. // value := NewValue(t, nil)
  438. // value.NotObject()
  439. func (v *Value) NotObject() *Value {
  440. opChain := v.chain.enter("NotObject()")
  441. defer opChain.leave()
  442. if opChain.failed() {
  443. return v
  444. }
  445. if _, ok := v.value.(map[string]interface{}); ok {
  446. opChain.fail(AssertionFailure{
  447. Type: AssertValid,
  448. Actual: &AssertionValue{v.value},
  449. Errors: []error{
  450. errors.New("expected: value is not object"),
  451. },
  452. })
  453. }
  454. return v
  455. }
  456. // IsArray succeeds if the underlying value is an array.
  457. //
  458. // If underlying value is not an array ([]interface{}), failure is reported.
  459. //
  460. // Example:
  461. //
  462. // value := NewValue(t, []interface{}{"foo", "123"})
  463. // value.IsArray()
  464. func (v *Value) IsArray() *Value {
  465. opChain := v.chain.enter("IsArray()")
  466. defer opChain.leave()
  467. if opChain.failed() {
  468. return v
  469. }
  470. if _, ok := v.value.([]interface{}); !ok {
  471. opChain.fail(AssertionFailure{
  472. Type: AssertValid,
  473. Actual: &AssertionValue{v.value},
  474. Errors: []error{
  475. errors.New("expected: value is array"),
  476. },
  477. })
  478. }
  479. return v
  480. }
  481. // NotArray succeeds if the underlying value is not an array.
  482. //
  483. // If underlying value is an array ([]interface{}), failure is reported.
  484. //
  485. // Example:
  486. //
  487. // value := NewValue(t, nil)
  488. // value.NotArray()
  489. func (v *Value) NotArray() *Value {
  490. opChain := v.chain.enter("NotArray()")
  491. defer opChain.leave()
  492. if opChain.failed() {
  493. return v
  494. }
  495. if _, ok := v.value.([]interface{}); ok {
  496. opChain.fail(AssertionFailure{
  497. Type: AssertValid,
  498. Actual: &AssertionValue{v.value},
  499. Errors: []error{
  500. errors.New("expected: value is not array"),
  501. },
  502. })
  503. }
  504. return v
  505. }
  506. // IsString succeeds if the underlying value is a string.
  507. //
  508. // If underlying value is not a string, failure is reported.
  509. //
  510. // Example:
  511. //
  512. // value := NewValue(t, "foo")
  513. // value.IsString()
  514. func (v *Value) IsString() *Value {
  515. opChain := v.chain.enter("IsString()")
  516. defer opChain.leave()
  517. if opChain.failed() {
  518. return v
  519. }
  520. if _, ok := v.value.(string); !ok {
  521. opChain.fail(AssertionFailure{
  522. Type: AssertValid,
  523. Actual: &AssertionValue{v.value},
  524. Errors: []error{
  525. errors.New("expected: value is string"),
  526. },
  527. })
  528. }
  529. return v
  530. }
  531. // NotString succeeds if the underlying value is not a string.
  532. //
  533. // If underlying value is a string, failure is reported.
  534. //
  535. // Example:
  536. //
  537. // value := NewValue(t, nil)
  538. // value.NotString()
  539. func (v *Value) NotString() *Value {
  540. opChain := v.chain.enter("NotString()")
  541. defer opChain.leave()
  542. if opChain.failed() {
  543. return v
  544. }
  545. if _, ok := v.value.(string); ok {
  546. opChain.fail(AssertionFailure{
  547. Type: AssertValid,
  548. Actual: &AssertionValue{v.value},
  549. Errors: []error{
  550. errors.New("expected: value is not string"),
  551. },
  552. })
  553. }
  554. return v
  555. }
  556. // IsNumber succeeds if the underlying value is a number.
  557. //
  558. // If underlying value is not a number (numeric type convertible to float64),
  559. // failure is reported
  560. //
  561. // Example:
  562. //
  563. // value := NewValue(t, 123)
  564. // value.IsNumber()
  565. func (v *Value) IsNumber() *Value {
  566. opChain := v.chain.enter("IsNumber()")
  567. defer opChain.leave()
  568. if opChain.failed() {
  569. return v
  570. }
  571. if _, ok := v.value.(float64); !ok {
  572. opChain.fail(AssertionFailure{
  573. Type: AssertValid,
  574. Actual: &AssertionValue{v.value},
  575. Errors: []error{
  576. errors.New("expected: value is number"),
  577. },
  578. })
  579. }
  580. return v
  581. }
  582. // NotNumber succeeds if the underlying value is a not a number.
  583. //
  584. // If underlying value is a number (numeric type convertible to float64),
  585. // failure is reported
  586. //
  587. // Example:
  588. //
  589. // value := NewValue(t, nil)
  590. // value.NotNumber()
  591. func (v *Value) NotNumber() *Value {
  592. opChain := v.chain.enter("NotNumber()")
  593. defer opChain.leave()
  594. if opChain.failed() {
  595. return v
  596. }
  597. if _, ok := v.value.(float64); ok {
  598. opChain.fail(AssertionFailure{
  599. Type: AssertValid,
  600. Actual: &AssertionValue{v.value},
  601. Errors: []error{
  602. errors.New("expected: value is not number"),
  603. },
  604. })
  605. }
  606. return v
  607. }
  608. // IsBoolean succeeds if the underlying value is a boolean.
  609. //
  610. // If underlying value is not a boolean, failure is reported.
  611. //
  612. // Example:
  613. //
  614. // value := NewValue(t, true)
  615. // value.IsBoolean()
  616. func (v *Value) IsBoolean() *Value {
  617. opChain := v.chain.enter("IsBoolean()")
  618. defer opChain.leave()
  619. if opChain.failed() {
  620. return v
  621. }
  622. if _, ok := v.value.(bool); !ok {
  623. opChain.fail(AssertionFailure{
  624. Type: AssertValid,
  625. Actual: &AssertionValue{v.value},
  626. Errors: []error{
  627. errors.New("expected: value is boolean"),
  628. },
  629. })
  630. }
  631. return v
  632. }
  633. // NotBoolean succeeds if the underlying value is not a boolean.
  634. //
  635. // If underlying value is a boolean, failure is reported.
  636. //
  637. // Example:
  638. //
  639. // value := NewValue(t, nil)
  640. // value.NotBoolean()
  641. func (v *Value) NotBoolean() *Value {
  642. opChain := v.chain.enter("NotBoolean()")
  643. defer opChain.leave()
  644. if opChain.failed() {
  645. return v
  646. }
  647. if _, ok := v.value.(bool); ok {
  648. opChain.fail(AssertionFailure{
  649. Type: AssertValid,
  650. Actual: &AssertionValue{v.value},
  651. Errors: []error{
  652. errors.New("expected: value is not boolean"),
  653. },
  654. })
  655. }
  656. return v
  657. }
  658. // IsEqual succeeds if value is equal to another value (e.g. map, slice, string, etc).
  659. // Before comparison, both values are converted to canonical form.
  660. //
  661. // Example:
  662. //
  663. // value := NewValue(t, "foo")
  664. // value.IsEqual("foo")
  665. func (v *Value) IsEqual(value interface{}) *Value {
  666. opChain := v.chain.enter("IsEqual()")
  667. defer opChain.leave()
  668. if opChain.failed() {
  669. return v
  670. }
  671. expected, ok := canonValue(opChain, value)
  672. if !ok {
  673. return v
  674. }
  675. if !reflect.DeepEqual(expected, v.value) {
  676. opChain.fail(AssertionFailure{
  677. Type: AssertEqual,
  678. Actual: &AssertionValue{v.value},
  679. Expected: &AssertionValue{expected},
  680. Errors: []error{
  681. errors.New("expected: values are equal"),
  682. },
  683. })
  684. }
  685. return v
  686. }
  687. // NotEqual succeeds if value is not equal to another value (e.g. map, slice,
  688. // string, etc). Before comparison, both values are converted to canonical form.
  689. //
  690. // Example:
  691. //
  692. // value := NewValue(t, "foo")
  693. // value.NorEqual("bar")
  694. func (v *Value) NotEqual(value interface{}) *Value {
  695. opChain := v.chain.enter("NotEqual()")
  696. defer opChain.leave()
  697. if opChain.failed() {
  698. return v
  699. }
  700. expected, ok := canonValue(opChain, value)
  701. if !ok {
  702. return v
  703. }
  704. if reflect.DeepEqual(expected, v.value) {
  705. opChain.fail(AssertionFailure{
  706. Type: AssertNotEqual,
  707. Actual: &AssertionValue{v.value},
  708. Expected: &AssertionValue{expected},
  709. Errors: []error{
  710. errors.New("expected: values are non-equal"),
  711. },
  712. })
  713. }
  714. return v
  715. }
  716. // Deprecated: use IsEqual instead.
  717. func (v *Value) Equal(value interface{}) *Value {
  718. return v.IsEqual(value)
  719. }
  720. // InList succeeds if whole value is equal to one of the values from given
  721. // list of values (e.g. map, slice, string, etc). Before comparison, all
  722. // values are converted to canonical form.
  723. //
  724. // If at least one value has wrong type, failure is reported.
  725. //
  726. // Example:
  727. //
  728. // value := NewValue(t, "foo")
  729. // value.InList("foo", 123)
  730. func (v *Value) InList(values ...interface{}) *Value {
  731. opChain := v.chain.enter("InList()")
  732. defer opChain.leave()
  733. if opChain.failed() {
  734. return v
  735. }
  736. if len(values) == 0 {
  737. opChain.fail(AssertionFailure{
  738. Type: AssertUsage,
  739. Errors: []error{
  740. errors.New("unexpected empty list argument"),
  741. },
  742. })
  743. return v
  744. }
  745. var isListed bool
  746. for _, val := range values {
  747. expected, ok := canonValue(opChain, val)
  748. if !ok {
  749. return v
  750. }
  751. if reflect.DeepEqual(expected, v.value) {
  752. isListed = true
  753. // continue loop to check that all values are correct
  754. }
  755. }
  756. if !isListed {
  757. opChain.fail(AssertionFailure{
  758. Type: AssertBelongs,
  759. Actual: &AssertionValue{v.value},
  760. Expected: &AssertionValue{AssertionList(values)},
  761. Errors: []error{
  762. errors.New("expected: value is equal to one of the values"),
  763. },
  764. })
  765. }
  766. return v
  767. }
  768. // NotInList succeeds if the whole value is not equal to any of the values from
  769. // given list of values (e.g. map, slice, string, etc).
  770. // Before comparison, all values are converted to canonical form.
  771. //
  772. // If at least one value has wrong type, failure is reported.
  773. //
  774. // Example:
  775. //
  776. // value := NewValue(t, "foo")
  777. // value.NotInList("bar", 123)
  778. func (v *Value) NotInList(values ...interface{}) *Value {
  779. opChain := v.chain.enter("NotInList()")
  780. defer opChain.leave()
  781. if opChain.failed() {
  782. return v
  783. }
  784. if len(values) == 0 {
  785. opChain.fail(AssertionFailure{
  786. Type: AssertUsage,
  787. Errors: []error{
  788. errors.New("unexpected empty list argument"),
  789. },
  790. })
  791. return v
  792. }
  793. for _, val := range values {
  794. expected, ok := canonValue(opChain, val)
  795. if !ok {
  796. return v
  797. }
  798. if reflect.DeepEqual(expected, v.value) {
  799. opChain.fail(AssertionFailure{
  800. Type: AssertNotBelongs,
  801. Actual: &AssertionValue{v.value},
  802. Expected: &AssertionValue{AssertionList(values)},
  803. Errors: []error{
  804. errors.New("expected: value is not equal to any of the values"),
  805. },
  806. })
  807. return v
  808. }
  809. }
  810. return v
  811. }