Documentation
¶
Overview ¶
Package fftest provides tools for testing flag sets.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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.
var DefaultConstructors = []Constructor{ CoreConstructor, StdConstructor, }
DefaultConstructors are used for test cases that don't specify constructors.
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 DiffString ¶
DiffString produces a git-like diff of two multi-line strings.
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 TestCase ¶
type TestCase struct {
Name string
Constructors []Constructor
Default Vars
ConfigFile string
Environment map[string]string
Args []string
Options []ff.Option
Want Vars
}
TestCase describes a specific ff.Parse test scenario.
type TestCases ¶
type TestCases []TestCase
TestCases are a collection of test cases that can be run as a group.
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.