github.com/mitranim/gg@v0.1.17/try_test.go (about)

     1  package gg_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mitranim/gg"
     7  	"github.com/mitranim/gg/gtest"
     8  )
     9  
    10  func TestCatch(t *testing.T) {
    11  	defer gtest.Catch(t)
    12  
    13  	err := gg.Catch(func() { panic(`string_panic`) }).(gg.Err)
    14  	gtest.Equal(err.Msg, `string_panic`)
    15  	gtest.True(err.Trace.IsNotEmpty())
    16  
    17  	err = gg.Catch(func() { panic(gg.ErrStr(`string_error`)) }).(gg.Err)
    18  	gtest.Zero(err.Msg)
    19  	gtest.Equal(err.Cause, error(gg.ErrStr(`string_error`)))
    20  	gtest.True(err.Trace.IsNotEmpty())
    21  }
    22  
    23  func TestDetailf(t *testing.T) {
    24  	defer gtest.Catch(t)
    25  
    26  	err := gg.Catch(func() {
    27  		defer gg.Detailf(`unable to %v`, `do stuff`)
    28  		panic(`string_panic`)
    29  	}).(gg.Err)
    30  
    31  	gtest.Equal(err.Msg, `unable to do stuff`)
    32  	gtest.Equal(err.Cause, error(gg.ErrStr(`string_panic`)))
    33  	gtest.True(err.Trace.IsNotEmpty())
    34  }