state_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package neighbors
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestNewState(t *testing.T) {
  7. s := NewState()
  8. assert.Equal(t, &state{present: false, was: false, initial: true}, s)
  9. }
  10. func TestState_Present(t *testing.T) {
  11. cases := []struct {
  12. name string
  13. s State
  14. exp bool
  15. }{
  16. {
  17. name: "true",
  18. s: &state{present: true},
  19. exp: true,
  20. },
  21. {
  22. name: "false",
  23. s: &state{present: false},
  24. },
  25. }
  26. for _, tc := range cases {
  27. tc := tc
  28. t.Run(tc.name, func(t *testing.T) {
  29. t.Parallel()
  30. assert.Equal(t, tc.exp, tc.s.Present())
  31. })
  32. }
  33. }
  34. func TestState_Changed(t *testing.T) {
  35. cases := []struct {
  36. name string
  37. s State
  38. exp bool
  39. }{
  40. {
  41. name: "true to true",
  42. s: &state{present: true, was: true},
  43. exp: false,
  44. },
  45. {
  46. name: "true to false",
  47. s: &state{present: false, was: true},
  48. exp: true,
  49. },
  50. {
  51. name: "false to true",
  52. s: &state{present: true, was: false},
  53. exp: true,
  54. },
  55. {
  56. name: "false to false",
  57. s: &state{present: false, was: false},
  58. exp: false,
  59. },
  60. }
  61. for _, tc := range cases {
  62. tc := tc
  63. t.Run(tc.name, func(t *testing.T) {
  64. t.Parallel()
  65. assert.Equal(t, tc.exp, tc.s.Changed())
  66. })
  67. }
  68. }
  69. func TestState_Set(t *testing.T) {
  70. cases := []struct {
  71. name string
  72. s, exp State
  73. p bool
  74. }{
  75. {
  76. name: "initial to true",
  77. s: &state{initial: true},
  78. p: true,
  79. exp: &state{present: true, was: false, initial: false},
  80. },
  81. {
  82. name: "initial to false",
  83. s: &state{initial: true},
  84. p: false,
  85. exp: &state{present: false, was: true, initial: false},
  86. },
  87. {
  88. name: "true to true",
  89. s: &state{present: true},
  90. p: true,
  91. exp: &state{present: true, was: true},
  92. },
  93. {
  94. name: "true to false",
  95. s: &state{present: true},
  96. p: false,
  97. exp: &state{present: false, was: true},
  98. },
  99. {
  100. name: "false to true",
  101. s: &state{present: false, was: true},
  102. p: true,
  103. exp: &state{present: true, was: false},
  104. },
  105. {
  106. name: "false to false",
  107. s: &state{present: false, was: true},
  108. p: false,
  109. exp: &state{present: false, was: false},
  110. },
  111. }
  112. for _, tc := range cases {
  113. tc := tc
  114. t.Run(tc.name, func(t *testing.T) {
  115. t.Parallel()
  116. tc.s.Set(tc.p)
  117. assert.Equal(t, tc.exp, tc.s)
  118. })
  119. }
  120. }
  121. func TestState_Reset(t *testing.T) {
  122. s := &state{initial: false}
  123. s.Reset()
  124. assert.Equal(t, &state{initial: true}, s)
  125. }