A package for assertions without dependency hell
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.

194 lines
3.4 KiB

11 months ago
10 months ago
  1. package assert
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. // -----------------------------------------
  7. // Slices
  8. // -----------------------------------------
  9. func TestSameElementsSlices(t *testing.T) {
  10. assert := New(t)
  11. ft, fa := newFakeT()
  12. a, b := []int{1, 2}, []int{1, 2}
  13. fa.SameElements(a, a)
  14. assert.False(ft.GotError())
  15. fa.SameElements(a, b)
  16. assert.False(ft.GotError())
  17. b = []int{2, 1}
  18. fa.SameElements(b, a) // SameElements ignores order
  19. assert.False(ft.GotError())
  20. b = []int{1, 2, 3}
  21. fa.SameElements(b, a)
  22. assert.True(ft.GotError())
  23. b = []int{2, 3}
  24. fa.SameElements(b, a)
  25. assert.True(ft.GotError())
  26. }
  27. func TestCmpSlices(t *testing.T) {
  28. assert := New(t)
  29. ft, fa := newFakeT()
  30. a, b := []int{1, 2}, []int{1, 2}
  31. fa.Cmp(a, a)
  32. assert.False(ft.GotError())
  33. fa.Cmp(a, b)
  34. assert.False(ft.GotError())
  35. b = []int{2, 1}
  36. fa.Cmp(a, b) // Cmp does not accept different order
  37. assert.True(ft.GotError())
  38. b = []int{1, 2, 3}
  39. fa.Cmp(a, b)
  40. assert.True(ft.GotError())
  41. b = []int{2, 3}
  42. fa.Cmp(a, b)
  43. assert.True(ft.GotError())
  44. }
  45. // -----------------------------------------
  46. // Maps
  47. // -----------------------------------------
  48. func TestCmpMaps(t *testing.T) {
  49. assert := New(t)
  50. ft, fa := newFakeT()
  51. a, b := map[int]int{1: 2, 3: 4}, map[int]int{1: 2, 3: 4}
  52. fa.Cmp(a, a)
  53. assert.False(ft.GotError())
  54. fa.Cmp(a, b)
  55. assert.False(ft.GotError())
  56. b = map[int]int{1: 2, 3: 5}
  57. fa.Cmp(a, b)
  58. assert.True(ft.GotError())
  59. b = map[int]int{1: 2, 3: 4, 5: 6}
  60. fa.Cmp(a, b)
  61. assert.True(ft.GotError())
  62. b = map[int]int{1: 2}
  63. fa.Cmp(a, b)
  64. assert.True(ft.GotError())
  65. }
  66. // -----------------------------------------
  67. // Pointers
  68. // -----------------------------------------
  69. func TestEqPointers(t *testing.T) {
  70. assert := New(t)
  71. ft, fa := newFakeT()
  72. var a, b int
  73. fa.Eq(&a, &a)
  74. assert.False(ft.GotError())
  75. fa.Eq(&a, &b)
  76. assert.True(ft.GotError())
  77. fa.Eq(&b, &a)
  78. assert.True(ft.GotError())
  79. }
  80. func TestCmpPointers(t *testing.T) {
  81. assert := New(t)
  82. ft, fa := newFakeT()
  83. type B struct {
  84. Value int
  85. }
  86. type A struct {
  87. B *B
  88. }
  89. fa.Cmp(A{B: &B{1}}, A{&B{1}})
  90. assert.False(ft.GotError())
  91. fa.Cmp(A{B: &B{1}}, A{&B{2}})
  92. assert.True(ft.GotError())
  93. fa.Cmp(A{B: &B{1}}, A{nil})
  94. assert.True(ft.GotError())
  95. }
  96. // -----------------------------------------
  97. // Timezones
  98. // -----------------------------------------
  99. func mustLoadLocation(zone string) *time.Location {
  100. loc, err := time.LoadLocation(zone)
  101. if err != nil {
  102. panic(err)
  103. }
  104. return loc
  105. }
  106. var locAmsterdam = mustLoadLocation(`Europe/Amsterdam`)
  107. var locTokyo = mustLoadLocation(`Asia/Tokyo`)
  108. func TestEqTimezones(t *testing.T) {
  109. assert := New(t)
  110. ft, fa := newFakeT()
  111. ti := time.Now()
  112. ti, ti2 := ti.In(locAmsterdam), ti.In(locTokyo)
  113. fa.Eq(ti, ti2)
  114. assert.True(ft.GotError())
  115. }
  116. func TestCmpTimezones(t *testing.T) {
  117. assert := New(t)
  118. ft, fa := newFakeT()
  119. type A struct {
  120. Time time.Time
  121. }
  122. ti := time.Now()
  123. ti2 := ti.In(time.UTC)
  124. fa.Cmp(&A{ti}, &A{ti2})
  125. assert.False(ft.GotError())
  126. ti2 = ti.In(locAmsterdam)
  127. fa.Cmp(&A{ti}, &A{ti2})
  128. assert.False(ft.GotError())
  129. ti = ti.In(locTokyo)
  130. fa.Cmp(&A{ti}, &A{ti2})
  131. assert.False(ft.GotError())
  132. ti2 = ti2.Add(time.Second)
  133. fa.Cmp(&A{ti}, &A{ti2})
  134. assert.True(ft.GotError())
  135. }
  136. func TestNCmp(t *testing.T) {
  137. assert := New(t)
  138. ft, fa := newFakeT()
  139. type A struct {
  140. S string
  141. }
  142. fa.NCmp(A{"not"}, A{"equal"})
  143. assert.False(ft.GotError())
  144. fa.NCmp(A{"equal"}, A{"equal"})
  145. assert.True(ft.GotError())
  146. }