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

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package parl
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/haraldrudell/parl/perrors"
    12  )
    13  
    14  func TestGoResult(t *testing.T) {
    15  	var text = "text"
    16  	var err error
    17  	_ = 1
    18  	err = runGoroutine(text)
    19  	if err == nil {
    20  		t.Error("err missing")
    21  	} else if message := err.Error(); message != text {
    22  		t.Errorf("err message: %q exp %q", message, text)
    23  	}
    24  }
    25  
    26  func runGoroutine(text string) (err error) {
    27  	var g = NewGoResult()
    28  	go goroutine(text, g)
    29  	defer g.ReceiveError(&err)
    30  
    31  	return
    32  }
    33  
    34  func goroutine(text string, g GoResult) {
    35  	var err error
    36  	defer g.SendError(&err)
    37  	defer RecoverErr(func() DA { return A() }, &err)
    38  
    39  	err = perrors.New(text)
    40  }