pkg/eval: Improve test coverage for exception.go.

This addresses #944.
This commit is contained in:
Qi Xiao 2020-03-29 17:54:05 +01:00
parent 9d280e9231
commit 8c47630af3
4 changed files with 56 additions and 8 deletions

View File

@ -3,21 +3,56 @@ package eval
import (
"errors"
"testing"
"unsafe"
"github.com/elves/elvish/pkg/diag"
"github.com/elves/elvish/pkg/eval/vals"
"github.com/elves/elvish/pkg/tt"
"github.com/xiaq/persistent/hash"
)
func TestCause(t *testing.T) {
err := errors.New("ordinary error")
exc := &Exception{Cause: err}
tt.Test(t, tt.Fn("Cause", Cause), tt.Table{
tt.Args(err).Rets(err),
tt.Args(exc).Rets(err),
tt.Args(makeException(err)).Rets(err),
})
}
func TestException(t *testing.T) {
Test(t,
That("kind-of ?(fail foo)").Puts("exception"),
)
err := errors.New("error")
exc := makeException(err)
vals.TestValue(t, exc).
Kind("exception").
Bool(false).
Hash(hash.Pointer(unsafe.Pointer(exc))).
Equal(exc).
NotEqual(makeException(errors.New("error"))).
AllKeys("cause").
Index("cause", err).
IndexError("stack", vals.NoSuchKey("stack")).
Repr("?(fail error)")
vals.TestValue(t, OK).
Kind("exception").
Bool(true).
Repr("$ok")
}
func makeException(cause error, entries ...*diag.Context) *Exception {
var s *stackTrace
for i := len(entries) - 1; i >= 0; i-- {
s = &stackTrace{head: entries[i], next: s}
}
return &Exception{cause, s}
}
func TestErrors(t *testing.T) {
tt.Test(t, tt.Fn("Error", error.Error), tt.Table{
tt.Args(Return).Rets("return"),
tt.Args(Break).Rets("break"),
tt.Args(Continue).Rets("continue"),
tt.Args(ExternalCmdExit{0, "ls", 100}).Rets("ls exited with 0"),
})
}

View File

@ -18,6 +18,7 @@ func TestPipe(t *testing.T) {
TestValue(t, NewPipe(r, w)).
Kind("pipe").
Bool(true).
Hash(hash.DJB(hash.UIntPtr(r.Fd()), hash.UIntPtr(w.Fd()))).
Repr(fmt.Sprintf("<pipe{%v %v}>", r.Fd(), w.Fd())).
Equal(NewPipe(r, w)).

View File

@ -24,6 +24,7 @@ func (testStructMap2) IsStructMap(StructMapMarker) {}
func TestStructMap(t *testing.T) {
TestValue(t, testStructMap{}).
Kind("structmap").
Bool(true).
Hash(hash.DJB(Hash(""), Hash(0.0))).
Repr(`[&name='' &score-number=(float64 0)]`).
Len(2).
@ -41,6 +42,7 @@ func TestStructMap(t *testing.T) {
TestValue(t, testStructMap{"a", 1.0}).
Kind("structmap").
Bool(true).
Hash(hash.DJB(Hash("a"), Hash(1.0))).
Repr(`[&name=a &score-number=(float64 1)]`).
Len(2).

View File

@ -28,12 +28,22 @@ func (vt ValueTester) Kind(wantKind string) ValueTester {
return vt
}
// Bool tests the Boool of the value.
func (vt ValueTester) Bool(wantBool bool) ValueTester {
vt.t.Helper()
b := Bool(vt.v)
if b != wantBool {
vt.t.Errorf("Bool(v) = %v, want %v", b, wantBool)
}
return vt
}
// Hash tests the Hash of the value.
func (vt ValueTester) Hash(wantHash uint32) ValueTester {
vt.t.Helper()
kind := Hash(vt.v)
if kind != wantHash {
vt.t.Errorf("Hash(v) = %v, want %v", kind, wantHash)
hash := Hash(vt.v)
if hash != wantHash {
vt.t.Errorf("Hash(v) = %v, want %v", hash, wantHash)
}
return vt
}