elvish/eval/builtin_fn_io_test.go

43 lines
1.3 KiB
Go
Raw Normal View History

2017-12-17 13:20:03 +08:00
package eval
import (
"testing"
2018-02-15 17:14:05 +08:00
"github.com/elves/elvish/eval/vals"
)
func TestBuiltinFnIO(t *testing.T) {
Test(t,
That(`put foo bar`).Puts("foo", "bar"),
That(`put $nil`).Puts(nil),
2017-12-17 13:20:03 +08:00
That(`print [foo bar]`).Prints("[foo bar]"),
That(`print foo bar &sep=,`).Prints("foo,bar"),
That(`echo [foo bar]`).Prints("[foo bar]\n"),
That(`pprint [foo bar]`).Prints("[\n foo\n bar\n]\n"),
That(`repr foo bar ['foo bar']`).Prints("foo bar ['foo bar']\n"),
2017-12-17 13:20:03 +08:00
// Baseline for only-{bytes,values}
That(`{ print bytes; put values }`).Prints("bytes").Puts("values"),
That(`{ print bytes; put values } | only-bytes`).Prints("bytes").Puts(),
That(`{ print bytes; put values } | only-values`).Prints("").Puts("values"),
That(`print "a\nb" | slurp`).Puts("a\nb"),
That(`print "a\nb" | from-lines`).Puts("a", "b"),
That(`print "a\nb\n" | from-lines`).Puts("a", "b"),
2018-07-06 08:32:42 +08:00
That(`echo '{"k": "v", "a": [1, 2]}' '"foo"' | from-json`).
Puts(vals.MakeMap("k", "v", "a", vals.MakeList(1.0, 2.0)),
2018-07-06 08:32:42 +08:00
"foo"),
That(`echo '[null, "foo"]' | from-json`).Puts(
vals.MakeList(nil, "foo")),
That(`echo 'invalid' | from-json`).Errors(),
2017-12-17 13:20:03 +08:00
That(`put "l\norem" ipsum | to-lines`).Prints("l\norem\nipsum\n"),
2018-07-06 08:32:42 +08:00
That(`put [&k=v &a=[1 2]] foo | to-json`).
Prints(`{"a":["1","2"],"k":"v"}
2017-12-17 13:20:03 +08:00
"foo"
`),
That(`put [$nil foo] | to-json`).Prints("[null,\"foo\"]\n"),
)
2017-12-17 13:20:03 +08:00
}