-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabSection_test.go
More file actions
146 lines (122 loc) · 4.74 KB
/
tabSection_test.go
File metadata and controls
146 lines (122 loc) · 4.74 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
137
138
139
140
141
142
143
144
145
146
package devtui
import (
"fmt"
"testing"
"time"
. "github.com/tinywasm/fmt"
)
func TestTabSectionWriter(t *testing.T) {
config := &TuiConfig{
Color: &ColorPalette{}, // Usando un ColorPalette vacío
Logger: func(messages ...any) {
// Mock function for logging
},
}
tui := NewTUI(config)
// Enable test mode for synchronous execution
tui.SetTestMode(true)
// Crear tab section de prueba
tab := tui.NewTabSection("TEST", "")
// Usar handlerWriter en vez de tabSection como Writer
tabSection := tab.(*tabSection)
handlerWriter := &handlerWriter{tabSection: tabSection, handlerName: ""}
testMsg := "Mensaje de prueba"
n, err := fmt.Fprintln(handlerWriter, testMsg)
if err != nil {
t.Fatalf("Error escribiendo en el Writer: %v", err)
}
if n != len(testMsg)+1 { // +1 por el newline
t.Errorf("Bytes escritos incorrectos: esperado %d, obtenido %d", len(testMsg)+1, n)
}
// Verificar que el mensaje llegó al canal
select {
case msg := <-tui.tabContentsChan:
if msg.Content != testMsg {
t.Errorf("Contenido incorrecto: esperado '%s', obtenido '%s'", testMsg, msg.Content)
}
if msg.Type != 0 { // 0 es el tipo por defecto para mensajes normales
t.Errorf("Tipo de mensaje incorrecto: esperado 0, obtenido %v", msg.Type)
}
case <-time.After(1 * time.Second):
t.Fatal("Timeout: el mensaje no llegó al canal")
}
}
func TestTabContentsIncrementWhenSendingMessages(t *testing.T) {
config := &TuiConfig{
Color: &ColorPalette{}, // Usando un ColorPalette vacío
Logger: func(messages ...any) {
// Mock function for logging
},
}
tui := NewTUI(config)
// Enable test mode for synchronous execution
tui.SetTestMode(true)
tab := tui.NewTabSection("TEST", "")
// Usar handlerWriter en vez de tabSection como Writer
tabSection := tab.(*tabSection)
handlerWriter := &handlerWriter{tabSection: tabSection, handlerName: ""}
// Test messages with different types and prefixes for detection
messages := []struct {
rawText string // Text sent via Fprintln
expectedText string // Text stored in tabContents (might be same or trimmed)
expectedType MessageType
}{
{"First message", "First message", Msg.Normal}, // Normal message
{"INFO: Second message", "INFO: Second message", Msg.Info},
{"ERROR: Third message", "ERROR: Third message", Msg.Error},
{"WARNING: Fourth message", "WARNING: Fourth message", Msg.Warning},
{"Fifth message", "Fifth message", Msg.Normal}, // Normal message again
}
// Send messages and verify increment
for i, message := range messages {
// Send message using the raw text
_, err := fmt.Fprintln(handlerWriter, message.rawText)
if err != nil {
t.Fatalf("Error writing message %d ('%s'): %v", i+1, message.rawText, err)
}
// Verify that the message arrived to the channel (sent by sendMessage)
// AND that tabContents was updated correctly (also by sendMessage)
select {
case msg := <-tui.tabContentsChan:
// Verify content received from channel matches expected stored text
if msg.Content != message.expectedText {
t.Errorf("Message %d: incorrect content from channel. Expected '%s', got '%s'",
i+1, message.expectedText, msg.Content)
}
// Verify type received from channel matches expected type
if msg.Type != message.expectedType {
t.Errorf("Message %d: incorrect type from channel. Expected %v, got %v",
i+1, message.expectedType, msg.Type)
}
// Verify that tabContents has the correct amount (should be updated by sendMessage)
if len(tabSection.tabContents) != i+1 {
t.Errorf("Message %d: incorrect amount in tabContents. Expected %d, got %d",
i+1, i+1, len(tabSection.tabContents))
}
// Verify that the last message added to the slice matches
if len(tabSection.tabContents) > 0 { // Check bounds
last := tabSection.tabContents[len(tabSection.tabContents)-1]
if last.Content != message.expectedText || last.Type != message.expectedType {
t.Errorf("Message %d: last record in slice does not match. Expected ('%s', %v), got ('%s', %v)",
i+1, message.expectedText, message.expectedType, last.Content, last.Type)
}
} else {
t.Errorf("Message %d: tabContents is empty after message should have been added", i+1)
}
case <-time.After(1 * time.Second):
t.Fatalf("Timeout: message %d ('%s') did not arrive to channel", i+1, message.rawText)
}
}
// Final verification of all messages
if len(tabSection.tabContents) != len(messages) {
t.Fatalf("Incorrect final amount. Expected %d, got %d",
len(messages), len(tabSection.tabContents))
}
// Verify message order
for i, message := range messages {
if tabSection.tabContents[i].Content != message.expectedText {
t.Errorf("Incorrect order in message %d. Expected '%s', got '%s'",
i+1, message.expectedText, tabSection.tabContents[i].Content)
}
}
}