Documentation ¶
Overview ¶
Package fakeio is a small library to fake stdout/stderr/stdin mainly for unit testing.
Following example fakes stdout/stderr/stdin. Set 'from stdin!' as stdin and get output from both stdout and stderr as string.
f := fakeio.Stdout().Stderr().Stdin("from stdin!") fromInput, err := bufio.NewReader(os.Stdin).ReadString('!') if err != nil { f.Restore() panic(err) } fmt.Println("from stdout!") fmt.Fprintln(os.Stderr, "from stderr!") fromOutput, err := f.String() if err != nil { f.Restore() panic(err) } // 'defer' is better, but here it's unavailable due to output test f.Restore() // Output: from stdin! fmt.Println(fromInput) // Output: // from stdout! // from stderr! fmt.Println(fromOutput)
Please read example/example_test.go to see live examples.
If you find some bugs, please report it to repository page: https://github.com/rhysd/go-fakeio
Index ¶
- type FakedIO
- func (fake *FakedIO) Bytes() ([]byte, error)
- func (fake *FakedIO) CloseStdin() *FakedIO
- func (fake *FakedIO) Do(f func()) (string, error)
- func (fake *FakedIO) Err() error
- func (fake *FakedIO) Read(p []byte) (int, error)
- func (fake *FakedIO) Restore()
- func (fake *FakedIO) Stderr() *FakedIO
- func (fake *FakedIO) Stdin(in string) *FakedIO
- func (fake *FakedIO) StdinBytes(in []byte) *FakedIO
- func (fake *FakedIO) Stdout() *FakedIO
- func (fake *FakedIO) String() (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FakedIO ¶
type FakedIO struct {
// contains filtered or unexported fields
}
FakedIO represents state of faking stdout/stderr/stdin. Restore() must be called finally to restore the state.
func Stderr ¶
func Stderr() *FakedIO
Stderr starts to fake stderr and returns fakedIO object to restore input/output finally
func Stdin ¶
Stdin sets given string as faked stdin input and returns fakedIO object to restore input/output finally
func StdinBytes ¶
StdinBytes sets given bytes as faked stdin input and returns fakedIO object to restore input/output finally
func Stdout ¶
func Stdout() *FakedIO
Stdout starts to fake stdout and returns fakedIO object to restore input/output finally
func (*FakedIO) CloseStdin ¶
CloseStdin closes faked stdin
func (*FakedIO) Restore ¶
func (fake *FakedIO) Restore()
Restore restores faked stdin/stdout/stderr. This must be called finally.
func (*FakedIO) StdinBytes ¶
StdinBytes sets input buffer for stdin with bytes