123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package gott
- import (
- "errors"
- "fmt"
- "testing"
- )
- func divide5(by ...interface{}) (interface{}, error) {
- if b := by[0].(int); b == 0 {
- return nil, errors.New("divideByZero")
- } else {
- return 5 / b, nil
- }
- }
- func TestBindOk(t *testing.T) {
- s, e := NewResult(5).Bind(divide5).Finish()
- if e != nil {
- t.Errorf("Error returned: %s\n", e)
- } else if s != 1 {
- t.Errorf("5 / 5 = %d, want 1\n", s)
- }
- }
- func TestBindErr(t *testing.T) {
- r := NewResult(0)
- r.LogLevel = Error
- _, e := r.Bind(divide5).Finish()
- if e == nil {
- t.Errorf("Error not returned\n")
- }
- }
- func add5(to ...interface{}) interface{} {
- return to[0].(int) + 5
- }
- func TestMap(t *testing.T) {
- s, e := NewResult(0).Map(add5).Finish()
- if e != nil {
- t.Errorf("Error returned: %s\n", e)
- } else if s != 5 {
- t.Errorf("0 + 5 = %d, want 5\n", s)
- }
- }
- func sideEffectWithError(...interface{}) error {
- fmt.Printf("side effect\n")
- return errors.New("Dummy error")
- }
- func sideEffect(...interface{}) {
- fmt.Printf("side effect\n")
- }
- func TestTee(t *testing.T) {
- r := NewResult(0)
- r.LogLevel = Error
- _, e := r.Tee(sideEffectWithError).Finish()
- if e == nil {
- t.Errorf("Error not returned\n")
- }
- }
- func TestSafeTee(t *testing.T) {
- s, e := NewResult(0).SafeTee(sideEffect).Finish()
- if e != nil {
- t.Errorf("Error returned\n")
- } else if s != 0 {
- t.Errorf("nop 0 = %d, want 0\n", s)
- }
- }
- var s int
- var e error
- func onSuccess(param ...interface{}) {
- s = param[0].(int)
- }
- func onError(param error) {
- e = param
- }
- func TestHandle(t *testing.T) {
- _s, _e := NewResult(5).Handle(onSuccess, onError).Finish()
- if _e != nil {
- t.Errorf("Error returned\n")
- } else if _s != 5 {
- t.Errorf("Success modified\n")
- } else if s != 5 {
- t.Errorf("onSuccess not run\n")
- } else if e != nil {
- t.Errorf("onError run\n")
- }
- }
- func histeric(...interface{}) interface{} {
- panic(42)
- }
- func TestCatch(t *testing.T) {
- r := NewResult(0)
- r.LogLevel = Error
- _, e := r.Catch(histeric).Finish()
- if e.(Exception).E != 42 {
- t.Errorf("%s, want 42\n", e)
- }
- }
- func TestFinishOK(t *testing.T) {
- s, e := NewResult(5).Bind(divide5).Finish()
- if e != nil {
- t.Errorf("Error returned: %s\n", e)
- } else if s != 1 {
- t.Errorf("5 / 5 = %d, want 1\n", s)
- }
- }
- func TestFinishErr(t *testing.T) {
- r := NewResult(0)
- r.LogLevel = Error
- _, e := r.Bind(divide5).Finish()
- if e == nil {
- t.Errorf("Error not returned\n")
- }
- }
- func join(strings ...interface{}) (interface{}, error) {
- return strings[0].(string) + strings[1].(string), nil
- }
- func TestTuple(t *testing.T) {
- x := [2]interface{}{
- "one", "two",
- }
- s, e := NewResult(Tuple(x[:])).Bind(join).Finish()
- if e != nil {
- t.Errorf("Error returned\n")
- } else if s != "onetwo" {
- t.Errorf("one + two = %s; want onetwo\n", x)
- }
- }
- func setZero(values ...interface{}) (interface{}, error) {
- return 0, nil
- }
- func TestRecover(t *testing.T) {
- s, e := NewResult(0).Bind(divide5).Recover(setZero).Finish()
- if e != nil {
- t.Errorf("Error returned\n")
- } else if s != 0 {
- t.Errorf("set %d; want 0\n", s)
- }
- }
|