Skip to content

Commit

Permalink
test: provided input flags
Browse files Browse the repository at this point in the history
tests for following functions:
- IsProvidedFlag
- IsLongProvidedFlag
- IsShortProvidedFlag
  • Loading branch information
Jose Quintana committed Nov 27, 2020
1 parent 966d25e commit bd2dbcd
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cline [![Build Status](https://travis-ci.com/joseluisq/cline.svg?branch=master)](https://travis-ci.com/joseluisq/cline) [![codecov](https://codecov.io/gh/joseluisq/cline/branch/master/graph/badge.svg)](https://codecov.io/gh/joseluisq/cline) [![Go Report Card](https://goreportcard.com/badge/github.com/joseluisq/cline)](https://goreportcard.com/report/github.com/joseluisq/cline) [![GoDoc](https://godoc.org/github.com/joseluisq/cline?status.svg)](https://pkg.go.dev/github.com/joseluisq/cline)
# CLIne [![Build Status](https://travis-ci.com/joseluisq/cline.svg?branch=master)](https://travis-ci.com/joseluisq/cline) [![codecov](https://codecov.io/gh/joseluisq/cline/branch/master/graph/badge.svg)](https://codecov.io/gh/joseluisq/cline) [![Go Report Card](https://goreportcard.com/badge/github.com/joseluisq/cline)](https://goreportcard.com/report/github.com/joseluisq/cline) [![GoDoc](https://godoc.org/github.com/joseluisq/cline?status.svg)](https://pkg.go.dev/github.com/joseluisq/cline)

> A fast and lightweight CLI package for Go without external dependencies.
Expand Down
190 changes: 190 additions & 0 deletions flag_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,193 @@ func TestFlagMapInput_GetProvidedFlags(t *testing.T) {
})
}
}

func TestFlagMapping_IsProvidedFlag(t *testing.T) {
type fields struct {
zFlags []Flag
zFlagsProvided []FlagProvided
}
type args struct {
flagName string
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "not provided flag",
fields: fields{
zFlags: []Flag{
FlagInt{Name: "int", Aliases: []string{"i"}},
},
},
args: args{
flagName: "int",
},
},
{
name: "provided flag",
fields: fields{
zFlags: []Flag{
FlagBool{Name: "bool", Aliases: []string{"b"}},
},
zFlagsProvided: []FlagProvided{
{Name: "bool"},
},
},
args: args{
flagName: "bool",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fm := &FlagMapping{
zFlags: tt.fields.zFlags,
zFlagsProvided: tt.fields.zFlagsProvided,
}
if got := fm.IsProvidedFlag(tt.args.flagName); got != tt.want {
t.Errorf("FlagMapping.IsProvidedFlag() = %v, want %v", got, tt.want)
}
})
}
}

func TestFlagMapping_IsLongProvidedFlag(t *testing.T) {
type fields struct {
zFlags []Flag
zFlagsProvided []FlagProvided
}
type args struct {
flagName string
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "not provided flag",
fields: fields{
zFlags: []Flag{
FlagInt{Name: "int", Aliases: []string{"i"}},
},
},
args: args{
flagName: "int",
},
},
{
name: "provided long name flag",
fields: fields{
zFlags: []Flag{
FlagBool{Name: "bool", Aliases: []string{"b"}},
},
zFlagsProvided: []FlagProvided{
{Name: "bool"},
},
},
args: args{
flagName: "bool",
},
want: true,
},
{
name: "provided short name flag (alias)",
fields: fields{
zFlags: []Flag{
FlagBool{Name: "int", Aliases: []string{"b"}},
},
zFlagsProvided: []FlagProvided{
{Name: "int", IsAlias: true},
},
},
args: args{
flagName: "int",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fm := &FlagMapping{
zFlags: tt.fields.zFlags,
zFlagsProvided: tt.fields.zFlagsProvided,
}
if got := fm.IsLongProvidedFlag(tt.args.flagName); got != tt.want {
t.Errorf("FlagMapping.IsLongProvidedFlag() = %v, want %v", got, tt.want)
}
})
}
}

func TestFlagMapping_IsShortProvidedFlag(t *testing.T) {
type fields struct {
zFlags []Flag
zFlagsProvided []FlagProvided
}
type args struct {
flagName string
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "not provided flag",
fields: fields{
zFlags: []Flag{
FlagStringSlice{Name: "strs", Aliases: []string{"s"}},
},
},
args: args{
flagName: "strs",
},
},
{
name: "provided long name flag",
fields: fields{
zFlags: []Flag{
FlagString{Name: "str", Aliases: []string{"s"}},
},
zFlagsProvided: []FlagProvided{
{Name: "str"},
},
},
args: args{
flagName: "str",
},
},
{
name: "provided short name flag (alias)",
fields: fields{
zFlags: []Flag{
FlagBool{Name: "int", Aliases: []string{"i"}},
},
zFlagsProvided: []FlagProvided{
{Name: "int", IsAlias: true},
},
},
args: args{
flagName: "int",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fm := &FlagMapping{
zFlags: tt.fields.zFlags,
zFlagsProvided: tt.fields.zFlagsProvided,
}
if got := fm.IsShortProvidedFlag(tt.args.flagName); got != tt.want {
t.Errorf("FlagMapping.IsShortProvidedFlag() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit bd2dbcd

Please sign in to comment.