Documentation
¶
Index ¶
- Variables
- func AssertDeepEqual[T any](t *testing.T, want, got T) bool
- func AssertEqual[T comparable](t *testing.T, want, got T) bool
- func AssertEqualErr(t *testing.T, want, got error) bool
- func ReplaceRandReader(r io.Reader) (done func())
- func ReplaceStderr() (r *os.File, done func())
- func ReplaceStdin() (w *os.File, done func())
- func ReplaceStdout() (r *os.File, done func())
- type MaxReader
- type MaxWriter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMaxRead tell that the read bytes reached maximum. ErrMaxRead = errors.New("go-tester/tester: max readable byte reached") )
var ( // ErrMaxWritten tell that the written bytes reached maximum. ErrMaxWritten = errors.New("go-tester/tester: max writable byte reached") )
Functions ¶
func AssertDeepEqual ¶
AssertDeepEqual asserts that the given values are deeply equal. reflect.DeepEqual is used for comparison. AssertDeepEqual returns true if two value are deeply equal.
func AssertEqual ¶
func AssertEqual[T comparable](t *testing.T, want, got T) bool
AssertEqual asserts that the given values are equal. `==` operator is used for comparison. AssertEqual returns true if two value are equal.
func AssertEqualErr ¶
AssertEqualErr asserts that the given errors are equal. errors.Is is used for comparison. Given got will be unwrapped and compare with want in the errors.Is. AssertEqualErr returns true if two errors are equal.
func ReplaceRandReader ¶
ReplaceRandReader replaces the global variable rand.Reader with r. Call done after tests finished to set the original Reader back. Do not run tests parallel until the done was called.
Example:
done := tester.ReplaceRandReader(YourReader) defer done() // Your test codes here.
func ReplaceStderr ¶
ReplaceStderr replaces the global variable os.Stderr and return reader. Call done after tests finished to set the original Stderr back. Do not run tests parallel until the done was called.
Example:
r, done := tester.ReplaceStderr() defer done() // Your test codes here.
func ReplaceStdin ¶
ReplaceStdin replaces the global variable os.Stdin and return writer. Call done after tests finished to set the original Stdin back. Do not run tests parallel until the done was called.
Example:
w, done := tester.ReplaceStdin() defer done() // Your test codes here.
func ReplaceStdout ¶
ReplaceStdout replaces the global variable os.Stdout and return reader. Call done after tests finished to set the original Stdout back. Do not run tests parallel until the done was called.
Example:
r, done := tester.ReplaceStdout() defer done() // Your test codes here.
Types ¶
type MaxReader ¶
type MaxReader struct {
// contains filtered or unexported fields
}
MaxReader is a io.Reader that can read n bytes at maximum. Use NewMaxReader to create a new reader.
func MaxErrorReader ¶
MaxErrorReader is the alias for NewMaxReader(r, n, ErrMaxRead).
func MaxSilentReader ¶
MaxSilentReader is the alias for NewMaxReader(r, n, nil).
func NewMaxReader ¶
NewMaxReader returns a new MaxReader that read n bytes at maximum. When the read bytes reached n, the reader returns n and err.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/aileron-projects/go-tester"
)
func main() {
s := strings.NewReader("1234567890")
r := tester.NewMaxReader(s, 5, tester.ErrMaxRead)
buf := make([]byte, 3)
fmt.Println(r.Read(buf))
fmt.Println(r.Read(buf))
fmt.Println(r.Read(buf))
}
Output: 3 <nil> 2 go-tester/tester: max readable byte reached 0 go-tester/tester: max readable byte reached
type MaxWriter ¶
type MaxWriter struct {
// contains filtered or unexported fields
}
MaxWriter is a io.Writer that accepts n bytes at maximum. Use NewMaxWriter to create a new writer.
func MaxErrorWriter ¶
MaxErrorWriter is the alias for NewMaxWriter(n, ErrMaxWritten).
func MaxSilentWriter ¶
MaxSilentWriter is the alias for NewMaxWriter(n, nil).
func NewMaxWriter ¶
NewMaxWriter returns a new MaxWriter that accepts n bytes at maximum. When the written bytes reached n, the writer returns n and err.
Example ¶
package main
import (
"fmt"
"github.com/aileron-projects/go-tester"
)
func main() {
w := tester.NewMaxWriter(5, tester.ErrMaxWritten)
fmt.Println(w.Write([]byte("123")))
fmt.Println(w.Write([]byte("456")))
fmt.Println(w.Write([]byte("789")))
}
Output: 3 <nil> 2 go-tester/tester: max writable byte reached 0 go-tester/tester: max writable byte reached