test

package
v0.9.20-beta-20 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package test provides some utilities for construct test files.

  • Case defines test cases stored in JSON files.
  • CountWriter is an io.Writer that counts lines and bytes and then throws them away.
  • Debugf provides a simplistic logging for use in test cases.
  • Various constants and variables for use in multiple testing files.

Index

Constants

View Source
const (
	Message = "This is a message. No, really!"
)

Variables

View Source
var (
	Level = slog.LevelInfo

	Now = time.Now()
)
View Source
var (
	Attributes = []slog.Attr{
		slog.Time("when", Now),
		slog.Duration("howLong", duration),
		slog.String("Goober", "Snoofus"),
		slog.Bool("boolean", true),
		slog.Float64("pi", math.Pi),
		slog.Int("skidoo", 23),
		slog.Int64("minus", -64),
		slog.Uint64("unsigned", 79),
		slog.Any("any", anything),
		slog.Any("ip", ip),
		slog.Any("ipNet", ipNet),
		slog.Any("macAddr", mac),
		slog.Group("group",
			slog.String("name", "Beatles"),
			infra.EmptyAttr(),
			slog.Float64("pi", math.Pi),
			infra.EmptyAttr(),
			slog.Group("subGroup",
				infra.EmptyAttr(),
				slog.String("name", "Rolling Stones"),
				infra.EmptyAttr()))}
	AttributeMap = map[string]any{
		"howLong":  float64(duration),
		"when":     Now.Format(time.RFC3339Nano),
		"Goober":   "Snoofus",
		"boolean":  true,
		"pi":       math.Pi,
		"skidoo":   float64(23),
		"minus":    float64(-64),
		"unsigned": float64(79),
		"any":      anything,
		"ip":       ip.String(),
		"ipNet":    ipNet.String(),
		"macAddr":  mac.String(),
		"group": map[string]any{
			"name": "Beatles",
			"pi":   math.Pi,
			"subGroup": map[string]any{
				"name": "Rolling Stones",
			},
		},
	}
)
View Source
var EscapeCases = map[string]string{
	"3": `3`,
	"The quick brown fox jumped over the lazy dog.":    `The quick brown fox jumped over the lazy dog.`,
	"Control characters:  \b, \f, \n, \r, \t":          `Control characters:  \b, \f, \n, \r, \t`,
	"Quote and slashes: \", \\, /":                     `Quote and slashes: \", \\, \/`,
	"UTF8 Characters: ϢӦֆĒ͖̈́Ͳ  ĦĪǂǼɆψϠѬӜԪ":             `UTF8 Characters: ϢӦֆĒ͖̈́Ͳ  ĦĪǂǼɆψϠѬӜԪ`,
	"Unicode Characters: 😀🐦‍🔥⛓️‍💥🍋‍🟩  ظۇ  ❂✈☯  亳亴亵亶亷亸": `Unicode Characters: 😀🐦‍🔥⛓️‍💥🍋‍🟩  ظۇ  ❂✈☯  亳亴亵亶亷亸`,
}

Functions

func DebugLevel

func DebugLevel() uint

DebugLevel returns the level set by the -debug flag or 0 for default.

func Debugf

func Debugf(level uint, format string, args ...interface{})

Debugf will only print the specified data if the -debug command flag is set. The level field determines whether the statement will be printed. The -debug flag must be greater than or equal to the specified level for printing.

func Files

func Files(t *testing.T, dir string) []string

Files returns a list of file names found in the 'test' subdirectory of the specified directory. If the directory is not provided (the string is empty) then 'testdata' is used.

func Load

func Load(t *testing.T, dir string, file string, tc Case)

Load reads a test case JSON file and unmarshals it into the provided Case instantiation. The pathname is constructed from the named file in the 'test' subdirectory of the specified directory:

<dir>/test/<file>

If the directory is not provided (the string is empty) then 'testdata' is used.

func MemoryTrap

func MemoryTrap(level uint, name string, fn func())

func MemoryTrapErr

func MemoryTrapErr(level uint, name string, fn func() error) error

Types

type BasicCase

type BasicCase struct {
	BasicName   string `json:"Description"`
	BasicSource string `json:"Source"`
	// contains filtered or unexported fields
}

BasicCase provides the common test definition items and methods. Compose the specific test case struct with this struct to inherit. Normally the BasicName and BasicSource fields would be private, but they can't be because of the JSON unmarshaler. Do not reference them externally.

func (*BasicCase) Name

func (bc *BasicCase) Name() string

Name returns the test case name.

func (*BasicCase) Source

func (bc *BasicCase) Source(t *testing.T) string

Source returns the test case source string. If the Source field was empty the data will be loaded from The pathname constructed from the named file in the 'json' subdirectory of the specified directory:

<dir>/json/<file>

where the dir was specified in the preceding Load call and saved to the basic test case struct.

type Case

type Case interface {
	Name() string

	Source(t *testing.T) string
	// contains filtered or unexported methods
}

Case defines an interface for test case definitions. The interface provides functions to access common data items. Cases are read from JSON files in 'test' subdirectory of the specified test directory. The JSON files must contain the following common fields:

Description                   test name
Source                 test source string

If the Source field is empty the test file name is used to read the source data from a JSON file in the 'json' subdirectory of the specified test directory.

type CountWriter

type CountWriter struct {
	// contains filtered or unexported fields
}

CountWriter is an io.Writer that throws away all input but counts `Write` calls and the number of bytes that would have been written. This is used during benchmarking.

func (*CountWriter) Write

func (cw *CountWriter) Write(p []byte) (n int, err error)

Write supplies the required io.Writer interface method.

func (*CountWriter) Written

func (cw *CountWriter) Written() uint64

Written returns the number of `Write` calls that the `CountWriter` ignored.

Jump to

Keyboard shortcuts

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