1
0

forward_requirements_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. package require
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. )
  7. func TestImplementsWrapper(t *testing.T) {
  8. require := New(t)
  9. require.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject))
  10. mockT := new(MockT)
  11. mockRequire := New(mockT)
  12. mockRequire.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject))
  13. if !mockT.Failed {
  14. t.Error("Check should fail")
  15. }
  16. }
  17. func TestIsTypeWrapper(t *testing.T) {
  18. require := New(t)
  19. require.IsType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject))
  20. mockT := new(MockT)
  21. mockRequire := New(mockT)
  22. mockRequire.IsType(new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject))
  23. if !mockT.Failed {
  24. t.Error("Check should fail")
  25. }
  26. }
  27. func TestEqualWrapper(t *testing.T) {
  28. require := New(t)
  29. require.Equal(1, 1)
  30. mockT := new(MockT)
  31. mockRequire := New(mockT)
  32. mockRequire.Equal(1, 2)
  33. if !mockT.Failed {
  34. t.Error("Check should fail")
  35. }
  36. }
  37. func TestNotEqualWrapper(t *testing.T) {
  38. require := New(t)
  39. require.NotEqual(1, 2)
  40. mockT := new(MockT)
  41. mockRequire := New(mockT)
  42. mockRequire.NotEqual(2, 2)
  43. if !mockT.Failed {
  44. t.Error("Check should fail")
  45. }
  46. }
  47. func TestExactlyWrapper(t *testing.T) {
  48. require := New(t)
  49. a := float32(1)
  50. b := float32(1)
  51. c := float64(1)
  52. require.Exactly(a, b)
  53. mockT := new(MockT)
  54. mockRequire := New(mockT)
  55. mockRequire.Exactly(a, c)
  56. if !mockT.Failed {
  57. t.Error("Check should fail")
  58. }
  59. }
  60. func TestNotNilWrapper(t *testing.T) {
  61. require := New(t)
  62. require.NotNil(t, new(AssertionTesterConformingObject))
  63. mockT := new(MockT)
  64. mockRequire := New(mockT)
  65. mockRequire.NotNil(nil)
  66. if !mockT.Failed {
  67. t.Error("Check should fail")
  68. }
  69. }
  70. func TestNilWrapper(t *testing.T) {
  71. require := New(t)
  72. require.Nil(nil)
  73. mockT := new(MockT)
  74. mockRequire := New(mockT)
  75. mockRequire.Nil(new(AssertionTesterConformingObject))
  76. if !mockT.Failed {
  77. t.Error("Check should fail")
  78. }
  79. }
  80. func TestTrueWrapper(t *testing.T) {
  81. require := New(t)
  82. require.True(true)
  83. mockT := new(MockT)
  84. mockRequire := New(mockT)
  85. mockRequire.True(false)
  86. if !mockT.Failed {
  87. t.Error("Check should fail")
  88. }
  89. }
  90. func TestFalseWrapper(t *testing.T) {
  91. require := New(t)
  92. require.False(false)
  93. mockT := new(MockT)
  94. mockRequire := New(mockT)
  95. mockRequire.False(true)
  96. if !mockT.Failed {
  97. t.Error("Check should fail")
  98. }
  99. }
  100. func TestContainsWrapper(t *testing.T) {
  101. require := New(t)
  102. require.Contains("Hello World", "Hello")
  103. mockT := new(MockT)
  104. mockRequire := New(mockT)
  105. mockRequire.Contains("Hello World", "Salut")
  106. if !mockT.Failed {
  107. t.Error("Check should fail")
  108. }
  109. }
  110. func TestNotContainsWrapper(t *testing.T) {
  111. require := New(t)
  112. require.NotContains("Hello World", "Hello!")
  113. mockT := new(MockT)
  114. mockRequire := New(mockT)
  115. mockRequire.NotContains("Hello World", "Hello")
  116. if !mockT.Failed {
  117. t.Error("Check should fail")
  118. }
  119. }
  120. func TestPanicsWrapper(t *testing.T) {
  121. require := New(t)
  122. require.Panics(func() {
  123. panic("Panic!")
  124. })
  125. mockT := new(MockT)
  126. mockRequire := New(mockT)
  127. mockRequire.Panics(func() {})
  128. if !mockT.Failed {
  129. t.Error("Check should fail")
  130. }
  131. }
  132. func TestNotPanicsWrapper(t *testing.T) {
  133. require := New(t)
  134. require.NotPanics(func() {})
  135. mockT := new(MockT)
  136. mockRequire := New(mockT)
  137. mockRequire.NotPanics(func() {
  138. panic("Panic!")
  139. })
  140. if !mockT.Failed {
  141. t.Error("Check should fail")
  142. }
  143. }
  144. func TestNoErrorWrapper(t *testing.T) {
  145. require := New(t)
  146. require.NoError(nil)
  147. mockT := new(MockT)
  148. mockRequire := New(mockT)
  149. mockRequire.NoError(errors.New("some error"))
  150. if !mockT.Failed {
  151. t.Error("Check should fail")
  152. }
  153. }
  154. func TestErrorWrapper(t *testing.T) {
  155. require := New(t)
  156. require.Error(errors.New("some error"))
  157. mockT := new(MockT)
  158. mockRequire := New(mockT)
  159. mockRequire.Error(nil)
  160. if !mockT.Failed {
  161. t.Error("Check should fail")
  162. }
  163. }
  164. func TestEqualErrorWrapper(t *testing.T) {
  165. require := New(t)
  166. require.EqualError(errors.New("some error"), "some error")
  167. mockT := new(MockT)
  168. mockRequire := New(mockT)
  169. mockRequire.EqualError(errors.New("some error"), "Not some error")
  170. if !mockT.Failed {
  171. t.Error("Check should fail")
  172. }
  173. }
  174. func TestEmptyWrapper(t *testing.T) {
  175. require := New(t)
  176. require.Empty("")
  177. mockT := new(MockT)
  178. mockRequire := New(mockT)
  179. mockRequire.Empty("x")
  180. if !mockT.Failed {
  181. t.Error("Check should fail")
  182. }
  183. }
  184. func TestNotEmptyWrapper(t *testing.T) {
  185. require := New(t)
  186. require.NotEmpty("x")
  187. mockT := new(MockT)
  188. mockRequire := New(mockT)
  189. mockRequire.NotEmpty("")
  190. if !mockT.Failed {
  191. t.Error("Check should fail")
  192. }
  193. }
  194. func TestWithinDurationWrapper(t *testing.T) {
  195. require := New(t)
  196. a := time.Now()
  197. b := a.Add(10 * time.Second)
  198. require.WithinDuration(a, b, 15*time.Second)
  199. mockT := new(MockT)
  200. mockRequire := New(mockT)
  201. mockRequire.WithinDuration(a, b, 5*time.Second)
  202. if !mockT.Failed {
  203. t.Error("Check should fail")
  204. }
  205. }
  206. func TestInDeltaWrapper(t *testing.T) {
  207. require := New(t)
  208. require.InDelta(1.001, 1, 0.01)
  209. mockT := new(MockT)
  210. mockRequire := New(mockT)
  211. mockRequire.InDelta(1, 2, 0.5)
  212. if !mockT.Failed {
  213. t.Error("Check should fail")
  214. }
  215. }
  216. func TestZeroWrapper(t *testing.T) {
  217. require := New(t)
  218. require.Zero(0)
  219. mockT := new(MockT)
  220. mockRequire := New(mockT)
  221. mockRequire.Zero(1)
  222. if !mockT.Failed {
  223. t.Error("Check should fail")
  224. }
  225. }
  226. func TestNotZeroWrapper(t *testing.T) {
  227. require := New(t)
  228. require.NotZero(1)
  229. mockT := new(MockT)
  230. mockRequire := New(mockT)
  231. mockRequire.NotZero(0)
  232. if !mockT.Failed {
  233. t.Error("Check should fail")
  234. }
  235. }
  236. func TestJSONEqWrapper_EqualSONString(t *testing.T) {
  237. mockT := new(MockT)
  238. mockRequire := New(mockT)
  239. mockRequire.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
  240. if mockT.Failed {
  241. t.Error("Check should pass")
  242. }
  243. }
  244. func TestJSONEqWrapper_EquivalentButNotEqual(t *testing.T) {
  245. mockT := new(MockT)
  246. mockRequire := New(mockT)
  247. mockRequire.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
  248. if mockT.Failed {
  249. t.Error("Check should pass")
  250. }
  251. }
  252. func TestJSONEqWrapper_HashOfArraysAndHashes(t *testing.T) {
  253. mockT := new(MockT)
  254. mockRequire := New(mockT)
  255. mockRequire.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}",
  256. "{\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}")
  257. if mockT.Failed {
  258. t.Error("Check should pass")
  259. }
  260. }
  261. func TestJSONEqWrapper_Array(t *testing.T) {
  262. mockT := new(MockT)
  263. mockRequire := New(mockT)
  264. mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
  265. if mockT.Failed {
  266. t.Error("Check should pass")
  267. }
  268. }
  269. func TestJSONEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
  270. mockT := new(MockT)
  271. mockRequire := New(mockT)
  272. mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
  273. if !mockT.Failed {
  274. t.Error("Check should fail")
  275. }
  276. }
  277. func TestJSONEqWrapper_HashesNotEquivalent(t *testing.T) {
  278. mockT := new(MockT)
  279. mockRequire := New(mockT)
  280. mockRequire.JSONEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
  281. if !mockT.Failed {
  282. t.Error("Check should fail")
  283. }
  284. }
  285. func TestJSONEqWrapper_ActualIsNotJSON(t *testing.T) {
  286. mockT := new(MockT)
  287. mockRequire := New(mockT)
  288. mockRequire.JSONEq(`{"foo": "bar"}`, "Not JSON")
  289. if !mockT.Failed {
  290. t.Error("Check should fail")
  291. }
  292. }
  293. func TestJSONEqWrapper_ExpectedIsNotJSON(t *testing.T) {
  294. mockT := new(MockT)
  295. mockRequire := New(mockT)
  296. mockRequire.JSONEq("Not JSON", `{"foo": "bar", "hello": "world"}`)
  297. if !mockT.Failed {
  298. t.Error("Check should fail")
  299. }
  300. }
  301. func TestJSONEqWrapper_ExpectedAndActualNotJSON(t *testing.T) {
  302. mockT := new(MockT)
  303. mockRequire := New(mockT)
  304. mockRequire.JSONEq("Not JSON", "Not JSON")
  305. if !mockT.Failed {
  306. t.Error("Check should fail")
  307. }
  308. }
  309. func TestJSONEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
  310. mockT := new(MockT)
  311. mockRequire := New(mockT)
  312. mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
  313. if !mockT.Failed {
  314. t.Error("Check should fail")
  315. }
  316. }