Add more tests to flag generation code

This commit is contained in:
Dan Buch 2022-05-01 23:36:59 -04:00
parent adc61ca06b
commit 71cd131794
Signed by: meatballhat
GPG Key ID: A12F782281063434
3 changed files with 212 additions and 28 deletions

View File

@ -5,13 +5,18 @@ package {{.TestPackageName}}
{{range .SortedFlagTypes}}
{{if .GenerateFlagInterface}}
func Test{{.TypeName}}_SatisfiesFlagInterface(t *testing.T) {
var _ {{$.UrfaveCLITestNamespace}}Flag = &{{$.UrfaveCLITestNamespace}}{{.TypeName}}{}
var f {{$.UrfaveCLITestNamespace}}Flag = &{{$.UrfaveCLITestNamespace}}{{.TypeName}}{}
_ = f.IsSet()
_ = f.Names()
}
{{end}}
{{if .GenerateFmtStringerInterface}}
func Test{{.TypeName}}_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &{{$.UrfaveCLITestNamespace}}{{.TypeName}}{}
var f fmt.Stringer = &{{$.UrfaveCLITestNamespace}}{{.TypeName}}{}
_ = f.String()
}
{{end}}
{{end}}

View File

@ -0,0 +1,112 @@
package genflags_test
import (
"reflect"
"testing"
"github.com/urfave/cli/v2/internal/genflags"
)
func TestSpec_SortedFlagTypes(t *testing.T) {
spec := &genflags.Spec{
FlagTypes: map[string]*genflags.FlagTypeConfig{
"nerf": &genflags.FlagTypeConfig{},
"gerf": nil,
},
}
actual := spec.SortedFlagTypes()
expected := []*genflags.FlagType{
{
GoType: "gerf",
Config: nil,
},
{
GoType: "nerf",
Config: &genflags.FlagTypeConfig{},
},
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("expected %#v, got %#v", expected, actual)
}
}
func genFlagType() *genflags.FlagType {
return &genflags.FlagType{
GoType: "blerf",
Config: &genflags.FlagTypeConfig{
SkipInterfaces: []string{"fmt.Stringer"},
StructFields: []*genflags.FlagStructField{
{
Name: "Foibles",
Type: "int",
},
{
Name: "Hoopled",
Type: "bool",
},
},
TypeName: "YeOldeBlerfFlag",
ValuePointer: true,
},
}
}
func TestFlagType_StructFields(t *testing.T) {
ft := genFlagType()
sf := ft.StructFields()
if 2 != len(sf) {
t.Errorf("expected 2 struct fields, got %v", len(sf))
return
}
if "Foibles" != sf[0].Name {
t.Errorf("expected struct field order to be retained")
}
}
func TestFlagType_ValuePointer(t *testing.T) {
ft := genFlagType()
if !ft.ValuePointer() {
t.Errorf("expected ValuePointer to be true")
return
}
ft.Config = nil
if ft.ValuePointer() {
t.Errorf("expected ValuePointer to be false")
}
}
func TestFlagType_GenerateFmtStringerInterface(t *testing.T) {
ft := genFlagType()
if ft.GenerateFmtStringerInterface() {
t.Errorf("expected GenerateFmtStringerInterface to be false")
return
}
ft.Config = nil
if !ft.GenerateFmtStringerInterface() {
t.Errorf("expected GenerateFmtStringerInterface to be true")
}
}
func TestFlagType_GenerateFlagInterface(t *testing.T) {
ft := genFlagType()
if !ft.GenerateFlagInterface() {
t.Errorf("expected GenerateFlagInterface to be true")
return
}
ft.Config = nil
if !ft.GenerateFlagInterface() {
t.Errorf("expected GenerateFlagInterface to be true")
}
}

View File

@ -10,107 +10,174 @@ import (
)
func TestFloat64SliceFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.Float64SliceFlag{}
var f cli.Flag = &cli.Float64SliceFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestGenericFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.GenericFlag{}
var f cli.Flag = &cli.GenericFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestGenericFlag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.GenericFlag{}
var f fmt.Stringer = &cli.GenericFlag{}
_ = f.String()
}
func TestInt64SliceFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.Int64SliceFlag{}
var f cli.Flag = &cli.Int64SliceFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestIntSliceFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.IntSliceFlag{}
var f cli.Flag = &cli.IntSliceFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestPathFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.PathFlag{}
var f cli.Flag = &cli.PathFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestPathFlag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.PathFlag{}
var f fmt.Stringer = &cli.PathFlag{}
_ = f.String()
}
func TestStringSliceFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.StringSliceFlag{}
var f cli.Flag = &cli.StringSliceFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestTimestampFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.TimestampFlag{}
var f cli.Flag = &cli.TimestampFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestTimestampFlag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.TimestampFlag{}
var f fmt.Stringer = &cli.TimestampFlag{}
_ = f.String()
}
func TestBoolFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.BoolFlag{}
var f cli.Flag = &cli.BoolFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestBoolFlag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.BoolFlag{}
var f fmt.Stringer = &cli.BoolFlag{}
_ = f.String()
}
func TestFloat64Flag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.Float64Flag{}
var f cli.Flag = &cli.Float64Flag{}
_ = f.IsSet()
_ = f.Names()
}
func TestFloat64Flag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.Float64Flag{}
var f fmt.Stringer = &cli.Float64Flag{}
_ = f.String()
}
func TestIntFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.IntFlag{}
var f cli.Flag = &cli.IntFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestIntFlag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.IntFlag{}
var f fmt.Stringer = &cli.IntFlag{}
_ = f.String()
}
func TestInt64Flag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.Int64Flag{}
var f cli.Flag = &cli.Int64Flag{}
_ = f.IsSet()
_ = f.Names()
}
func TestInt64Flag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.Int64Flag{}
var f fmt.Stringer = &cli.Int64Flag{}
_ = f.String()
}
func TestStringFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.StringFlag{}
var f cli.Flag = &cli.StringFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestStringFlag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.StringFlag{}
var f fmt.Stringer = &cli.StringFlag{}
_ = f.String()
}
func TestDurationFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.DurationFlag{}
var f cli.Flag = &cli.DurationFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestDurationFlag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.DurationFlag{}
var f fmt.Stringer = &cli.DurationFlag{}
_ = f.String()
}
func TestUintFlag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.UintFlag{}
var f cli.Flag = &cli.UintFlag{}
_ = f.IsSet()
_ = f.Names()
}
func TestUintFlag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.UintFlag{}
var f fmt.Stringer = &cli.UintFlag{}
_ = f.String()
}
func TestUint64Flag_SatisfiesFlagInterface(t *testing.T) {
var _ cli.Flag = &cli.Uint64Flag{}
var f cli.Flag = &cli.Uint64Flag{}
_ = f.IsSet()
_ = f.Names()
}
func TestUint64Flag_SatisfiesFmtStringerInterface(t *testing.T) {
var _ fmt.Stringer = &cli.Uint64Flag{}
var f fmt.Stringer = &cli.Uint64Flag{}
_ = f.String()
}
// vim:ro