Exemplo n.º 1
0
func TestIntExample(t *testing.T) {
	addTwo := func(x int) result_int.Result {
		return result_int.Success(2 + x)
	}

	success := result_int.Success(7).Bind(addTwo)
	assert.Equal(t, result_int.Success(9), success)

	err := errors.New("The error")
	failure := result_int.Failure(err).Bind(addTwo)
	assert.Equal(t, result_int.Failure(err), failure)
}
Exemplo n.º 2
0
func TestSuccessChain(t *testing.T) {
	r := result_int.Success(15).Chain(
		func(x int) result_int.Result {
			return result_int.Success(x + 2)
		},

		func(x int) result_int.Result {
			return result_int.Success(x * 2)
		},

		func(x int) result_int.Result {
			return result_int.Success(x / 3)
		},
	)

	assert.Equal(t, result_int.Success(11), r)
}
Exemplo n.º 3
0
func TestDeferCallToErrIsRequired(t *testing.T) {
	executed := false

	result_int.
		Success(25).
		Defer(func(x int) { executed = true })

	assert.Equal(t, false, executed)
}
Exemplo n.º 4
0
func TestDeferOnSuccess(t *testing.T) {
	executed := false
	var got int

	result_int.
		Success(35).
		Defer(func(x int) { executed = true; got = x }).
		Err()

	assert.Equal(t, true, executed)
	assert.Equal(t, 35, got)
}
Exemplo n.º 5
0
func TestDeferIsPreserved(t *testing.T) {
	executed := false
	var got int
	err := errors.New("The error")

	result_int.
		Success(24).
		Defer(func(x int) { executed = true; got = x }).Bind(
		func(x int) result_int.Result {
			return result_int.Failure(err)
		}).
		Err()

	assert.Equal(t, true, executed)
	assert.Equal(t, 24, got)
}