github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/exception_test.go (about)

     1  package eval_test
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"runtime"
     7  	"testing"
     8  	"unsafe"
     9  
    10  	"src.elv.sh/pkg/diag"
    11  	. "src.elv.sh/pkg/eval"
    12  
    13  	. "src.elv.sh/pkg/eval/evaltest"
    14  	"src.elv.sh/pkg/eval/vals"
    15  	"src.elv.sh/pkg/persistent/hash"
    16  	"src.elv.sh/pkg/tt"
    17  )
    18  
    19  func TestReason(t *testing.T) {
    20  	err := errors.New("ordinary error")
    21  	tt.Test(t, tt.Fn("Reason", Reason), tt.Table{
    22  		tt.Args(err).Rets(err),
    23  		tt.Args(makeException(err)).Rets(err),
    24  	})
    25  }
    26  
    27  func TestException(t *testing.T) {
    28  	err := FailError{"error"}
    29  	exc := makeException(err)
    30  	vals.TestValue(t, exc).
    31  		Kind("exception").
    32  		Bool(false).
    33  		Hash(hash.Pointer(unsafe.Pointer(reflect.ValueOf(exc).Pointer()))).
    34  		Equal(exc).
    35  		NotEqual(makeException(errors.New("error"))).
    36  		AllKeys("reason").
    37  		Index("reason", err).
    38  		IndexError("stack", vals.NoSuchKey("stack")).
    39  		Repr("[&reason=[&content=error &type=fail]]")
    40  
    41  	vals.TestValue(t, OK).
    42  		Kind("exception").
    43  		Bool(true).
    44  		Repr("$ok")
    45  }
    46  
    47  func makeException(cause error, entries ...*diag.Context) Exception {
    48  	return NewException(cause, MakeStackTrace(entries...))
    49  }
    50  
    51  func TestFlow_Fields(t *testing.T) {
    52  	Test(t,
    53  		That("put ?(return)[reason][type name]").Puts("flow", "return"),
    54  	)
    55  }
    56  
    57  func TestExternalCmdExit_Fields(t *testing.T) {
    58  	badCmd := "false"
    59  	if runtime.GOOS == "windows" {
    60  		badCmd = "cmd /c exit 1"
    61  	}
    62  	Test(t,
    63  		That("put ?("+badCmd+")[reason][type exit-status]").
    64  			Puts("external-cmd/exited", "1"),
    65  		// TODO: Test killed and stopped commands
    66  	)
    67  }
    68  
    69  func TestPipelineError_Fields(t *testing.T) {
    70  	Test(t,
    71  		That("put ?(fail 1 | fail 2)[reason][type]").Puts("pipeline"),
    72  		That("count ?(fail 1 | fail 2)[reason][exceptions]").Puts(2),
    73  		That("put ?(fail 1 | fail 2)[reason][exceptions][0][reason][type]").
    74  			Puts("fail"),
    75  	)
    76  }
    77  
    78  func TestErrorMethods(t *testing.T) {
    79  	tt.Test(t, tt.Fn("Error", error.Error), tt.Table{
    80  		tt.Args(makeException(errors.New("err"))).Rets("err"),
    81  
    82  		tt.Args(MakePipelineError([]Exception{
    83  			makeException(errors.New("err1")),
    84  			makeException(errors.New("err2"))})).Rets("(err1 | err2)"),
    85  
    86  		tt.Args(Return).Rets("return"),
    87  		tt.Args(Break).Rets("break"),
    88  		tt.Args(Continue).Rets("continue"),
    89  		tt.Args(Flow(1000)).Rets("!(BAD FLOW: 1000)"),
    90  	})
    91  }