mock

package
v0.54.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

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 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

func Stderr() *os.File

Stderr mock standard error to temporary file.

func Stdin

func Stdin() *os.File

Stdin mock the standar input using temporary file.

func Stdout

func Stdout() *os.File

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.

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

type ReadWriter struct {
	BufRead  bytes.Buffer
	BufWrite bytes.Buffer
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL