gott_test.go 2.9 KB

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