Documentation ¶
Overview ¶
Package mock provide a mocking for standard input, standard output, standard error, io.ReadWriter, and [rand.Reader].
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
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.
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) x int n int err error ) rand.Reader = rr for x = 0; x <= len(seed); x++ { n, err = rand.Read(b) if err != nil { log.Fatal(err) } fmt.Println(n, string(b)) } }
Output: 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) 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.