ziotest

package
v0.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: May 18, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ASCII = []byte{
		32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
		52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
		72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
		92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
		112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
	}
	CharsetAscii   = string(ASCII)
	CharsetDigit   = "0123456789"
	CharsetLetter1 = "abcdefghijklmnopqrstuvwxyz"
	CharsetLetter2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	CharsetLetter  = CharsetLetter1 + CharsetLetter2
	CharsetSymbol  = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
)

Functions

func CharsetReader

func CharsetReader(charset string, loop bool) io.Reader

CharsetReader returns io.Reader that reads from the given charset. The reader reads the charset repeatedly if the loop is true.

Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	// r should returns "abc" repeatedly.
	r := ziotest.CharsetReader("abc", true)

	buf := make([]byte, 9)
	fmt.Println(r.Read(buf))
	fmt.Println(string(buf))
}
Output:

9 <nil>
abcabcabc

func ErrReadCloser

func ErrReadCloser(r io.Reader, err error) io.ReadCloser

ErrReadCloser returns the given error when io.ReadCloser.Close is called for the returned ReadCloser.

Example
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	r := strings.NewReader("abc")
	erc := ziotest.ErrReadCloser(r, os.ErrClosed)

	fmt.Println(erc.Close())
}
Output:

file already closed

func ErrReader

func ErrReader(r io.Reader, n int64) io.Reader

ErrReader returns an io.Reader that returns io.ErrClosedPipe after n bytes read. The returned reader returns the error if the inner io.Reader returned an error before n bytes were read. Use ErrReaderWith to specify returned error instead of io.ErrClosedPipe. nil reader will cause panic.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	// er should return the first 10 characters from the r.
	r := strings.NewReader("abcdefghijklmnopqrstuvwxyz")
	er := ziotest.ErrReader(r, 10)

	buf := make([]byte, 4)
	fmt.Println(er.Read(buf))
	fmt.Println(er.Read(buf))
	fmt.Println(er.Read(buf))
}
Output:

4 <nil>
4 <nil>
2 io: read/write on closed pipe
Example (InnerError)
package main

import (
	"fmt"
	"io"
	"testing/iotest"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	// The internal reader r returns error.
	r := iotest.ErrReader(io.ErrUnexpectedEOF)
	er := ziotest.ErrReader(r, 10)

	buf := make([]byte, 4)
	fmt.Println(er.Read(buf))
}
Output:

0 unexpected EOF

func ErrReaderWith

func ErrReaderWith(r io.Reader, n int64, err error) io.Reader

ErrReaderWith works almost the same as ErrReader except for the point that the returned reader returns the given error after n bytes were read.

Example
package main

import (
	"fmt"
	"io"
	"strings"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	// er should return the EOF error after read 10 characters from the r.
	r := strings.NewReader("abcdefghijklmnopqrstuvwxyz")
	er := ziotest.ErrReaderWith(r, 10, io.EOF)

	buf := make([]byte, 4)
	fmt.Println(er.Read(buf))
	fmt.Println(er.Read(buf))
	fmt.Println(er.Read(buf))
}
Output:

4 <nil>
4 <nil>
2 EOF

func ErrWriteCloser

func ErrWriteCloser(w io.Writer, err error) io.WriteCloser

ErrWriteCloser returns the given error when io.WriteCloser.Close is called for the returned WriteCloser.

Example
package main

import (
	"bytes"
	"fmt"
	"os"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	w := bytes.NewBuffer(nil)
	ewc := ziotest.ErrWriteCloser(w, os.ErrClosed)

	fmt.Println(ewc.Close())
}
Output:

file already closed

func ErrWriter

func ErrWriter(w io.Writer, n int64) io.Writer

ErrWriter returns an io.Writer that returns io.ErrClosedPipe after n bytes written. The returned writer returns the error if the inner io.Writer returned an error before n bytes were written. It ignores w if it is nil. Use ErrWriterWith to specify returned error instead of io.ErrClosedPipe.

Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	// ew should return an error after 10 characters written.
	ew := ziotest.ErrWriter(nil, 10)

	fmt.Println(ew.Write([]byte("abcd")))
	fmt.Println(ew.Write([]byte("abcd")))
	fmt.Println(ew.Write([]byte("abcd")))
	fmt.Println(ew.Write([]byte("abcd")))
}
Output:

4 <nil>
4 <nil>
2 io: read/write on closed pipe
0 io: read/write on closed pipe

func ErrWriterWith

func ErrWriterWith(w io.Writer, n int64, err error) io.Writer

ErrWriterWith works almost the same as ErrWriter except for the point that the returned writer returns the given error after n bytes were written.

Example
package main

import (
	"fmt"
	"io"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	// ew should return an EOF error after 10 characters written.
	ew := ziotest.ErrWriterWith(nil, 10, io.EOF)

	fmt.Println(ew.Write([]byte("abcd")))
	fmt.Println(ew.Write([]byte("abcd")))
	fmt.Println(ew.Write([]byte("abcd")))
	fmt.Println(ew.Write([]byte("abcd")))
}
Output:

4 <nil>
4 <nil>
2 EOF
0 EOF

func ShortReader

func ShortReader(r io.Reader, n int64) io.Reader

ShortReader reads n bytes at maximum. The returned reader does not return any error even the read bytes reached to the n.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	r := strings.NewReader("abcdefg")
	sr := ziotest.ShortReader(r, 3)

	buf := make([]byte, 10)
	n, err := sr.Read(buf)
	fmt.Println(n, string(buf[:n]), err)
}
Output:

3 abc <nil>

func ShortWriter

func ShortWriter(w io.Writer, n int64) io.Writer

ShortWriter accepts n bytes at maximum. The returned writer does not return any error even the written bytes reached to the n.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/aileron-projects/go/ztesting/ziotest"
)

func main() {
	w := bytes.NewBuffer(nil)
	sw := ziotest.ShortWriter(w, 3)

	n, err := sw.Write([]byte("abcdefg"))
	fmt.Println(n, w.String(), err)
}
Output:

3 abc <nil>

Types

type Charset

type Charset string

Jump to

Keyboard shortcuts

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