You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

github.com/dancsecs/szTest

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/dancsecs/szTest

v0.1.1
Source
Go
Version published
Created
Source

Package sztest

Overview

Provides a single self contained go package of test helpers.

  • got/wnt helper testing
  • 'error' testing
  • 'panic' testing
  • 'os.Stdout' and 'os.Stderr' capture and testing
  • 'package log' capture and testing
  • 'io.Reader and 'io.Writer' interface testing
  • 'os.Args' and 'os.Flag' setup testing
  • temporary directories, files and scripts
  • timestamps

Package sztest only imports core go libraries minimizing your module's dependencies.

Contents

Usage

A'*sztest.Chk' object is created in the test function by calling one of the sztest.Capture* functions and then deferring its Release() method to run on the completion of the test function. Common got/want type testing is provided for all go builtin types as well as some common aliases and interfaces.

Example: General Form

cat ./examples/general_form/example_test.go
package example

import (
    "testing"

    "github.com/dancsecs/sztest"
)

func Test_PASS_GeneralForm(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    s1 := "Value Got/Wnt"
    s2 := "Value Got/Wnt"

    chk.Str(s1, s2)
    chk.Str(s1, s2, "unformatted", " message", " not", " displayed")
    chk.Strf(s1, s2, "formatted %s %s %s", "message", "not", "displayed")
}

func Test_FAIL_GeneralForm(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    chk.FailFast(false) // Do not stop on first problem.

    s1 := "Value Got"
    s2 := "Value Wnt"

    chk.Str(s1, s2)
    chk.Str(s1, s2, "unformatted", " message", " displayed")
    chk.Strf(s1, s2, "formatted %s %s", "message", "displayed")
}
go test -v -cover ./examples/general_form

$\small{\texttt{=== RUN   Test̲PASS̲GeneralForm}}$
$\small{\texttt{--- PASS: Test̲PASS̲GeneralForm (0.0s)}}$
$\small{\texttt{=== RUN   Test̲FAIL̲GeneralForm}}$
$\small{\texttt{    example̲test.go:30: unexpected string:}}$
$\small{\texttt{        \color{magenta}GOT: \color{default}Value \color{darkturquoise}Got\color{default}}}$
$\small{\texttt{        \color{cyan}WNT: \color{default}Value \color{darkturquoise}Wnt\color{default}}}$
$\small{\texttt{    example̲test.go:31: unexpected string:}}$
$\small{\texttt{        \emph{unformatted message displayed}:}}$
$\small{\texttt{        \color{magenta}GOT: \color{default}Value \color{darkturquoise}Got\color{default}}}$
$\small{\texttt{        \color{cyan}WNT: \color{default}Value \color{darkturquoise}Wnt\color{default}}}$
$\small{\texttt{    example̲test.go:32: unexpected string:}}$
$\small{\texttt{        \emph{formatted message displayed}:}}$
$\small{\texttt{        \color{magenta}GOT: \color{default}Value \color{darkturquoise}Got\color{default}}}$
$\small{\texttt{        \color{cyan}WNT: \color{default}Value \color{darkturquoise}Wnt\color{default}}}$
$\small{\texttt{--- FAIL: Test̲FAIL̲GeneralForm (0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: [no statements]}}$
$\small{\texttt{FAIL github.com/dancsecs/sztest/examples/general̲form 0.0s}}$
$\small{\texttt{FAIL}}$

The *sztest.Chk object is created without capturing anything with the Release method being deferred until the function exits (This opening pattern will be used by all test functions). There are three string tests. The first two pass but the third fails producing the highlighted test differences.

Contents

Builtin Got/Wnt Checks

The most basic test is to compare something "Got" from the code being tested to the "Wnt" expected by the test. If the values do not exactly match an error is registered for the test using the (testing.T.Error) function and the error is displayed as exampled above. Got/Want functions are provided for all core data types as well as some aliases and interfaces. The general forms are:

func (*CHK) Type(got, wnt Type, msg ...any) bool
func (*CHK) Typef(got, wnt Type, msgFmt string, msgArgs ...any) bool

/*
Were Type is one of:

    // Basic Types
        Bool,
        Byte,
        Complex64, Complex128
        Float32, Float64,  // Includes extra tolerance parameter.
        Int, Int8, Int16, Int32, Int64,
        Rune, Str,
        Uint, Uint8, Uint16 , Uint32, Uint64,
        Uintptr

    // Aliases.
        Dur  // time.Duration
*/

providing for an optional message and returning true if the test passed.

Contents

Array Slices

Array slice tests are provided for all core data types. The arrays must match exactly (except for float types which have a tolerance argument) otherwise a failure will be registered. The general forms are:

func (*CHK) TypeSlice(got, wnt []Type, msg ...any)
func (*CHK) TypeSlicef(got, wnt []Type, fmtMsg string, msgArgs ...any)

/*
Were Type is one of:

    // Basic Types
        Bool,
        Byte,
        Complex64, Complex128,
        Float32, Float64,  // Includes extra tolerance parameter.
        Int, Int8, Int16, Int32, Int64,
        Rune, Str,
        Uint, Uint8, Uint16 , Uint32, Uint64,
        Uintptr

    // Aliases
        Dur  // time.Duration
*/

with error slices tested with:

// Errors NOTE:  Got/Wnt are different types.
func (*Chk) ErrSlice(got []error, wnt []string, msg ...any) bool

For a complete list of builtin got/wnt slice tests and their helpers see Appendix C: List of got/wnt slice test methods.

Contents

Bounded Intervals

These tests compare a comparable got against a range of values. The general forms are:

// BoundedOption constant type.
type BoundedOption int
const (
    // BoundedOpen (a,b) = { x | a < x < b }.
    BoundedOpen BoundedOption = iota
    // BoundedClosed [a,b] = { x | a ≦ x ≦ b }.
    BoundedClosed
    // BoundedMinOpen (a,b] = { x | a < x ≦ b }.
    BoundedMinOpen
    // BoundedMaxClosed (a,b] = { x | a < x ≦ b }.
    BoundedMaxClosed
    // BoundedMaxOpen [a,b) = { x | a ≦ x < b }.
    BoundedMaxOpen
    // BoundedMinClosed [a,b) = { x | a ≦ x < b }.
    BoundedMinClosed
)

func (*CHK) TypeBounded(got Type, option BoundedOption,  min, max Type, msg ...any)
func (*CHK) TypeBoundedf(got Type, option BoundedOption,  min, max Type, fmtMsg string, msgArgs ...any)

/*
Were Type is one of:

    // Basic Types
        Byte,
        Float32, Float64,
        Int, Int8, Int16, Int32, Int64,
        Rune, Str,
        Uint, Uint8, Uint16 , Uint32, Uint64

    // Aliases
        Dur  // time.Duration
*/

Contents

Unbounded Intervals

These tests compare a comparable got against a range of values. The general forms are:

// UnboundedOption constant type.
type UnboundedOption int
const (
    // UnboundedMinOpen (a,+∞) = { x | x > a }.
    UnboundedMinOpen UnboundedOption = iota
    // UnboundedMinClosed [a,+∞) = { x | x ≧ a }.
    UnboundedMinClosed
    // UnboundedMaxOpen (-∞, b) = { x | x < b }.
    UnboundedMaxOpen
    // UnboundedMaxClosed (-∞, b] = { x | x ≦ b }.
    UnboundedMaxClosed
)

func (*CHK) TypeUnbounded(got Type, option UnboundedOption, bound Type, msg ...any)
func (*CHK) TypeUnboundedf(got Type, option UnboundedOption, bound Type, fmtMsg string, msgArgs ...any)

/*
Were Type is one of:

    // Basic Types
        Byte,
        Float32, Float64,
        Int, Int8, Int16, Int32, Int64,
        Rune, Str,
        Uint, Uint8, Uint16 , Uint32, Uint64

    // Aliases
        Dur  // time.Duration
*/

Contents

Errors

Error conditions are checked using the following method:

func (chk *Chk) Err(got error, want string, msg ...any) bool

and its helper methods:

func (chk *Chk) Errf(got error, want string, msgFmt string, msgArgs ...any) bool
func (chk *Chk) NoErr(got error, msg ...any) bool
func (chk *Chk) NoErrf(got error, msgFmt string, msgArgs ...any) bool

Please note with these methods the got and wnt are different data types with the got being an error and the wnt being a string. So what happens if the error is not nil but empty?

errors.New("")

then the error returned is represented by the constant

const BlankErrorMessage = "sztest.BlankErrorMessage"

Contents

Panics

Insuring that your code properly terminates when it encounters an untenable state is important to verify. To facilitate this the library defines a panic check function:

func (chk *Chk) Panic(gotF func(), want string, msg ...any) bool

where gotF is a function that is expected to issue a panic and wnt is the string representation of the expected panic. An empty ("") wnt string represents that no panic should be thrown. The string

const BlankPanicMessage = "sztest.BlankPanicMessage"

is returned to represent an empty ("") panic was thrown differentiating it from no panic being thrown.

There are three helper functions:

func (chk *Chk) Panicf(gotF func(), want string, msgFmt string, msgArgs ...any) bool
func (chk *Chk) NoPanic(gotF func(), msg ...any) bool
func (chk *Chk) NoPanicf(gotF func(), msgFmt string, msgArgs ...any) bool

Contents

Output

Programs writing to standard outputs (os.Stdout, os.Stderr) and the go log package (which may be distinct from os.Stderr) can have the outputs captured and reviewed as part of testing. This can be to confirm failing conditions are properly logged and reported as part of full testing.

Each can be captured individually or the log package and os.Stderr can be combined together into a single captured feed. Selection of the feeds is instantiated when the check object is initially created. See Appendix A: Capture* creation functions for a complete list.

Contents

IO Interface

The check object implements some io interfaces permitting easy simulation of hard to duplicate error and panic situations. IO interface methods implemented by the *Chk object are:

func (chk *Chk) Seek(_ int64, _ int) (int64, error)
func (chk *Chk) Read(dataBuf []byte) (int, error)
func (chk *Chk) Write(data []byte) (int, error)
func (chk *Chk) Close() error

Each of the above functions can have errors set to be returned on the next call with the following methods:

func (chk *Chk) SetReadError(pos int, err error)
func (chk *Chk) SetWriteError(pos int, err error)
func (chk *Chk) SetSeekError(pos int64, err error)
func (chk *Chk) SetCloseError(err error)

Once set the next call to the corresponding io method (read, Write ,Seek, Close or a composition function) will return the pos and error provided. The error is cleared so subsequent calls result in the default action.

Data for read actions (and position errors) is setup using the following methods:

func (chk *Chk) SetIOReaderData(d ...string)
func (chk *Chk) SetIOReaderError(byteCount int, err error)

The data will be returned (one byte at time) until the data is exhausted resulting in an io.EOF error or the optional byteCount is reached then the supplied error will be returned.

Data written can be retrieved using the following method:

func (chk *Chk) GetIOWriterData() []byte

while a positional error condition can be setup to be returned when the nth byte is written. This is setup with:

func (chk *Chk) SetIOWriterError(n int, err error)

Contents

Arguments And Flags

In order to test main default argument processing, test args and a clean flag environment are implemented with:

func (chk *Chk) SetArgs(progName string, args ...string)

where both os.Args and flags.CommandLine are saved and replaced with the provided args and a NewFlagSet respectively. Original vales are restored when the chk.Release() method is called. NOTE: The new default flag set is set to panicOnError.

Contents

Environment Variables

System environment variables mat be set or deleted using the following:

func (chk *Chk) SetEnv(name, value string)
func (chk *Chk) DelEnv(name string)

Original values are restores when the chk.Release() method is called.

Contents

Temporary directories, files, scripts

Testing underlying os file interfacing code can be somewhat automated by using some builtin helpers. Directories, files and scripts created through *chk methods will be automatically deleted on a successful test. The items are not deleted on failure or if the following helper method is invoked from the test.

func (chk *Chk) KeepTmpFiles()

The default temporary dir for the current test function is both created and identified with:

func (chk *Chk) CreateTmpDir() string

directly (or indirectly by one of these helper functions)

func (chk *Chk) CreateTmpFile(data []byte) string
func (chk *Chk) CreateTmpFileIn(path string, data []byte) string
func (chk *Chk) CreateTmpFileAs(path, fName string, data []byte) string
func (chk *Chk) CreateTmpUnixScript(lines []string) string
func (chk *Chk) CreateTmpUnixScriptIn(path string, lines []string) string
func (chk *Chk) CreateTmpUnixScriptAs(path, fName string, lines []string) string
func (chk *Chk) CreateTmpSubDir(subDirs ...string) string

which all return the path constructed by creating a new sub directory in the default temp directory.

This can be set using the environment variable:

SZTEST_TMP_DIR="/custom/tmp"

or set from within the test with:

func (chk *Chk) SetTmpDir(dir string) string

otherwise it defaults to

os.TempDir()

Permissions used when creating these objects can be defined with the following environment variables

SZTEST_PERM_DIR="0700"
SZTEST_PERM_FILE="0600"
SZTEST_PERM_EXE="0700"

or from within the test with:

func (chk *Chk) SetPermDir(p os.FileMode) os.FileMode
func (chk *Chk) SetPermFile(p os.FileMode) os.FileMode
func (chk *Chk) SetPermExe(p os.FileMode) os.FileMode

Contents

Timestamps

Predictable timestamps are provided to permit full testing of applications using timestamps. In order to facilitate this the application must use its own timestamp function pointer that defaults to be the standardtime.Now function and can be replaced by the the testing clock function (*Chk).ClockNext.

Replacing the internal time.Now method is possible using an external monkey patch library such as go-mpatch using something similar to:

//  ...

import (
   "github.com/dancsecs/sztest"
   "github.com/undefinedlabs/go-mpatch"
)

func Test_UsesTimeStamps(t *testing) {
  chk:=sztest.CaptureStdout(t)
  defer chk.Release()

  patch,err:=mpatch.PatchMethod(time.Now, chk.ClockNext)
  chk.NoErr(err)
  defer func() {
    _ = patch.Unpatch()
  }()

  // Run tests that use golang's default time.Now function.
  // ...
}

Chk.ClockNext may be invoked indirectly with the formatting convenience methods:

func (chk *Chk) ClockNextFmtTime() string
func (chk *Chk) ClockNextFmtDate() string
func (chk *Chk) ClockNextFmtTS() string
func (chk *Chk) ClockNextFmtNano() string
func (chk *Chk) ClockNextFmtCusA() string
func (chk *Chk) ClockNextFmtCusB() string
func (chk *Chk) ClockNextFmtCusC() string

As timestamps are generated they are saved and can be queried with the function:

func (chk *Chk) ClockTick(i int) time.Time

ClockTick returns i'th time returned.

Further chk substitutions can be generated for each timestamp produced including up to three custom date formats with the following constants and methods:

Clock substitutions.

func (chk *Chk) ClockSetSub(i int)

ClockSetSub sets the fields to set substitutions for.

func (chk *Chk) ClockAddSub(i int)

ClockAddSub sets the fields to set substitutions for.

func (chk *Chk) ClockRemoveSub(i int)

ClockRemoveSub resets the fields to set substitutions for.

func (chk *Chk) ClockSetCusA(f string)

ClockSetCusA sets the custom date format to set tick substitution values.

func (chk *Chk) ClockSetCusB(f string)

ClockSetCusB sets the custom date format to set tick substitution values.

func (chk *Chk) ClockSetCusC(f string)

ClockSetCusC sets the custom date format to set tick substitution values.

The time (and increments used between successive timestamps can be set with:

func (chk *Chk) ClockSet(setTime time.Time, inc ...time.Duration) func()

ClockSet set the current test time and optionally sets the increments if provided. It returns a func to reset the clk back to its state when this function was called.

or

func (chk *Chk) ClockOffsetDay(dayOffset int, inc ...time.Duration) func()

ClockOffsetDay adjusts the current clock by the number of specified days with negative numbers representing the past. It returns a func to reset the clk back to its state when this function was called.

or the clock can be adjusted with:

func (chk *Chk) ClockOffset(d time.Duration) func()

ClockOffset moves the current clock by the specified amount. No defined increments are applied and if a clock has not yet been set the current time advanced by the specified amount will be used. Nothing is returned.

while the last time returned can e retrieved with:

func (chk *Chk) ClockLast() time.Time

ClockLast returns the last timestamp generated.

or the formatting convenience methods:

func (chk *Chk) ClockLastFmtTime() string
func (chk *Chk) ClockLastFmtDate() string
func (chk *Chk) ClockLastFmtTS() string
func (chk *Chk) ClockLastFmtNano() string
func (chk *Chk) ClockLastFmtCusA() string
func (chk *Chk) ClockLastFmtCusB() string
func (chk *Chk) ClockLastFmtCusC() string

Contents

Appendices

Appendix A: List of sztest.Capture* Create Functions

func CaptureNothing(t testingT) *Chk

CaptureNothing returns a new sztest object without any logs or standard io being captured.

func CaptureStdout(t testingT) *Chk

CaptureStdout returns a new *sztest.Chk reference capturing:

  • os.Stdout

which must be tested by calling the methods:

  • (*Chk).Stdout(wantLines ...string) bool

before (*Chk).Release() is invoked.

func CaptureLog(t testingT) *Chk

CaptureLog returns a new *sztest.Chk reference capturing:

  • log.Writer() io.Writer

which must be tested by calling the methods:

  • (*Chk).Log(wantLines ...string) bool

before (*Chk).Release() is invoked.

func CaptureLogAndStdout(t testingT) *Chk

CaptureLogAndStdout returns a new *sztest.Chk reference capturing:

  • log.Writer() io.Writer
  • os.Stdout

which must be tested by calling the methods:

  • (*Chk).Log(wantLines ...string) bool
  • (*Chk).Stdout(wantLines ...string) bool

before (*Chk).Release() is invoked.

func CaptureLogAndStderr(t testingT) *Chk

CaptureLogAndStderr returns a new *sztest.Chk reference capturing:

  • log.Writer() io.Writer
  • os.Stderr

which must be tested by calling the methods:

  • (*Chk).Log(wantLines ...string) bool
  • (*Chk).Stderr(wantLines ...string) bool

before (*Chk).Release() is invoked.

func CaptureLogAndStderrAndStdout(t testingT) *Chk

CaptureLogAndStderrAndStdout returns a new *sztest.Chk reference capturing:

  • log.Writer() io.Writer
  • os.Stderr
  • os.Stdout

which must be tested by calling the methods:

  • (*Chk).Log(wantLines ...string) bool
  • (*Chk).Stderr(wantLines ...string) bool
  • (*Chk).Stdout(wantLines ...string) bool

before (*Chk).Release() is invoked.

func CaptureLogWithStderr(t testingT) *Chk

CaptureLogWithStderr returns a new *sztest.Chk reference combining and capturing:

  • (log.Writer() io.Writer) + os.Stderr

which must be tested by calling ONE the methods:

  • (*Chk).Log(wantLines ...string) bool
  • OR
  • (*Chk).Stderr(wantLines ...string) bool

before (*Chk).Release() is invoked.

func CaptureLogWithStderrAndStdout(t testingT) *Chk

CaptureLogWithStderrAndStdout returns a new *sztest.Chk reference capturing:

  • (log.Writer() io.Writer) + os.Stderr
  • os.Stdout

which must be tested by calling ONE the methods:

  • (*Chk).Log(wantLines ...string) bool
  • OR
  • (*Chk).Stderr(wantLines ...string) bool

and the method:

  • (*Chk).Stdout(wantLines ...string) bool

before (*Chk).Release() is invoked.

func CaptureStderr(t testingT) *Chk

CaptureStderr returns a new *sztest.Chk reference capturing:

  • os.Stderr

which must be tested by calling the method:

  • (*Chk).Stderr(wantLines ...string) bool

before (*Chk).Release() is invoked.

func CaptureStderrAndStdout(t testingT) *Chk

CaptureStderrAndStdout returns a new *sztest.Chk reference capturing:

  • os.Stderr
  • os.Stdout

which must be tested by calling the methods:

  • (*Chk).Stderr(wantLines ...string) bool
  • (*Chk).Stdout(wantLines ...string) bool

before (*Chk).Release() is invoked.

Contents

Appendix B: List of got/wnt test methods

Unformatted

func (chk *Chk) Bool(got, want bool, msg ...any) bool
func (chk *Chk) False(got bool, msg ...any) bool
func (chk *Chk) True(got bool, msg ...any) bool
func (chk *Chk) Byte(got, want byte, msg ...any) bool
func (chk *Chk) Complex64(got, want complex64, msg ...any) bool
func (chk *Chk) Complex128(got, want complex128, msg ...any) bool
func (chk *Chk) Float32(got, want, tolerance float32, msg ...any) bool
func (chk *Chk) Float64(got, want, tolerance float64, msg ...any) bool
func (chk *Chk) Int(got, want int, msg ...any) bool
func (chk *Chk) Int8(got, want int8, msg ...any) bool
func (chk *Chk) Int16(got, want int16, msg ...any) bool
func (chk *Chk) Int32(got, want int32, msg ...any) bool
func (chk *Chk) Int64(got, want int64, msg ...any) bool
func (chk *Chk) Rune(got, want rune, msg ...any) bool
func (chk *Chk) Str(got, want string, msg ...any) bool
func (chk *Chk) Uint(got, want uint, msg ...any) bool
func (chk *Chk) Uint8(got, want uint8, msg ...any) bool
func (chk *Chk) Uint16(got, want uint16, msg ...any) bool
func (chk *Chk) Uint32(got, want uint32, msg ...any) bool
func (chk *Chk) Uint64(got, want uint64, msg ...any) bool
func (chk *Chk) Uintptr(got, want uintptr, msg ...any) bool
func (chk *Chk) Dur(got, want time.Duration, msg ...any) bool

Formatted

func (chk *Chk) Boolf(got, want bool, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Falsef(got bool, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Truef(got bool, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Bytef(got, want byte, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Complex64f(got, want complex64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Complex128f(got, want complex128, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Float32f(got, want, tolerance float32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Float64f(got, want, tolerance float64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Intf(got, want int, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int8f(got, want int8, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int16f(got, want int16, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int32f(got, want int32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int64f(got, want int64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Runef(got, want rune, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Strf(got, want string, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uintf(got, want uint, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint8f(got, want uint8, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint16f(got, want uint16, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint32f(got, want uint32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint64f(got, want uint64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uintptrf(got, want uintptr, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Durf(got, want time.Duration, msgFmt string, msgArgs ...any) bool

Pointers and References Unformatted

func (chk *Chk) Nil(got any, msg ...any) bool
func (chk *Chk) NotNil(got any, msg ...any) bool

Pointers and References Formatted

func (chk *Chk) Nilf(got any, msgFmt string, msgArgs ...any) bool
func (chk *Chk) NotNilf(got any, msgFmt string, msgArgs ...any) bool

Contents

Appendix C: List of got/wnt slice test methods

Unformatted Slice

func (chk *Chk) BoolSlice(got, want []bool, msg ...any) bool
func (chk *Chk) ByteSlice(got, want []byte, msg ...any) bool
func (chk *Chk) Complex64Slice(got, want []complex64, msg ...any) bool
func (chk *Chk) Complex128Slice(got, want []complex128, msg ...any) bool
func (chk *Chk) Float32Slice(got, want []float32, tolerance float32, msg ...any) bool
func (chk *Chk) Float64Slice(got, want []float64, tolerance float64, msg ...any) bool
func (chk *Chk) IntSlice(got, want []int, msg ...any) bool
func (chk *Chk) Int8Slice(got, want []int8, msg ...any) bool
func (chk *Chk) Int16Slice(got, want []int16, msg ...any) bool
func (chk *Chk) Int32Slice(got, want []int32, msg ...any) bool
func (chk *Chk) Int64Slice(got, want []int64, msg ...any) bool
func (chk *Chk) RuneSlice(got, want []rune, msg ...any) bool
func (chk *Chk) StrSlice(got, want []string, msg ...any) bool
func (chk *Chk) UintSlice(got, want []uint, msg ...any) bool
func (chk *Chk) Uint8Slice(got, want []uint8, msg ...any) bool
func (chk *Chk) Uint16Slice(got, want []uint16, msg ...any) bool
func (chk *Chk) Uint32Slice(got, want []uint32, msg ...any) bool
func (chk *Chk) Uint64Slice(got, want []uint64, msg ...any) bool
func (chk *Chk) UintptrSlice(got, want []uintptr, msg ...any) bool
func (chk *Chk) DurSlice(got, want []time.Duration, msg ...any) bool
func (chk *Chk) ErrSlice(got []error, want []string, msg ...any) bool

Formatted Slice

func (chk *Chk) BoolSlicef(got, want []bool, msgFmt string, msgArgs ...any) bool
func (chk *Chk) ByteSlicef(got, want []byte, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Complex64Slicef(got, want []complex64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Complex128Slicef(got, want []complex128, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Float32Slicef(got, want []float32, tolerance float32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Float64Slicef(got, want []float64, tolerance float64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) IntSlicef(got, want []int, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int8Slicef(got, want []int8, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int16Slicef(got, want []int16, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int32Slicef(got, want []int32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int64Slicef(got, want []int64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) RuneSlicef(got, want []rune, msgFmt string, msgArgs ...any) bool
func (chk *Chk) StrSlicef(got, want []string, msgFmt string, msgArgs ...any) bool
func (chk *Chk) UintSlicef(got, want []uint, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint8Slicef(got, want []uint8, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint16Slicef(got, want []uint16, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint32Slicef(got, want []uint32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint64Slicef(got, want []uint64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) UintptrSlicef(got, want []uintptr, msgFmt string, msgArgs ...any) bool
func (chk *Chk) DurSlicef(got, want []time.Duration, msgFmt string, msgArgs ...any) bool
func (chk *Chk) ErrSlicef(got []error, want []string, msgFmt string, msgArgs ...any) bool

Contents

Appendix D: List of Bounded and Unbounded Interval tests

Bounded Unformatted

func (chk *Chk) ByteBounded(got byte, option BoundedOption, min, max byte, msg ...any) bool
func (chk *Chk) Float32Bounded(got float32, option BoundedOption, min, max float32, msg ...any) bool
func (chk *Chk) Float64Bounded(got float64, option BoundedOption, min, max float64, msg ...any) bool
func (chk *Chk) IntBounded(got int, option BoundedOption, min, max int, msg ...any) bool
func (chk *Chk) Int8Bounded(got int8, option BoundedOption, min, max int8, msg ...any) bool
func (chk *Chk) Int16Bounded(got int16, option BoundedOption, min, max int16, msg ...any) bool
func (chk *Chk) Int32Bounded(got int32, option BoundedOption, min, max int32, msg ...any) bool
func (chk *Chk) Int64Bounded(got int64, option BoundedOption, min, max int64, msg ...any) bool
func (chk *Chk) RuneBounded(got rune, option BoundedOption, min, max rune, msg ...any) bool
func (chk *Chk) StrBounded(got string, option BoundedOption, min, max string, msg ...any) bool
func (chk *Chk) UintBounded(got uint, option BoundedOption, min, max uint, msg ...any) bool
func (chk *Chk) Uint8Bounded(got uint8, option BoundedOption, min, max uint8, msg ...any) bool
func (chk *Chk) Uint16Bounded(got uint16, option BoundedOption, min, max uint16, msg ...any) bool
func (chk *Chk) Uint32Bounded(got uint32, option BoundedOption, min, max uint32, msg ...any) bool
func (chk *Chk) Uint64Bounded(got uint64, option BoundedOption, min, max uint64, msg ...any) bool
func (chk *Chk) DurBounded(got time.Duration, option BoundedOption, min, max time.Duration, msg ...any) bool

Bounded Formatted

func (chk *Chk) ByteBoundedf(got byte, option BoundedOption, min, max byte, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Float32Boundedf(got float32, option BoundedOption, min, max float32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Float64Boundedf(got float64, option BoundedOption, min, max float64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) IntBoundedf(got int, option BoundedOption, min, max int, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int8Boundedf(got int8, option BoundedOption, min, max int8, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int16Boundedf(got int16, option BoundedOption, min, max int16, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int32Boundedf(got int32, option BoundedOption, min, max int32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int64Boundedf(got int64, option BoundedOption, min, max int64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) RuneBoundedf(got rune, option BoundedOption, min, max rune, msgFmt string, msgArgs ...any) bool
func (chk *Chk) StrBoundedf(got string, option BoundedOption, min, max string, msgFmt string, msgArgs ...any) bool
func (chk *Chk) UintBoundedf(got uint, option BoundedOption, min, max uint, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint8Boundedf(got uint8, option BoundedOption, min, max uint8, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint16Boundedf(got uint16, option BoundedOption, min, max uint16, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint32Boundedf(got uint32, option BoundedOption, min, max uint32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint64Boundedf(got uint64, option BoundedOption, min, max uint64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) DurBoundedf(got time.Duration, option BoundedOption, min, max time.Duration, msgFmt string, msgArgs ...any) bool

Unbounded Unformatted

func (chk *Chk) ByteUnbounded(got byte, option UnboundedOption, bound byte, msg ...any) bool
func (chk *Chk) Float32Unbounded(got float32, option UnboundedOption, bound float32, msg ...any) bool
func (chk *Chk) Float64Unbounded(got float64, option UnboundedOption, bound float64, msg ...any) bool
func (chk *Chk) IntUnbounded(got int, option UnboundedOption, bound int, msg ...any) bool
func (chk *Chk) Int8Unbounded(got int8, option UnboundedOption, bound int8, msg ...any) bool
func (chk *Chk) Int16Unbounded(got int16, option UnboundedOption, bound int16, msg ...any) bool
func (chk *Chk) Int32Unbounded(got int32, option UnboundedOption, bound int32, msg ...any) bool
func (chk *Chk) Int64Unbounded(got int64, option UnboundedOption, bound int64, msg ...any) bool
func (chk *Chk) RuneUnbounded(got rune, option UnboundedOption, bound rune, msg ...any) bool
func (chk *Chk) StrUnbounded(got string, option UnboundedOption, bound string, msg ...any) bool
func (chk *Chk) UintUnbounded(got uint, option UnboundedOption, bound uint, msg ...any) bool
func (chk *Chk) Uint8Unbounded(got uint8, option UnboundedOption, bound uint8, msg ...any) bool
func (chk *Chk) Uint16Unbounded(got uint16, option UnboundedOption, bound uint16, msg ...any) bool
func (chk *Chk) Uint32Unbounded(got uint32, option UnboundedOption, bound uint32, msg ...any) bool
func (chk *Chk) Uint64Unbounded(got uint64, option UnboundedOption, bound uint64, msg ...any) bool
func (chk *Chk) DurUnbounded(got time.Duration, option UnboundedOption, bound time.Duration, msg ...any) bool

Unbounded Formatted

func (chk *Chk) ByteUnboundedf(got byte, option UnboundedOption, bound byte, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Float32Unboundedf(got float32, option UnboundedOption, bound float32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Float64Unboundedf(got float64, option UnboundedOption, bound float64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) IntUnboundedf(got int, option UnboundedOption, bound int, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int8Unboundedf(got int8, option UnboundedOption, bound int8, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int16Unboundedf(got int16, option UnboundedOption, bound int16, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int32Unboundedf(got int32, option UnboundedOption, bound int32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Int64Unboundedf(got int64, option UnboundedOption, bound int64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) RuneUnboundedf(got rune, option UnboundedOption, bound rune, msgFmt string, msgArgs ...any) bool
func (chk *Chk) StrUnboundedf(got string, option UnboundedOption, bound string, msgFmt string, msgArgs ...any) bool
func (chk *Chk) UintUnboundedf(got uint, option UnboundedOption, bound uint, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint8Unboundedf(got uint8, option UnboundedOption, bound uint8, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint16Unboundedf(got uint16, option UnboundedOption, bound uint16, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint32Unboundedf(got uint32, option UnboundedOption, bound uint32, msgFmt string, msgArgs ...any) bool
func (chk *Chk) Uint64Unboundedf(got uint64, option UnboundedOption, bound uint64, msgFmt string, msgArgs ...any) bool
func (chk *Chk) DurUnboundedf(got time.Duration, option UnboundedOption, bound time.Duration, msgFmt string, msgArgs ...any) bool

Contents

Appendix E: Builtin Ansi Terminal Markup

See CONFIGURE.md Appendix E: Builtin Ansi Terminal Markup

Contents

Appendix F: Large Example Function

See Appendix F Example: Large Example Function

Contents

Appendix G: Large Example Main Function

See Appendix G: Large Example Main Function

Contents

Appendix H: License

/* Golang testing utility. Copyright (C) 2023 Leslie Dancsecs

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. */

Contents

Appendix I: To Be Documented

func (chk *Chk) FailFast(failFast bool) bool
func (chk *Chk) Logf(msgFmt string, msgArgs ...any)
func (chk *Chk) Errorf(msgFmt string, msgArgs ...any)
func (chk *Chk) Error(args ...any)
func (chk *Chk) Fatalf(msgFmt string, msgArgs ...any)
func (chk *Chk) Name() string
func (chk *Chk) T() testingT
func (chk *Chk) PushPreReleaseFunc(newFunc func() error)
func (chk *Chk) PushPostReleaseFunc(newFunc func() error)
func (chk *Chk) Release()
func CompareArrays[T chkType](got, wnt []T) string
func (*Chk) LastErr(args ...any) error
func (chk *Chk) Log(wantLines ...string) bool
func (chk *Chk) Stderr(wantLines ...string) bool
func (chk *Chk) Stdout(wantLines ...string) bool
func (chk *Chk) SetStdinData(lines ...string)

Errors can be tested using:

func (chk *Chk) Err(got error, want string, msg ...any) bool

noting that the got and wnt are different types. Finally, checking for nil pointers or nil references can be tested with:

func (chk *Chk) Nil(got any, msg ...any) bool

Some checks have helper functions provided for convenience.

func (chk *Chk) False(got bool, msg ...any) bool
func (chk *Chk) True(got bool, msg ...any) bool
func (chk *Chk) NoErr(got error, msg ...any) bool
func (chk *Chk) NotNil(got any, msg ...any) bool

For a complete list of builtin Got/Wnt tests and their helpers see Appendix B: List of got/wnt Test Methods

Contents

FAQs

Package last updated on 07 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts