2017-05-01 20:52:58 +00:00
|
|
|
package cmd
|
|
|
|
|
2019-01-31 03:47:38 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
2017-07-31 17:04:27 +00:00
|
|
|
|
2019-01-31 03:47:38 +00:00
|
|
|
func TestGoldenAddCmd(t *testing.T) {
|
|
|
|
command := &Command{
|
|
|
|
CmdName: "test",
|
|
|
|
CmdParent: parentName,
|
2019-09-17 15:02:42 +00:00
|
|
|
Project: getProject(),
|
2019-01-31 03:47:38 +00:00
|
|
|
}
|
2019-09-17 15:02:42 +00:00
|
|
|
defer os.RemoveAll(command.AbsolutePath)
|
2017-05-05 08:06:27 +00:00
|
|
|
|
2019-01-31 04:09:57 +00:00
|
|
|
command.Project.Create()
|
2019-01-31 03:47:38 +00:00
|
|
|
if err := command.Create(); err != nil {
|
2017-05-05 08:06:27 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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"},
|
2017-05-05 08:30:41 +00:00
|
|
|
{"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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|