github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/compile_value_test.go (about) 1 package eval 2 3 import ( 4 "testing" 5 6 "github.com/u-root/u-root/cmds/core/elvish/eval/vals" 7 ) 8 9 var valueTests = []Test{ 10 // Compounding 11 // ----------- 12 That("put {fi,elvi}sh{1.0,1.1}").Puts( 13 "fish1.0", "fish1.1", "elvish1.0", "elvish1.1"), 14 15 // List, Map and Indexing 16 // ---------------------- 17 18 That("echo [a b c] [&key=value] | each $put~").Puts( 19 "[a b c] [&key=value]"), 20 That("put [a b c][2]").Puts("c"), 21 That("put [&key=value][key]").Puts("value"), 22 23 // String Literals 24 // --------------- 25 That(`put 'such \"''literal'`).Puts(`such \"'literal`), 26 That(`put "much \n\033[31;1m$cool\033[m"`).Puts( 27 "much \n\033[31;1m$cool\033[m"), 28 29 // Captures 30 // --------- 31 32 // Output capture 33 That("put (put lorem ipsum)").Puts("lorem", "ipsum"), 34 That("put (print \"lorem\nipsum\")").Puts("lorem", "ipsum"), 35 36 // Exception capture 37 That("bool ?(nop); bool ?(e:false)").Puts(true, false), 38 39 // Variable Use 40 // ------------ 41 42 // Compounding 43 That("x='SHELL'\nput 'WOW, SUCH '$x', MUCH COOL'\n").Puts( 44 "WOW, SUCH SHELL, MUCH COOL"), 45 // Splicing 46 That("x=[elvish rules]; put $@x").Puts("elvish", "rules"), 47 48 // Wildcard; see testmain_test.go for FS setup 49 // ------------------------------------------- 50 51 That("put *").PutsStrings(fileListing), 52 That("put a/b/nonexistent*").ErrorsWith(ErrWildcardNoMatch), 53 That("put a/b/nonexistent*[nomatch-ok]").DoesNothing(), 54 55 // Character set and range 56 That("put ?[set:ab]*").PutsStrings(getFilesWithPrefix("a", "b")), 57 That("put ?[range:a-c]*").PutsStrings(getFilesWithPrefix("a", "b", "c")), 58 That("put ?[range:a~c]*").PutsStrings(getFilesWithPrefix("a", "b")), 59 That("put *[range:a-z]").Puts("bar", "dir", "foo", "ipsum", "lorem"), 60 61 // Exclusion 62 That("put *[but:foo][but:lorem]").PutsStrings(getFilesBut("foo", "lorem")), 63 64 // Tilde 65 // ----- 66 That("h=$E:HOME; E:HOME=/foo; put ~ ~/src; E:HOME=$h").Puts("/foo", "/foo/src"), 67 68 // Closure 69 // ------- 70 71 That("[]{ }").DoesNothing(), 72 That("[x]{put $x} foo").Puts("foo"), 73 74 // Variable capture 75 That("x=lorem; []{x=ipsum}; put $x").Puts("ipsum"), 76 That("x=lorem; []{ put $x; x=ipsum }; put $x").Puts("lorem", "ipsum"), 77 78 // Shadowing 79 That("x=ipsum; []{ local:x=lorem; put $x }; put $x").Puts("lorem", "ipsum"), 80 81 // Shadowing by argument 82 That("x=ipsum; [x]{ put $x; x=BAD } lorem; put $x").Puts("lorem", "ipsum"), 83 84 // Closure captures new local variables every time 85 That(`fn f []{ x=0; put []{x=(+ $x 1)} []{put $x} } 86 {inc1,put1}=(f); $put1; $inc1; $put1 87 {inc2,put2}=(f); $put2; $inc2; $put2`).Puts("0", "1", "0", "1"), 88 89 // Rest argument. 90 That("[x @xs]{ put $x $xs } a b c").Puts("a", vals.MakeList("b", "c")), 91 // Options. 92 That("[a &k=v]{ put $a $k } foo &k=bar").Puts("foo", "bar"), 93 // Option default value. 94 That("[a &k=v]{ put $a $k } foo").Puts("foo", "v"), 95 } 96 97 func TestValue(t *testing.T) { 98 runTests(t, valueTests) 99 }