Example #1
0
func TestStringExample(t *testing.T) {
	helloFn := func(name string) result_string.Result {
		return result_string.Success("hello, " + name)
	}

	success := result_string.Success("world").Bind(helloFn)
	assert.Equal(t, result_string.Success("hello, world"), success)

	err := errors.New("The error")
	failure := result_string.Failure(err).Bind(helloFn)
	assert.Equal(t, result_string.Failure(err), failure)
}
Example #2
0
func TestFailedChain(t *testing.T) {
	err := errors.New("The error")

	r := result_string.Success("world").Chain(
		func(name string) result_string.Result {
			return result_string.Success("hello, " + name)
		},

		func(greeting string) result_string.Result {
			return result_string.Failure(err)
		},

		func(_ string) result_string.Result {
			return result_string.Success("bye")
		},
	)

	assert.Equal(t, result_string.Failure(err), r)
}
Example #3
0
func TestOnErrorFn(t *testing.T) {
	var called bool
	var got error
	var r result_string.Result

	called = false
	r = result_string.Success("yep!").
		OnErrorFn(func(e error) { called = true })
	assert.Equal(t, false, called)
	assert.Equal(t, result_string.Success("yep!"), r)

	called = false
	err := errors.New("The error")
	r = result_string.Failure(err).
		OnErrorFn(func(e error) { called = true; got = e })
	assert.Equal(t, true, called)
	assert.Equal(t, err, got)
	assert.Equal(t, result_string.Failure(err), r)
}