Commit db941f12 authored by Kirill Smelkov's avatar Kirill Smelkov

exc += XRun() - utility to run a function which returns regular error, and...

exc += XRun() - utility to run a function which returns regular error, and raise exception if error is not nil

This might be frequently needed in tests, possibly with some kind of
"apply" syntatic sugar.

Complements 486ede30 (exc += Runx() - utility to run a function and
translate caught exception to regular error)
parent f674c8f7
...@@ -190,3 +190,9 @@ func Runx(xf func()) (err error) { ...@@ -190,3 +190,9 @@ func Runx(xf func()) (err error) {
xf() xf()
return return
} }
// run a function which returns regular error, and raise exception if error is not nil.
func XRun(f func() error) {
err := f()
Raiseif(err)
}
...@@ -167,3 +167,24 @@ func TestRunx(t *testing.T) { ...@@ -167,3 +167,24 @@ func TestRunx(t *testing.T) {
} }
} }
} }
func TestXRun(t *testing.T) {
var tests = []struct { f func() error; wanterr string } {
{func() error { return nil }, ""},
{func() error { return errors.New("abc") }, "X abc"},
}
for _, tt := range tests {
errStr := ""
func() {
defer Catch(func(e *Error) {
errStr = "X " + e.Error()
})
XRun(tt.f)
}()
if errStr != tt.wanterr {
t.Errorf("xrun(%v) -> %q ; want %q", funcname(tt.f), errStr, tt.wanterr)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment