fakeio

package
v0.30.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 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("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.

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.

Jump to

Keyboard shortcuts

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