gott_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 nil, 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. _, e := NewResult(0).Bind(divide5).Finish()
  24. if e == nil {
  25. t.Errorf("Error not returned\n")
  26. }
  27. }
  28. func add5(to ...interface{}) interface{} {
  29. return to[0].(int) + 5
  30. }
  31. func TestMap(t *testing.T) {
  32. s, e := NewResult(0).Map(add5).Finish()
  33. if e != nil {
  34. t.Errorf("Error returned: %s\n", e)
  35. } else if s != 5 {
  36. t.Errorf("0 + 5 = %d, want 5\n", s)
  37. }
  38. }
  39. func sideEffectWithError(...interface{}) error {
  40. fmt.Printf("side effect\n")
  41. return errors.New("Dummy error")
  42. }
  43. func sideEffect(...interface{}) {
  44. fmt.Printf("side effect\n")
  45. }
  46. func TestTee(t *testing.T) {
  47. _, e := NewResult(0).Tee(sideEffectWithError).Finish()
  48. if e == nil {
  49. t.Errorf("Error not returned\n")
  50. }
  51. }
  52. func TestSafeTee(t *testing.T) {
  53. s, e := NewResult(0).SafeTee(sideEffect).Finish()
  54. if e != nil {
  55. t.Errorf("Error returned\n")
  56. } else if s != 0 {
  57. t.Errorf("nop 0 = %d, want 0\n", s)
  58. }
  59. }
  60. var s int
  61. var e error
  62. func onSuccess(param ...interface{}) {
  63. s = param[0].(int)
  64. }
  65. func onError(param error) {
  66. e = param
  67. }
  68. func TestHandle(t *testing.T) {
  69. _s, _e := NewResult(5).Handle(onSuccess, onError).Finish()
  70. if _e != nil {
  71. t.Errorf("Error returned\n")
  72. } else if _s != 5 {
  73. t.Errorf("Success modified\n")
  74. } else if s != 5 {
  75. t.Errorf("onSuccess not run\n")
  76. } else if e != nil {
  77. t.Errorf("onError run\n")
  78. }
  79. }
  80. func histeric(...interface{}) interface{} {
  81. panic(42)
  82. }
  83. func TestCatch(t *testing.T) {
  84. _, e := NewResult(0).Catch(histeric).Finish()
  85. if e.(Exception).e != 42 {
  86. t.Errorf("%s, want 42\n", e)
  87. }
  88. }
  89. func TestFinishOK(t *testing.T) {
  90. s, e := NewResult(5).Bind(divide5).Finish()
  91. if e != nil {
  92. t.Errorf("Error returned: %s\n", e)
  93. } else if s != 1 {
  94. t.Errorf("5 / 5 = %d, want 1\n", s)
  95. }
  96. }
  97. func TestFinishErr(t *testing.T) {
  98. _, e := NewResult(0).Bind(divide5).Finish()
  99. if e == nil {
  100. t.Errorf("Error not returned\n")
  101. }
  102. }
  103. func join(strings ...interface{}) (interface{}, error) {
  104. return strings[0].(string) + strings[1].(string), nil
  105. }
  106. func TestTuple(t *testing.T) {
  107. x := [2]interface{}{
  108. "one", "two",
  109. }
  110. s, e := NewResult(Tuple(x[:])).Bind(join).Finish()
  111. if e != nil {
  112. t.Errorf("Error returned")
  113. } else if s != "onetwo" {
  114. t.Errorf("one + two = %s; want onetwo\n", x)
  115. }
  116. }