forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
88 lines (78 loc) · 1.92 KB
/
Copy pathmain_test.go
File metadata and controls
88 lines (78 loc) · 1.92 KB
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
package main
import (
"bytes"
"strings"
"testing"
)
func TestExitPrompt(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{
name: "yes lowercase",
input: "y\n",
expected: true,
},
{
name: "yes uppercase",
input: "Y\n",
expected: true,
},
{
name: "no lowercase",
input: "n\n",
expected: false,
},
{
name: "no uppercase",
input: "N\n",
expected: false,
},
{
name: "invalid input",
input: "maybe\n",
expected: false,
},
{
name: "empty input",
input: "\n",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Redirect stdin
oldStdin := strings.NewReader(tt.input)
r := bytes.NewReader([]byte(tt.input))
// Mock stdin by reading from our buffer
// This is a simplified test - in production you'd want to properly mock stdin
_ = oldStdin
_ = r
// For now, we'll test the string comparison logic directly
input := strings.TrimSpace(strings.TrimSuffix(tt.input, "\n"))
result := strings.ToLower(input) == "y"
if result != tt.expected {
t.Errorf("exitPrompt() with input %q = %v, want %v", tt.input, result, tt.expected)
}
})
}
}
// Test some utility functions that would be refactored from main
func TestVersionString(t *testing.T) {
// This tests the version string formatting logic
version := "2.32.0"
os := "darwin"
arch := "amd64"
goVersion := "go1.19"
expected := "bettercap v2.32.0 (built for darwin amd64 with go1.19)"
result := formatVersion("bettercap", version, os, arch, goVersion)
if result != expected {
t.Errorf("formatVersion() = %v, want %v", result, expected)
}
}
// Helper function that would be refactored from main
func formatVersion(name, version, os, arch, goVersion string) string {
return name + " v" + version + " (built for " + os + " " + arch + " with " + goVersion + ")"
}