1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// Framework for testing Elvish script. This file does not file a _test.go
// suffix so that it can be used from other packages that also want to test the
// modules they implement (e.g. edit: and re:).
//
// The entry point for the framework is the Test function, which accepts a
// *testing.T and a variadic number of test cases. Test cases are constructed
// using the That function followed by methods that add constraints on the test
// case. Overall, a test looks like:
//
// Test(t,
// That("put x").Puts("x"),
// That("echo x").Prints("x\n"))
//
// If some setup is needed, use the TestWithSetup function instead.
package eval
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/elves/elvish/eval/vals"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/util"
)
// TestCase is a test case for Test.
type TestCase struct {
text string
want
}
type want struct {
out []interface{}
bytesOut []byte
err error
}
// A special value for want.err to indicate that any error, as long as not nil,
// is OK
var errAny = errors.New("any error")
// The following functions and methods are used to build Test structs. They are
// supposed to read like English, so a test that "put x" should put "x" reads:
//
// That("put x").Puts("x")
// That returns a new Test with the specified source code.
func That(text string) TestCase {
return TestCase{text: text}
}
// DoesNothing returns t unchanged. It is used to mark that a piece of code
// should simply does nothing. In particular, it shouldn't have any output and
// does not error.
func (t TestCase) DoesNothing() TestCase {
return t
}
// Puts returns an altered Test that requires the source code to produce the
// specified values in the value channel when evaluated.
func (t TestCase) Puts(vs ...interface{}) TestCase {
t.want.out = vs
return t
}
// Puts returns an altered Test that requires the source code to produce the
// specified strings in the value channel when evaluated.
func (t TestCase) PutsStrings(ss []string) TestCase {
t.want.out = make([]interface{}, len(ss))
for i, s := range ss {
t.want.out[i] = s
}
return t
}
// Prints returns an altered test that requires the source code to produce
// the specified output in the byte pipe when evaluated.
func (t TestCase) Prints(s string) TestCase {
t.want.bytesOut = []byte(s)
return t
}
// ErrorsWith returns an altered Test that requires the source code to result in
// the specified error when evaluted.
func (t TestCase) ErrorsWith(err error) TestCase {
t.want.err = err
return t
}
// Errors returns an altered Test that requires the source code to result in any
// error when evaluated.
func (t TestCase) Errors() TestCase {
return t.ErrorsWith(errAny)
}
// Test runs test cases. For each test case, a new Evaler is created with
// NewEvaler.
func Test(t *testing.T, tests ...TestCase) {
TestWithSetup(t, func(*Evaler) {}, tests...)
}
// Test runs test cases. For each test case, a new Evaler is created with
// NewEvaler and passed to the setup function.
func TestWithSetup(t *testing.T, setup func(*Evaler), tests ...TestCase) {
for _, tt := range tests {
ev := NewEvaler()
setup(ev)
out, bytesOut, err := evalAndCollect(t, ev, []string{tt.text}, len(tt.want.out))
first := true
errorf := func(format string, args ...interface{}) {
if first {
first = false
t.Errorf("eval(%q) fails:", tt.text)
}
t.Errorf(" "+format, args...)
}
if !matchOut(tt.want.out, out) {
errorf("got out=%v, want %v", out, tt.want.out)
}
if !bytes.Equal(tt.want.bytesOut, bytesOut) {
errorf("got bytesOut=%q, want %q", bytesOut, tt.want.bytesOut)
}
if !matchErr(tt.want.err, err) {
errorf("got err=%v, want %v", err, tt.want.err)
}
ev.Close()
}
}
func evalAndCollect(t *testing.T, ev *Evaler, texts []string, chsize int) ([]interface{}, []byte, error) {
// Collect byte output
bytesOut := []byte{}
pr, pw, _ := os.Pipe()
bytesDone := make(chan struct{})
go func() {
for {
var buf [64]byte
nr, err := pr.Read(buf[:])
bytesOut = append(bytesOut, buf[:nr]...)
if err != nil {
break
}
}
close(bytesDone)
}()
// Channel output
outs := []interface{}{}
// Eval error. Only that of the last text is saved.
var ex error
for i, text := range texts {
name := fmt.Sprintf("test%d.elv", i)
src := NewScriptSource(name, name, text)
op := mustParseAndCompile(t, ev, src)
outCh := make(chan interface{}, chsize)
outDone := make(chan struct{})
go func() {
for v := range outCh {
outs = append(outs, v)
}
close(outDone)
}()
ports := []*Port{
{File: os.Stdin, Chan: ClosedChan},
{File: pw, Chan: outCh},
{File: os.Stderr, Chan: BlackholeChan},
}
ex = ev.eval(op, ports, src)
close(outCh)
<-outDone
}
pw.Close()
<-bytesDone
pr.Close()
return outs, bytesOut, ex
}
func mustParseAndCompile(t *testing.T, ev *Evaler, src *Source) Op {
n, err := parse.Parse(src.name, src.code)
if err != nil {
t.Fatalf("Parse(%q) error: %s", src.code, err)
}
op, err := ev.Compile(n, src)
if err != nil {
t.Fatalf("Compile(Parse(%q)) error: %s", src.code, err)
}
return op
}
func matchOut(want, got []interface{}) bool {
if len(got) == 0 && len(want) == 0 {
return true
}
if len(got) != len(want) {
return false
}
for i := range got {
if !vals.Equal(got[i], want[i]) {
return false
}
}
return true
}
func matchErr(want, got error) bool {
if got == nil {
return want == nil
}
return want == errAny || reflect.DeepEqual(got.(*Exception).Cause, want)
}
// MustMkdirAll calls os.MkdirAll and panics if an error is returned. It is
// mainly useful in tests.
func MustMkdirAll(name string, perm os.FileMode) {
err := os.MkdirAll(name, perm)
if err != nil {
panic(err)
}
}
// MustCreateEmpty creates an empty file, and panics if an error occurs. It is
// mainly useful in tests.
func MustCreateEmpty(name string) {
file, err := os.Create(name)
if err != nil {
panic(err)
}
file.Close()
}
// MustWriteFile calls ioutil.WriteFile and panics if an error occurs. It is
// mainly useful in tests.
func MustWriteFile(filename string, data []byte, perm os.FileMode) {
err := ioutil.WriteFile(filename, data, perm)
if err != nil {
panic(err)
}
}
// InTempHome is like util.InTempDir, but it also sets HOME to the temporary
// directory when f is called.
func InTempHome(f func(string)) {
util.InTempDir(func(tmpHome string) {
oldHome := os.Getenv("HOME")
os.Setenv("HOME", tmpHome)
f(tmpHome)
os.Setenv("HOME", oldHome)
})
}
|