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"
|
2020-01-13 05:52:12 +08:00
|
|
|
"sync"
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 05:52:12 +08:00
|
|
|
func TestConcurrentEval(t *testing.T) {
|
|
|
|
// Run this test with "go test -race".
|
|
|
|
ev := NewEvaler()
|
|
|
|
src := NewInternalElvishSource(true, "[test]", "")
|
2020-04-15 07:04:23 +08:00
|
|
|
op, err := ev.ParseAndCompile(src)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-01-13 05:52:12 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
2020-04-15 07:04:23 +08:00
|
|
|
ev.EvalInTTY(op)
|
2020-01-13 05:52:12 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
2020-04-15 07:04:23 +08:00
|
|
|
ev.EvalInTTY(op)
|
2020-01-13 05:52:12 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2017-08-13 07:17:41 +08:00
|
|
|
func BenchmarkOutputCaptureOverhead(b *testing.B) {
|
2020-04-10 08:12:25 +08:00
|
|
|
benchmarkOutputCapture(b.N, func(fm *Frame) {})
|
2017-08-13 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOutputCaptureValues(b *testing.B) {
|
2020-04-10 08:12:25 +08:00
|
|
|
benchmarkOutputCapture(b.N, func(fm *Frame) {
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.ports[1].Chan <- "test"
|
2020-04-10 08:12:25 +08:00
|
|
|
})
|
2017-08-13 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOutputCaptureBytes(b *testing.B) {
|
|
|
|
bytesToWrite := []byte("test")
|
2020-04-10 08:12:25 +08:00
|
|
|
benchmarkOutputCapture(b.N, func(fm *Frame) {
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.ports[1].File.Write(bytesToWrite)
|
2020-04-10 08:12:25 +08:00
|
|
|
})
|
2017-08-13 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOutputCaptureMixed(b *testing.B) {
|
|
|
|
bytesToWrite := []byte("test")
|
2020-04-10 08:12:25 +08:00
|
|
|
benchmarkOutputCapture(b.N, func(fm *Frame) {
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.ports[1].Chan <- false
|
|
|
|
fm.ports[1].File.Write(bytesToWrite)
|
2020-04-10 08:12:25 +08:00
|
|
|
})
|
2017-08-13 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:12:25 +08:00
|
|
|
func benchmarkOutputCapture(n int, f func(*Frame)) {
|
2017-12-23 07:45:47 +08:00
|
|
|
ev := NewEvaler()
|
2017-12-28 04:43:43 +08:00
|
|
|
defer ev.Close()
|
2020-04-10 08:12:25 +08:00
|
|
|
fm := NewTopFrame(ev, NewInternalGoSource("[benchmark]"), []*Port{{}, {}, {}})
|
2017-08-13 07:17:41 +08:00
|
|
|
for i := 0; i < n; i++ {
|
2020-04-10 08:12:25 +08:00
|
|
|
captureOutput(fm, func(fm *Frame) error {
|
|
|
|
f(fm)
|
|
|
|
return nil
|
|
|
|
})
|
2017-08-13 07:17:41 +08:00
|
|
|
}
|
|
|
|
}
|