testutil

package
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: GPL-3.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	YT        = "3V8rpJDpbKg"                          // YTID is an example YouTube video ID
	UID       = "123e4567-e89b-12d3-a456-426614174000" // UUID is a generic Universal Unique ID
	UID4      = "bb2310e1-93aa-475e-8b88-59eb1fb984a4" // UID4 is a UUID version 4
	SCREENPNG = 328_468                                // SCREENPNG is the byte file size of testdata/SCREEN.PNG
	// RTF is an example of Rich-Text-Format encoded text.
	RTF = `{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
 This is some {\b bold} text.\par
}`
)

Variables

View Source
var Content1 = [...]string{
	"file1.nfo",
	"file1.txt",
	"file1.unp",
	"file1.doc",
}
View Source
var Content2 = [...]string{
	"file.diz",
	"file.asc",
	"file.1st",
	"group2.dox",
}
View Source
var Content3 = [...]string{
	"file3.nfo",
	"file.txt",
	"file30.unp",
	"file3x.doc",
	"filex3.diz",
	"file3.asc",
	"file3.1st",
	"file3.dox",
}

Functions

func CopyPNG

func CopyPNG(tb testing.TB, dest string)

CopyPNG copies a sample PNG image to the target destination.

func CopyTXT

func CopyTXT(tb testing.TB, dest string)

CopyTXT copies a sample textfile to the target destination.

func CountTest

func CountTest() int

CountTest returns the number of TestData entries that have a filename prefix of "TEST." (test plus a dot).

func DB

func DB(tb testing.TB) *sql.DB

DB returns a shared Postgres database connection for use with tests. If no connection can be made, the test is skipped.

func DOSBIN

func DOSBIN(tb testing.TB, s string) [4000]byte

DOSBIN converts the string into a single page, MS-DOS BIN binary text. If s is left empty, "hello world" will be used. Long strings will be cropped to fit the 80 column * 25 row per page limit.

func EmbedFS

func EmbedFS(tb testing.TB) fs.FS

EmbedFS returns the embed file system of the testdata subdirectory.

func Middleware

func Middleware(tb testing.TB, e *echo.Echo, target string) *echo.Context

Middleware returns an *echo.Context for a GET request to target. If no target is provided, it is set to root "/".

The echo.Echo can be provided using echo.New.

func MiddlewareStatus

func MiddlewareStatus(tb testing.TB, e *echo.Echo, target string, status int) *echo.Context

MiddlewareStatus returns an *echo.Context for a GET request to target with the response status header already written.

The echo.Echo can be provided using echo.New.

func MkDOSBIN

func MkDOSBIN(tb testing.TB, name, s string) (string, error)

MkDOSBIN converts the string into a single line, MS-DOS BIN file saved to the named file and placed in a temporary test directory. The file location is returned.

See DOSBIN.

func NewContext

func NewContext(tb testing.TB, target string) *echo.Context

NewContext creates a new instance of Echo and returns an *echo.Context for a GET request to target. If no target is provided, it is set to root "/". Query params can also be provided using target, for example:

target="/myurl?id=1&name=hello"

func NewFile

func NewFile(tb testing.TB, target, fieldname, filename string) *echo.Context

NewFile creates a new Echo context with a multipart POST containing an embedded textfile upload.

  • fieldname is the named input of the form containing the upload.
  • filename is the original filename of the file upload.
  • The upload contains the following string, "Hello world!".

func NewFileInputs

func NewFileInputs(tb testing.TB, target, fieldname, filename string, formInputs Input,
) *echo.Context

NewFileInputs functions the same as NewFile but also allows for additional form inputs using Input, for example:

inputs := testutil.Input{
	"id":   "1",
	"uuid": "123e4567-e89b-12d3-a456-426614174000",
}

func NewInput

func NewInput(tb testing.TB, target, key, value string) *echo.Context

NewInput creates a new instance of Echo and returns an *echo.Context for a POST request with a single application/x-www-form-urlencoded form key and value. If no target is provided, it is set to root "/".

func NewInputs

func NewInputs(tb testing.TB, target string, formInputs Input) *echo.Context

NewInputs creates a new Echo context for a multipart/form-data POST request populated with the form fields in formInputs.

func NewInputsPath

func NewInputsPath(tb testing.TB, target string, formInputs Input, pathValues echo.PathValues) *echo.Context

NewInputsPath functions as both NewInputs and NewPath.

func NewModel

func NewModel(tb testing.TB) *models.File

NewModel returns a new models.File entry with the following boilerplate:

  • ID: 1
  • UUID: bb2310e1-93aa-475e-8b88-59eb1fb984a4
  • Filename: filename.txt
  • Filesize: 9876

func NewPath

func NewPath(tb testing.TB, target string, pathValues echo.PathValues) *echo.Context

NewPath creates a new Echo context for a GET request with a collection of echo.PathValues, providing path parameters using Name and Value pairs.

func OpenFS

func OpenFS(tb testing.TB) fs.FS

OpenFS opens the root directory of this Go project as an fs.FS and closes it on cleanup.

func OpenRoot

func OpenRoot(tb testing.TB) *os.Root

OpenRoot opens the root directory of this Go project and closes it on cleanup.

func Project

func Project() string

Project returns the absolute path to the root directory of this project repository.

func ProjectFS

func ProjectFS(tb testing.TB) fs.FS

ProjectFS opens the root directory of this Go project as an fs.FS and closes it on cleanup.

func Testdata

func Testdata() string

Testdata returns the absolute representation to the "testdata/" subdirectory found in this testutil package.

func Tx

func Tx(tb testing.TB) *sql.Tx

Tx returns a shared Postgres database connection for use with tests. The transaction with any database edits get rolled back during the test cleanup. If no connection can be made, the test is skipped.

Note: Only one Tx should be used per-test otherwise the test may never resolve.

Types

type ANSITest

type ANSITest struct {
	Name string
	Data []byte
}

func ANSITests

func ANSITests(tb testing.TB) []ANSITest

ANSITests generates a set of testing data containing randomized data embedded with ANSI escape codes. Being intended for use in tests requiring detection and removal.

type Input

type Input map[string]string

type Logger

type Logger struct {
	Log *slog.Logger
	// contains filtered or unexported fields
}

func Buffer

func Buffer(tb testing.TB) *Logger

Buffer returns a concurrency-safe debug logger that writes to Logger.

func (*Logger) Contains

func (l *Logger) Contains(substr string) bool

Contains returns true if the substr is found in the content of the logger.

func (*Logger) Reset

func (l *Logger) Reset()

Reset empties the content of the logger.

func (*Logger) String

func (l *Logger) String() string

String returns the content of the logger.

type TestData

type TestData = map[string]Testfile // TestData is a map of the filenames found in the "testdata/" subdirectory of testutil.

func Files

func Files() TestData

Files returns a map of the TestData.

type Testfile

type Testfile string // Testfile is the key used to reference TestData.

func FileData

func FileData(key string) Testfile

FileData returns the filename of the key testdata.

For example:

FileData("logotxt") = "LOGO.TXT"

func (Testfile) Abs

func (tf Testfile) Abs() string

Abs returns an absolute representation of the testfile path. If the testfile is empty, it returns an empty string.

func (Testfile) Dir

func (tf Testfile) Dir() string

Dir returns the absolute directory path containing the testfile. If the testfile is empty, it returns an empty string.

Jump to

Keyboard shortcuts

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