github.com/elves/Elvish@v0.12.0/eval/compile_op_test.go (about)

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