From e2c3e22d578c1026e54857aa6f8166e6c73d4e46 Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Fri, 15 Jan 2021 03:55:31 -0500 Subject: [PATCH] testing: add tests for constructing Options with defaults and parsing those Options using the CLI parsing tools --- cli/parse_test.go | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/cli/parse_test.go b/cli/parse_test.go index 33d8aeb..5fd5219 100644 --- a/cli/parse_test.go +++ b/cli/parse_test.go @@ -178,6 +178,148 @@ func TestOptionParsing(t *testing.T) { testFail("-zz--- --") } +func TestDefaultOptionParsing(t *testing.T) { + testPanic := func(f func()) { + fnFinished := false + defer func() { + if r := recover(); fnFinished == true { + panic(r) + } + }() + f() + fnFinished = true + t.Error("expected panic") + } + + testPanic(func() { cmds.StringOption("string", "s", "a string").WithDefault(0) }) + testPanic(func() { cmds.StringOption("string", "s", "a string").WithDefault(false) }) + testPanic(func() { cmds.StringOption("string", "s", "a string").WithDefault(nil) }) + testPanic(func() { cmds.StringOption("string", "s", "a string").WithDefault([]string{"foo"}) }) + testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault(0) }) + testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault(false) }) + testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault(nil) }) + testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault("foo") }) + testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault([]bool{false}) }) + testPanic(func() { cmds.DelimitedStringsOption(",", "dstrings", "d", "delimited string array").WithDefault(0) }) + testPanic(func() { cmds.DelimitedStringsOption(",", "dstrs", "d", "delimited string array").WithDefault(false) }) + testPanic(func() { cmds.DelimitedStringsOption(",", "dstrings", "d", "delimited string array").WithDefault(nil) }) + testPanic(func() { cmds.DelimitedStringsOption(",", "dstrs", "d", "delimited string array").WithDefault("foo") }) + testPanic(func() { cmds.DelimitedStringsOption(",", "dstrs", "d", "delimited string array").WithDefault([]int{0}) }) + + testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault(0) }) + testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault(1) }) + testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault(nil) }) + testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault([]bool{false}) }) + testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault([]string{"foo"}) }) + + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(int(0)) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(int32(0)) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(int64(0)) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(uint64(0)) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(uint32(0)) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(float32(0)) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(float64(0)) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(nil) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault([]uint{0}) }) + testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault([]string{"foo"}) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(int(0)) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(int32(0)) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(int64(0)) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(uint(0)) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(uint32(0)) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(float32(0)) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(float64(0)) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(nil) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault([]uint64{0}) }) + testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault([]string{"foo"}) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(int32(0)) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(int64(0)) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(uint(0)) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(uint32(0)) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(uint64(0)) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(float32(0)) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(float64(0)) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(nil) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault([]int{0}) }) + testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault([]string{"foo"}) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(int(0)) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(int32(0)) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(uint(0)) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(uint32(0)) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(uint64(0)) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(float32(0)) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(float64(0)) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(nil) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault([]int64{0}) }) + testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault([]string{"foo"}) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(int(0)) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(int32(0)) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(int64(0)) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(uint(0)) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(uint32(0)) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(uint64(0)) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(float32(0)) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(nil) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault([]int{0}) }) + testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault([]string{"foo"}) }) + + cmd := &cmds.Command{ + Subcommands: map[string]*cmds.Command{ + "defaults": &cmds.Command{ + Options: []cmds.Option{ + cmds.StringOption("string", "s", "a string").WithDefault("foo"), + cmds.StringsOption("strings1", "a", "a string array").WithDefault([]string{"foo"}), + cmds.StringsOption("strings2", "b", "a string array").WithDefault([]string{"foo", "bar"}), + cmds.DelimitedStringsOption(",", "dstrings1", "c", "a delimited string array").WithDefault([]string{"foo"}), + cmds.DelimitedStringsOption(",", "dstrings2", "d", "a delimited string array").WithDefault([]string{"foo", "bar"}), + + cmds.BoolOption("boolT", "t", "a bool").WithDefault(true), + cmds.BoolOption("boolF", "a bool").WithDefault(false), + + cmds.UintOption("uint", "u", "a uint").WithDefault(uint(1)), + cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(uint64(1)), + cmds.IntOption("int", "i", "an int").WithDefault(int(1)), + cmds.Int64Option("int64", "j", "an int64").WithDefault(int64(1)), + cmds.FloatOption("float", "f", "a float64").WithDefault(float64(1)), + }, + }, + }, + } + + test := func(args string, expectedOpts kvs, expectedWords words) { + testOptionHelper(t, cmd, args, expectedOpts, expectedWords, false) + } + + test("defaults", kvs{ + "string": "foo", + "strings1": []string{"foo"}, + "strings2": []string{"foo", "bar"}, + "dstrings1": []string{"foo"}, + "dstrings2": []string{"foo", "bar"}, + "boolT": true, + "boolF": false, + "uint": uint(1), + "uint64": uint64(1), + "int": int(1), + "int64": int64(1), + "float": float64(1), + }, words{}) + test("defaults --string baz --strings1=baz -b baz -b=foo -c=foo -d=foo,baz,bing -d=zip,zap -d=zorp -t=false --boolF -u=0 -v=10 -i=-5 -j=10 -f=-3.14", kvs{ + "string": "baz", + "strings1": []string{"baz"}, + "strings2": []string{"baz", "foo"}, + "dstrings1": []string{"foo"}, + "dstrings2": []string{"foo", "baz", "bing", "zip", "zap", "zorp"}, + "boolT": false, + "boolF": true, + "uint": uint(0), + "uint64": uint64(10), + "int": int(-5), + "int64": int64(10), + "float": float64(-3.14), + }, words{}) +} + func TestArgumentParsing(t *testing.T) { rootCmd := &cmds.Command{ Subcommands: map[string]*cmds.Command{ -- GitLab