test: input types & naming cleanup
continuous-integration/drone/push Build is passing Details

This commit is contained in:
decentral1se 2023-12-29 14:59:35 +01:00
parent c300ba4b03
commit 88278955b1
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
4 changed files with 60 additions and 23 deletions

View File

@ -18,18 +18,6 @@ const cmdHelp = `/acn on | Turn on the Tor
/start | Show getting started guide
/quit | Quit the program`
func isAcnOnCmd(cmds []string) bool {
return cmds[0] == "acn" && cmds[1] == "on"
}
func isAcnOffCmd(cmds []string) bool {
return cmds[0] == "acn" && cmds[1] == "off"
}
func isClearCmd(cmds []string) bool {
return cmds[0] == "clear"
}
func handleCommand(cmd, hiddenInput string) tea.Msg {
cmds := strings.Split(cmd, " ")
@ -71,23 +59,36 @@ func handleCommand(cmd, hiddenInput string) tea.Msg {
}
passwords := strings.Split(strings.TrimSpace(hiddenInput), " ")
if len(passwords) != 2 {
return cmdMsg{output: []string{"Profile create: unable to parse hidden input"}}
}
if passwords[0] != passwords[1] {
return cmdMsg{output: []string{"Profile create: passwords do not match?"}}
}
return createProfileMsg{
return profileCreateMsg{
name: cmds[2],
password: passwords[1],
}
}
case isAcnOnCmd(cmds):
return turnAcnOnMsg{}
case cmds[0] == "acn":
if cmds[1] != "on" && cmds[1] != "off" {
acnHelp := `Unknown "acn" command?
/acn on | Turn on the Tor ACN
/acn off | Turn off the Tor ACN`
return cmdMsg{output: strings.Split(acnHelp, "\n")}
}
case isAcnOffCmd(cmds):
return turnAcnOffMsg{}
switch {
case cmds[1] == "on":
return turnAcnOnMsg{}
case cmds[1] == "off":
return turnAcnOffMsg{}
}
case isClearCmd(cmds):
case cmds[0] == "clear":
return clearScreenMsg{}
}

View File

@ -6,8 +6,34 @@ import (
"github.com/stretchr/testify/assert"
)
func TestHandleCommand(t *testing.T) {
msg := handleCommand("/foo", "")
func TestHandleCommandTypes(t *testing.T) {
tests := []struct {
cmdInput string
hiddenInput string
cmdOutput interface{}
}{
{"help", "", cmdMsg{}},
{"quit", "", turnAcnOffMsg{}},
{"start", "", showGettingStartedGuideMsg{}},
{"profile unlock foo", "", profileUnlockMsg{}},
{"profile info", "", profileInfoMsg{}},
{"profile create foo *** ***", "bar bar", profileCreateMsg{}},
{"acn on", "", turnAcnOnMsg{}},
{"acn off", "", turnAcnOffMsg{}},
{"clear", "", clearScreenMsg{}},
}
for _, test := range tests {
msg := handleCommand(test.cmdInput, test.hiddenInput)
switch msg := msg.(type) {
default:
assert.IsType(t, msg, test.cmdOutput)
}
}
}
func TestHandleCommandUnknownCommand(t *testing.T) {
msg := handleCommand("foo", "")
switch msg := msg.(type) {
case cmdMsg:
assert.Equal(t, msg.output, []string{unknownCmdHelp})
@ -15,3 +41,13 @@ func TestHandleCommand(t *testing.T) {
assert.IsType(t, msg, cmdMsg{})
}
}
func TestHandleCommandIncorrectCommand(t *testing.T) {
msg := handleCommand("profile foo", "")
switch msg := msg.(type) {
case cmdMsg:
assert.Contains(t, msg.output, `Unknown "profile" command?`)
default:
assert.IsType(t, msg, cmdMsg{})
}
}

View File

@ -49,7 +49,7 @@ type profileInfoMsg struct{}
type startProfileQueuePollMsg struct{ onion string }
type createProfileMsg struct {
type profileCreateMsg struct {
name string
password string
}

View File

@ -332,12 +332,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, m.sendStatusCmd(msg.Error()))
case showGettingStartedGuideMsg:
m.input.Reset()
cmds = append(cmds, m.sendStatusCmd(
strings.Split(gettingStartedMessage, "\n")...,
))
m.input.Reset()
case createProfileMsg:
case profileCreateMsg:
m.hiddenInput = ""
m.input.Reset()