mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
pkg/eval/vals: Change TestValue to a method-style API.
This commit is contained in:
parent
3cbbe120aa
commit
d19b9c5ab7
|
@ -16,11 +16,10 @@ func TestPipe(t *testing.T) {
|
|||
defer r.Close()
|
||||
defer w.Close()
|
||||
|
||||
TestValue(t, NewPipe(r, w), Want{
|
||||
Kind: "pipe",
|
||||
Hash: hash.DJB(hash.UIntPtr(r.Fd()), hash.UIntPtr(w.Fd())),
|
||||
Repr: fmt.Sprintf("<pipe{%v %v}>", r.Fd(), w.Fd()),
|
||||
EqualTo: NewPipe(r, w),
|
||||
NotEqualTo: []interface{}{123, "a string", NewPipe(w, r)},
|
||||
})
|
||||
TestValue(t, NewPipe(r, w)).
|
||||
HasKind("pipe").
|
||||
HasHash(hash.DJB(hash.UIntPtr(r.Fd()), hash.UIntPtr(w.Fd()))).
|
||||
HasRepr(fmt.Sprintf("<pipe{%v %v}>", r.Fd(), w.Fd())).
|
||||
IsEqualTo(NewPipe(r, w)).
|
||||
IsNotEqualTo(123, "a string", NewPipe(w, r))
|
||||
}
|
||||
|
|
|
@ -6,44 +6,69 @@ import (
|
|||
"github.com/elves/elvish/pkg/tt"
|
||||
)
|
||||
|
||||
// Want keeps wanted properties of a value.
|
||||
type Want struct {
|
||||
Kind string
|
||||
Hash uint32
|
||||
Repr string
|
||||
|
||||
EqualTo interface{}
|
||||
NotEqualTo []interface{}
|
||||
// ValueTester is a helper for testing properties of a value.
|
||||
type ValueTester struct {
|
||||
t *testing.T
|
||||
v interface{}
|
||||
}
|
||||
|
||||
// TestValue tests properties of a value.
|
||||
func TestValue(t *testing.T, v interface{}, want Want) {
|
||||
t.Helper()
|
||||
if want.Kind != "" {
|
||||
if kind := Kind(v); kind != want.Kind {
|
||||
t.Errorf("Kind() = %s, want %s", kind, want.Kind)
|
||||
// TestValue returns a ValueTester.
|
||||
func TestValue(t *testing.T, v interface{}) ValueTester {
|
||||
return ValueTester{t, v}
|
||||
}
|
||||
|
||||
// HasKind tests the Kind of the value.
|
||||
func (vt ValueTester) HasKind(wantKind string) ValueTester {
|
||||
vt.t.Helper()
|
||||
kind := Kind(vt.v)
|
||||
if kind != wantKind {
|
||||
vt.t.Errorf("Kind(v) = %s, want %s", kind, wantKind)
|
||||
}
|
||||
return vt
|
||||
}
|
||||
|
||||
// HasHash tests the Hash of the value.
|
||||
func (vt ValueTester) HasHash(wantHash uint32) ValueTester {
|
||||
vt.t.Helper()
|
||||
kind := Hash(vt.v)
|
||||
if kind != wantHash {
|
||||
vt.t.Errorf("Hash(v) = %v, want %v", kind, wantHash)
|
||||
}
|
||||
return vt
|
||||
}
|
||||
|
||||
// HasRepr tests the Repr of the value.
|
||||
func (vt ValueTester) HasRepr(wantRepr string) ValueTester {
|
||||
vt.t.Helper()
|
||||
kind := Repr(vt.v, -1)
|
||||
if kind != wantRepr {
|
||||
vt.t.Errorf("Repr(v) = %s, want %s", kind, wantRepr)
|
||||
}
|
||||
return vt
|
||||
}
|
||||
|
||||
// IsEqualTo tests that the value is Equal to another.
|
||||
func (vt ValueTester) IsEqualTo(others ...interface{}) ValueTester {
|
||||
vt.t.Helper()
|
||||
for _, other := range others {
|
||||
eq := Equal(vt.v, other)
|
||||
if !eq {
|
||||
vt.t.Errorf("Equal(v, %v) = false, want true", other)
|
||||
}
|
||||
}
|
||||
if want.Hash != 0 {
|
||||
if hash := Hash(v); hash != want.Hash {
|
||||
t.Errorf("Hash() = %v, want %v", hash, want.Hash)
|
||||
}
|
||||
}
|
||||
if want.Repr != "" {
|
||||
if repr := Repr(v, -1); repr != want.Repr {
|
||||
t.Errorf("Repr() = %q, want %q", repr, want.Repr)
|
||||
}
|
||||
}
|
||||
if want.EqualTo != nil {
|
||||
if eq := Equal(v, want.EqualTo); !eq {
|
||||
t.Errorf("Equal(%v) = false, want true", want.EqualTo)
|
||||
}
|
||||
}
|
||||
for _, other := range want.NotEqualTo {
|
||||
if eq := Equal(v, other); eq {
|
||||
t.Errorf("Equal(%v) = true, want false", other)
|
||||
return vt
|
||||
}
|
||||
|
||||
// IsNotEqualTo tests that the value is not Equal to another.
|
||||
func (vt ValueTester) IsNotEqualTo(others ...interface{}) ValueTester {
|
||||
vt.t.Helper()
|
||||
for _, other := range others {
|
||||
eq := Equal(vt.v, other)
|
||||
if eq {
|
||||
vt.t.Errorf("Equal(v, %v) = true, want false", other)
|
||||
}
|
||||
}
|
||||
return vt
|
||||
}
|
||||
|
||||
// Eq returns a tt.Matcher that matches using the Equal function.
|
||||
|
|
Loading…
Reference in New Issue
Block a user