2014-01-28 15:02:48 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
2014-09-21 05:39:16 +08:00
|
|
|
"reflect"
|
2014-01-28 15:02:48 +08:00
|
|
|
"strconv"
|
2014-01-29 21:17:04 +08:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
2018-01-01 05:01:21 +08:00
|
|
|
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
2014-01-28 15:02:48 +08:00
|
|
|
)
|
|
|
|
|
2017-06-25 00:08:47 +08:00
|
|
|
func TestBuiltinPid(t *testing.T) {
|
2014-01-28 15:02:48 +08:00
|
|
|
pid := strconv.Itoa(syscall.Getpid())
|
2018-02-15 17:14:05 +08:00
|
|
|
builtinPid := vals.ToString(builtinNs["pid"].Get())
|
2017-06-25 00:08:47 +08:00
|
|
|
if builtinPid != pid {
|
|
|
|
t.Errorf(`ev.builtin["pid"] = %v, want %v`, builtinPid, pid)
|
2014-01-28 15:02:48 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-21 05:39:16 +08:00
|
|
|
|
2018-10-10 00:39:24 +08:00
|
|
|
func TestNumBgJobs(t *testing.T) {
|
|
|
|
Test(t,
|
|
|
|
That("put $num-bg-jobs").Puts("0"),
|
|
|
|
// TODO(xiaq): Test cases where $num-bg-jobs > 0. This cannot be done
|
|
|
|
// with { put $num-bg-jobs }& because the output channel may have
|
|
|
|
// already been closed when the closure is run.
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-12-22 04:49:14 +08:00
|
|
|
func TestMiscEval(t *testing.T) {
|
2018-05-22 08:08:11 +08:00
|
|
|
Test(t,
|
2018-03-03 14:08:09 +08:00
|
|
|
// Pseudo-namespace E:
|
|
|
|
That("E:FOO=lorem; put $E:FOO").Puts("lorem"),
|
|
|
|
That("del E:FOO; put $E:FOO").Puts(""),
|
2018-05-22 08:08:11 +08:00
|
|
|
)
|
2017-08-15 06:57:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleEval(t *testing.T) {
|
|
|
|
texts := []string{"x=hello", "put $x"}
|
2020-01-09 06:39:56 +08:00
|
|
|
r := evalAndCollect(t, NewEvaler(), texts)
|
2018-03-03 14:08:09 +08:00
|
|
|
wantOuts := []interface{}{"hello"}
|
2020-01-09 06:39:56 +08:00
|
|
|
if r.exception != nil {
|
|
|
|
t.Errorf("eval %s => %v, want nil", texts, r.exception)
|
2017-08-15 06:57:36 +08:00
|
|
|
}
|
2020-01-09 06:39:56 +08:00
|
|
|
if !reflect.DeepEqual(r.valueOut, wantOuts) {
|
|
|
|
t.Errorf("eval %s outputs %v, want %v", texts, r.valueOut, wantOuts)
|
2017-08-15 06:57:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 07:17:41 +08:00
|
|
|
func BenchmarkOutputCaptureOverhead(b *testing.B) {
|
2018-09-29 03:45:11 +08:00
|
|
|
op := effectOp{funcOp(func(*Frame) error { return nil }), 0, 0}
|
2017-08-13 07:17:41 +08:00
|
|
|
benchmarkOutputCapture(op, b.N)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOutputCaptureValues(b *testing.B) {
|
2018-09-29 03:45:11 +08:00
|
|
|
op := effectOp{funcOp(func(fm *Frame) error {
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.ports[1].Chan <- "test"
|
2018-01-21 08:25:53 +08:00
|
|
|
return nil
|
2018-01-21 20:49:25 +08:00
|
|
|
}), 0, 0}
|
2017-08-13 07:17:41 +08:00
|
|
|
benchmarkOutputCapture(op, b.N)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOutputCaptureBytes(b *testing.B) {
|
|
|
|
bytesToWrite := []byte("test")
|
2018-09-29 03:45:11 +08:00
|
|
|
op := effectOp{funcOp(func(fm *Frame) error {
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.ports[1].File.Write(bytesToWrite)
|
2018-01-21 08:25:53 +08:00
|
|
|
return nil
|
2018-01-21 20:49:25 +08:00
|
|
|
}), 0, 0}
|
2017-08-13 07:17:41 +08:00
|
|
|
benchmarkOutputCapture(op, b.N)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOutputCaptureMixed(b *testing.B) {
|
|
|
|
bytesToWrite := []byte("test")
|
2018-09-29 03:45:11 +08:00
|
|
|
op := effectOp{funcOp(func(fm *Frame) error {
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.ports[1].Chan <- false
|
|
|
|
fm.ports[1].File.Write(bytesToWrite)
|
2018-01-21 08:25:53 +08:00
|
|
|
return nil
|
2018-01-21 20:49:25 +08:00
|
|
|
}), 0, 0}
|
2017-08-13 07:17:41 +08:00
|
|
|
benchmarkOutputCapture(op, b.N)
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func benchmarkOutputCapture(op effectOp, n int) {
|
2017-12-23 07:45:47 +08:00
|
|
|
ev := NewEvaler()
|
2017-12-28 04:43:43 +08:00
|
|
|
defer ev.Close()
|
2020-01-05 22:07:03 +08:00
|
|
|
ec := NewTopFrame(ev, NewInternalGoSource("[benchmark]"), []*Port{{}, {}, {}})
|
2017-08-13 07:17:41 +08:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
pcaptureOutput(ec, op)
|
|
|
|
}
|
|
|
|
}
|