Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added parse flag SetDefaultIfEmpty #416

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added tests for SetDefaultIfEmpty parse option
  • Loading branch information
encobrain committed Sep 26, 2024
commit b648c33b7412d6415b5444f79e3505b25d4aeb5f
114 changes: 114 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,120 @@ func TestDefaults(t *testing.T) {
}
}

func TestDefaultsWithParseOpts(t *testing.T) {
var tests = []struct {
parseOpts Options
msg string
args []string
opts defaultOptions
expected defaultOptions
expectedErr string
}{
{
parseOpts: Default | SetDefaultIfEmpty,
msg: "parseOpts=SetDefaultIfEmpty, no arguments, expecting default values",
args: []string{},
expected: defaultOptions{
Int: 0,
IntDefault: 1,
IntUnderscore: 10,

Float64: 0.0,
Float64Default: -3.14,
Float64Underscore: -33.14,

NumericFlag: false,

String: "",
StringDefault: "abc",

Time: 0,
TimeDefault: time.Minute,

Map: map[string]int{},
MapDefault: map[string]int{"a": 1},

Slice: []int{},
SliceDefault: []int{1, 2},
},
},
{
parseOpts: Default | SetDefaultIfEmpty,
msg: "parseOpts=SetDefaultOfEmpty, set opts values, expecting no default values of non zero value and default on zero",
args: []string{},
opts: defaultOptions{
Int: 0,
IntDefault: 2,
IntUnderscore: 11,

Float64: 0.0,
Float64Default: -3.15,
Float64Underscore: -33.15,

NumericFlag: false,

String: "",
StringDefault: "abcd",

Time: 0,
TimeDefault: time.Hour,

Map: map[string]int{},
MapDefault: map[string]int{"a": 1, "b": 2},

Slice: []int{},
SliceDefault: []int{1, 2, 3},
},
expected: defaultOptions{
Int: 0,
IntDefault: 2,
IntUnderscore: 11,

Float64: 0.0,
Float64Default: -3.15,
Float64Underscore: -33.15,

NumericFlag: false,

String: "",
StringDefault: "abcd",

Time: 0,
TimeDefault: time.Hour,

Map: map[string]int{},
MapDefault: map[string]int{"a": 1, "b": 2},

Slice: []int{},
SliceDefault: []int{1, 2, 3},
},
},
}

for _, test := range tests {
_, err := NewParser(&test.opts, test.parseOpts).ParseArgs(test.args)
if test.expectedErr != "" {
if err == nil {
t.Errorf("%s:\nExpected error containing substring %q", test.msg, test.expectedErr)
} else if !strings.Contains(err.Error(), test.expectedErr) {
t.Errorf("%s:\nExpected error %q to contain substring %q", test.msg, err, test.expectedErr)
}
} else {
if err != nil {
t.Fatalf("%s:\nUnexpected error: %v", test.msg, err)
}

if test.opts.Slice == nil {
test.opts.Slice = []int{}
}

if !reflect.DeepEqual(test.opts, test.expected) {
t.Errorf("%s:\nUnexpected options with arguments %+v\nexpected\n%+v\nbut got\n%+v\n", test.msg, test.args, test.expected, test.opts)
}
}
}
}

func TestNoDefaultsForBools(t *testing.T) {
var opts struct {
DefaultBool bool `short:"d" default:"true"`
Expand Down