Skip to content

Commit

Permalink
Move tests to *_test package
Browse files Browse the repository at this point in the history
  • Loading branch information
hoshsadiq committed Mar 13, 2022
1 parent dcce2bc commit 5c899f9
Show file tree
Hide file tree
Showing 15 changed files with 715 additions and 683 deletions.
106 changes: 54 additions & 52 deletions args_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package zulu
package zulu_test

import (
"fmt"
"strings"
"testing"

"github.com/gowarden/zulu"
)

type argsTestcase struct {
exerr string // Expected error key (see map[string][string])
args PositionalArgs // Args validator
wValid bool // Define `ValidArgs` in the command
rargs []string // Runtime args
exerr string // Expected error key (see map[string][string])
args zulu.PositionalArgs // Args validator
wValid bool // Define `ValidArgs` in the command
rargs []string // Runtime args
}

var errStrings = map[string]string{
Expand All @@ -23,7 +25,7 @@ var errStrings = map[string]string{
}

func (tc *argsTestcase) test(t *testing.T) {
c := &Command{
c := &zulu.Command{
Use: "c",
Args: tc.args,
RunE: emptyRun,
Expand Down Expand Up @@ -66,9 +68,9 @@ func testArgs(t *testing.T, tests map[string]argsTestcase) {

func TestArgs_No(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | ": {"", NoArgs, false, []string{}},
" | Arb": {"unknown", NoArgs, false, []string{"one"}},
"Valid | Valid": {"unknown", NoArgs, true, []string{"one"}},
" | ": {"", zulu.NoArgs, false, []string{}},
" | Arb": {"unknown", zulu.NoArgs, false, []string{"one"}},
"Valid | Valid": {"unknown", zulu.NoArgs, true, []string{"one"}},
})
}
func TestArgs_Nil(t *testing.T) {
Expand All @@ -80,57 +82,57 @@ func TestArgs_Nil(t *testing.T) {
}
func TestArgs_Arbitrary(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", ArbitraryArgs, false, []string{"a", "b"}},
"Valid | Valid": {"", ArbitraryArgs, true, []string{"one", "two"}},
"Valid | Invalid": {"invalid", ArbitraryArgs, true, []string{"a"}},
" | Arb": {"", zulu.ArbitraryArgs, false, []string{"a", "b"}},
"Valid | Valid": {"", zulu.ArbitraryArgs, true, []string{"one", "two"}},
"Valid | Invalid": {"invalid", zulu.ArbitraryArgs, true, []string{"a"}},
})
}
func TestArgs_MinimumN(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", MinimumNArgs(2), false, []string{"a", "b", "c"}},
"Valid | Valid": {"", MinimumNArgs(2), true, []string{"one", "three"}},
"Valid | Invalid": {"invalid", MinimumNArgs(2), true, []string{"a", "b"}},
" | Less": {"less", MinimumNArgs(2), false, []string{"a"}},
"Valid | Less": {"less", MinimumNArgs(2), true, []string{"one"}},
"Valid | LessInvalid": {"invalid", MinimumNArgs(2), true, []string{"a"}},
" | Arb": {"", zulu.MinimumNArgs(2), false, []string{"a", "b", "c"}},
"Valid | Valid": {"", zulu.MinimumNArgs(2), true, []string{"one", "three"}},
"Valid | Invalid": {"invalid", zulu.MinimumNArgs(2), true, []string{"a", "b"}},
" | Less": {"less", zulu.MinimumNArgs(2), false, []string{"a"}},
"Valid | Less": {"less", zulu.MinimumNArgs(2), true, []string{"one"}},
"Valid | LessInvalid": {"invalid", zulu.MinimumNArgs(2), true, []string{"a"}},
})
}
func TestArgs_MaximumN(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", MaximumNArgs(3), false, []string{"a", "b"}},
"Valid | Valid": {"", MaximumNArgs(2), true, []string{"one", "three"}},
"Valid | Invalid": {"invalid", MaximumNArgs(2), true, []string{"a", "b"}},
" | More": {"more", MaximumNArgs(2), false, []string{"a", "b", "c"}},
"Valid | More": {"more", MaximumNArgs(2), true, []string{"one", "three", "two"}},
"Valid | MoreInvalid": {"invalid", MaximumNArgs(2), true, []string{"a", "b", "c"}},
" | Arb": {"", zulu.MaximumNArgs(3), false, []string{"a", "b"}},
"Valid | Valid": {"", zulu.MaximumNArgs(2), true, []string{"one", "three"}},
"Valid | Invalid": {"invalid", zulu.MaximumNArgs(2), true, []string{"a", "b"}},
" | More": {"more", zulu.MaximumNArgs(2), false, []string{"a", "b", "c"}},
"Valid | More": {"more", zulu.MaximumNArgs(2), true, []string{"one", "three", "two"}},
"Valid | MoreInvalid": {"invalid", zulu.MaximumNArgs(2), true, []string{"a", "b", "c"}},
})
}
func TestArgs_Exact(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", ExactArgs(3), false, []string{"a", "b", "c"}},
"Valid | Valid": {"", ExactArgs(3), true, []string{"three", "one", "two"}},
"Valid | Invalid": {"invalid", ExactArgs(3), true, []string{"three", "a", "two"}},
" | InvalidCount": {"notexact", ExactArgs(2), false, []string{"a", "b", "c"}},
"Valid | InvalidCount": {"notexact", ExactArgs(2), true, []string{"three", "one", "two"}},
"Valid | InvalidCountInvalid": {"invalid", ExactArgs(2), true, []string{"three", "a", "two"}},
" | Arb": {"", zulu.ExactArgs(3), false, []string{"a", "b", "c"}},
"Valid | Valid": {"", zulu.ExactArgs(3), true, []string{"three", "one", "two"}},
"Valid | Invalid": {"invalid", zulu.ExactArgs(3), true, []string{"three", "a", "two"}},
" | InvalidCount": {"notexact", zulu.ExactArgs(2), false, []string{"a", "b", "c"}},
"Valid | InvalidCount": {"notexact", zulu.ExactArgs(2), true, []string{"three", "one", "two"}},
"Valid | InvalidCountInvalid": {"invalid", zulu.ExactArgs(2), true, []string{"three", "a", "two"}},
})
}
func TestArgs_Range(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", RangeArgs(2, 4), false, []string{"a", "b", "c"}},
"Valid | Valid": {"", RangeArgs(2, 4), true, []string{"three", "one", "two"}},
"Valid | Invalid": {"invalid", RangeArgs(2, 4), true, []string{"three", "a", "two"}},
" | InvalidCount": {"notinrange", RangeArgs(2, 4), false, []string{"a"}},
"Valid | InvalidCount": {"notinrange", RangeArgs(2, 4), true, []string{"two"}},
"Valid | InvalidCountInvalid": {"invalid", RangeArgs(2, 4), true, []string{"a"}},
" | Arb": {"", zulu.RangeArgs(2, 4), false, []string{"a", "b", "c"}},
"Valid | Valid": {"", zulu.RangeArgs(2, 4), true, []string{"three", "one", "two"}},
"Valid | Invalid": {"invalid", zulu.RangeArgs(2, 4), true, []string{"three", "a", "two"}},
" | InvalidCount": {"notinrange", zulu.RangeArgs(2, 4), false, []string{"a"}},
"Valid | InvalidCount": {"notinrange", zulu.RangeArgs(2, 4), true, []string{"two"}},
"Valid | InvalidCountInvalid": {"invalid", zulu.RangeArgs(2, 4), true, []string{"a"}},
})
}

// Takes(No)Args

func TestRootTakesNoArgs(t *testing.T) {
rootCmd := &Command{Use: "root", RunE: emptyRun}
childCmd := &Command{Use: "child", RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root", RunE: emptyRun}
childCmd := &zulu.Command{Use: "child", RunE: emptyRun}
rootCmd.AddCommand(childCmd)

_, err := executeCommand(rootCmd, "illegal", "args")
Expand All @@ -146,8 +148,8 @@ func TestRootTakesNoArgs(t *testing.T) {
}

func TestRootTakesArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Args: ArbitraryArgs, RunE: emptyRun}
childCmd := &Command{Use: "child", RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root", Args: zulu.ArbitraryArgs, RunE: emptyRun}
childCmd := &zulu.Command{Use: "child", RunE: emptyRun}
rootCmd.AddCommand(childCmd)

_, err := executeCommand(rootCmd, "legal", "args")
Expand All @@ -157,8 +159,8 @@ func TestRootTakesArgs(t *testing.T) {
}

func TestChildTakesNoArgs(t *testing.T) {
rootCmd := &Command{Use: "root", RunE: emptyRun}
childCmd := &Command{Use: "child", Args: NoArgs, RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root", RunE: emptyRun}
childCmd := &zulu.Command{Use: "child", Args: zulu.NoArgs, RunE: emptyRun}
rootCmd.AddCommand(childCmd)

_, err := executeCommand(rootCmd, "child", "illegal", "args")
Expand All @@ -174,8 +176,8 @@ func TestChildTakesNoArgs(t *testing.T) {
}

func TestChildTakesArgs(t *testing.T) {
rootCmd := &Command{Use: "root", RunE: emptyRun}
childCmd := &Command{Use: "child", Args: ArbitraryArgs, RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root", RunE: emptyRun}
childCmd := &zulu.Command{Use: "child", Args: zulu.ArbitraryArgs, RunE: emptyRun}
rootCmd.AddCommand(childCmd)

_, err := executeCommand(rootCmd, "child", "legal", "args")
Expand All @@ -187,9 +189,9 @@ func TestChildTakesArgs(t *testing.T) {
func TestMatchAll(t *testing.T) {
// Somewhat contrived example check that ensures there are exactly 3
// arguments, and each argument is exactly 2 bytes long.
pargs := MatchAll(
ExactArgs(3),
func(cmd *Command, args []string) error {
pargs := zulu.MatchAll(
zulu.ExactArgs(3),
func(cmd *zulu.Command, args []string) error {
for _, arg := range args {
if len([]byte(arg)) != 2 {
return fmt.Errorf("expected to be exactly 2 bytes long")
Expand Down Expand Up @@ -217,7 +219,7 @@ func TestMatchAll(t *testing.T) {
},
}

rootCmd := &Command{Use: "root", Args: pargs, RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root", Args: pargs, RunE: emptyRun}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
Expand All @@ -237,7 +239,7 @@ func TestMatchAll(t *testing.T) {
// It makes sure the root command accepts arguments if it does not have
// sub-commands.
func TestLegacyArgsRootAcceptsArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Args: nil, RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root", Args: nil, RunE: emptyRun}

_, err := executeCommand(rootCmd, "somearg")
if err != nil {
Expand All @@ -249,9 +251,9 @@ func TestLegacyArgsRootAcceptsArgs(t *testing.T) {
// to the legacyArgs() function.
// It makes sure a sub-command accepts arguments and further sub-commands
func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Args: nil, RunE: emptyRun}
childCmd := &Command{Use: "child", Args: nil, RunE: emptyRun}
grandchildCmd := &Command{Use: "grandchild", Args: nil, RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root", Args: nil, RunE: emptyRun}
childCmd := &zulu.Command{Use: "child", Args: nil, RunE: emptyRun}
grandchildCmd := &zulu.Command{Use: "grandchild", Args: nil, RunE: emptyRun}
rootCmd.AddCommand(childCmd)
childCmd.AddCommand(grandchildCmd)

Expand Down
30 changes: 16 additions & 14 deletions bash_completions_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package zulu
package zulu_test

import (
"bytes"
"log"
"os"
"strings"
"testing"

"github.com/gowarden/zulu"
)

func checkOmit(t *testing.T, found, unexpected string) {
Expand All @@ -21,8 +23,8 @@ func check(t *testing.T, found, expected string) {
}

func TestCompleteNoDesCmdInBashScript(t *testing.T) {
rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun}
child := &Command{
rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun}
child := &zulu.Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
RunE: emptyRun,
Expand All @@ -33,12 +35,12 @@ func TestCompleteNoDesCmdInBashScript(t *testing.T) {
assertNoErr(t, rootCmd.GenBashCompletion(buf, false))
output := buf.String()

check(t, output, ShellCompNoDescRequestCmd)
check(t, output, zulu.ShellCompNoDescRequestCmd)
}

func TestCompleteCmdInBashScript(t *testing.T) {
rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun}
child := &Command{
rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun}
child := &zulu.Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
RunE: emptyRun,
Expand All @@ -49,12 +51,12 @@ func TestCompleteCmdInBashScript(t *testing.T) {
assertNoErr(t, rootCmd.GenBashCompletion(buf, true))
output := buf.String()

check(t, output, ShellCompRequestCmd)
checkOmit(t, output, ShellCompNoDescRequestCmd)
check(t, output, zulu.ShellCompRequestCmd)
checkOmit(t, output, zulu.ShellCompNoDescRequestCmd)
}

func TestBashProgWithDash(t *testing.T) {
rootCmd := &Command{Use: "root-dash", Args: NoArgs, RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root-dash", Args: zulu.NoArgs, RunE: emptyRun}
buf := new(bytes.Buffer)
assertNoErr(t, rootCmd.GenBashCompletion(buf, false))
output := buf.String()
Expand All @@ -69,7 +71,7 @@ func TestBashProgWithDash(t *testing.T) {
}

func TestBashProgWithColon(t *testing.T) {
rootCmd := &Command{Use: "root:colon", Args: NoArgs, RunE: emptyRun}
rootCmd := &zulu.Command{Use: "root:colon", Args: zulu.NoArgs, RunE: emptyRun}
buf := new(bytes.Buffer)
assertNoErr(t, rootCmd.GenBashCompletion(buf, false))
output := buf.String()
Expand All @@ -91,8 +93,8 @@ func TestGenBashCompletionFile(t *testing.T) {

defer os.RemoveAll("./tmp")

rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun}
child := &Command{
rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun}
child := &zulu.Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
RunE: emptyRun,
Expand All @@ -113,8 +115,8 @@ func TestFailGenBashCompletionFile(t *testing.T) {
f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0400)
defer f.Close()

rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun}
child := &Command{
rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun}
child := &zulu.Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
RunE: emptyRun,
Expand Down
Loading

0 comments on commit 5c899f9

Please sign in to comment.