array.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. package httpexpect
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. )
  7. // Array provides methods to inspect attached []interface{} object
  8. // (Go representation of JSON array).
  9. type Array struct {
  10. noCopy noCopy
  11. chain *chain
  12. value []interface{}
  13. }
  14. // NewArray returns a new Array instance.
  15. //
  16. // If reporter is nil, the function panics.
  17. // If value is nil, failure is reported.
  18. //
  19. // Example:
  20. //
  21. // array := NewArray(t, []interface{}{"foo", 123})
  22. func NewArray(reporter Reporter, value []interface{}) *Array {
  23. return newArray(newChainWithDefaults("Array()", reporter), value)
  24. }
  25. // NewArrayC returns a new Array instance with config.
  26. //
  27. // Requirements for config are same as for WithConfig function.
  28. // If value is nil, failure is reported.
  29. //
  30. // Example:
  31. //
  32. // array := NewArrayC(config, []interface{}{"foo", 123})
  33. func NewArrayC(config Config, value []interface{}) *Array {
  34. return newArray(newChainWithConfig("Array()", config.withDefaults()), value)
  35. }
  36. func newArray(parent *chain, val []interface{}) *Array {
  37. a := &Array{chain: parent.clone(), value: nil}
  38. opChain := a.chain.enter("")
  39. defer opChain.leave()
  40. if val == nil {
  41. opChain.fail(AssertionFailure{
  42. Type: AssertNotNil,
  43. Actual: &AssertionValue{val},
  44. Errors: []error{
  45. errors.New("expected: non-nil array"),
  46. },
  47. })
  48. } else {
  49. a.value, _ = canonArray(opChain, val)
  50. }
  51. return a
  52. }
  53. // Raw returns underlying value attached to Array.
  54. // This is the value originally passed to NewArray, converted to canonical form.
  55. //
  56. // Example:
  57. //
  58. // array := NewArray(t, []interface{}{"foo", 123})
  59. // assert.Equal(t, []interface{}{"foo", 123.0}, array.Raw())
  60. func (a *Array) Raw() []interface{} {
  61. return a.value
  62. }
  63. // Decode unmarshals the underlying value attached to the Array to a target variable.
  64. // target should be one of these:
  65. //
  66. // - pointer to an empty interface
  67. // - pointer to a slice of any type
  68. //
  69. // Example:
  70. //
  71. // type S struct{
  72. // Foo int `json:foo`
  73. // }
  74. // value := []interface{}{
  75. // map[string]interface{}{
  76. // "foo": 123,
  77. // },
  78. // map[string]interface{}{
  79. // "foo": 456,
  80. // },
  81. // }
  82. // array := NewArray(t, value)
  83. //
  84. // var target []S
  85. // arr.Decode(&target)
  86. //
  87. // assert.Equal(t, []S{{123}, {456}}, target)
  88. func (a *Array) Decode(target interface{}) *Array {
  89. opChain := a.chain.enter("Decode()")
  90. defer opChain.leave()
  91. if opChain.failed() {
  92. return a
  93. }
  94. canonDecode(opChain, a.value, target)
  95. return a
  96. }
  97. // Alias is similar to Value.Alias.
  98. func (a *Array) Alias(name string) *Array {
  99. opChain := a.chain.enter("Alias(%q)", name)
  100. defer opChain.leave()
  101. a.chain.setAlias(name)
  102. return a
  103. }
  104. // Path is similar to Value.Path.
  105. func (a *Array) Path(path string) *Value {
  106. opChain := a.chain.enter("Path(%q)", path)
  107. defer opChain.leave()
  108. return jsonPath(opChain, a.value, path)
  109. }
  110. // Schema is similar to Value.Schema.
  111. func (a *Array) Schema(schema interface{}) *Array {
  112. opChain := a.chain.enter("Schema()")
  113. defer opChain.leave()
  114. jsonSchema(opChain, a.value, schema)
  115. return a
  116. }
  117. // Length returns a new Number instance with array length.
  118. //
  119. // Example:
  120. //
  121. // array := NewArray(t, []interface{}{1, 2, 3})
  122. // array.Length().IsEqual(3)
  123. func (a *Array) Length() *Number {
  124. opChain := a.chain.enter("Length()")
  125. defer opChain.leave()
  126. if opChain.failed() {
  127. return newNumber(opChain, 0)
  128. }
  129. return newNumber(opChain, float64(len(a.value)))
  130. }
  131. // Value returns a new Value instance with array element for given index.
  132. //
  133. // If index is out of array bounds, Value reports failure and returns empty
  134. // (but non-nil) instance.
  135. //
  136. // Example:
  137. //
  138. // array := NewArray(t, []interface{}{"foo", 123})
  139. // array.Value(0).String().IsEqual("foo")
  140. // array.Value(1).Number().IsEqual(123)
  141. func (a *Array) Value(index int) *Value {
  142. opChain := a.chain.enter("Value(%d)", index)
  143. defer opChain.leave()
  144. if opChain.failed() {
  145. return newValue(opChain, nil)
  146. }
  147. if index < 0 || index >= len(a.value) {
  148. opChain.fail(AssertionFailure{
  149. Type: AssertInRange,
  150. Actual: &AssertionValue{index},
  151. Expected: &AssertionValue{AssertionRange{
  152. Min: 0,
  153. Max: len(a.value) - 1,
  154. }},
  155. Errors: []error{
  156. errors.New("expected: valid element index"),
  157. },
  158. })
  159. return newValue(opChain, nil)
  160. }
  161. return newValue(opChain, a.value[index])
  162. }
  163. // Deprecated: use Value instead.
  164. func (a *Array) Element(index int) *Value {
  165. return a.Value(index)
  166. }
  167. // HasValue succeeds if array's value at the given index is equal to given value.
  168. //
  169. // Before comparison, both values are converted to canonical form. value should be
  170. // map[string]interface{} or struct.
  171. //
  172. // Example:
  173. //
  174. // array := NewArray(t, []interface{}{"foo", "123"})
  175. // array.HasValue(1, 123)
  176. func (a *Array) HasValue(index int, value interface{}) *Array {
  177. opChain := a.chain.enter("HasValue(%d)", index)
  178. defer opChain.leave()
  179. if opChain.failed() {
  180. return a
  181. }
  182. if index < 0 || index >= len(a.value) {
  183. opChain.fail(AssertionFailure{
  184. Type: AssertInRange,
  185. Actual: &AssertionValue{index},
  186. Expected: &AssertionValue{AssertionRange{
  187. Min: 0,
  188. Max: len(a.value) - 1,
  189. }},
  190. Errors: []error{
  191. errors.New("expected: valid element index"),
  192. },
  193. })
  194. return a
  195. }
  196. expected, ok := canonValue(opChain, value)
  197. if !ok {
  198. return a
  199. }
  200. if !reflect.DeepEqual(expected, a.value[index]) {
  201. opChain.fail(AssertionFailure{
  202. Type: AssertEqual,
  203. Actual: &AssertionValue{a.value[index]},
  204. Expected: &AssertionValue{value},
  205. Errors: []error{
  206. fmt.Errorf(
  207. "expected: array value at index %d is equal to given value",
  208. index),
  209. },
  210. })
  211. return a
  212. }
  213. return a
  214. }
  215. // NotHasValue succeeds if array's value at the given index is not equal to given value.
  216. //
  217. // Before comparison, both values are converted to canonical form. value should be
  218. // map[string]interface{} or struct.
  219. //
  220. // Example:
  221. //
  222. // array := NewArray(t, []interface{}{"foo", "123"})
  223. // array.NotHasValue(1, 234)
  224. func (a *Array) NotHasValue(index int, value interface{}) *Array {
  225. opChain := a.chain.enter("NotHasValue(%d)", index)
  226. defer opChain.leave()
  227. if opChain.failed() {
  228. return a
  229. }
  230. if index < 0 || index >= len(a.value) {
  231. opChain.fail(AssertionFailure{
  232. Type: AssertInRange,
  233. Actual: &AssertionValue{index},
  234. Expected: &AssertionValue{AssertionRange{
  235. Min: 0,
  236. Max: len(a.value) - 1,
  237. }},
  238. Errors: []error{
  239. errors.New("expected: valid element index"),
  240. },
  241. })
  242. return a
  243. }
  244. expected, ok := canonValue(opChain, value)
  245. if !ok {
  246. return a
  247. }
  248. if reflect.DeepEqual(expected, a.value[index]) {
  249. opChain.fail(AssertionFailure{
  250. Type: AssertNotEqual,
  251. Actual: &AssertionValue{a.value[index]},
  252. Expected: &AssertionValue{value},
  253. Errors: []error{
  254. fmt.Errorf(
  255. "expected: array value at index %d is not equal to given value",
  256. index),
  257. },
  258. })
  259. return a
  260. }
  261. return a
  262. }
  263. // Deprecated: use Value or HasValue instead.
  264. func (a *Array) First() *Value {
  265. opChain := a.chain.enter("First()")
  266. defer opChain.leave()
  267. if opChain.failed() {
  268. return newValue(opChain, nil)
  269. }
  270. if len(a.value) == 0 {
  271. opChain.fail(AssertionFailure{
  272. Type: AssertNotEmpty,
  273. Actual: &AssertionValue{a.value},
  274. Errors: []error{
  275. errors.New("expected: non-empty array"),
  276. },
  277. })
  278. return newValue(opChain, nil)
  279. }
  280. return newValue(opChain, a.value[0])
  281. }
  282. // Deprecated: use Value or HasValue instead.
  283. func (a *Array) Last() *Value {
  284. opChain := a.chain.enter("Last()")
  285. defer opChain.leave()
  286. if opChain.failed() {
  287. return newValue(opChain, nil)
  288. }
  289. if len(a.value) == 0 {
  290. opChain.fail(AssertionFailure{
  291. Type: AssertNotEmpty,
  292. Actual: &AssertionValue{a.value},
  293. Errors: []error{
  294. errors.New("expected: non-empty array"),
  295. },
  296. })
  297. return newValue(opChain, nil)
  298. }
  299. return newValue(opChain, a.value[len(a.value)-1])
  300. }
  301. // Iter returns a new slice of Values attached to array elements.
  302. //
  303. // Example:
  304. //
  305. // strings := []interface{}{"foo", "bar"}
  306. // array := NewArray(t, strings)
  307. //
  308. // for index, value := range array.Iter() {
  309. // value.String().IsEqual(strings[index])
  310. // }
  311. func (a *Array) Iter() []Value {
  312. opChain := a.chain.enter("Iter()")
  313. defer opChain.leave()
  314. if opChain.failed() {
  315. return []Value{}
  316. }
  317. ret := []Value{}
  318. for index, element := range a.value {
  319. func() {
  320. valueChain := opChain.replace("Iter[%d]", index)
  321. defer valueChain.leave()
  322. ret = append(ret, *newValue(valueChain, element))
  323. }()
  324. }
  325. return ret
  326. }
  327. // Every runs the passed function on all the elements in the array.
  328. //
  329. // If assertion inside function fails, the original Array is marked failed.
  330. //
  331. // Every will execute the function for all values in the array irrespective
  332. // of assertion failures for some values in the array.
  333. //
  334. // Example:
  335. //
  336. // array := NewArray(t, []interface{}{"foo", "bar"})
  337. //
  338. // array.Every(func(index int, value *httpexpect.Value) {
  339. // value.String().NotEmpty()
  340. // })
  341. func (a *Array) Every(fn func(index int, value *Value)) *Array {
  342. opChain := a.chain.enter("Every()")
  343. defer opChain.leave()
  344. if opChain.failed() {
  345. return a
  346. }
  347. if fn == nil {
  348. opChain.fail(AssertionFailure{
  349. Type: AssertUsage,
  350. Errors: []error{
  351. errors.New("unexpected nil function argument"),
  352. },
  353. })
  354. return a
  355. }
  356. for index, element := range a.value {
  357. func() {
  358. valueChain := opChain.replace("Every[%d]", index)
  359. defer valueChain.leave()
  360. fn(index, newValue(valueChain, element))
  361. }()
  362. }
  363. return a
  364. }
  365. // Filter accepts a function that returns a boolean. The function is ran
  366. // over the array elements. If the function returns true, the element passes
  367. // the filter and is added to the new array of filtered elements. If false,
  368. // the element is skipped (or in other words filtered out). After iterating
  369. // through all the elements of the original array, the new filtered array
  370. // is returned.
  371. //
  372. // If there are any failed assertions in the filtering function, the
  373. // element is omitted without causing test failure.
  374. //
  375. // Example:
  376. //
  377. // array := NewArray(t, []interface{}{1, 2, "foo", "bar"})
  378. // filteredArray := array.Filter(func(index int, value *httpexpect.Value) bool {
  379. // value.String().NotEmpty() //fails on 1 and 2
  380. // return value.Raw() != "bar" //fails on "bar"
  381. // })
  382. // filteredArray.IsEqual([]interface{}{"foo"}) //succeeds
  383. func (a *Array) Filter(fn func(index int, value *Value) bool) *Array {
  384. opChain := a.chain.enter("Filter()")
  385. defer opChain.leave()
  386. if opChain.failed() {
  387. return newArray(opChain, nil)
  388. }
  389. if fn == nil {
  390. opChain.fail(AssertionFailure{
  391. Type: AssertUsage,
  392. Errors: []error{
  393. errors.New("unexpected nil function argument"),
  394. },
  395. })
  396. return newArray(opChain, nil)
  397. }
  398. filteredArray := []interface{}{}
  399. for index, element := range a.value {
  400. func() {
  401. valueChain := opChain.replace("Filter[%d]", index)
  402. defer valueChain.leave()
  403. valueChain.setRoot()
  404. valueChain.setSeverity(SeverityLog)
  405. if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() {
  406. filteredArray = append(filteredArray, element)
  407. }
  408. }()
  409. }
  410. return newArray(opChain, filteredArray)
  411. }
  412. // Transform runs the passed function on all the elements in the array
  413. // and returns a new array without effeecting original array.
  414. //
  415. // Example:
  416. //
  417. // array := NewArray(t, []interface{}{"foo", "bar"})
  418. // transformedArray := array.Transform(
  419. // func(index int, value interface{}) interface{} {
  420. // return strings.ToUpper(value.(string))
  421. // })
  422. // transformedArray.IsEqual([]interface{}{"FOO", "BAR"})
  423. func (a *Array) Transform(fn func(index int, value interface{}) interface{}) *Array {
  424. opChain := a.chain.enter("Transform()")
  425. defer opChain.leave()
  426. if opChain.failed() {
  427. return newArray(opChain, nil)
  428. }
  429. if fn == nil {
  430. opChain.fail(AssertionFailure{
  431. Type: AssertUsage,
  432. Errors: []error{
  433. errors.New("unexpected nil function argument"),
  434. },
  435. })
  436. return newArray(opChain, nil)
  437. }
  438. transformedArray := []interface{}{}
  439. for index, element := range a.value {
  440. transformedArray = append(transformedArray, fn(index, element))
  441. }
  442. return newArray(opChain, transformedArray)
  443. }
  444. // Find accepts a function that returns a boolean, runs it over the array
  445. // elements, and returns the first element on which it returned true.
  446. //
  447. // If there are any failed assertions in the predicate function, the
  448. // element is skipped without causing test failure.
  449. //
  450. // If no elements were found, a failure is reported.
  451. //
  452. // Example:
  453. //
  454. // array := NewArray(t, []interface{}{1, "foo", 101, "bar", 201})
  455. // foundValue := array.Find(func(index int, value *httpexpect.Value) bool {
  456. // num := value.Number() // skip if element is not a number
  457. // return num.Raw() > 100 // check element value
  458. // })
  459. // foundValue.IsEqual(101) // succeeds
  460. func (a *Array) Find(fn func(index int, value *Value) bool) *Value {
  461. opChain := a.chain.enter("Find()")
  462. defer opChain.leave()
  463. if opChain.failed() {
  464. return newValue(opChain, nil)
  465. }
  466. if fn == nil {
  467. opChain.fail(AssertionFailure{
  468. Type: AssertUsage,
  469. Errors: []error{
  470. errors.New("unexpected nil function argument"),
  471. },
  472. })
  473. return newValue(opChain, nil)
  474. }
  475. for index, element := range a.value {
  476. found := false
  477. func() {
  478. valueChain := opChain.replace("Find[%d]", index)
  479. defer valueChain.leave()
  480. valueChain.setRoot()
  481. valueChain.setSeverity(SeverityLog)
  482. if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() {
  483. found = true
  484. }
  485. }()
  486. if found {
  487. return newValue(opChain, element)
  488. }
  489. }
  490. opChain.fail(AssertionFailure{
  491. Type: AssertValid,
  492. Actual: &AssertionValue{a.value},
  493. Errors: []error{
  494. errors.New("expected: at least one array element matches predicate"),
  495. },
  496. })
  497. return newValue(opChain, nil)
  498. }
  499. // FindAll accepts a function that returns a boolean, runs it over the array
  500. // elements, and returns all the elements on which it returned true.
  501. //
  502. // If there are any failed assertions in the predicate function, the
  503. // element is skipped without causing test failure.
  504. //
  505. // If no elements were found, empty slice is returned without reporting error.
  506. //
  507. // Example:
  508. //
  509. // array := NewArray(t, []interface{}{1, "foo", 101, "bar", 201})
  510. // foundValues := array.FindAll(func(index int, value *httpexpect.Value) bool {
  511. // num := value.Number() // skip if element is not a number
  512. // return num.Raw() > 100 // check element value
  513. // })
  514. //
  515. // assert.Equal(t, len(foundValues), 2)
  516. // foundValues[0].IsEqual(101)
  517. // foundValues[1].IsEqual(201)
  518. func (a *Array) FindAll(fn func(index int, value *Value) bool) []*Value {
  519. opChain := a.chain.enter("FindAll()")
  520. defer opChain.leave()
  521. if opChain.failed() {
  522. return []*Value{}
  523. }
  524. if fn == nil {
  525. opChain.fail(AssertionFailure{
  526. Type: AssertUsage,
  527. Errors: []error{
  528. errors.New("unexpected nil function argument"),
  529. },
  530. })
  531. return []*Value{}
  532. }
  533. foundValues := make([]*Value, 0, len(a.value))
  534. for index, element := range a.value {
  535. func() {
  536. valueChain := opChain.replace("FindAll[%d]", index)
  537. defer valueChain.leave()
  538. valueChain.setRoot()
  539. valueChain.setSeverity(SeverityLog)
  540. if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() {
  541. foundValues = append(foundValues, newValue(opChain, element))
  542. }
  543. }()
  544. }
  545. return foundValues
  546. }
  547. // NotFind accepts a function that returns a boolean, runs it over the array
  548. // elelements, and checks that it does not return true for any of the elements.
  549. //
  550. // If there are any failed assertions in the predicate function, the
  551. // element is skipped without causing test failure.
  552. //
  553. // If the predicate function did not fail and returned true for at least
  554. // one element, a failure is reported.
  555. //
  556. // Example:
  557. //
  558. // array := NewArray(t, []interface{}{1, "foo", 2, "bar"})
  559. // array.NotFind(func(index int, value *httpexpect.Value) bool {
  560. // num := value.Number() // skip if element is not a number
  561. // return num.Raw() > 100 // check element value
  562. // }) // succeeds
  563. func (a *Array) NotFind(fn func(index int, value *Value) bool) *Array {
  564. opChain := a.chain.enter("NotFind()")
  565. defer opChain.leave()
  566. if opChain.failed() {
  567. return a
  568. }
  569. if fn == nil {
  570. opChain.fail(AssertionFailure{
  571. Type: AssertUsage,
  572. Errors: []error{
  573. errors.New("unexpected nil function argument"),
  574. },
  575. })
  576. return a
  577. }
  578. for index, element := range a.value {
  579. found := false
  580. func() {
  581. valueChain := opChain.replace("NotFind[%d]", index)
  582. defer valueChain.leave()
  583. valueChain.setRoot()
  584. valueChain.setSeverity(SeverityLog)
  585. if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() {
  586. found = true
  587. }
  588. }()
  589. if found {
  590. opChain.fail(AssertionFailure{
  591. Type: AssertNotContainsElement,
  592. Expected: &AssertionValue{element},
  593. Actual: &AssertionValue{a.value},
  594. Errors: []error{
  595. errors.New("expected: none of the array elements match predicate"),
  596. fmt.Errorf("element with index %d matches predicate", index),
  597. },
  598. })
  599. return a
  600. }
  601. }
  602. return a
  603. }
  604. // IsEmpty succeeds if array is empty.
  605. //
  606. // Example:
  607. //
  608. // array := NewArray(t, []interface{}{})
  609. // array.IsEmpty()
  610. func (a *Array) IsEmpty() *Array {
  611. opChain := a.chain.enter("IsEmpty()")
  612. defer opChain.leave()
  613. if opChain.failed() {
  614. return a
  615. }
  616. if !(len(a.value) == 0) {
  617. opChain.fail(AssertionFailure{
  618. Type: AssertEmpty,
  619. Actual: &AssertionValue{a.value},
  620. Errors: []error{
  621. errors.New("expected: empty array"),
  622. },
  623. })
  624. }
  625. return a
  626. }
  627. // NotEmpty succeeds if array is non-empty.
  628. //
  629. // Example:
  630. //
  631. // array := NewArray(t, []interface{}{"foo", 123})
  632. // array.NotEmpty()
  633. func (a *Array) NotEmpty() *Array {
  634. opChain := a.chain.enter("NotEmpty()")
  635. defer opChain.leave()
  636. if opChain.failed() {
  637. return a
  638. }
  639. if len(a.value) == 0 {
  640. opChain.fail(AssertionFailure{
  641. Type: AssertNotEmpty,
  642. Actual: &AssertionValue{a.value},
  643. Errors: []error{
  644. errors.New("expected: non-empty array"),
  645. },
  646. })
  647. }
  648. return a
  649. }
  650. // Deprecated: use IsEmpty instead.
  651. func (a *Array) Empty() *Array {
  652. return a.IsEmpty()
  653. }
  654. // IsEqual succeeds if array is equal to given value.
  655. // Before comparison, both array and value are converted to canonical form.
  656. //
  657. // value should be a slice of any type.
  658. //
  659. // Example:
  660. //
  661. // array := NewArray(t, []interface{}{"foo", 123})
  662. // array.IsEqual([]interface{}{"foo", 123})
  663. //
  664. // array := NewArray(t, []interface{}{"foo", "bar"})
  665. // array.IsEqual([]string{}{"foo", "bar"})
  666. //
  667. // array := NewArray(t, []interface{}{123, 456})
  668. // array.IsEqual([]int{}{123, 456})
  669. func (a *Array) IsEqual(value interface{}) *Array {
  670. opChain := a.chain.enter("IsEqual()")
  671. defer opChain.leave()
  672. if opChain.failed() {
  673. return a
  674. }
  675. expected, ok := canonArray(opChain, value)
  676. if !ok {
  677. return a
  678. }
  679. if !reflect.DeepEqual(expected, a.value) {
  680. opChain.fail(AssertionFailure{
  681. Type: AssertEqual,
  682. Actual: &AssertionValue{a.value},
  683. Expected: &AssertionValue{expected},
  684. Errors: []error{
  685. errors.New("expected: arrays are equal"),
  686. },
  687. })
  688. }
  689. return a
  690. }
  691. // NotEqual succeeds if array is not equal to given value.
  692. // Before comparison, both array and value are converted to canonical form.
  693. //
  694. // value should be a slice of any type.
  695. //
  696. // Example:
  697. //
  698. // array := NewArray(t, []interface{}{"foo", 123})
  699. // array.NotEqual([]interface{}{123, "foo"})
  700. func (a *Array) NotEqual(value interface{}) *Array {
  701. opChain := a.chain.enter("NotEqual()")
  702. defer opChain.leave()
  703. if opChain.failed() {
  704. return a
  705. }
  706. expected, ok := canonArray(opChain, value)
  707. if !ok {
  708. return a
  709. }
  710. if reflect.DeepEqual(expected, a.value) {
  711. opChain.fail(AssertionFailure{
  712. Type: AssertNotEqual,
  713. Actual: &AssertionValue{a.value},
  714. Expected: &AssertionValue{expected},
  715. Errors: []error{
  716. errors.New("expected: arrays are non-equal"),
  717. },
  718. })
  719. }
  720. return a
  721. }
  722. // Deprecated: use IsEqual instead.
  723. func (a *Array) Equal(value interface{}) *Array {
  724. return a.IsEqual(value)
  725. }
  726. // IsEqualUnordered succeeds if array is equal to another array, ignoring element
  727. // order. Before comparison, both arrays are converted to canonical form.
  728. //
  729. // Example:
  730. //
  731. // array := NewArray(t, []interface{}{"foo", 123})
  732. // array.IsEqualUnordered([]interface{}{123, "foo"})
  733. func (a *Array) IsEqualUnordered(value interface{}) *Array {
  734. opChain := a.chain.enter("IsEqualUnordered()")
  735. defer opChain.leave()
  736. if opChain.failed() {
  737. return a
  738. }
  739. expected, ok := canonArray(opChain, value)
  740. if !ok {
  741. return a
  742. }
  743. for _, element := range expected {
  744. expectedCount := countElement(expected, element)
  745. actualCount := countElement(a.value, element)
  746. if actualCount != expectedCount {
  747. if expectedCount == 1 && actualCount == 0 {
  748. opChain.fail(AssertionFailure{
  749. Type: AssertContainsElement,
  750. Actual: &AssertionValue{a.value},
  751. Expected: &AssertionValue{element},
  752. Reference: &AssertionValue{value},
  753. Errors: []error{
  754. errors.New("expected: array contains element from reference array"),
  755. },
  756. })
  757. } else {
  758. opChain.fail(AssertionFailure{
  759. Type: AssertNotContainsElement,
  760. Actual: &AssertionValue{a.value},
  761. Expected: &AssertionValue{element},
  762. Reference: &AssertionValue{value},
  763. Errors: []error{
  764. fmt.Errorf(
  765. "expected: element occurs %d time(s), as in reference array,"+
  766. " but it occurs %d time(s)",
  767. expectedCount,
  768. actualCount),
  769. },
  770. })
  771. }
  772. return a
  773. }
  774. }
  775. for _, element := range a.value {
  776. expectedCount := countElement(expected, element)
  777. actualCount := countElement(a.value, element)
  778. if actualCount != expectedCount {
  779. if expectedCount == 0 && actualCount == 1 {
  780. opChain.fail(AssertionFailure{
  781. Type: AssertNotContainsElement,
  782. Actual: &AssertionValue{a.value},
  783. Expected: &AssertionValue{element},
  784. Reference: &AssertionValue{value},
  785. Errors: []error{
  786. errors.New("expected: array does not contain elements" +
  787. " that are not present in reference array"),
  788. },
  789. })
  790. } else {
  791. opChain.fail(AssertionFailure{
  792. Type: AssertNotContainsElement,
  793. Actual: &AssertionValue{a.value},
  794. Expected: &AssertionValue{element},
  795. Reference: &AssertionValue{value},
  796. Errors: []error{
  797. fmt.Errorf(
  798. "expected: element occurs %d time(s), as in reference array,"+
  799. " but it occurs %d time(s)",
  800. expectedCount,
  801. actualCount),
  802. },
  803. })
  804. }
  805. return a
  806. }
  807. }
  808. return a
  809. }
  810. // NotEqualUnordered succeeds if array is not equal to another array, ignoring
  811. // element order. Before comparison, both arrays are converted to canonical form.
  812. //
  813. // Example:
  814. //
  815. // array := NewArray(t, []interface{}{"foo", 123})
  816. // array.NotEqualUnordered([]interface{}{123, "foo", "bar"})
  817. func (a *Array) NotEqualUnordered(value interface{}) *Array {
  818. opChain := a.chain.enter("NotEqualUnordered()")
  819. defer opChain.leave()
  820. if opChain.failed() {
  821. return a
  822. }
  823. expected, ok := canonArray(opChain, value)
  824. if !ok {
  825. return a
  826. }
  827. different := false
  828. for _, element := range expected {
  829. expectedCount := countElement(expected, element)
  830. actualCount := countElement(a.value, element)
  831. if actualCount != expectedCount {
  832. different = true
  833. break
  834. }
  835. }
  836. for _, element := range a.value {
  837. expectedCount := countElement(expected, element)
  838. actualCount := countElement(a.value, element)
  839. if actualCount != expectedCount {
  840. different = true
  841. break
  842. }
  843. }
  844. if !different {
  845. opChain.fail(AssertionFailure{
  846. Type: AssertNotEqual,
  847. Actual: &AssertionValue{a.value},
  848. Expected: &AssertionValue{value},
  849. Reference: &AssertionValue{value},
  850. Errors: []error{
  851. errors.New("expected: arrays are non-equal (ignoring order)"),
  852. },
  853. })
  854. }
  855. return a
  856. }
  857. // Deprecated: use IsEqualUnordered instead.
  858. func (a *Array) EqualUnordered(value interface{}) *Array {
  859. return a.IsEqualUnordered(value)
  860. }
  861. // InList succeeds if the whole array is equal to one of the values from given
  862. // list of arrays. Before comparison, both array and each value are converted
  863. // to canonical form.
  864. //
  865. // Each value should be a slice of any type. If at least one value has wrong
  866. // type, failure is reported.
  867. //
  868. // Example:
  869. //
  870. // array := NewArray(t, []interface{}{"foo", 123})
  871. // array.InList([]interface{}{"foo", 123}, []interface{}{"bar", "456"})
  872. func (a *Array) InList(values ...interface{}) *Array {
  873. opChain := a.chain.enter("InList()")
  874. defer opChain.leave()
  875. if opChain.failed() {
  876. return a
  877. }
  878. if len(values) == 0 {
  879. opChain.fail(AssertionFailure{
  880. Type: AssertUsage,
  881. Errors: []error{
  882. errors.New("unexpected empty list argument"),
  883. },
  884. })
  885. return a
  886. }
  887. var isListed bool
  888. for _, v := range values {
  889. expected, ok := canonArray(opChain, v)
  890. if !ok {
  891. return a
  892. }
  893. if reflect.DeepEqual(expected, a.value) {
  894. isListed = true
  895. // continue loop to check that all values are correct
  896. }
  897. }
  898. if !isListed {
  899. opChain.fail(AssertionFailure{
  900. Type: AssertBelongs,
  901. Actual: &AssertionValue{a.value},
  902. Expected: &AssertionValue{AssertionList(values)},
  903. Errors: []error{
  904. errors.New("expected: array is equal to one of the values"),
  905. },
  906. })
  907. }
  908. return a
  909. }
  910. // NotInList succeeds if the whole array is not equal to any of the values from
  911. // given list of arrays. Before comparison, both array and each value are
  912. // converted to canonical form.
  913. //
  914. // Each value should be a slice of any type. If at least one value has wrong
  915. // type, failure is reported.
  916. //
  917. // Example:
  918. //
  919. // array := NewArray(t, []interface{}{"foo", 123})
  920. // array.NotInList([]interface{}{"bar", 456}, []interface{}{"baz", "foo"})
  921. func (a *Array) NotInList(values ...interface{}) *Array {
  922. opChain := a.chain.enter("NotInList()")
  923. defer opChain.leave()
  924. if opChain.failed() {
  925. return a
  926. }
  927. if len(values) == 0 {
  928. opChain.fail(AssertionFailure{
  929. Type: AssertUsage,
  930. Errors: []error{
  931. errors.New("unexpected empty list argument"),
  932. },
  933. })
  934. return a
  935. }
  936. for _, v := range values {
  937. expected, ok := canonArray(opChain, v)
  938. if !ok {
  939. return a
  940. }
  941. if reflect.DeepEqual(expected, a.value) {
  942. opChain.fail(AssertionFailure{
  943. Type: AssertNotBelongs,
  944. Actual: &AssertionValue{a.value},
  945. Expected: &AssertionValue{AssertionList(values)},
  946. Errors: []error{
  947. errors.New("expected: array is not equal to any of the values"),
  948. },
  949. })
  950. return a
  951. }
  952. }
  953. return a
  954. }
  955. // ConsistsOf succeeds if array contains all given elements, in given order, and only
  956. // them. Before comparison, array and all elements are converted to canonical form.
  957. //
  958. // Example:
  959. //
  960. // array := NewArray(t, []interface{}{"foo", 123})
  961. // array.ConsistsOf("foo", 123)
  962. //
  963. // These calls are equivalent:
  964. //
  965. // array.ConsistsOf("a", "b")
  966. // array.IsEqual([]interface{}{"a", "b"})
  967. func (a *Array) ConsistsOf(values ...interface{}) *Array {
  968. opChain := a.chain.enter("ConsistsOf()")
  969. defer opChain.leave()
  970. if opChain.failed() {
  971. return a
  972. }
  973. expected, ok := canonArray(opChain, values)
  974. if !ok {
  975. return a
  976. }
  977. if !reflect.DeepEqual(expected, a.value) {
  978. opChain.fail(AssertionFailure{
  979. Type: AssertEqual,
  980. Actual: &AssertionValue{a.value},
  981. Expected: &AssertionValue{expected},
  982. Errors: []error{
  983. errors.New("expected: array consists of given elements"),
  984. },
  985. })
  986. }
  987. return a
  988. }
  989. // NotConsistsOf is opposite to ConsistsOf.
  990. //
  991. // Example:
  992. //
  993. // array := NewArray(t, []interface{}{"foo", 123})
  994. // array.NotConsistsOf("foo")
  995. // array.NotConsistsOf("foo", 123, 456)
  996. // array.NotConsistsOf(123, "foo")
  997. //
  998. // These calls are equivalent:
  999. //
  1000. // array.NotConsistsOf("a", "b")
  1001. // array.NotEqual([]interface{}{"a", "b"})
  1002. func (a *Array) NotConsistsOf(values ...interface{}) *Array {
  1003. opChain := a.chain.enter("NotConsistsOf()")
  1004. defer opChain.leave()
  1005. if opChain.failed() {
  1006. return a
  1007. }
  1008. expected, ok := canonArray(opChain, values)
  1009. if !ok {
  1010. return a
  1011. }
  1012. if reflect.DeepEqual(expected, a.value) {
  1013. opChain.fail(AssertionFailure{
  1014. Type: AssertNotEqual,
  1015. Actual: &AssertionValue{a.value},
  1016. Expected: &AssertionValue{expected},
  1017. Errors: []error{
  1018. errors.New("expected: arrays does not consist of given elements"),
  1019. },
  1020. })
  1021. }
  1022. return a
  1023. }
  1024. // Deprecated: use ConsistsOf instead.
  1025. func (a *Array) Elements(values ...interface{}) *Array {
  1026. return a.ConsistsOf(values...)
  1027. }
  1028. // Deprecated: use NotConsistsOf instead.
  1029. func (a *Array) NotElements(values ...interface{}) *Array {
  1030. return a.NotConsistsOf(values...)
  1031. }
  1032. // Deprecated: use ContainsAll or ContainsAny instead.
  1033. func (a *Array) Contains(values ...interface{}) *Array {
  1034. opChain := a.chain.enter("Contains()")
  1035. defer opChain.leave()
  1036. if opChain.failed() {
  1037. return a
  1038. }
  1039. elements, ok := canonArray(opChain, values)
  1040. if !ok {
  1041. return a
  1042. }
  1043. for _, expected := range elements {
  1044. if countElement(a.value, expected) == 0 {
  1045. opChain.fail(AssertionFailure{
  1046. Type: AssertContainsElement,
  1047. Actual: &AssertionValue{a.value},
  1048. Expected: &AssertionValue{expected},
  1049. Reference: &AssertionValue{values},
  1050. Errors: []error{
  1051. errors.New("expected: array contains element from reference array"),
  1052. },
  1053. })
  1054. break
  1055. }
  1056. }
  1057. return a
  1058. }
  1059. // Deprecated: use NotContainsAll or NotContainsAny instead.
  1060. func (a *Array) NotContains(values ...interface{}) *Array {
  1061. opChain := a.chain.enter("NotContains()")
  1062. defer opChain.leave()
  1063. if opChain.failed() {
  1064. return a
  1065. }
  1066. elements, ok := canonArray(opChain, values)
  1067. if !ok {
  1068. return a
  1069. }
  1070. for _, expected := range elements {
  1071. if !(countElement(a.value, expected) == 0) {
  1072. opChain.fail(AssertionFailure{
  1073. Type: AssertNotContainsElement,
  1074. Actual: &AssertionValue{a.value},
  1075. Expected: &AssertionValue{expected},
  1076. Reference: &AssertionValue{values},
  1077. Errors: []error{
  1078. errors.New("expected:" +
  1079. " array does not contain any elements from reference array"),
  1080. },
  1081. })
  1082. break
  1083. }
  1084. }
  1085. return a
  1086. }
  1087. // ContainsAll succeeds if array contains all given elements (in any order).
  1088. // Before comparison, array and all elements are converted to canonical form.
  1089. //
  1090. // Example:
  1091. //
  1092. // array := NewArray(t, []interface{}{"foo", 123})
  1093. // array.ContainsAll(123, "foo")
  1094. func (a *Array) ContainsAll(values ...interface{}) *Array {
  1095. opChain := a.chain.enter("ContainsAll()")
  1096. defer opChain.leave()
  1097. if opChain.failed() {
  1098. return a
  1099. }
  1100. elements, ok := canonArray(opChain, values)
  1101. if !ok {
  1102. return a
  1103. }
  1104. for _, expected := range elements {
  1105. if countElement(a.value, expected) == 0 {
  1106. opChain.fail(AssertionFailure{
  1107. Type: AssertContainsElement,
  1108. Actual: &AssertionValue{a.value},
  1109. Expected: &AssertionValue{expected},
  1110. Reference: &AssertionValue{values},
  1111. Errors: []error{
  1112. errors.New("expected: array contains element from reference array"),
  1113. },
  1114. })
  1115. break
  1116. }
  1117. }
  1118. return a
  1119. }
  1120. // NotContainsAll succeeds if array does not contain at least one of the elements.
  1121. // Before comparison, array and all elements are converted to canonical form.
  1122. //
  1123. // Example:
  1124. //
  1125. // array := NewArray(t, []interface{}{"foo", 123})
  1126. // array.NotContainsAll("bar") // success
  1127. // array.NotContainsAll(123, "foo") // failure
  1128. func (a *Array) NotContainsAll(values ...interface{}) *Array {
  1129. opChain := a.chain.enter("NotContainsAll()")
  1130. defer opChain.leave()
  1131. if opChain.failed() {
  1132. return a
  1133. }
  1134. elements, ok := canonArray(opChain, values)
  1135. if !ok {
  1136. return a
  1137. }
  1138. haveMissing := false
  1139. for _, expected := range elements {
  1140. if countElement(a.value, expected) == 0 {
  1141. haveMissing = true
  1142. break
  1143. }
  1144. }
  1145. if !haveMissing {
  1146. opChain.fail(AssertionFailure{
  1147. Type: AssertNotContainsElement,
  1148. Actual: &AssertionValue{a.value},
  1149. Reference: &AssertionValue{values},
  1150. Errors: []error{
  1151. errors.New("expected:" +
  1152. " array does not contain at least one element from reference array"),
  1153. },
  1154. })
  1155. }
  1156. return a
  1157. }
  1158. // ContainsAny succeeds if array contains at least one element from the given elements.
  1159. // Before comparison, array and all elements are converted to canonical form.
  1160. //
  1161. // Example:
  1162. //
  1163. // array := NewArray(t, []interface{}{"foo", 123, 123})
  1164. // array.ContainsAny(123, "foo", "FOO") // success
  1165. // array.ContainsAny("FOO") // failure
  1166. func (a *Array) ContainsAny(values ...interface{}) *Array {
  1167. opChain := a.chain.enter("ContainsAny()")
  1168. defer opChain.leave()
  1169. if opChain.failed() {
  1170. return a
  1171. }
  1172. elements, ok := canonArray(opChain, values)
  1173. if !ok {
  1174. return a
  1175. }
  1176. foundAny := false
  1177. for _, expected := range elements {
  1178. if countElement(a.value, expected) != 0 {
  1179. foundAny = true
  1180. break
  1181. }
  1182. }
  1183. if !foundAny {
  1184. opChain.fail(AssertionFailure{
  1185. Type: AssertContainsElement,
  1186. Actual: &AssertionValue{a.value},
  1187. Reference: &AssertionValue{values},
  1188. Errors: []error{
  1189. errors.New("expected:" +
  1190. " array contains at least one element from reference array"),
  1191. },
  1192. })
  1193. }
  1194. return a
  1195. }
  1196. // NotContainsAny succeeds if none of the given elements are in the array.
  1197. // Before comparison, array and all elements are converted to canonical form.
  1198. //
  1199. // Example:
  1200. //
  1201. // array := NewArray(t, []interface{}{"foo", 123})
  1202. // array.NotContainsAny("bar", 124) // success
  1203. // array.NotContainsAny(123) // failure
  1204. func (a *Array) NotContainsAny(values ...interface{}) *Array {
  1205. opChain := a.chain.enter("NotContainsAny()")
  1206. defer opChain.leave()
  1207. if opChain.failed() {
  1208. return a
  1209. }
  1210. elements, ok := canonArray(opChain, values)
  1211. if !ok {
  1212. return a
  1213. }
  1214. for _, expected := range elements {
  1215. if countElement(a.value, expected) != 0 {
  1216. opChain.fail(AssertionFailure{
  1217. Type: AssertNotContainsElement,
  1218. Actual: &AssertionValue{a.value},
  1219. Expected: &AssertionValue{expected},
  1220. Reference: &AssertionValue{values},
  1221. Errors: []error{
  1222. errors.New("expected:" +
  1223. " array does not contain any elements from reference array"),
  1224. },
  1225. })
  1226. return a
  1227. }
  1228. }
  1229. return a
  1230. }
  1231. // ContainsOnly succeeds if array contains all given elements, in any order, and only
  1232. // them, ignoring duplicates. Before comparison, array and all elements are converted
  1233. // to canonical form.
  1234. //
  1235. // Example:
  1236. //
  1237. // array := NewArray(t, []interface{}{"foo", 123, 123})
  1238. // array.ContainsOnly(123, "foo")
  1239. //
  1240. // These calls are equivalent:
  1241. //
  1242. // array.ContainsOnly("a", "b")
  1243. // array.ContainsOnly("b", "a")
  1244. func (a *Array) ContainsOnly(values ...interface{}) *Array {
  1245. opChain := a.chain.enter("ContainsOnly()")
  1246. defer opChain.leave()
  1247. if opChain.failed() {
  1248. return a
  1249. }
  1250. elements, ok := canonArray(opChain, values)
  1251. if !ok {
  1252. return a
  1253. }
  1254. for _, element := range elements {
  1255. if countElement(a.value, element) == 0 {
  1256. opChain.fail(AssertionFailure{
  1257. Type: AssertContainsElement,
  1258. Actual: &AssertionValue{a.value},
  1259. Expected: &AssertionValue{element},
  1260. Reference: &AssertionValue{values},
  1261. Errors: []error{
  1262. errors.New("expected: array contains element from reference array"),
  1263. },
  1264. })
  1265. return a
  1266. }
  1267. }
  1268. for _, element := range a.value {
  1269. if countElement(elements, element) == 0 {
  1270. opChain.fail(AssertionFailure{
  1271. Type: AssertNotContainsElement,
  1272. Actual: &AssertionValue{a.value},
  1273. Expected: &AssertionValue{element},
  1274. Reference: &AssertionValue{values},
  1275. Errors: []error{
  1276. errors.New("expected: array does not contain elements" +
  1277. " that are not present in reference array"),
  1278. },
  1279. })
  1280. return a
  1281. }
  1282. }
  1283. return a
  1284. }
  1285. // NotContainsOnly is opposite to ContainsOnly.
  1286. //
  1287. // Example:
  1288. //
  1289. // array := NewArray(t, []interface{}{"foo", 123})
  1290. // array.NotContainsOnly(123)
  1291. // array.NotContainsOnly(123, "foo", "bar")
  1292. //
  1293. // These calls are equivalent:
  1294. //
  1295. // array.NotContainsOnly("a", "b")
  1296. // array.NotContainsOnly("b", "a")
  1297. func (a *Array) NotContainsOnly(values ...interface{}) *Array {
  1298. opChain := a.chain.enter("NotContainsOnly()")
  1299. defer opChain.leave()
  1300. if opChain.failed() {
  1301. return a
  1302. }
  1303. elements, ok := canonArray(opChain, values)
  1304. if !ok {
  1305. return a
  1306. }
  1307. different := false
  1308. for _, element := range elements {
  1309. if countElement(a.value, element) == 0 {
  1310. different = true
  1311. break
  1312. }
  1313. }
  1314. for _, element := range a.value {
  1315. if countElement(elements, element) == 0 {
  1316. different = true
  1317. break
  1318. }
  1319. }
  1320. if !different {
  1321. opChain.fail(AssertionFailure{
  1322. Type: AssertNotEqual,
  1323. Actual: &AssertionValue{a.value},
  1324. Expected: &AssertionValue{values},
  1325. Reference: &AssertionValue{values},
  1326. Errors: []error{
  1327. errors.New("expected:" +
  1328. " array does not contain only elements from reference array" +
  1329. " (at least one distinguishing element needed)"),
  1330. },
  1331. })
  1332. }
  1333. return a
  1334. }
  1335. // IsOrdered succeeds if every element is not less than the previous element
  1336. // as defined on the given `less` comparator function.
  1337. // For default, it will use built-in comparator function for each data type.
  1338. // Built-in comparator requires all elements in the array to have same data type.
  1339. // Array with 0 or 1 element will always succeed
  1340. //
  1341. // Example:
  1342. //
  1343. // array := NewArray(t, []interface{}{100, 101, 102})
  1344. // array.IsOrdered() // succeeds
  1345. // array.IsOrdered(func(x, y *httpexpect.Value) bool {
  1346. // return x.Number().Raw() < y.Number().Raw()
  1347. // }) // succeeds
  1348. func (a *Array) IsOrdered(less ...func(x, y *Value) bool) *Array {
  1349. opChain := a.chain.enter("IsOrdered()")
  1350. defer opChain.leave()
  1351. if opChain.failed() {
  1352. return a
  1353. }
  1354. if len(less) > 1 {
  1355. opChain.fail(AssertionFailure{
  1356. Type: AssertUsage,
  1357. Errors: []error{
  1358. errors.New("unexpected multiple less arguments"),
  1359. },
  1360. })
  1361. return a
  1362. }
  1363. var lessFn func(x, y *Value) bool
  1364. if len(less) == 1 {
  1365. lessFn = less[0]
  1366. if lessFn == nil {
  1367. opChain.fail(AssertionFailure{
  1368. Type: AssertUsage,
  1369. Errors: []error{
  1370. errors.New("unexpected nil less argument"),
  1371. },
  1372. })
  1373. return a
  1374. }
  1375. } else {
  1376. lessFn = builtinComparator(opChain, a.value)
  1377. if lessFn == nil {
  1378. return a
  1379. }
  1380. }
  1381. if len(a.value) <= 1 {
  1382. return a
  1383. }
  1384. for i := 0; i < len(a.value)-1; i++ {
  1385. var unordered bool
  1386. func() {
  1387. xChain := opChain.replace("IsOrdered[%d]", i)
  1388. defer xChain.leave()
  1389. yChain := opChain.replace("IsOrdered[%d]", i+1)
  1390. defer yChain.leave()
  1391. x := newValue(xChain, a.value[i])
  1392. y := newValue(yChain, a.value[i+1])
  1393. unordered = lessFn(y, x)
  1394. }()
  1395. if opChain.failed() {
  1396. return a
  1397. }
  1398. if unordered {
  1399. opChain.fail(AssertionFailure{
  1400. Type: AssertLt,
  1401. Actual: &AssertionValue{a.value[i]},
  1402. Expected: &AssertionValue{a.value[i+1]},
  1403. Reference: &AssertionValue{a.value},
  1404. Errors: []error{
  1405. errors.New("expected: reference array is ordered"),
  1406. fmt.Errorf("element %v must not be less than element %v",
  1407. i+1, i),
  1408. },
  1409. })
  1410. return a
  1411. }
  1412. }
  1413. return a
  1414. }
  1415. // NotOrdered succeeds if at least one element is less than the previous element
  1416. // as defined on the given `less` comparator function.
  1417. // For default, it will use built-in comparator function for each data type.
  1418. // Built-in comparator requires all elements in the array to have same data type.
  1419. // Array with 0 or 1 element will always succeed
  1420. //
  1421. // Example:
  1422. //
  1423. // array := NewArray(t, []interface{}{102, 101, 100})
  1424. // array.NotOrdered() // succeeds
  1425. // array.NotOrdered(func(x, y *httpexpect.Value) bool {
  1426. // return x.Number().Raw() < y.Number().Raw()
  1427. // }) // succeeds
  1428. func (a *Array) NotOrdered(less ...func(x, y *Value) bool) *Array {
  1429. opChain := a.chain.enter("NotOrdered()")
  1430. defer opChain.leave()
  1431. if opChain.failed() {
  1432. return a
  1433. }
  1434. if len(less) > 1 {
  1435. opChain.fail(AssertionFailure{
  1436. Type: AssertUsage,
  1437. Errors: []error{
  1438. errors.New("unexpected multiple less arguments"),
  1439. },
  1440. })
  1441. return a
  1442. }
  1443. var lessFn func(x, y *Value) bool
  1444. if len(less) == 1 {
  1445. lessFn = less[0]
  1446. if lessFn == nil {
  1447. opChain.fail(AssertionFailure{
  1448. Type: AssertUsage,
  1449. Errors: []error{
  1450. errors.New("unexpected nil less argument"),
  1451. },
  1452. })
  1453. return a
  1454. }
  1455. } else {
  1456. lessFn = builtinComparator(opChain, a.value)
  1457. if lessFn == nil {
  1458. return a
  1459. }
  1460. }
  1461. if len(a.value) <= 1 {
  1462. return a
  1463. }
  1464. ordered := true
  1465. for i := 0; i < len(a.value)-1; i++ {
  1466. func() {
  1467. xChain := opChain.replace("IsOrdered[%d]", i)
  1468. defer xChain.leave()
  1469. yChain := opChain.replace("IsOrdered[%d]", i+1)
  1470. defer yChain.leave()
  1471. x := newValue(xChain, a.value[i])
  1472. y := newValue(yChain, a.value[i+1])
  1473. if lessFn(y, x) {
  1474. ordered = false
  1475. }
  1476. }()
  1477. if opChain.failed() {
  1478. return a
  1479. }
  1480. if !ordered {
  1481. break
  1482. }
  1483. }
  1484. if ordered {
  1485. opChain.fail(AssertionFailure{
  1486. Type: AssertValid,
  1487. Actual: &AssertionValue{a.value},
  1488. Errors: []error{
  1489. errors.New("expected: array is not ordered, but it is"),
  1490. },
  1491. })
  1492. }
  1493. return a
  1494. }
  1495. func countElement(array []interface{}, element interface{}) int {
  1496. count := 0
  1497. for _, e := range array {
  1498. if reflect.DeepEqual(element, e) {
  1499. count++
  1500. }
  1501. }
  1502. return count
  1503. }
  1504. func builtinComparator(opChain *chain, array []interface{}) func(x, y *Value) bool {
  1505. var prev interface{}
  1506. for index, curr := range array {
  1507. switch curr.(type) {
  1508. case bool, float64, string, nil:
  1509. // ok, do nothing
  1510. default:
  1511. opChain.fail(AssertionFailure{
  1512. Type: AssertBelongs,
  1513. Actual: &AssertionValue{
  1514. unquotedType(fmt.Sprintf("%T", curr)),
  1515. },
  1516. Expected: &AssertionValue{AssertionList{
  1517. unquotedType("Boolean (bool)"),
  1518. unquotedType("Number (int*, uint*, float*)"),
  1519. unquotedType("String (string)"),
  1520. unquotedType("Null (nil)"),
  1521. }},
  1522. Reference: &AssertionValue{
  1523. array,
  1524. },
  1525. Errors: []error{
  1526. errors.New("expected: type of each element of reference array" +
  1527. " belongs to allowed list"),
  1528. fmt.Errorf("element %v has disallowed type %T", index, curr),
  1529. },
  1530. })
  1531. return nil
  1532. }
  1533. if index > 0 && fmt.Sprintf("%T", curr) != fmt.Sprintf("%T", prev) {
  1534. opChain.fail(AssertionFailure{
  1535. Type: AssertEqual,
  1536. Actual: &AssertionValue{
  1537. unquotedType(fmt.Sprintf("%T (type of element %v)", curr, index)),
  1538. },
  1539. Expected: &AssertionValue{
  1540. unquotedType(fmt.Sprintf("%T (type of element %v)", prev, index-1)),
  1541. },
  1542. Reference: &AssertionValue{
  1543. array,
  1544. },
  1545. Errors: []error{
  1546. errors.New("expected:" +
  1547. " types of all elements of reference array are the same"),
  1548. fmt.Errorf("element %v has type %T, but element %v has type %T",
  1549. index-1, prev, index, curr),
  1550. },
  1551. })
  1552. return nil
  1553. }
  1554. prev = curr
  1555. }
  1556. if len(array) > 1 {
  1557. switch array[0].(type) {
  1558. case bool:
  1559. return func(x, y *Value) bool {
  1560. xVal := x.Raw().(bool)
  1561. yVal := y.Raw().(bool)
  1562. return (!xVal && yVal)
  1563. }
  1564. case float64:
  1565. return func(x, y *Value) bool {
  1566. xVal := x.Raw().(float64)
  1567. yVal := y.Raw().(float64)
  1568. return xVal < yVal
  1569. }
  1570. case string:
  1571. return func(x, y *Value) bool {
  1572. xVal := x.Raw().(string)
  1573. yVal := y.Raw().(string)
  1574. return xVal < yVal
  1575. }
  1576. case nil:
  1577. return func(x, y *Value) bool {
  1578. // `nil` is never less than `nil`
  1579. return false
  1580. }
  1581. }
  1582. }
  1583. return nil
  1584. }
  1585. type unquotedType string
  1586. func (t unquotedType) String() string {
  1587. return string(t)
  1588. }