fakeio

package
v0.35.11 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: Unlicense Imports: 1 Imported by: 0

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(testutil.UnexpectedCall(arg1, arg2))

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.

func (*Closer) Close added in v0.23.1

func (w *Closer) Close() (err error)

Close implements the io.Closer interface for *Closer.

type Reader

type Reader struct {
	OnRead func(b []byte) (n int, err error)
}

Reader is the io.Reader implementation for tests.

func (*Reader) Read

func (w *Reader) Read(b []byte) (n int, err error)

Read implements the io.Reader interface for *Reader.

type Writer

type Writer struct {
	OnWrite func(b []byte) (n int, err error)
}

Writer is the io.Writer implementation for tests.

func (*Writer) Write

func (w *Writer) Write(b []byte) (n int, err error)

Write implements the io.Writer interface for *Writer.

Directories

Path Synopsis
Package fakefs contains fake implementations of interfaces from package io/fs from the standard library.
Package fakefs contains fake implementations of interfaces from package io/fs from the standard library.

Jump to

Keyboard shortcuts

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