This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathauto_correct_test.go
115 lines (95 loc) · 2.54 KB
/
auto_correct_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package parsecli
import (
"testing"
"github.com/facebookgo/ensure"
)
func TestCmdScoreLess(t *testing.T) {
t.Parallel()
scores := cmdScores([]cmdScore{
{levenshtein: 1, jaroWinkler: 0.9},
{levenshtein: 2, jaroWinkler: 0.8},
})
ensure.True(t, scores.Less(0, 1))
scores = cmdScores([]cmdScore{
{levenshtein: 1, jaroWinkler: 0.9},
{levenshtein: 1, jaroWinkler: 0.8},
})
ensure.True(t, scores.Less(0, 1))
scores = cmdScores([]cmdScore{
{levenshtein: 1, jaroWinkler: 0.8},
{levenshtein: 1, jaroWinkler: 0.9},
})
ensure.False(t, scores.Less(0, 1))
scores = cmdScores([]cmdScore{
{levenshtein: 2, jaroWinkler: 0.9},
{levenshtein: 1, jaroWinkler: 0.8},
})
ensure.False(t, scores.Less(0, 1))
}
func TestMakeCorrections(t *testing.T) {
t.Parallel()
ensure.DeepEqual(t, MakeCorrections(nil, nil), "")
ensure.DeepEqual(t, MakeCorrections(nil, []string{"arg"}), "")
subCommands := []string{"cmd"}
ensure.DeepEqual(t, MakeCorrections(subCommands, nil), "")
ensure.DeepEqual(t, MakeCorrections(subCommands, []string{"--file"}), "")
ensure.DeepEqual(t, MakeCorrections(subCommands, []string{"-f"}), "")
ensure.DeepEqual(t, MakeCorrections(subCommands, []string{"--file", "-p"}), "")
ensure.DeepEqual(t, MakeCorrections(subCommands, []string{"-p", "--file"}), "")
args := []string{"--flags", "cmd", "args"}
ensure.DeepEqual(t, MakeCorrections(subCommands, args), "")
ensure.DeepEqual(t, args, []string{"--flags", "cmd", "args"})
}
func TestMatchesForSubCommands(t *testing.T) {
t.Parallel()
subCommands := []string{"version", "deploy", "deplore"}
testCases := []struct {
args []string
modArgs []string
message string
}{
{
[]string{"version"},
[]string{"version"},
"",
},
{
[]string{"vers"},
[]string{"version"},
"(assuming by `vers` you meant `version`)",
},
{
[]string{"depluy", "-f", "-v"},
[]string{"deploy", "-f", "-v"},
"(assuming by `depluy` you meant `deploy`)",
},
{
[]string{"-v", "deple", "-f"},
[]string{"-v", "deple", "-f"},
`ambiguous subcommand: did you mean one of:
+ deploy
+ deplore
`,
},
}
for _, testCase := range testCases {
ensure.DeepEqual(t, MakeCorrections(subCommands, testCase.args), testCase.message)
ensure.DeepEqual(t, testCase.args, testCase.modArgs)
}
errorCases := []struct {
args []string
modeArgs []string
}{
{
[]string{"clivers"},
[]string{"version"},
},
{
[]string{"dupiay"},
[]string{"deploy"},
},
}
for _, errorCase := range errorCases {
ensure.DeepEqual(t, MakeCorrections(subCommands, errorCase.args), "")
}
}