gott_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package gott
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func divide5(by int) (int, error) {
  7. if by == 0 {
  8. return by, errors.New("divideByZero")
  9. } else {
  10. return 5 / by, nil
  11. }
  12. }
  13. func TestBindOk(t *testing.T) {
  14. r := R[int]{S: 5}.Bind(divide5)
  15. if r.E != nil {
  16. t.Errorf("error returned: %s\n", r.E)
  17. } else if r.S != 1 {
  18. t.Errorf("5 / 5 = %d, want 1\n", r.S)
  19. }
  20. }
  21. func TestBindErr(t *testing.T) {
  22. r := R[int]{S: 0}.Bind(divide5)
  23. if r.E == nil {
  24. t.Errorf("Error not returned\n")
  25. }
  26. }
  27. func add5(to int) int {
  28. return to + 5
  29. }
  30. func TestMap(t *testing.T) {
  31. r := R[int]{S: 0}.Map(add5)
  32. if r.E != nil {
  33. t.Errorf("Error returned: %s\n", r.E)
  34. } else if r.S != 5 {
  35. t.Errorf("0 + 5 = %d, want 5\n", r.S)
  36. }
  37. }
  38. func sideEffectWithError(int) error {
  39. return errors.New("Dummy error")
  40. }
  41. func TestTee(t *testing.T) {
  42. r := R[int]{S: 0}.Tee(sideEffectWithError)
  43. if r.E == nil {
  44. t.Errorf("Error not returned\n")
  45. }
  46. }
  47. func sideEffect(int) {
  48. }
  49. func TestSafeTee(t *testing.T) {
  50. r := R[int]{S: 1}.SafeTee(sideEffect)
  51. if r.E != nil {
  52. t.Errorf("Error returned\n")
  53. } else if r.S != 1 {
  54. t.Errorf("nop 0 = %d, want 0\n", r.S)
  55. }
  56. }
  57. var globalS int
  58. var globalE error
  59. func onSuccess(s int) {
  60. globalS = s
  61. }
  62. func onError(e error) {
  63. globalE = e
  64. }
  65. func TestHandleSuccess(t *testing.T) {
  66. globalS = 0
  67. globalE = nil
  68. r := R[int]{S: 5}.Handle(onSuccess, onError)
  69. if r.E != nil {
  70. t.Errorf("Error returned\n")
  71. } else if r.S != 5 {
  72. t.Errorf("Success modified\n")
  73. } else if globalS != 5 {
  74. t.Errorf("onSuccess not run\n")
  75. } else if globalE != nil {
  76. t.Errorf("onError run\n")
  77. }
  78. }
  79. func TestHandleError(t *testing.T) {
  80. globalS = 0
  81. globalE = nil
  82. r := R[int]{S: 5, E: errors.New("Dummy error")}.Handle(onSuccess, onError)
  83. if r.E == nil {
  84. t.Errorf("Error modified\n")
  85. } else if r.S != 5 {
  86. t.Errorf("Success modified\n")
  87. } else if globalS == 5 {
  88. t.Errorf("onSuccess run\n")
  89. } else if globalE == nil {
  90. t.Errorf("onError not run\n")
  91. }
  92. }
  93. func histeric(int) int {
  94. panic(42)
  95. }
  96. func TestCatchErr(t *testing.T) {
  97. exception := Exception{}
  98. r := R[int]{S: 0}.Catch(histeric)
  99. if errors.As(r.E, &exception) {
  100. if exception.E != 42 {
  101. t.Errorf("%s, want 42\n", r.E)
  102. }
  103. } else {
  104. t.Errorf("want Exception\n")
  105. }
  106. }
  107. func calm(int) int {
  108. return 5
  109. }
  110. func TestCatchNoErr(t *testing.T) {
  111. r := R[int]{S: 0}.Catch(calm)
  112. if r.E != nil {
  113. t.Errorf("error returned\n")
  114. }
  115. if r.S != 5 {
  116. t.Errorf("calm %d; want 5\n", r.S)
  117. }
  118. }
  119. func setZero(_ int, _ error) (int, error) {
  120. return 0, nil
  121. }
  122. func TestRecoverErr(t *testing.T) {
  123. r := R[int]{S: 0}.Bind(divide5).Recover(setZero)
  124. if r.E != nil {
  125. t.Errorf("Error returned\n")
  126. } else if r.S != 0 {
  127. t.Errorf("set %d; want 0\n", r.S)
  128. }
  129. }
  130. func TestRecoverNoErr(t *testing.T) {
  131. r := R[int]{S: 5}.Bind(divide5).Recover(setZero)
  132. if r.E != nil {
  133. t.Errorf("Error returned\n")
  134. } else if r.S != 1 {
  135. t.Errorf("set %d; want 1\n", r.S)
  136. }
  137. }