Documentation ¶
Overview ¶
Package fakeio contains fake implementations of interfaces from package io from the standard library.
It is recommended to fill all methods that shouldn't be called with:
panic("not implemented")
in the body of the test, so that if the method is called the panic backtrace points to the method definition in the test. See the package example.
Example ¶
package main import ( "fmt" "io" "slices" "github.com/AdguardTeam/golibs/testutil/fakeio" ) func main() { var written []byte fakeWriter := &fakeio.Writer{ OnWrite: func(b []byte) (n int, err error) { written = slices.Clone(b) return len(b), nil }, } // The function that is expected to call Write. testedFunction := func(w io.Writer) (err error) { _, err = io.WriteString(w, "test message") if err != nil { return fmt.Errorf("writing: %w", err) } return nil } // A simulation of a successful test. gotErr := testedFunction(fakeWriter) fmt.Printf("written: %v %q\n", gotErr, written) }
Output: written: <nil> "test message"
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Closer ¶ added in v0.23.1
type Closer struct {
OnClose func() (err error)
}
Closer is the io.Closer implementation for tests.
Click to show internal directories.
Click to hide internal directories.