-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui_test.go
More file actions
136 lines (125 loc) · 3.39 KB
/
Copy pathtui_test.go
File metadata and controls
136 lines (125 loc) · 3.39 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
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
package main
import (
"strings"
"testing"
"time"
)
func TestClampInt(t *testing.T) {
tests := []struct {
name string
v, lo, hi int
want int
}{
{"below range", -5, 0, 10, 0},
{"above range", 15, 0, 10, 10},
{"in range", 5, 0, 10, 5},
{"equal to lo", 0, 0, 10, 0},
{"equal to hi", 10, 0, 10, 10},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := clampInt(tt.v, tt.lo, tt.hi); got != tt.want {
t.Errorf("clampInt(%d, %d, %d) = %d, want %d", tt.v, tt.lo, tt.hi, got, tt.want)
}
})
}
}
func TestTruncateEllipsis(t *testing.T) {
tests := []struct {
name string
s string
width int
want string
}{
{"fits exactly", "hello", 5, "hello"},
{"shorter than width", "hi", 10, "hi"},
{"zero width", "hello", 0, ""},
{"negative width", "hello", -1, ""},
{"width one", "hello", 1, "…"},
{"truncated", "hello world", 8, "hello w…"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := truncateEllipsis(tt.s, tt.width); got != tt.want {
t.Errorf("truncateEllipsis(%q, %d) = %q, want %q", tt.s, tt.width, got, tt.want)
}
})
}
}
func TestProgressFractionNoLimitConfigured(t *testing.T) {
cfg := Config{}
_, ok := progressFraction(cfg, Snapshot{})
if ok {
t.Fatalf("ok = true, want false when neither MaxCount nor TestDuration is set")
}
}
func TestProgressFractionMaxCountOnly(t *testing.T) {
cfg := Config{MaxCount: 10}
frac, ok := progressFraction(cfg, Snapshot{Launched: 5})
if !ok {
t.Fatalf("ok = false, want true")
}
if frac != 0.5 {
t.Errorf("frac = %v, want 0.5", frac)
}
}
func TestProgressFractionDurationOnly(t *testing.T) {
cfg := Config{TestDuration: 10 * time.Second}
frac, ok := progressFraction(cfg, Snapshot{Elapsed: 3 * time.Second})
if !ok {
t.Fatalf("ok = false, want true")
}
if frac != 0.3 {
t.Errorf("frac = %v, want 0.3", frac)
}
}
func TestProgressFractionTakesLarger(t *testing.T) {
cfg := Config{MaxCount: 10, TestDuration: 10 * time.Second}
// 20% by count, 80% by duration — should report the larger.
frac, ok := progressFraction(cfg, Snapshot{Launched: 2, Elapsed: 8 * time.Second})
if !ok {
t.Fatalf("ok = false, want true")
}
if frac != 0.8 {
t.Errorf("frac = %v, want 0.8 (the larger of the two fractions)", frac)
}
}
func TestProgressFractionClampedToOne(t *testing.T) {
cfg := Config{MaxCount: 10}
frac, ok := progressFraction(cfg, Snapshot{Launched: 999})
if !ok {
t.Fatalf("ok = false, want true")
}
if frac != 1 {
t.Errorf("frac = %v, want 1 (clamped)", frac)
}
}
func TestLatencyCellNoSamples(t *testing.T) {
if got := latencyCell(Percentiles{}, 0); got != "-" {
t.Errorf("latencyCell with Count=0 = %q, want %q", got, "-")
}
}
func TestLatencyCellWithSamples(t *testing.T) {
p := Percentiles{Count: 1}
got := latencyCell(p, 1500*time.Microsecond)
want := (1500 * time.Microsecond).Round(time.Millisecond).String()
if got != want {
t.Errorf("latencyCell = %q, want %q", got, want)
}
}
func TestFormatLogLine(t *testing.T) {
tests := []struct {
stream string
}{
{"system"}, {"stdout"}, {"stderr"},
}
for _, tt := range tests {
t.Run(tt.stream, func(t *testing.T) {
l := LogLine{ProcID: 7, Stream: tt.stream, Text: "hello"}
got := formatLogLine(l)
if !strings.Contains(got, "[7]") || !strings.Contains(got, "hello") {
t.Errorf("formatLogLine(%+v) = %q, want it to contain proc id and text", l, got)
}
})
}
}