github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/eval/eval_test.go (about)

     1  package eval
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  	"syscall"
     7  	"testing"
     8  
     9  	"github.com/u-root/u-root/cmds/elvish/eval/vals"
    10  )
    11  
    12  func TestBuiltinPid(t *testing.T) {
    13  	pid := strconv.Itoa(syscall.Getpid())
    14  	builtinPid := vals.ToString(builtinNs["pid"].Get())
    15  	if builtinPid != pid {
    16  		t.Errorf(`ev.builtin["pid"] = %v, want %v`, builtinPid, pid)
    17  	}
    18  }
    19  
    20  func TestMiscEval(t *testing.T) {
    21  	runTests(t, []Test{
    22  		// Pseudo-namespaces local: and up:
    23  		That("x=lorem; { local:x=ipsum; put $up:x $local:x }").Puts(
    24  			"lorem", "ipsum"),
    25  		That("x=lorem; { up:x=ipsum; put $x }; put $x").Puts("ipsum", "ipsum"),
    26  		// Pseudo-namespace E:
    27  		That("E:FOO=lorem; put $E:FOO").Puts("lorem"),
    28  		That("del E:FOO; put $E:FOO").Puts(""),
    29  	})
    30  }
    31  
    32  func testMultipleEval(t *testing.T) {
    33  	texts := []string{"x=hello", "put $x"}
    34  	outs, _, err := evalAndCollect(NewEvaler(), texts, 1)
    35  	wantOuts := []interface{}{"hello"}
    36  	if err != nil {
    37  		t.Errorf("eval %s => %v, want nil", texts, err)
    38  	}
    39  	if !reflect.DeepEqual(outs, wantOuts) {
    40  		t.Errorf("eval %s outputs %v, want %v", texts, outs, wantOuts)
    41  	}
    42  }
    43  
    44  func BenchmarkOutputCaptureOverhead(b *testing.B) {
    45  	op := Op{funcOp(func(*Frame) error { return nil }), 0, 0}
    46  	benchmarkOutputCapture(op, b.N)
    47  }
    48  
    49  func BenchmarkOutputCaptureValues(b *testing.B) {
    50  	op := Op{funcOp(func(fm *Frame) error {
    51  		fm.ports[1].Chan <- "test"
    52  		return nil
    53  	}), 0, 0}
    54  	benchmarkOutputCapture(op, b.N)
    55  }
    56  
    57  func BenchmarkOutputCaptureBytes(b *testing.B) {
    58  	bytesToWrite := []byte("test")
    59  	op := Op{funcOp(func(fm *Frame) error {
    60  		fm.ports[1].File.Write(bytesToWrite)
    61  		return nil
    62  	}), 0, 0}
    63  	benchmarkOutputCapture(op, b.N)
    64  }
    65  
    66  func BenchmarkOutputCaptureMixed(b *testing.B) {
    67  	bytesToWrite := []byte("test")
    68  	op := Op{funcOp(func(fm *Frame) error {
    69  		fm.ports[1].Chan <- false
    70  		fm.ports[1].File.Write(bytesToWrite)
    71  		return nil
    72  	}), 0, 0}
    73  	benchmarkOutputCapture(op, b.N)
    74  }
    75  
    76  func benchmarkOutputCapture(op Op, n int) {
    77  	ev := NewEvaler()
    78  	defer ev.Close()
    79  	ec := NewTopFrame(ev, NewInternalSource("[benchmark]"), []*Port{{}, {}, {}})
    80  	for i := 0; i < n; i++ {
    81  		pcaptureOutput(ec, op)
    82  	}
    83  }