You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

566 lines
11 KiB

  1. package require
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "testing"
  6. "time"
  7. )
  8. // AssertionTesterInterface defines an interface to be used for testing assertion methods
  9. type AssertionTesterInterface interface {
  10. TestMethod()
  11. }
  12. // AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface
  13. type AssertionTesterConformingObject struct {
  14. }
  15. func (a *AssertionTesterConformingObject) TestMethod() {
  16. }
  17. // AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface
  18. type AssertionTesterNonConformingObject struct {
  19. }
  20. type MockT struct {
  21. Failed bool
  22. }
  23. func (t *MockT) FailNow() {
  24. t.Failed = true
  25. }
  26. func (t *MockT) Errorf(format string, args ...interface{}) {
  27. _, _ = format, args
  28. }
  29. func TestImplements(t *testing.T) {
  30. Implements(t, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject))
  31. mockT := new(MockT)
  32. Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject))
  33. if !mockT.Failed {
  34. t.Error("Check should fail")
  35. }
  36. }
  37. func TestIsType(t *testing.T) {
  38. IsType(t, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject))
  39. mockT := new(MockT)
  40. IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject))
  41. if !mockT.Failed {
  42. t.Error("Check should fail")
  43. }
  44. }
  45. func TestEqual(t *testing.T) {
  46. Equal(t, 1, 1)
  47. mockT := new(MockT)
  48. Equal(mockT, 1, 2)
  49. if !mockT.Failed {
  50. t.Error("Check should fail")
  51. }
  52. }
  53. func TestNotEqual(t *testing.T) {
  54. NotEqual(t, 1, 2)
  55. mockT := new(MockT)
  56. NotEqual(mockT, 2, 2)
  57. if !mockT.Failed {
  58. t.Error("Check should fail")
  59. }
  60. }
  61. func TestExactly(t *testing.T) {
  62. a := float32(1)
  63. b := float32(1)
  64. c := float64(1)
  65. Exactly(t, a, b)
  66. mockT := new(MockT)
  67. Exactly(mockT, a, c)
  68. if !mockT.Failed {
  69. t.Error("Check should fail")
  70. }
  71. }
  72. func TestNotNil(t *testing.T) {
  73. NotNil(t, new(AssertionTesterConformingObject))
  74. mockT := new(MockT)
  75. NotNil(mockT, nil)
  76. if !mockT.Failed {
  77. t.Error("Check should fail")
  78. }
  79. }
  80. func TestNil(t *testing.T) {
  81. Nil(t, nil)
  82. mockT := new(MockT)
  83. Nil(mockT, new(AssertionTesterConformingObject))
  84. if !mockT.Failed {
  85. t.Error("Check should fail")
  86. }
  87. }
  88. func TestTrue(t *testing.T) {
  89. True(t, true)
  90. mockT := new(MockT)
  91. True(mockT, false)
  92. if !mockT.Failed {
  93. t.Error("Check should fail")
  94. }
  95. }
  96. func TestFalse(t *testing.T) {
  97. False(t, false)
  98. mockT := new(MockT)
  99. False(mockT, true)
  100. if !mockT.Failed {
  101. t.Error("Check should fail")
  102. }
  103. }
  104. func TestContains(t *testing.T) {
  105. Contains(t, "Hello World", "Hello")
  106. mockT := new(MockT)
  107. Contains(mockT, "Hello World", "Salut")
  108. if !mockT.Failed {
  109. t.Error("Check should fail")
  110. }
  111. }
  112. func TestNotContains(t *testing.T) {
  113. NotContains(t, "Hello World", "Hello!")
  114. mockT := new(MockT)
  115. NotContains(mockT, "Hello World", "Hello")
  116. if !mockT.Failed {
  117. t.Error("Check should fail")
  118. }
  119. }
  120. func TestPanics(t *testing.T) {
  121. Panics(t, func() {
  122. panic("Panic!")
  123. })
  124. mockT := new(MockT)
  125. Panics(mockT, func() {})
  126. if !mockT.Failed {
  127. t.Error("Check should fail")
  128. }
  129. }
  130. func TestNotPanics(t *testing.T) {
  131. NotPanics(t, func() {})
  132. mockT := new(MockT)
  133. NotPanics(mockT, func() {
  134. panic("Panic!")
  135. })
  136. if !mockT.Failed {
  137. t.Error("Check should fail")
  138. }
  139. }
  140. func TestNoError(t *testing.T) {
  141. NoError(t, nil)
  142. mockT := new(MockT)
  143. NoError(mockT, errors.New("some error"))
  144. if !mockT.Failed {
  145. t.Error("Check should fail")
  146. }
  147. }
  148. func TestError(t *testing.T) {
  149. Error(t, errors.New("some error"))
  150. mockT := new(MockT)
  151. Error(mockT, nil)
  152. if !mockT.Failed {
  153. t.Error("Check should fail")
  154. }
  155. }
  156. func TestEqualError(t *testing.T) {
  157. EqualError(t, errors.New("some error"), "some error")
  158. mockT := new(MockT)
  159. EqualError(mockT, errors.New("some error"), "Not some error")
  160. if !mockT.Failed {
  161. t.Error("Check should fail")
  162. }
  163. }
  164. func TestEmpty(t *testing.T) {
  165. Empty(t, "")
  166. mockT := new(MockT)
  167. Empty(mockT, "x")
  168. if !mockT.Failed {
  169. t.Error("Check should fail")
  170. }
  171. }
  172. func TestNotEmpty(t *testing.T) {
  173. NotEmpty(t, "x")
  174. mockT := new(MockT)
  175. NotEmpty(mockT, "")
  176. if !mockT.Failed {
  177. t.Error("Check should fail")
  178. }
  179. }
  180. func TestWithinDuration(t *testing.T) {
  181. a := time.Now()
  182. b := a.Add(10 * time.Second)
  183. WithinDuration(t, a, b, 15*time.Second)
  184. mockT := new(MockT)
  185. WithinDuration(mockT, a, b, 5*time.Second)
  186. if !mockT.Failed {
  187. t.Error("Check should fail")
  188. }
  189. }
  190. func TestInDelta(t *testing.T) {
  191. InDelta(t, 1.001, 1, 0.01)
  192. mockT := new(MockT)
  193. InDelta(mockT, 1, 2, 0.5)
  194. if !mockT.Failed {
  195. t.Error("Check should fail")
  196. }
  197. }
  198. func TestZero(t *testing.T) {
  199. Zero(t, "")
  200. mockT := new(MockT)
  201. Zero(mockT, "x")
  202. if !mockT.Failed {
  203. t.Error("Check should fail")
  204. }
  205. }
  206. func TestNotZero(t *testing.T) {
  207. NotZero(t, "x")
  208. mockT := new(MockT)
  209. NotZero(mockT, "")
  210. if !mockT.Failed {
  211. t.Error("Check should fail")
  212. }
  213. }
  214. func TestJSONEq_EqualSONString(t *testing.T) {
  215. mockT := new(MockT)
  216. JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
  217. if mockT.Failed {
  218. t.Error("Check should pass")
  219. }
  220. }
  221. func TestJSONEq_EquivalentButNotEqual(t *testing.T) {
  222. mockT := new(MockT)
  223. JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
  224. if mockT.Failed {
  225. t.Error("Check should pass")
  226. }
  227. }
  228. func TestJSONEq_HashOfArraysAndHashes(t *testing.T) {
  229. mockT := new(MockT)
  230. 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}",
  231. "{\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}")
  232. if mockT.Failed {
  233. t.Error("Check should pass")
  234. }
  235. }
  236. func TestJSONEq_Array(t *testing.T) {
  237. mockT := new(MockT)
  238. JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
  239. if mockT.Failed {
  240. t.Error("Check should pass")
  241. }
  242. }
  243. func TestJSONEq_HashAndArrayNotEquivalent(t *testing.T) {
  244. mockT := new(MockT)
  245. JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
  246. if !mockT.Failed {
  247. t.Error("Check should fail")
  248. }
  249. }
  250. func TestJSONEq_HashesNotEquivalent(t *testing.T) {
  251. mockT := new(MockT)
  252. JSONEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
  253. if !mockT.Failed {
  254. t.Error("Check should fail")
  255. }
  256. }
  257. func TestJSONEq_ActualIsNotJSON(t *testing.T) {
  258. mockT := new(MockT)
  259. JSONEq(mockT, `{"foo": "bar"}`, "Not JSON")
  260. if !mockT.Failed {
  261. t.Error("Check should fail")
  262. }
  263. }
  264. func TestJSONEq_ExpectedIsNotJSON(t *testing.T) {
  265. mockT := new(MockT)
  266. JSONEq(mockT, "Not JSON", `{"foo": "bar", "hello": "world"}`)
  267. if !mockT.Failed {
  268. t.Error("Check should fail")
  269. }
  270. }
  271. func TestJSONEq_ExpectedAndActualNotJSON(t *testing.T) {
  272. mockT := new(MockT)
  273. JSONEq(mockT, "Not JSON", "Not JSON")
  274. if !mockT.Failed {
  275. t.Error("Check should fail")
  276. }
  277. }
  278. func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) {
  279. mockT := new(MockT)
  280. JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
  281. if !mockT.Failed {
  282. t.Error("Check should fail")
  283. }
  284. }
  285. func ExampleComparisonAssertionFunc() {
  286. t := &testing.T{} // provided by test
  287. adder := func(x, y int) int {
  288. return x + y
  289. }
  290. type args struct {
  291. x int
  292. y int
  293. }
  294. tests := []struct {
  295. name string
  296. args args
  297. expect int
  298. assertion ComparisonAssertionFunc
  299. }{
  300. {"2+2=4", args{2, 2}, 4, Equal},
  301. {"2+2!=5", args{2, 2}, 5, NotEqual},
  302. {"2+3==5", args{2, 3}, 5, Exactly},
  303. }
  304. for _, tt := range tests {
  305. t.Run(tt.name, func(t *testing.T) {
  306. tt.assertion(t, tt.expect, adder(tt.args.x, tt.args.y))
  307. })
  308. }
  309. }
  310. func TestComparisonAssertionFunc(t *testing.T) {
  311. type iface interface {
  312. Name() string
  313. }
  314. tests := []struct {
  315. name string
  316. expect interface{}
  317. got interface{}
  318. assertion ComparisonAssertionFunc
  319. }{
  320. {"implements", (*iface)(nil), t, Implements},
  321. {"isType", (*testing.T)(nil), t, IsType},
  322. {"equal", t, t, Equal},
  323. {"equalValues", t, t, EqualValues},
  324. {"exactly", t, t, Exactly},
  325. {"notEqual", t, nil, NotEqual},
  326. {"notContains", []int{1, 2, 3}, 4, NotContains},
  327. {"subset", []int{1, 2, 3, 4}, []int{2, 3}, Subset},
  328. {"notSubset", []int{1, 2, 3, 4}, []int{0, 3}, NotSubset},
  329. {"elementsMatch", []byte("abc"), []byte("bac"), ElementsMatch},
  330. {"regexp", "^t.*y$", "testify", Regexp},
  331. {"notRegexp", "^t.*y$", "Testify", NotRegexp},
  332. }
  333. for _, tt := range tests {
  334. t.Run(tt.name, func(t *testing.T) {
  335. tt.assertion(t, tt.expect, tt.got)
  336. })
  337. }
  338. }
  339. func ExampleValueAssertionFunc() {
  340. t := &testing.T{} // provided by test
  341. dumbParse := func(input string) interface{} {
  342. var x interface{}
  343. json.Unmarshal([]byte(input), &x)
  344. return x
  345. }
  346. tests := []struct {
  347. name string
  348. arg string
  349. assertion ValueAssertionFunc
  350. }{
  351. {"true is not nil", "true", NotNil},
  352. {"empty string is nil", "", Nil},
  353. {"zero is not nil", "0", NotNil},
  354. {"zero is zero", "0", Zero},
  355. {"false is zero", "false", Zero},
  356. }
  357. for _, tt := range tests {
  358. t.Run(tt.name, func(t *testing.T) {
  359. tt.assertion(t, dumbParse(tt.arg))
  360. })
  361. }
  362. }
  363. func TestValueAssertionFunc(t *testing.T) {
  364. tests := []struct {
  365. name string
  366. value interface{}
  367. assertion ValueAssertionFunc
  368. }{
  369. {"notNil", true, NotNil},
  370. {"nil", nil, Nil},
  371. {"empty", []int{}, Empty},
  372. {"notEmpty", []int{1}, NotEmpty},
  373. {"zero", false, Zero},
  374. {"notZero", 42, NotZero},
  375. }
  376. for _, tt := range tests {
  377. t.Run(tt.name, func(t *testing.T) {
  378. tt.assertion(t, tt.value)
  379. })
  380. }
  381. }
  382. func ExampleBoolAssertionFunc() {
  383. t := &testing.T{} // provided by test
  384. isOkay := func(x int) bool {
  385. return x >= 42
  386. }
  387. tests := []struct {
  388. name string
  389. arg int
  390. assertion BoolAssertionFunc
  391. }{
  392. {"-1 is bad", -1, False},
  393. {"42 is good", 42, True},
  394. {"41 is bad", 41, False},
  395. {"45 is cool", 45, True},
  396. }
  397. for _, tt := range tests {
  398. t.Run(tt.name, func(t *testing.T) {
  399. tt.assertion(t, isOkay(tt.arg))
  400. })
  401. }
  402. }
  403. func TestBoolAssertionFunc(t *testing.T) {
  404. tests := []struct {
  405. name string
  406. value bool
  407. assertion BoolAssertionFunc
  408. }{
  409. {"true", true, True},
  410. {"false", false, False},
  411. }
  412. for _, tt := range tests {
  413. t.Run(tt.name, func(t *testing.T) {
  414. tt.assertion(t, tt.value)
  415. })
  416. }
  417. }
  418. func ExampleErrorAssertionFunc() {
  419. t := &testing.T{} // provided by test
  420. dumbParseNum := func(input string, v interface{}) error {
  421. return json.Unmarshal([]byte(input), v)
  422. }
  423. tests := []struct {
  424. name string
  425. arg string
  426. assertion ErrorAssertionFunc
  427. }{
  428. {"1.2 is number", "1.2", NoError},
  429. {"1.2.3 not number", "1.2.3", Error},
  430. {"true is not number", "true", Error},
  431. {"3 is number", "3", NoError},
  432. }
  433. for _, tt := range tests {
  434. t.Run(tt.name, func(t *testing.T) {
  435. var x float64
  436. tt.assertion(t, dumbParseNum(tt.arg, &x))
  437. })
  438. }
  439. }
  440. func TestErrorAssertionFunc(t *testing.T) {
  441. tests := []struct {
  442. name string
  443. err error
  444. assertion ErrorAssertionFunc
  445. }{
  446. {"noError", nil, NoError},
  447. {"error", errors.New("whoops"), Error},
  448. }
  449. for _, tt := range tests {
  450. t.Run(tt.name, func(t *testing.T) {
  451. tt.assertion(t, tt.err)
  452. })
  453. }
  454. }