gott_test.go 3.0 KB

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