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
|
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow
package repl
import (
"strings"
"testing"
"github.com/c-bata/go-prompt"
)
// TestCompleterLogic tests the completer logic directly
func TestCompleterLogic(t *testing.T) {
// Simulate the completer logic
testCases := []struct {
name string
text string
match bool
}{
{"h", "h", true}, // "help"
{"he", "he", true}, // "help"
{"hel", "hel", true}, // "help"
{"help", "help", true},
{"c", "c", true}, // "clear", "calc"
{"cl", "cl", true}, // "clear"
{"cle", "cle", true}, // "clear"
{"clear", "clear", true},
{"ca", "ca", true}, // "calc"
{"cal", "cal", true}, // "calc"
{"calc", "calc", true},
{"q", "q", true}, // "quit"
{"qu", "qu", true}, // "quit"
{"qui", "qui", true}, // "quit"
{"quit", "quit", true},
{"e", "e", true}, // "exit"
{"ex", "ex", true}, // "exit"
{"exi", "exi", true}, // "exit"
{"exit", "exit", true},
{"r", "r", true}, // "rpn"
{"rp", "rp", true}, // "rpn"
{"rpn", "rpn", true},
{"x", "x", false}, // no match
{"xyz", "xyz", false}, // no match
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Simulate the completer logic
var found bool
for _, cmd := range builtinCommands() {
if strings.HasPrefix(strings.ToLower(cmd), strings.ToLower(tc.text)) {
found = true
break
}
}
if found != tc.match {
t.Errorf("For text %q, expected match=%v, got match=%v", tc.text, tc.match, found)
}
})
}
}
// TestCompleterEmptyText tests completer with empty text
func TestCompleterEmptyText(t *testing.T) {
suggestions := completer(prompt.Document{Text: ""})
if suggestions != nil {
t.Errorf("Expected nil for empty text, got %d suggestions", len(suggestions))
}
}
// TestCompleterNoPrefix tests completer with no matching prefix
func TestCompleterNoPrefix(t *testing.T) {
suggestions := completer(prompt.Document{Text: "xyz "})
if len(suggestions) > 0 {
t.Errorf("Expected no suggestions for 'xyz', got %d", len(suggestions))
}
}
|