Add validation args test whith PostTerminatorArgs

This commit is contained in:
malko 2023-03-30 01:47:33 +02:00
parent c18bddc73d
commit 94b8e4d5df

View file

@ -89,6 +89,22 @@ func TestNoArgs(t *testing.T) {
output, err := executeCommand(c)
expectSuccess(output, err, t)
}
func TestNoArgs_WithPostTerminatorArgs(t *testing.T) {
c := getCommand(NoArgs, false)
_, err := executeCommand(c, "--", "post", "args")
noArgsWithArgs(err, t, "post")
// got := c.PostTerminatorArgs()
// expected := []string{"post", "args"}
// if strings.Join(got, ",") != strings.Join(expected, ",") {
// t.Fatalf("Expected %q, got %q", expected, got)
// }
}
func TestNoArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
c := getCommand(NoArgs, false)
c.IgnorePostTerminatorArgs = true
output, err := executeCommand(c, "--", "post", "args")
expectSuccess(output, err, t)
}
func TestNoArgs_WithArgs(t *testing.T) {
c := getCommand(NoArgs, false)
@ -122,6 +138,19 @@ func TestOnlyValidArgs(t *testing.T) {
expectSuccess(output, err, t)
}
func TestOnlyValidArgs_WithPostTerminatorArgs(t *testing.T) {
c := getCommand(OnlyValidArgs, true)
_, err := executeCommand(c, "one", "two", "--", "post", "args")
expectError("invalid argument \"post\" for \"c\"", err, t)
}
func TestOnlyValidArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
c := getCommand(OnlyValidArgs, true)
c.IgnorePostTerminatorArgs = true
output, err := executeCommand(c, "one", "two", "--", "post", "args")
expectSuccess(output, err, t)
}
func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) {
c := getCommand(OnlyValidArgs, true)
_, err := executeCommand(c, "a")
@ -185,10 +214,21 @@ func TestMinimumNArgs_WithLessArgs(t *testing.T) {
_, err := executeCommand(c, "a")
minimumNArgsWithLessArgs(err, t)
}
func TestMinimumNArgs_WithLessArgs_WithPostTerminatorArgs(t *testing.T) {
c := getCommand(MinimumNArgs(2), false)
output, err := executeCommand(c, "a", "--", "post", "args")
expectSuccess(output, err, t)
}
func TestMinimumNArgs_WithLessArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
c := getCommand(MinimumNArgs(2), false)
c.IgnorePostTerminatorArgs = true
_, err := executeCommand(c, "a", "--", "post", "args")
minimumNArgsWithLessArgs(err, t)
}
func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) {
c := getCommand(MinimumNArgs(2), true)
_, err := executeCommand(c, "one")
_, err := executeCommand(c, "one") // @todo check
minimumNArgsWithLessArgs(err, t)
}
@ -212,12 +252,37 @@ func TestMaximumNArgs(t *testing.T) {
expectSuccess(output, err, t)
}
func TestMaximumNArgs_WithPostTerminatorArgs(t *testing.T) {
c := getCommand(MaximumNArgs(3), false)
_, err := executeCommand(c, "a", "b", "--", "post", "args")
expectError("accepts at most 3 arg(s), received 4", err, t)
}
func TestMaximumNArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
c := getCommand(MaximumNArgs(3), false)
c.IgnorePostTerminatorArgs = true
output, err := executeCommand(c, "a", "b", "--", "post", "args")
expectSuccess(output, err, t)
}
func TestMaximumNArgs_WithValid(t *testing.T) {
c := getCommand(MaximumNArgs(2), true)
output, err := executeCommand(c, "one", "three")
expectSuccess(output, err, t)
}
func TestMaximumNArgs_WithValid_WithPostTerminatorArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), true)
_, err := executeCommand(c, "one", "three", "--", "post", "args")
expectError("accepts at most 2 arg(s), received 4", err, t)
}
func TestMaximumNArgs_WithValid_WithIgnoredPostTermintatorArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), true)
c.IgnorePostTerminatorArgs = true
output, err := executeCommand(c, "one", "three", "--", "post", "args")
expectSuccess(output, err, t)
}
func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), true)
output, err := executeCommand(c, "a", "b")
@ -236,6 +301,19 @@ func TestMaximumNArgs_WithMoreArgs(t *testing.T) {
maximumNArgsWithMoreArgs(err, t)
}
func TestMaximumNArgs_WithMoreArgs_WithPostTerminatorArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), false)
_, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
expectError("accepts at most 2 arg(s), received 5", err, t)
}
func TestMaximumNArgs_WithMoreArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), false)
c.IgnorePostTerminatorArgs = true
_, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
maximumNArgsWithMoreArgs(err, t)
}
func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) {
c := getCommand(MaximumNArgs(2), true)
_, err := executeCommand(c, "one", "three", "two")
@ -262,6 +340,19 @@ func TestExactArgs(t *testing.T) {
expectSuccess(output, err, t)
}
func TestExactArgs_WithPostTerminatorArgs(t *testing.T) {
c := getCommand(ExactArgs(3), false)
_, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
expectError("accepts 3 arg(s), received 5", err, t)
}
func TestExactArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
c := getCommand(ExactArgs(3), false)
c.IgnorePostTerminatorArgs = true
output, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
expectSuccess(output, err, t)
}
func TestExactArgs_WithValid(t *testing.T) {
c := getCommand(ExactArgs(3), true)
output, err := executeCommand(c, "three", "one", "two")
@ -312,6 +403,19 @@ func TestRangeArgs(t *testing.T) {
expectSuccess(output, err, t)
}
func TestRangeArgs_WithPostTerminatorArgs(t *testing.T) {
c := getCommand(RangeArgs(2, 4), false)
_, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
expectError("accepts between 2 and 4 arg(s), received 5", err, t)
}
func TestRangeArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
c := getCommand(RangeArgs(2, 4), false)
c.IgnorePostTerminatorArgs = true
output, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
expectSuccess(output, err, t)
}
func TestRangeArgs_WithValid(t *testing.T) {
c := getCommand(RangeArgs(2, 4), true)
output, err := executeCommand(c, "three", "one", "two")
@ -428,27 +532,42 @@ func TestMatchAll(t *testing.T) {
)
testCases := map[string]struct {
args []string
fail bool
args []string
ignorePostArgs bool
fail bool
}{
"happy path": {
[]string{"aa", "bb", "cc"},
false,
false,
},
"incorrect number of args": {
[]string{"aa", "bb", "cc", "dd"},
false,
true,
},
"incorrect number of bytes in one arg": {
[]string{"aa", "bb", "abc"},
false,
true,
},
"happy path with post args makes unhappy": {
[]string{"aa", "bb", "cc", "--", "post", "args"},
false,
true,
},
"happy path with ignored post args stay happy": {
[]string{"aa", "bb", "cc", "--", "post", "args"},
true,
false,
},
}
rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
rootCmd.IgnorePostTerminatorArgs = tc.ignorePostArgs
_, err := executeCommand(rootCmd, tc.args...)
if err != nil && !tc.fail {
t.Errorf("unexpected: %v\n", err)