2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2017-12-24 00:27:49 +08:00
|
|
|
|
2018-10-09 21:21:26 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
2021-01-08 08:56:45 +08:00
|
|
|
"reflect"
|
2020-07-01 05:26:01 +08:00
|
|
|
"runtime"
|
2018-10-09 21:21:26 +08:00
|
|
|
"testing"
|
2020-03-30 00:54:05 +08:00
|
|
|
"unsafe"
|
2018-10-09 21:21:26 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/diag"
|
|
|
|
. "src.elv.sh/pkg/eval"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
|
|
|
"src.elv.sh/pkg/eval/vals"
|
2021-05-04 05:17:46 +08:00
|
|
|
"src.elv.sh/pkg/persistent/hash"
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/tt"
|
2018-10-09 21:21:26 +08:00
|
|
|
)
|
|
|
|
|
2020-09-05 04:04:58 +08:00
|
|
|
func TestReason(t *testing.T) {
|
2018-10-09 21:21:26 +08:00
|
|
|
err := errors.New("ordinary error")
|
2020-09-05 04:04:58 +08:00
|
|
|
tt.Test(t, tt.Fn("Reason", Reason), tt.Table{
|
2022-03-25 10:41:49 +08:00
|
|
|
Args(err).Rets(err),
|
|
|
|
Args(makeException(err)).Rets(err),
|
2018-10-09 21:21:26 +08:00
|
|
|
})
|
|
|
|
}
|
2017-12-24 00:27:49 +08:00
|
|
|
|
|
|
|
func TestException(t *testing.T) {
|
2020-07-01 05:26:01 +08:00
|
|
|
err := FailError{"error"}
|
2020-03-30 00:54:05 +08:00
|
|
|
exc := makeException(err)
|
|
|
|
vals.TestValue(t, exc).
|
|
|
|
Kind("exception").
|
|
|
|
Bool(false).
|
2021-01-08 08:56:45 +08:00
|
|
|
Hash(hash.Pointer(unsafe.Pointer(reflect.ValueOf(exc).Pointer()))).
|
2020-03-30 00:54:05 +08:00
|
|
|
Equal(exc).
|
|
|
|
NotEqual(makeException(errors.New("error"))).
|
2023-02-27 07:52:26 +08:00
|
|
|
AllKeys("reason", "stack-trace").
|
2020-06-30 04:26:10 +08:00
|
|
|
Index("reason", err).
|
2020-03-30 00:54:05 +08:00
|
|
|
IndexError("stack", vals.NoSuchKey("stack")).
|
2023-07-18 01:53:01 +08:00
|
|
|
Repr("[^exception &reason=[^fail-error &content=error &type=fail] &stack-trace=<...>]")
|
2020-03-30 00:54:05 +08:00
|
|
|
|
|
|
|
vals.TestValue(t, OK).
|
|
|
|
Kind("exception").
|
|
|
|
Bool(true).
|
|
|
|
Repr("$ok")
|
|
|
|
}
|
|
|
|
|
2021-01-08 08:56:45 +08:00
|
|
|
func makeException(cause error, entries ...*diag.Context) Exception {
|
2021-06-21 01:17:53 +08:00
|
|
|
return NewException(cause, makeStackTrace(entries...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new StackTrace, using the first entry as the head.
|
|
|
|
func makeStackTrace(entries ...*diag.Context) *StackTrace {
|
|
|
|
var s *StackTrace
|
|
|
|
for i := len(entries) - 1; i >= 0; i-- {
|
|
|
|
s = &StackTrace{Head: entries[i], Next: s}
|
|
|
|
}
|
|
|
|
return s
|
2020-03-30 00:54:05 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 05:26:01 +08:00
|
|
|
func TestFlow_Fields(t *testing.T) {
|
|
|
|
Test(t,
|
|
|
|
That("put ?(return)[reason][type name]").Puts("flow", "return"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExternalCmdExit_Fields(t *testing.T) {
|
|
|
|
badCmd := "false"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
badCmd = "cmd /c exit 1"
|
|
|
|
}
|
|
|
|
Test(t,
|
|
|
|
That("put ?("+badCmd+")[reason][type exit-status]").
|
|
|
|
Puts("external-cmd/exited", "1"),
|
|
|
|
// TODO: Test killed and stopped commands
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipelineError_Fields(t *testing.T) {
|
|
|
|
Test(t,
|
|
|
|
That("put ?(fail 1 | fail 2)[reason][type]").Puts("pipeline"),
|
2021-04-04 20:37:38 +08:00
|
|
|
That("count ?(fail 1 | fail 2)[reason][exceptions]").Puts(2),
|
2020-07-01 05:26:01 +08:00
|
|
|
That("put ?(fail 1 | fail 2)[reason][exceptions][0][reason][type]").
|
|
|
|
Puts("fail"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-31 07:36:55 +08:00
|
|
|
func TestErrorMethods(t *testing.T) {
|
2020-03-30 00:54:05 +08:00
|
|
|
tt.Test(t, tt.Fn("Error", error.Error), tt.Table{
|
2022-03-25 10:41:49 +08:00
|
|
|
Args(makeException(errors.New("err"))).Rets("err"),
|
2020-03-31 07:36:55 +08:00
|
|
|
|
2022-03-25 10:41:49 +08:00
|
|
|
Args(MakePipelineError([]Exception{
|
2020-03-31 07:36:55 +08:00
|
|
|
makeException(errors.New("err1")),
|
|
|
|
makeException(errors.New("err2"))})).Rets("(err1 | err2)"),
|
|
|
|
|
2022-03-25 10:41:49 +08:00
|
|
|
Args(Return).Rets("return"),
|
|
|
|
Args(Break).Rets("break"),
|
|
|
|
Args(Continue).Rets("continue"),
|
|
|
|
Args(Flow(1000)).Rets("!(BAD FLOW: 1000)"),
|
2020-03-30 00:54:05 +08:00
|
|
|
})
|
2017-12-24 00:27:49 +08:00
|
|
|
}
|