package gerrors

import (
	"errors"
	"fmt"
	"testing"
)

var err0 = errors.New("err0")
var err1 = errors.New("err1")

func fn0() error {
	return WithStack(WithAnnotating(err0, "comment0"))
}

func fn1() error {
	if err := fn0(); err != nil {
		e := ChainErrors(err1, err)
		return WithStack(WithAnnotating(e, "comment1"))
	}
	return nil
}

func fn2() error {
	if err := fn1(); err != nil {
		return WithMessage(err, "err2")
	}
	return nil
}

func TestError(t *testing.T) {

	e := WithAnnotating(err0, "comment0")
	fmt.Printf("%+v\n", e)
	fmt.Println()

	if !Is(fn2(), err0) || !Is(fn2(), err1) {
		t.Fail()
	}
	fmt.Println(fn2())
	fmt.Println()
	// output:
	// err2, caused by err1(comment1), caused by err0(comment0)

	fmt.Printf("%+v\n", fn2())
}
