package mock
import "git.sr.ht/~shulhan/pakakeh.go/lib/test/mock"
Package mock provide a mocking for standard input, standard output, standard error, io.ReadWriter, and [rand.Reader].
Index ¶
- func Close()
- func Error() string
- func Output() string
- func Reset(truncate bool)
- func ResetStderr(truncate bool)
- func ResetStdin(truncate bool)
- func ResetStdout(truncate bool)
- func Stderr() *os.File
- func Stdin() *os.File
- func Stdout() *os.File
- type RandReader
- func NewRandReader(seed []byte) (r *RandReader)
- func (rr *RandReader) Read(raw []byte) (n int, err error)
- type ReadWriter
Examples ¶
Functions ¶
func Close ¶
func Close()
Close all mocked output and/or error.
func Error ¶
func Error() string
Error get stream of standard error as string.
func Output ¶
func Output() string
Output get stream of standard output.
func Reset ¶
func Reset(truncate bool)
Reset all mocked standard output and error.
func ResetStderr ¶
func ResetStderr(truncate bool)
ResetStderr reset mocked standard error offset back to 0. If truncated is true, it also reset the size to 0.
func ResetStdin ¶
func ResetStdin(truncate bool)
ResetStdin reset mocked standard input offset back to 0. If truncated is true, it also reset the size to 0.
func ResetStdout ¶
func ResetStdout(truncate bool)
ResetStdout reset mocked standard output offset back to 0. If truncated is true, it also reset the size to 0.
func Stderr ¶
Stderr mock standard error to temporary file.
func Stdin ¶
Stdin mock the standar input using temporary file.
func Stdout ¶
Stdout mock standard output to temporary file.
Types ¶
type RandReader ¶
type RandReader struct {
// contains filtered or unexported fields
}
RandReader implement io.Reader for mocking crypto [rand.Reader].
To provide predictable result, the RandReader is seeded with the same
slice of bytes.
A call to Read will fill the passed bytes with those seed.
Code:play
Output:Example¶
package main
import (
"crypto/rand"
"fmt"
"log"
"git.sr.ht/~shulhan/pakakeh.go/lib/test/mock"
)
func main() {
var (
seed = []byte(`123`)
rr = mock.NewRandReader(seed)
b = make([]byte, 8)
n int
err error
)
rand.Reader = rr
for range len(seed) + 1 {
n, err = rand.Read(b)
if err != nil {
log.Fatal(err)
}
fmt.Println(n, string(b))
}
}
8 12312312
8 23232323
8 33333333
8 12312312
func NewRandReader ¶
func NewRandReader(seed []byte) (r *RandReader)
NewRandReader create new random reader using seed as generator. The longer the seed, the longer the random values become unique.
func (*RandReader) Read ¶
func (rr *RandReader) Read(raw []byte) (n int, err error)
Read fill the raw bytes with seed. If raw length larger than the seed, it will be filled with the same seed until all bytes filled.
For example, given seed as "abc" (length is three), and raw length is five, then Read will return "abcab".
type ReadWriter ¶
ReadWriter mock for testing with io.ReadWriter or io.StringWriter. Every call to Read will read from BufRead and every call to Write or WriteString will write to BufWrite.
func (*ReadWriter) Read ¶
func (rw *ReadWriter) Read(b []byte) (n int, err error)
Read read a stream of byte from read buffer.
func (*ReadWriter) Reset ¶
func (rw *ReadWriter) Reset()
Reset reset read and write buffers.
func (*ReadWriter) Write ¶
func (rw *ReadWriter) Write(b []byte) (n int, err error)
Write write a stream of byte into write buffer.
func (*ReadWriter) WriteString ¶
func (rw *ReadWriter) WriteString(s string) (n int, err error)
WriteString write a string into write buffer.
Source Files ¶
mock.go rand_reader.go readwriter.go
- Version
- v0.60.0 (latest)
- Published
- Feb 1, 2025
- Platform
- linux/amd64
- Imports
- 4 packages
- Last checked
- 9 hours ago –
Tools for package owners.