fftest

package
v4.0.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package fftest provides tools for testing flag sets.

Index

Examples

Constants

This section is empty.

Variables

View Source
var CoreConstructor = Constructor{
	Name: "core",
	Make: func(def Vars) (ff.Flags, *Vars) {
		var v Vars
		fs := ff.NewFlags("fftest")
		fs.StringVar(&v.S, 's', "str", def.S, "string")
		fs.IntVar(&v.I, 'i', "int", def.I, "int")
		fs.Float64Var(&v.F, 'f', "flt", def.F, "float64")
		fs.BoolVar(&v.A, 'a', "aflag", def.A, "bool a")
		fs.BoolVar(&v.B, 'b', "bflag", def.B, "bool b")
		fs.BoolVar(&v.C, 'c', "cflag", def.C, "bool c")
		fs.DurationVar(&v.D, 'd', "dur", def.D, "time.Duration")
		fs.AddFlag(ff.CoreFlagConfig{ShortName: 'x', LongName: "xxx", Placeholder: "STR", Usage: "collection of strings (repeatable)", Value: ffval.NewList(&v.X)})
		return fs, &v
	},
}

CoreConstructor produces a core flag set, with both short and long flag names for each value.

View Source
var DefaultConstructors = []Constructor{
	CoreConstructor,
	StdConstructor,
}

DefaultConstructors are used for test cases that don't specify constructors.

View Source
var StdConstructor = Constructor{
	Name: "std",
	Make: func(def Vars) (ff.Flags, *Vars) {
		var v Vars
		fs := flag.NewFlagSet("fftest", flag.ContinueOnError)
		fs.StringVar(&v.S, "s", def.S, "string")
		fs.IntVar(&v.I, "i", def.I, "int")
		fs.Float64Var(&v.F, "f", def.F, "float64")
		fs.BoolVar(&v.A, "a", def.A, "bool a")
		fs.BoolVar(&v.B, "b", def.B, "bool b")
		fs.BoolVar(&v.C, "c", def.C, "bool c")
		fs.DurationVar(&v.D, "d", def.D, "time.Duration")
		fs.Var(ffval.NewList(&v.X), "x", "collection of strings (repeatable)")
		return ff.NewStdFlags(fs), &v
	},
}

StdConstructor produces a stdlib flag set adapter.

Functions

func Compare

func Compare(t *testing.T, want, have *Vars)

Compare one set of vars with another and t.Error on any difference.

func DiffString

func DiffString(a, b string) string

DiffString produces a git-like diff of two multi-line strings.

func TestFlags

func TestFlags(t *testing.T, fs ff.Flags, args []string)

TestFlags checks the core invariants of a flag set and its flags. The provided flag set should contain at least two flags, and calling parse with the provided args should succeed.

func Unindent

func Unindent(s string, prefixes ...string) string

Unindent trims s of leading and trailing whitespace, and then trims each line of every given prefix. If no prefixes are provided, each line is trimmed of all leading tab characters. This allows multi-line strings to be written in-line with other code, while remaining comparable.

Example
fs := ff.NewFlags("testcommand")
fs.String('f', "foo", "", "the foo parameter")
fs.IntLong("bar", 3, "the bar parameter")

want := fftest.Unindent(`
		NAME
		  testcommand

		FLAGS
		  -f, --foo STRING   the foo parameter
		      --bar INT      the bar parameter (default: 3)
	`)

have := fftest.Unindent(ffhelp.Flags(fs).String())

if want == have {
	fmt.Println("strings are identical")
} else {
	fmt.Println(fftest.DiffString(want, have))
}
Output:

strings are identical

Types

type Constructor

type Constructor struct {
	// Name of the constructor, used in test names.
	Name string

	// Make should return a flag set and vars structure, where each value in the
	// vars structure is updated by a corresponding flag in the flag set. The
	// default value for each flag should be taken from the def parameter.
	Make func(def Vars) (ff.Flags, *Vars)
}

Constructor produces a flag set, and a set of vars managed by that flag set.

func NewNestedConstructor

func NewNestedConstructor(delim string) Constructor

NewNestedConstructor returns a constructor where flags have specific hierarchical names delimited by the provided delim. This is useful for testing config file formats that allow nested configuration.

type ParseTest

type ParseTest struct {
	Name         string
	Constructors []Constructor
	Default      Vars
	ConfigFile   string
	Environment  map[string]string
	Args         []string
	Options      []ff.Option
	Want         Vars
}

ParseTest describes a parsing test scenario.

func (*ParseTest) Run

func (tc *ParseTest) Run(t *testing.T, options ...ff.Option)

Run the test case.

type TestCases

type TestCases []ParseTest

TestCases are a collection of test cases that can be run as a group.

func (TestCases) Run

func (tcs TestCases) Run(t *testing.T, options ...ff.Option)

Run the test cases in order.

type Vars

type Vars struct {
	S       string        // flag name `s`
	I       int           // flag name `i`
	F       float64       // flag name `f`
	A, B, C bool          // flag name `a`, `b`, `c`
	D       time.Duration // flag name `d`
	X       []string      // flag name `x`

	// ParseError should be assigned as the result of Parse in tests.
	ParseError error

	// If a test case expects an input to generate a parse error,
	// it can specify that error here. The Compare helper will
	// look for it using errors.Is.
	WantParseErrorIs error

	// If a test case expects an input to generate a parse error,
	// it can specify part of that error string here. The Compare
	// helper will look for it using strings.Contains.
	WantParseErrorString string

	// Args left over after a successful parse.
	Args []string
}

Vars are a common set of variables used for testing.

Jump to

Keyboard shortcuts

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