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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
package positionalpredictor
import (
"strings"
"testing"
"unicode"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
)
func TestPositionalPredictor_position(t *testing.T) {
posPredictor := &PositionalPredictor{
BoolFlags: []string{"--mybool", "-b"},
ArgFlags: []string{"--myarg", "-a"},
}
for args, want := range map[string]int{
``: 0,
`foo`: 0,
`foo `: 1,
`-b foo `: 1,
`-a foo `: 0,
`-a=omg foo `: 1,
`--myarg omg foo `: 1,
`--myarg=omg foo `: 1,
`foo bar`: 1,
`foo bar `: 2,
} {
t.Run(args, func(t *testing.T) {
got := posPredictor.predictorIndex(newArgs("foo " + args))
assert.Equal(t, want, got)
})
}
}
func TestPositionalPredictor_predictor(t *testing.T) {
predictor1 := complete.PredictSet("1")
predictor2 := complete.PredictSet("2")
posPredictor := &PositionalPredictor{
Predictors: []complete.Predictor{predictor1, predictor2},
}
for args, want := range map[string]complete.Predictor{
``: predictor1,
`foo`: predictor1,
`foo `: predictor2,
`foo bar`: predictor2,
`foo bar `: nil,
} {
t.Run(args, func(t *testing.T) {
got := posPredictor.predictor(newArgs("app " + args))
assert.Equal(t, want, got)
})
}
}
func TestPositionalPredictor_cumulativepredictor(t *testing.T) {
predictor1 := complete.PredictSet("1")
predictor2 := complete.PredictSet("2")
posPredictor := &PositionalPredictor{
Predictors: []complete.Predictor{predictor1, predictor2},
IsCumulative: true,
}
for args, want := range map[string]complete.Predictor{
``: predictor1,
`foo`: predictor1,
`foo `: predictor2,
`foo bar`: predictor2,
`foo bar `: predictor2,
`foo bar baz `: predictor2,
} {
t.Run(args, func(t *testing.T) {
got := posPredictor.predictor(newArgs("app " + args))
assert.Equal(t, want, got)
})
}
}
// The code below is taken from https://github.com/posener/complete/blob/f6dd29e97e24f8cb51a8d4050781ce2b238776a4/args.go
// to assist in tests.
func newArgs(line string) complete.Args {
var (
all []string
completed []string
)
parts := splitFields(line)
if len(parts) > 0 {
all = parts[1:]
completed = removeLast(parts[1:])
}
return complete.Args{
All: all,
Completed: completed,
Last: last(parts),
LastCompleted: last(completed),
}
}
// splitFields returns a list of fields from the given command line.
// If the last character is space, it appends an empty field in the end
// indicating that the field before it was completed.
// If the last field is of the form "a=b", it splits it to two fields: "a", "b",
// So it can be completed.
func splitFields(line string) []string {
parts := strings.Fields(line)
// Add empty field if the last field was completed.
if len(line) > 0 && unicode.IsSpace(rune(line[len(line)-1])) {
parts = append(parts, "")
}
// Treat the last field if it is of the form "a=b"
parts = splitLastEqual(parts)
return parts
}
func splitLastEqual(line []string) []string {
if len(line) == 0 {
return line
}
parts := strings.Split(line[len(line)-1], "=")
return append(line[:len(line)-1], parts...)
}
func removeLast(a []string) []string {
if len(a) > 0 {
return a[:len(a)-1]
}
return a
}
func last(args []string) string {
if len(args) == 0 {
return ""
}
return args[len(args)-1]
}
|