github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/builtin_fn_io_test.go (about) 1 package eval_test 2 3 import ( 4 "testing" 5 6 . "src.elv.sh/pkg/eval/evaltest" 7 "src.elv.sh/pkg/eval/vals" 8 ) 9 10 func TestPut(t *testing.T) { 11 Test(t, 12 That(`put foo bar`).Puts("foo", "bar"), 13 That(`put $nil`).Puts(nil), 14 ) 15 } 16 17 func TestReadUpto(t *testing.T) { 18 Test(t, 19 That("print abcd | read-upto c").Puts("abc"), 20 // read-upto does not consume more than needed 21 That("print abcd | { read-upto c; slurp }").Puts("abc", "d"), 22 // read-upto reads up to EOF 23 That("print abcd | read-upto z").Puts("abcd"), 24 ) 25 } 26 27 func TestReadLine(t *testing.T) { 28 Test(t, 29 That(`print eof-ending | read-line`).Puts("eof-ending"), 30 That(`print "lf-ending\n" | read-line`).Puts("lf-ending"), 31 That(`print "crlf-ending\r\n" | read-line`).Puts("crlf-ending"), 32 That(`print "extra-cr\r\r\n" | read-line`).Puts("extra-cr\r"), 33 ) 34 } 35 36 func TestPrint(t *testing.T) { 37 Test(t, 38 That(`print [foo bar]`).Prints("[foo bar]"), 39 That(`print foo bar &sep=,`).Prints("foo,bar"), 40 ) 41 } 42 43 func TestEcho(t *testing.T) { 44 Test(t, 45 That(`echo [foo bar]`).Prints("[foo bar]\n"), 46 ) 47 } 48 49 func TestPprint(t *testing.T) { 50 Test(t, 51 That(`pprint [foo bar]`).Prints("[\n foo\n bar\n]\n"), 52 ) 53 } 54 55 func TestReprCmd(t *testing.T) { 56 Test(t, 57 That(`repr foo bar ['foo bar']`).Prints("foo bar ['foo bar']\n"), 58 ) 59 } 60 61 func TestShow(t *testing.T) { 62 Test(t, 63 // A sanity test that show writes something. 64 That(`show ?(fail foo) | !=s (slurp) ''`).Puts(true), 65 ) 66 } 67 68 func TestOnlyBytesAndOnlyValues(t *testing.T) { 69 Test(t, 70 // Baseline for only-{bytes,values} 71 That(`{ print bytes; put values }`).Prints("bytes").Puts("values"), 72 That(`{ print bytes; put values } | only-bytes`).Prints("bytes").Puts(), 73 That(`{ print bytes; put values } | only-values`).Prints("").Puts("values"), 74 ) 75 } 76 77 func TestSlurp(t *testing.T) { 78 Test(t, 79 That(`print "a\nb" | slurp`).Puts("a\nb"), 80 ) 81 } 82 83 func TestFromLines(t *testing.T) { 84 Test(t, 85 That(`print "a\nb" | from-lines`).Puts("a", "b"), 86 That(`print "a\nb\n" | from-lines`).Puts("a", "b"), 87 ) 88 } 89 90 func TestFromJson(t *testing.T) { 91 Test(t, 92 That(`echo '{"k": "v", "a": [1, 2]}' '"foo"' | from-json`). 93 Puts(vals.MakeMap("k", "v", "a", vals.MakeList(1.0, 2.0)), 94 "foo"), 95 That(`echo '[null, "foo"]' | from-json`).Puts( 96 vals.MakeList(nil, "foo")), 97 That(`echo 'invalid' | from-json`).Throws(AnyError), 98 ) 99 } 100 101 func TestToLines(t *testing.T) { 102 Test(t, 103 That(`put "l\norem" ipsum | to-lines`).Prints("l\norem\nipsum\n"), 104 ) 105 } 106 107 func TestToJson(t *testing.T) { 108 Test(t, 109 That(`put [&k=v &a=[1 2]] foo | to-json`). 110 Prints(`{"a":["1","2"],"k":"v"} 111 "foo" 112 `), 113 That(`put [$nil foo] | to-json`).Prints("[null,\"foo\"]\n"), 114 ) 115 } 116 117 func TestPrintf(t *testing.T) { 118 Test(t, 119 That(`printf abcd`).Prints("abcd"), 120 121 That(`printf "%s\n%s\n" abc xyz`).Prints("abc\nxyz\n"), 122 That(`printf "%q" "abc xyz"`).Prints(`'abc xyz'`), 123 That(`printf "%q" ['a b']`).Prints(`['a b']`), 124 That(`printf "%v" abc`).Prints("abc"), 125 That(`printf "%#v" "abc xyz"`).Prints(`'abc xyz'`), 126 That(`printf '%5.3s' 3.1415`).Prints(" 3.1"), 127 That(`printf '%5.3s' (float64 3.1415)`).Prints(" 3.1"), 128 129 That(`printf '%t' $true`).Prints("true"), 130 That(`printf '%t' $nil`).Prints("false"), 131 132 That(`printf '%3d' (num 5)`).Prints(" 5"), 133 That(`printf '%3d' 5`).Prints(" 5"), 134 That(`printf '%08b' (num 5)`).Prints("00000101"), 135 That(`printf '%08b' 5`).Prints("00000101"), 136 137 That(`printf '%.1f' 3.1415`).Prints("3.1"), 138 That(`printf '%.1f' (float64 3.1415)`).Prints("3.1"), 139 140 // Does not interpret escape sequences 141 That(`printf '%s\n%s\n' abc xyz`).Prints("abc\\nxyz\\n"), 142 143 // Error cases 144 145 // Float verb with argument that can't be converted to float 146 That(`printf '%f' 1.3x`).Prints("%!f(cannot parse as number: 1.3x)"), 147 // Integer verb with argument that can't be converted to integer 148 That(`printf '%d' 3.5`).Prints("%!d(cannot parse as integer: 3.5)"), 149 // Unsupported verb 150 That(`printf '%A' foo`).Prints("%!A(unsupported formatting verb)"), 151 ) 152 }