forward_assertions_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. package assert
  2. import (
  3. "errors"
  4. "regexp"
  5. "testing"
  6. "time"
  7. )
  8. func TestImplementsWrapper(t *testing.T) {
  9. assert := New(new(testing.T))
  10. if !assert.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
  11. t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface")
  12. }
  13. if assert.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
  14. t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")
  15. }
  16. }
  17. func TestIsTypeWrapper(t *testing.T) {
  18. assert := New(new(testing.T))
  19. if !assert.IsType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
  20. t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")
  21. }
  22. if assert.IsType(new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {
  23. t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")
  24. }
  25. }
  26. func TestEqualWrapper(t *testing.T) {
  27. assert := New(new(testing.T))
  28. if !assert.Equal("Hello World", "Hello World") {
  29. t.Error("Equal should return true")
  30. }
  31. if !assert.Equal(123, 123) {
  32. t.Error("Equal should return true")
  33. }
  34. if !assert.Equal(123.5, 123.5) {
  35. t.Error("Equal should return true")
  36. }
  37. if !assert.Equal([]byte("Hello World"), []byte("Hello World")) {
  38. t.Error("Equal should return true")
  39. }
  40. if !assert.Equal(nil, nil) {
  41. t.Error("Equal should return true")
  42. }
  43. }
  44. func TestEqualValuesWrapper(t *testing.T) {
  45. assert := New(new(testing.T))
  46. if !assert.EqualValues(uint32(10), int32(10)) {
  47. t.Error("EqualValues should return true")
  48. }
  49. }
  50. func TestNotNilWrapper(t *testing.T) {
  51. assert := New(new(testing.T))
  52. if !assert.NotNil(new(AssertionTesterConformingObject)) {
  53. t.Error("NotNil should return true: object is not nil")
  54. }
  55. if assert.NotNil(nil) {
  56. t.Error("NotNil should return false: object is nil")
  57. }
  58. }
  59. func TestNilWrapper(t *testing.T) {
  60. assert := New(new(testing.T))
  61. if !assert.Nil(nil) {
  62. t.Error("Nil should return true: object is nil")
  63. }
  64. if assert.Nil(new(AssertionTesterConformingObject)) {
  65. t.Error("Nil should return false: object is not nil")
  66. }
  67. }
  68. func TestTrueWrapper(t *testing.T) {
  69. assert := New(new(testing.T))
  70. if !assert.True(true) {
  71. t.Error("True should return true")
  72. }
  73. if assert.True(false) {
  74. t.Error("True should return false")
  75. }
  76. }
  77. func TestFalseWrapper(t *testing.T) {
  78. assert := New(new(testing.T))
  79. if !assert.False(false) {
  80. t.Error("False should return true")
  81. }
  82. if assert.False(true) {
  83. t.Error("False should return false")
  84. }
  85. }
  86. func TestExactlyWrapper(t *testing.T) {
  87. assert := New(new(testing.T))
  88. a := float32(1)
  89. b := float64(1)
  90. c := float32(1)
  91. d := float32(2)
  92. if assert.Exactly(a, b) {
  93. t.Error("Exactly should return false")
  94. }
  95. if assert.Exactly(a, d) {
  96. t.Error("Exactly should return false")
  97. }
  98. if !assert.Exactly(a, c) {
  99. t.Error("Exactly should return true")
  100. }
  101. if assert.Exactly(nil, a) {
  102. t.Error("Exactly should return false")
  103. }
  104. if assert.Exactly(a, nil) {
  105. t.Error("Exactly should return false")
  106. }
  107. }
  108. func TestNotEqualWrapper(t *testing.T) {
  109. assert := New(new(testing.T))
  110. if !assert.NotEqual("Hello World", "Hello World!") {
  111. t.Error("NotEqual should return true")
  112. }
  113. if !assert.NotEqual(123, 1234) {
  114. t.Error("NotEqual should return true")
  115. }
  116. if !assert.NotEqual(123.5, 123.55) {
  117. t.Error("NotEqual should return true")
  118. }
  119. if !assert.NotEqual([]byte("Hello World"), []byte("Hello World!")) {
  120. t.Error("NotEqual should return true")
  121. }
  122. if !assert.NotEqual(nil, new(AssertionTesterConformingObject)) {
  123. t.Error("NotEqual should return true")
  124. }
  125. }
  126. func TestContainsWrapper(t *testing.T) {
  127. assert := New(new(testing.T))
  128. list := []string{"Foo", "Bar"}
  129. if !assert.Contains("Hello World", "Hello") {
  130. t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
  131. }
  132. if assert.Contains("Hello World", "Salut") {
  133. t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
  134. }
  135. if !assert.Contains(list, "Foo") {
  136. t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
  137. }
  138. if assert.Contains(list, "Salut") {
  139. t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
  140. }
  141. }
  142. func TestNotContainsWrapper(t *testing.T) {
  143. assert := New(new(testing.T))
  144. list := []string{"Foo", "Bar"}
  145. if !assert.NotContains("Hello World", "Hello!") {
  146. t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
  147. }
  148. if assert.NotContains("Hello World", "Hello") {
  149. t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
  150. }
  151. if !assert.NotContains(list, "Foo!") {
  152. t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"")
  153. }
  154. if assert.NotContains(list, "Foo") {
  155. t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
  156. }
  157. }
  158. func TestConditionWrapper(t *testing.T) {
  159. assert := New(new(testing.T))
  160. if !assert.Condition(func() bool { return true }, "Truth") {
  161. t.Error("Condition should return true")
  162. }
  163. if assert.Condition(func() bool { return false }, "Lie") {
  164. t.Error("Condition should return false")
  165. }
  166. }
  167. func TestDidPanicWrapper(t *testing.T) {
  168. if funcDidPanic, _ := didPanic(func() {
  169. panic("Panic!")
  170. }); !funcDidPanic {
  171. t.Error("didPanic should return true")
  172. }
  173. if funcDidPanic, _ := didPanic(func() {
  174. }); funcDidPanic {
  175. t.Error("didPanic should return false")
  176. }
  177. }
  178. func TestPanicsWrapper(t *testing.T) {
  179. assert := New(new(testing.T))
  180. if !assert.Panics(func() {
  181. panic("Panic!")
  182. }) {
  183. t.Error("Panics should return true")
  184. }
  185. if assert.Panics(func() {
  186. }) {
  187. t.Error("Panics should return false")
  188. }
  189. }
  190. func TestNotPanicsWrapper(t *testing.T) {
  191. assert := New(new(testing.T))
  192. if !assert.NotPanics(func() {
  193. }) {
  194. t.Error("NotPanics should return true")
  195. }
  196. if assert.NotPanics(func() {
  197. panic("Panic!")
  198. }) {
  199. t.Error("NotPanics should return false")
  200. }
  201. }
  202. func TestNoErrorWrapper(t *testing.T) {
  203. assert := New(t)
  204. mockAssert := New(new(testing.T))
  205. // start with a nil error
  206. var err error
  207. assert.True(mockAssert.NoError(err), "NoError should return True for nil arg")
  208. // now set an error
  209. err = errors.New("Some error")
  210. assert.False(mockAssert.NoError(err), "NoError with error should return False")
  211. }
  212. func TestErrorWrapper(t *testing.T) {
  213. assert := New(t)
  214. mockAssert := New(new(testing.T))
  215. // start with a nil error
  216. var err error
  217. assert.False(mockAssert.Error(err), "Error should return False for nil arg")
  218. // now set an error
  219. err = errors.New("Some error")
  220. assert.True(mockAssert.Error(err), "Error with error should return True")
  221. }
  222. func TestEqualErrorWrapper(t *testing.T) {
  223. assert := New(t)
  224. mockAssert := New(new(testing.T))
  225. // start with a nil error
  226. var err error
  227. assert.False(mockAssert.EqualError(err, ""),
  228. "EqualError should return false for nil arg")
  229. // now set an error
  230. err = errors.New("some error")
  231. assert.False(mockAssert.EqualError(err, "Not some error"),
  232. "EqualError should return false for different error string")
  233. assert.True(mockAssert.EqualError(err, "some error"),
  234. "EqualError should return true")
  235. }
  236. func TestEmptyWrapper(t *testing.T) {
  237. assert := New(t)
  238. mockAssert := New(new(testing.T))
  239. assert.True(mockAssert.Empty(""), "Empty string is empty")
  240. assert.True(mockAssert.Empty(nil), "Nil is empty")
  241. assert.True(mockAssert.Empty([]string{}), "Empty string array is empty")
  242. assert.True(mockAssert.Empty(0), "Zero int value is empty")
  243. assert.True(mockAssert.Empty(false), "False value is empty")
  244. assert.False(mockAssert.Empty("something"), "Non Empty string is not empty")
  245. assert.False(mockAssert.Empty(errors.New("something")), "Non nil object is not empty")
  246. assert.False(mockAssert.Empty([]string{"something"}), "Non empty string array is not empty")
  247. assert.False(mockAssert.Empty(1), "Non-zero int value is not empty")
  248. assert.False(mockAssert.Empty(true), "True value is not empty")
  249. }
  250. func TestNotEmptyWrapper(t *testing.T) {
  251. assert := New(t)
  252. mockAssert := New(new(testing.T))
  253. assert.False(mockAssert.NotEmpty(""), "Empty string is empty")
  254. assert.False(mockAssert.NotEmpty(nil), "Nil is empty")
  255. assert.False(mockAssert.NotEmpty([]string{}), "Empty string array is empty")
  256. assert.False(mockAssert.NotEmpty(0), "Zero int value is empty")
  257. assert.False(mockAssert.NotEmpty(false), "False value is empty")
  258. assert.True(mockAssert.NotEmpty("something"), "Non Empty string is not empty")
  259. assert.True(mockAssert.NotEmpty(errors.New("something")), "Non nil object is not empty")
  260. assert.True(mockAssert.NotEmpty([]string{"something"}), "Non empty string array is not empty")
  261. assert.True(mockAssert.NotEmpty(1), "Non-zero int value is not empty")
  262. assert.True(mockAssert.NotEmpty(true), "True value is not empty")
  263. }
  264. func TestLenWrapper(t *testing.T) {
  265. assert := New(t)
  266. mockAssert := New(new(testing.T))
  267. assert.False(mockAssert.Len(nil, 0), "nil does not have length")
  268. assert.False(mockAssert.Len(0, 0), "int does not have length")
  269. assert.False(mockAssert.Len(true, 0), "true does not have length")
  270. assert.False(mockAssert.Len(false, 0), "false does not have length")
  271. assert.False(mockAssert.Len('A', 0), "Rune does not have length")
  272. assert.False(mockAssert.Len(struct{}{}, 0), "Struct does not have length")
  273. ch := make(chan int, 5)
  274. ch <- 1
  275. ch <- 2
  276. ch <- 3
  277. cases := []struct {
  278. v interface{}
  279. l int
  280. }{
  281. {[]int{1, 2, 3}, 3},
  282. {[...]int{1, 2, 3}, 3},
  283. {"ABC", 3},
  284. {map[int]int{1: 2, 2: 4, 3: 6}, 3},
  285. {ch, 3},
  286. {[]int{}, 0},
  287. {map[int]int{}, 0},
  288. {make(chan int), 0},
  289. {[]int(nil), 0},
  290. {map[int]int(nil), 0},
  291. {(chan int)(nil), 0},
  292. }
  293. for _, c := range cases {
  294. assert.True(mockAssert.Len(c.v, c.l), "%#v have %d items", c.v, c.l)
  295. }
  296. }
  297. func TestWithinDurationWrapper(t *testing.T) {
  298. assert := New(t)
  299. mockAssert := New(new(testing.T))
  300. a := time.Now()
  301. b := a.Add(10 * time.Second)
  302. assert.True(mockAssert.WithinDuration(a, b, 10*time.Second), "A 10s difference is within a 10s time difference")
  303. assert.True(mockAssert.WithinDuration(b, a, 10*time.Second), "A 10s difference is within a 10s time difference")
  304. assert.False(mockAssert.WithinDuration(a, b, 9*time.Second), "A 10s difference is not within a 9s time difference")
  305. assert.False(mockAssert.WithinDuration(b, a, 9*time.Second), "A 10s difference is not within a 9s time difference")
  306. assert.False(mockAssert.WithinDuration(a, b, -9*time.Second), "A 10s difference is not within a 9s time difference")
  307. assert.False(mockAssert.WithinDuration(b, a, -9*time.Second), "A 10s difference is not within a 9s time difference")
  308. assert.False(mockAssert.WithinDuration(a, b, -11*time.Second), "A 10s difference is not within a 9s time difference")
  309. assert.False(mockAssert.WithinDuration(b, a, -11*time.Second), "A 10s difference is not within a 9s time difference")
  310. }
  311. func TestInDeltaWrapper(t *testing.T) {
  312. assert := New(new(testing.T))
  313. True(t, assert.InDelta(1.001, 1, 0.01), "|1.001 - 1| <= 0.01")
  314. True(t, assert.InDelta(1, 1.001, 0.01), "|1 - 1.001| <= 0.01")
  315. True(t, assert.InDelta(1, 2, 1), "|1 - 2| <= 1")
  316. False(t, assert.InDelta(1, 2, 0.5), "Expected |1 - 2| <= 0.5 to fail")
  317. False(t, assert.InDelta(2, 1, 0.5), "Expected |2 - 1| <= 0.5 to fail")
  318. False(t, assert.InDelta("", nil, 1), "Expected non numerals to fail")
  319. cases := []struct {
  320. a, b interface{}
  321. delta float64
  322. }{
  323. {uint8(2), uint8(1), 1},
  324. {uint16(2), uint16(1), 1},
  325. {uint32(2), uint32(1), 1},
  326. {uint64(2), uint64(1), 1},
  327. {int(2), int(1), 1},
  328. {int8(2), int8(1), 1},
  329. {int16(2), int16(1), 1},
  330. {int32(2), int32(1), 1},
  331. {int64(2), int64(1), 1},
  332. {float32(2), float32(1), 1},
  333. {float64(2), float64(1), 1},
  334. }
  335. for _, tc := range cases {
  336. True(t, assert.InDelta(tc.a, tc.b, tc.delta), "Expected |%V - %V| <= %v", tc.a, tc.b, tc.delta)
  337. }
  338. }
  339. func TestInEpsilonWrapper(t *testing.T) {
  340. assert := New(new(testing.T))
  341. cases := []struct {
  342. a, b interface{}
  343. epsilon float64
  344. }{
  345. {uint8(2), uint16(2), .001},
  346. {2.1, 2.2, 0.1},
  347. {2.2, 2.1, 0.1},
  348. {-2.1, -2.2, 0.1},
  349. {-2.2, -2.1, 0.1},
  350. {uint64(100), uint8(101), 0.01},
  351. {0.1, -0.1, 2},
  352. }
  353. for _, tc := range cases {
  354. True(t, assert.InEpsilon(tc.a, tc.b, tc.epsilon, "Expected %V and %V to have a relative difference of %v", tc.a, tc.b, tc.epsilon))
  355. }
  356. cases = []struct {
  357. a, b interface{}
  358. epsilon float64
  359. }{
  360. {uint8(2), int16(-2), .001},
  361. {uint64(100), uint8(102), 0.01},
  362. {2.1, 2.2, 0.001},
  363. {2.2, 2.1, 0.001},
  364. {2.1, -2.2, 1},
  365. {2.1, "bla-bla", 0},
  366. {0.1, -0.1, 1.99},
  367. }
  368. for _, tc := range cases {
  369. False(t, assert.InEpsilon(tc.a, tc.b, tc.epsilon, "Expected %V and %V to have a relative difference of %v", tc.a, tc.b, tc.epsilon))
  370. }
  371. }
  372. func TestRegexpWrapper(t *testing.T) {
  373. assert := New(new(testing.T))
  374. cases := []struct {
  375. rx, str string
  376. }{
  377. {"^start", "start of the line"},
  378. {"end$", "in the end"},
  379. {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12.34"},
  380. }
  381. for _, tc := range cases {
  382. True(t, assert.Regexp(tc.rx, tc.str))
  383. True(t, assert.Regexp(regexp.MustCompile(tc.rx), tc.str))
  384. False(t, assert.NotRegexp(tc.rx, tc.str))
  385. False(t, assert.NotRegexp(regexp.MustCompile(tc.rx), tc.str))
  386. }
  387. cases = []struct {
  388. rx, str string
  389. }{
  390. {"^asdfastart", "Not the start of the line"},
  391. {"end$", "in the end."},
  392. {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12a.34"},
  393. }
  394. for _, tc := range cases {
  395. False(t, assert.Regexp(tc.rx, tc.str), "Expected \"%s\" to not match \"%s\"", tc.rx, tc.str)
  396. False(t, assert.Regexp(regexp.MustCompile(tc.rx), tc.str))
  397. True(t, assert.NotRegexp(tc.rx, tc.str))
  398. True(t, assert.NotRegexp(regexp.MustCompile(tc.rx), tc.str))
  399. }
  400. }
  401. func TestZeroWrapper(t *testing.T) {
  402. assert := New(t)
  403. mockAssert := New(new(testing.T))
  404. for _, test := range zeros {
  405. assert.True(mockAssert.Zero(test), "Zero should return true for %v", test)
  406. }
  407. for _, test := range nonZeros {
  408. assert.False(mockAssert.Zero(test), "Zero should return false for %v", test)
  409. }
  410. }
  411. func TestNotZeroWrapper(t *testing.T) {
  412. assert := New(t)
  413. mockAssert := New(new(testing.T))
  414. for _, test := range zeros {
  415. assert.False(mockAssert.NotZero(test), "Zero should return true for %v", test)
  416. }
  417. for _, test := range nonZeros {
  418. assert.True(mockAssert.NotZero(test), "Zero should return false for %v", test)
  419. }
  420. }
  421. func TestJSONEqWrapper_EqualSONString(t *testing.T) {
  422. assert := New(new(testing.T))
  423. if !assert.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`) {
  424. t.Error("JSONEq should return true")
  425. }
  426. }
  427. func TestJSONEqWrapper_EquivalentButNotEqual(t *testing.T) {
  428. assert := New(new(testing.T))
  429. if !assert.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) {
  430. t.Error("JSONEq should return true")
  431. }
  432. }
  433. func TestJSONEqWrapper_HashOfArraysAndHashes(t *testing.T) {
  434. assert := New(new(testing.T))
  435. if !assert.JSONEq("{\r\n\t\"numeric\": 1.5,\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]],\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\"\r\n}",
  436. "{\r\n\t\"numeric\": 1.5,\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\",\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]]\r\n}") {
  437. t.Error("JSONEq should return true")
  438. }
  439. }
  440. func TestJSONEqWrapper_Array(t *testing.T) {
  441. assert := New(new(testing.T))
  442. if !assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`) {
  443. t.Error("JSONEq should return true")
  444. }
  445. }
  446. func TestJSONEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
  447. assert := New(new(testing.T))
  448. if assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`) {
  449. t.Error("JSONEq should return false")
  450. }
  451. }
  452. func TestJSONEqWrapper_HashesNotEquivalent(t *testing.T) {
  453. assert := New(new(testing.T))
  454. if assert.JSONEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) {
  455. t.Error("JSONEq should return false")
  456. }
  457. }
  458. func TestJSONEqWrapper_ActualIsNotJSON(t *testing.T) {
  459. assert := New(new(testing.T))
  460. if assert.JSONEq(`{"foo": "bar"}`, "Not JSON") {
  461. t.Error("JSONEq should return false")
  462. }
  463. }
  464. func TestJSONEqWrapper_ExpectedIsNotJSON(t *testing.T) {
  465. assert := New(new(testing.T))
  466. if assert.JSONEq("Not JSON", `{"foo": "bar", "hello": "world"}`) {
  467. t.Error("JSONEq should return false")
  468. }
  469. }
  470. func TestJSONEqWrapper_ExpectedAndActualNotJSON(t *testing.T) {
  471. assert := New(new(testing.T))
  472. if assert.JSONEq("Not JSON", "Not JSON") {
  473. t.Error("JSONEq should return false")
  474. }
  475. }
  476. func TestJSONEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
  477. assert := New(new(testing.T))
  478. if assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`) {
  479. t.Error("JSONEq should return false")
  480. }
  481. }