mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
eval: Test evalerPorts.
This commit is contained in:
parent
f4e266cf78
commit
15d78cf217
53
eval/evaler_ports_test.go
Normal file
53
eval/evaler_ports_test.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package eval
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEvalerPorts(t *testing.T) {
|
||||
stdoutReader, stdout := mustPipe()
|
||||
defer stdoutReader.Close()
|
||||
|
||||
stderrReader, stderr := mustPipe()
|
||||
defer stderrReader.Close()
|
||||
|
||||
prefix := "> "
|
||||
ep := newEvalerPorts(DevNull, stdout, stderr, &prefix)
|
||||
ep.ports[1].Chan <- String("x")
|
||||
ep.ports[1].Chan <- String("y")
|
||||
ep.ports[2].Chan <- String("bad")
|
||||
ep.ports[2].Chan <- String("err")
|
||||
ep.close()
|
||||
stdout.Close()
|
||||
stderr.Close()
|
||||
|
||||
stdoutAll := mustReadAllString(stdoutReader)
|
||||
wantStdoutAll := "> x\n> y\n"
|
||||
if stdoutAll != wantStdoutAll {
|
||||
t.Errorf("stdout is %q, want %q", stdoutAll, wantStdoutAll)
|
||||
}
|
||||
stderrAll := mustReadAllString(stderrReader)
|
||||
wantStderrAll := "> bad\n> err\n"
|
||||
if stderrAll != wantStderrAll {
|
||||
t.Errorf("stderr is %q, want %q", stderrAll, wantStderrAll)
|
||||
}
|
||||
}
|
||||
|
||||
func mustPipe() (*os.File, *os.File) {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return r, w
|
||||
}
|
||||
|
||||
func mustReadAllString(r io.Reader) string {
|
||||
b, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(b)
|
||||
}
|
Loading…
Reference in New Issue
Block a user