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

     1  package eval
     2  
     3  import "testing"
     4  
     5  var opTests = []Test{
     6  	// Chunks
     7  	// ------
     8  
     9  	// Empty chunk
    10  	That("").DoesNothing(),
    11  	// Outputs of pipelines in a chunk are concatenated
    12  	That("put x; put y; put z").Puts("x", "y", "z"),
    13  	// A failed pipeline cause the whole chunk to fail
    14  	That("put a; e:false; put b").Puts("a").Errors(),
    15  
    16  	// Pipelines
    17  	// ---------
    18  
    19  	// Pure byte pipeline
    20  	That(`echo "Albert\nAllan\nAlbraham\nBerlin" | sed s/l/1/g | grep e`).Prints(
    21  		"A1bert\nBer1in\n"),
    22  	// Pure channel pipeline
    23  	That(`put 233 42 19 | each [x]{+ $x 10}`).Puts("243", "52", "29"),
    24  	// Pipeline draining.
    25  	That(`range 100 | put x`).Puts("x"),
    26  	// TODO: Add a useful hybrid pipeline sample
    27  
    28  	// Assignments
    29  	// -----------
    30  
    31  	// List element assignment
    32  	That("li=[foo bar]; li[0]=233; put $@li").Puts("233", "bar"),
    33  	// Map element assignment
    34  	That("di=[&k=v]; di[k]=lorem; di[k2]=ipsum; put $di[k] $di[k2]").Puts(
    35  		"lorem", "ipsum"),
    36  	That("d=[&a=[&b=v]]; put $d[a][b]; d[a][b]=u; put $d[a][b]").Puts("v", "u"),
    37  	// Multi-assignments.
    38  	That("{a,b}=(put a b); put $a $b").Puts("a", "b"),
    39  	That("@a=(put a b); put $@a").Puts("a", "b"),
    40  	That("{a,@b}=(put a b c); put $@b").Puts("b", "c"),
    41  	//That("di=[&]; di[a b]=(put a b); put $di[a] $di[b]").Puts("a", "b"),
    42  
    43  	// Temporary assignment.
    44  	That("a=alice b=bob; {a,@b}=(put amy ben) put $a $@b; put $a $b").Puts(
    45  		"amy", "ben", "alice", "bob"),
    46  	// Temporary assignment of list element.
    47  	That("l = [a]; l[0]=x put $l[0]; put $l[0]").Puts("x", "a"),
    48  	// Temporary assignment of map element.
    49  	That("m = [&k=v]; m[k]=v2 put $m[k]; put $m[k]").Puts("v2", "v"),
    50  	// Temporary assignment before special form.
    51  	That("li=[foo bar] for x $li { put $x }").Puts("foo", "bar"),
    52  
    53  	// Spacey assignment.
    54  	That("a @b = 2 3 foo; put $a $b[1]").Puts("2", "foo"),
    55  	// Spacey assignment with temporary assignment
    56  	That("x = 1; x=2 y = (+ 1 $x); put $x $y").Puts("1", "3"),
    57  
    58  	// Redirections
    59  	// ------------
    60  
    61  	That("f=(mktemp elvXXXXXX); echo 233 > $f; cat < $f; rm $f").Prints("233\n"),
    62  
    63  	// Redirections from special form.
    64  	That(`f = (mktemp elvXXXXXX);
    65  	for x [lorem ipsum] { echo $x } > $f
    66  	cat $f
    67  	rm $f`).Prints("lorem\nipsum\n"),
    68  
    69  	// Redirections from File object.
    70  	That(`fname=(mktemp elvXXXXXX); echo haha > $fname;
    71  			f=(fopen $fname); cat <$f; fclose $f; rm $fname`).Prints("haha\n"),
    72  
    73  	// Redirections from Pipe object.
    74  	That(`p=(pipe); echo haha > $p; pwclose $p; cat < $p; prclose $p`).Prints(
    75  		"haha\n"),
    76  }
    77  
    78  func TestOp(t *testing.T) {
    79  	runTests(t, opTests)
    80  }