spf13--cobra/cobra/cmd/add_test.go

51 lines
1.1 KiB
Go
Raw Normal View History

2017-05-01 20:52:58 +00:00
package cmd
2019-01-31 03:47:38 +00:00
import (
"fmt"
"os"
"testing"
)
2019-01-31 03:47:38 +00:00
func TestGoldenAddCmd(t *testing.T) {
command := &Command{
CmdName: "test",
CmdParent: parentName,
Project: getProject(),
2019-01-31 03:47:38 +00:00
}
defer os.RemoveAll(command.AbsolutePath)
2017-05-05 08:06:27 +00:00
assertNoErr(t, command.Project.Create())
assertNoErr(t, command.Create())
2017-05-05 08:06:27 +00:00
2019-01-31 03:47:38 +00:00
generatedFile := fmt.Sprintf("%s/cmd/%s.go", command.AbsolutePath, command.CmdName)
goldenFile := fmt.Sprintf("testdata/%s.go.golden", command.CmdName)
err := compareFiles(generatedFile, goldenFile)
if err != nil {
2017-05-05 08:06:27 +00:00
t.Fatal(err)
}
}
2017-05-01 20:52:58 +00:00
func TestValidateCmdName(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"cmdName", "cmdName"},
{"cmd_name", "cmdName"},
{"cmd-name", "cmdName"},
{"cmd______Name", "cmdName"},
{"cmd------Name", "cmdName"},
{"cmd______name", "cmdName"},
{"cmd------name", "cmdName"},
{"cmdName-----", "cmdName"},
{"cmdname-", "cmdname"},
2017-05-01 20:52:58 +00:00
}
for _, testCase := range testCases {
got := validateCmdName(testCase.input)
if testCase.expected != got {
t.Errorf("Expected %q, got %q", testCase.expected, got)
}
}
}