testutils

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertError added in v0.51.0

func AssertError(t testing.TB, got error, want any) bool

AssertError verifies that an observed error satisfies the expected condition.

Returns:

  • bool: True if the test should continue to value checks (i.e., no error occurred).

Behavior:

  1. If err is nil: - If want is nil or false: Success. - If want is anything else: Fails test (Expected error but got nil).
  2. If err is non-nil: - If want is nil or false: Fails test (Unexpected error). - If want is true: Success (Any error accepted). - If want is string: Asserts err.Error() contains the string. - If want is error: Asserts errors.Is(err, want) or type match.

func AssertValue added in v0.51.0

func AssertValue[T any](t testing.TB, got, want T, opts ...ValueComparisonOption)

AssertValue compares two values with cmp.Diff while allowing callers to tweak the diff strategy via ValueComparisonOption. A non-empty diff is reported as an error containing the diff output.

func RunParseInputCase added in v0.51.0

func RunParseInputCase[T any](t *testing.T, tc ParseInputTestCase[T], opts ...ParseInputCaseOption)

RunParseInputCase executes a single parse-input test case using the provided configuration. It mirrors the typical table-driven pattern while removing the boilerplate repeated across tests. The helper short-circuits as soon as an expected error is encountered.

func TestParseInput

func TestParseInput[T any](t *testing.T, cmdFactory func(*types.CmdParams) *cobra.Command, parseInputFunc func(*print.Printer, *cobra.Command, []string) (T, error), expectedModel T, argValues []string, flagValues map[string]string, isValid bool)

TestParseInput centralizes the logic to test a combination of inputs (arguments, flags) for a cobra command

func TestParseInputWithAdditionalFlags

func TestParseInputWithAdditionalFlags[T any](t *testing.T, cmdFactory func(*types.CmdParams) *cobra.Command, parseInputFunc func(*print.Printer, *cobra.Command, []string) (T, error), expectedModel T, argValues []string, flagValues map[string]string, additionalFlagValues map[string][]string, isValid bool)

TestParseInputWithAdditionalFlags centralizes the logic to test a combination of inputs (arguments, flags) for a cobra command. It allows to pass multiple instances of a single flag to the cobra command using the `additionalFlagValues` parameter.

func TestParseInputWithOptions added in v0.50.0

func TestParseInputWithOptions[T any](t *testing.T, cmdFactory func(*types.CmdParams) *cobra.Command, parseInputFunc func(*print.Printer, *cobra.Command, []string) (T, error), expectedModel T, argValues []string, flagValues map[string]string, additionalFlagValues map[string][]string, isValid bool, testingOptions []TestingOption)

Types

type DiffFunc added in v0.51.0

type DiffFunc func(got, want any) string

DiffFunc compares two values and returns a diff string. An empty string means equality.

type Option added in v0.50.0

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

type ParseInputCaseOption added in v0.51.0

type ParseInputCaseOption func(*parseInputCaseConfig)

ParseInputCaseOption allows configuring the test execution behavior.

func WithParseInputCmpOptions added in v0.51.0

func WithParseInputCmpOptions(opts ...ValueComparisonOption) ParseInputCaseOption

WithParseInputCmpOptions sets custom comparison options for AssertValue.

type ParseInputTestCase added in v0.51.0

type ParseInputTestCase[T any] struct {
	Name string
	// Args simulates positional arguments passed to the command.
	Args []string
	// Flags sets simple single-value flags.
	Flags map[string]string
	// RepeatFlags sets flags that can be specified multiple times (e.g. slice flags).
	RepeatFlags map[string][]string
	WantModel   T
	WantErr     any
	CmdFactory  func(*types.CmdParams) *cobra.Command
	// ParseInputFunc is the function under test. It must accept the printer, command, and args.
	ParseInputFunc func(*print.Printer, *cobra.Command, []string) (T, error)
}

ParseInputTestCase aggregates all required elements to exercise a CLI parseInput function. It centralizes the common flag setup, validation, and result assertions used throughout the edge command test suites.

type TestingOption added in v0.50.0

type TestingOption func(options *Option) error

func WithCmpOptions added in v0.50.0

func WithCmpOptions(cmpOptions ...cmp.Option) TestingOption

type ValueComparisonOption added in v0.51.0

type ValueComparisonOption func(*valueComparisonConfig)

ValueComparisonOption configures how HandleValueResult applies cmp options or diffing strategies.

func WithAllowUnexported added in v0.51.0

func WithAllowUnexported(types ...any) ValueComparisonOption

WithAllowUnexported enables comparison of unexported fields for the provided struct types.

func WithAssertionCmpOptions added in v0.51.0

func WithAssertionCmpOptions(opts ...cmp.Option) ValueComparisonOption

WithCmpOptions accumulates cmp.Options used during value comparison.

func WithDiffFunc added in v0.51.0

func WithDiffFunc(diffFunc DiffFunc) ValueComparisonOption

WithDiffFunc sets a custom diffing function. Providing this option overrides the default cmp-based diff logic.

func WithIgnoreFields added in v0.51.0

func WithIgnoreFields(typ any, names ...string) ValueComparisonOption

WithIgnoreFields ignores the specified fields on the provided type during comparison. It uses cmpopts.IgnoreFields to ensure type-safe filtering.

Jump to

Keyboard shortcuts

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