requirements_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package require
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. )
  7. // AssertionTesterInterface defines an interface to be used for testing assertion methods
  8. type AssertionTesterInterface interface {
  9. TestMethod()
  10. }
  11. // AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface
  12. type AssertionTesterConformingObject struct {
  13. }
  14. func (a *AssertionTesterConformingObject) TestMethod() {
  15. }
  16. // AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface
  17. type AssertionTesterNonConformingObject struct {
  18. }
  19. type MockT struct {
  20. Failed bool
  21. }
  22. func (t *MockT) FailNow() {
  23. t.Failed = true
  24. }
  25. func (t *MockT) Errorf(format string, args ...interface{}) {
  26. _, _ = format, args
  27. }
  28. func TestImplements(t *testing.T) {
  29. Implements(t, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject))
  30. mockT := new(MockT)
  31. Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject))
  32. if !mockT.Failed {
  33. t.Error("Check should fail")
  34. }
  35. }
  36. func TestIsType(t *testing.T) {
  37. IsType(t, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject))
  38. mockT := new(MockT)
  39. IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject))
  40. if !mockT.Failed {
  41. t.Error("Check should fail")
  42. }
  43. }
  44. func TestEqual(t *testing.T) {
  45. Equal(t, 1, 1)
  46. mockT := new(MockT)
  47. Equal(mockT, 1, 2)
  48. if !mockT.Failed {
  49. t.Error("Check should fail")
  50. }
  51. }
  52. func TestNotEqual(t *testing.T) {
  53. NotEqual(t, 1, 2)
  54. mockT := new(MockT)
  55. NotEqual(mockT, 2, 2)
  56. if !mockT.Failed {
  57. t.Error("Check should fail")
  58. }
  59. }
  60. func TestExactly(t *testing.T) {
  61. a := float32(1)
  62. b := float32(1)
  63. c := float64(1)
  64. Exactly(t, a, b)
  65. mockT := new(MockT)
  66. Exactly(mockT, a, c)
  67. if !mockT.Failed {
  68. t.Error("Check should fail")
  69. }
  70. }
  71. func TestNotNil(t *testing.T) {
  72. NotNil(t, new(AssertionTesterConformingObject))
  73. mockT := new(MockT)
  74. NotNil(mockT, nil)
  75. if !mockT.Failed {
  76. t.Error("Check should fail")
  77. }
  78. }
  79. func TestNil(t *testing.T) {
  80. Nil(t, nil)
  81. mockT := new(MockT)
  82. Nil(mockT, new(AssertionTesterConformingObject))
  83. if !mockT.Failed {
  84. t.Error("Check should fail")
  85. }
  86. }
  87. func TestTrue(t *testing.T) {
  88. True(t, true)
  89. mockT := new(MockT)
  90. True(mockT, false)
  91. if !mockT.Failed {
  92. t.Error("Check should fail")
  93. }
  94. }
  95. func TestFalse(t *testing.T) {
  96. False(t, false)
  97. mockT := new(MockT)
  98. False(mockT, true)
  99. if !mockT.Failed {
  100. t.Error("Check should fail")
  101. }
  102. }
  103. func TestContains(t *testing.T) {
  104. Contains(t, "Hello World", "Hello")
  105. mockT := new(MockT)
  106. Contains(mockT, "Hello World", "Salut")
  107. if !mockT.Failed {
  108. t.Error("Check should fail")
  109. }
  110. }
  111. func TestNotContains(t *testing.T) {
  112. NotContains(t, "Hello World", "Hello!")
  113. mockT := new(MockT)
  114. NotContains(mockT, "Hello World", "Hello")
  115. if !mockT.Failed {
  116. t.Error("Check should fail")
  117. }
  118. }
  119. func TestPanics(t *testing.T) {
  120. Panics(t, func() {
  121. panic("Panic!")
  122. })
  123. mockT := new(MockT)
  124. Panics(mockT, func() {})
  125. if !mockT.Failed {
  126. t.Error("Check should fail")
  127. }
  128. }
  129. func TestNotPanics(t *testing.T) {
  130. NotPanics(t, func() {})
  131. mockT := new(MockT)
  132. NotPanics(mockT, func() {
  133. panic("Panic!")
  134. })
  135. if !mockT.Failed {
  136. t.Error("Check should fail")
  137. }
  138. }
  139. func TestNoError(t *testing.T) {
  140. NoError(t, nil)
  141. mockT := new(MockT)
  142. NoError(mockT, errors.New("some error"))
  143. if !mockT.Failed {
  144. t.Error("Check should fail")
  145. }
  146. }
  147. func TestError(t *testing.T) {
  148. Error(t, errors.New("some error"))
  149. mockT := new(MockT)
  150. Error(mockT, nil)
  151. if !mockT.Failed {
  152. t.Error("Check should fail")
  153. }
  154. }
  155. func TestEqualError(t *testing.T) {
  156. EqualError(t, errors.New("some error"), "some error")
  157. mockT := new(MockT)
  158. EqualError(mockT, errors.New("some error"), "Not some error")
  159. if !mockT.Failed {
  160. t.Error("Check should fail")
  161. }
  162. }
  163. func TestEmpty(t *testing.T) {
  164. Empty(t, "")
  165. mockT := new(MockT)
  166. Empty(mockT, "x")
  167. if !mockT.Failed {
  168. t.Error("Check should fail")
  169. }
  170. }
  171. func TestNotEmpty(t *testing.T) {
  172. NotEmpty(t, "x")
  173. mockT := new(MockT)
  174. NotEmpty(mockT, "")
  175. if !mockT.Failed {
  176. t.Error("Check should fail")
  177. }
  178. }
  179. func TestWithinDuration(t *testing.T) {
  180. a := time.Now()
  181. b := a.Add(10 * time.Second)
  182. WithinDuration(t, a, b, 15*time.Second)
  183. mockT := new(MockT)
  184. WithinDuration(mockT, a, b, 5*time.Second)
  185. if !mockT.Failed {
  186. t.Error("Check should fail")
  187. }
  188. }
  189. func TestInDelta(t *testing.T) {
  190. InDelta(t, 1.001, 1, 0.01)
  191. mockT := new(MockT)
  192. InDelta(mockT, 1, 2, 0.5)
  193. if !mockT.Failed {
  194. t.Error("Check should fail")
  195. }
  196. }
  197. func TestZero(t *testing.T) {
  198. Zero(t, "")
  199. mockT := new(MockT)
  200. Zero(mockT, "x")
  201. if !mockT.Failed {
  202. t.Error("Check should fail")
  203. }
  204. }
  205. func TestNotZero(t *testing.T) {
  206. NotZero(t, "x")
  207. mockT := new(MockT)
  208. NotZero(mockT, "")
  209. if !mockT.Failed {
  210. t.Error("Check should fail")
  211. }
  212. }
  213. func TestJSONEq_EqualSONString(t *testing.T) {
  214. mockT := new(MockT)
  215. JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
  216. if mockT.Failed {
  217. t.Error("Check should pass")
  218. }
  219. }
  220. func TestJSONEq_EquivalentButNotEqual(t *testing.T) {
  221. mockT := new(MockT)
  222. JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
  223. if mockT.Failed {
  224. t.Error("Check should pass")
  225. }
  226. }
  227. func TestJSONEq_HashOfArraysAndHashes(t *testing.T) {
  228. mockT := new(MockT)
  229. JSONEq(mockT, "{\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}",
  230. "{\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}")
  231. if mockT.Failed {
  232. t.Error("Check should pass")
  233. }
  234. }
  235. func TestJSONEq_Array(t *testing.T) {
  236. mockT := new(MockT)
  237. JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
  238. if mockT.Failed {
  239. t.Error("Check should pass")
  240. }
  241. }
  242. func TestJSONEq_HashAndArrayNotEquivalent(t *testing.T) {
  243. mockT := new(MockT)
  244. JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
  245. if !mockT.Failed {
  246. t.Error("Check should fail")
  247. }
  248. }
  249. func TestJSONEq_HashesNotEquivalent(t *testing.T) {
  250. mockT := new(MockT)
  251. JSONEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
  252. if !mockT.Failed {
  253. t.Error("Check should fail")
  254. }
  255. }
  256. func TestJSONEq_ActualIsNotJSON(t *testing.T) {
  257. mockT := new(MockT)
  258. JSONEq(mockT, `{"foo": "bar"}`, "Not JSON")
  259. if !mockT.Failed {
  260. t.Error("Check should fail")
  261. }
  262. }
  263. func TestJSONEq_ExpectedIsNotJSON(t *testing.T) {
  264. mockT := new(MockT)
  265. JSONEq(mockT, "Not JSON", `{"foo": "bar", "hello": "world"}`)
  266. if !mockT.Failed {
  267. t.Error("Check should fail")
  268. }
  269. }
  270. func TestJSONEq_ExpectedAndActualNotJSON(t *testing.T) {
  271. mockT := new(MockT)
  272. JSONEq(mockT, "Not JSON", "Not JSON")
  273. if !mockT.Failed {
  274. t.Error("Check should fail")
  275. }
  276. }
  277. func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) {
  278. mockT := new(MockT)
  279. JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
  280. if !mockT.Failed {
  281. t.Error("Check should fail")
  282. }
  283. }