github.com/haraldrudell/parl@v0.4.176/g0/go-error_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package g0
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/haraldrudell/parl"
    14  )
    15  
    16  func TestGoError(t *testing.T) {
    17  	err := errors.New("x")
    18  	errContext := parl.GePreDoneExit
    19  
    20  	var goError parl.GoError
    21  	var g0 parl.Go
    22  
    23  	goError = NewGoError(err, errContext, g0)
    24  	if goError == nil {
    25  		t.Error("goError nil")
    26  		t.FailNow()
    27  	}
    28  	if !errors.Is(goError.Err(), err) {
    29  		t.Error("bad error value")
    30  	}
    31  
    32  	if goError.ErrContext() != errContext {
    33  		t.Error("bad error context")
    34  	}
    35  	if goError.Error() != err.Error() {
    36  		t.Error("bad err message")
    37  	}
    38  	if goError.Time().IsZero() {
    39  		t.Error("bad error time")
    40  	}
    41  	if goError.Go() != g0 {
    42  		t.Error("bad g0")
    43  	}
    44  	goError.IsThreadExit()
    45  	goError.IsFatal()
    46  	// "GePreDoneExit x g1ID::g0.TestNewG1Error-g1-error_test.go:18"
    47  	t.Log(goError.String())
    48  
    49  	goError = NewGoError(nil, errContext, g0)
    50  	_ = goError.String()
    51  }
    52  
    53  func TestGoErrorString(t *testing.T) {
    54  	var f = "is "
    55  
    56  	var goErrorImpl = GoError{}
    57  	var goErrorp = &goErrorImpl
    58  	var goError parl.GoError = goErrorp
    59  
    60  	var isStringer bool
    61  	_, isStringer = goError.(fmt.Stringer)
    62  	t.Logf("isStringer: %t", isStringer)
    63  
    64  	var s = fmt.Sprintf(f+"%s", goError.String())
    65  	t.Logf("resulting s: %q", s)
    66  	if s == f {
    67  		t.Error("s empty")
    68  	}
    69  	//t.Fail()
    70  }