-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
186 lines (143 loc) · 5.77 KB
/
integration_test.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package vimtea
import (
"testing"
tea "github.com/charmbracelet/bubbletea"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEditorIntegration(t *testing.T) {
initialContent := "Hello, world!"
editor := NewEditor(WithContent(initialContent))
assert.Equal(t, ModeNormal, editor.GetMode(), "Initial mode should be Normal")
buffer := editor.GetBuffer()
assert.Equal(t, initialContent, buffer.Text(), "Buffer content should match initial content")
editor.SetMode(ModeInsert)
assert.Equal(t, ModeInsert, editor.GetMode(), "Mode should be Insert after setting")
model := editor.(*editorModel)
model.buffer.insertAt(0, 13, " This is a test.")
expectedContent := "Hello, world! This is a test."
assert.Equal(t, expectedContent, buffer.Text(), "Buffer content should match expected after insertion")
editor.SetMode(ModeNormal)
assert.Equal(t, ModeNormal, editor.GetMode(), "Mode should be Normal after setting")
testStatusMsg := "Test status"
cmd := editor.SetStatusMessage(testStatusMsg)
cmd()
assert.Equal(t, testStatusMsg, model.statusMessage, "Status message should match set message")
}
func TestViewportIntegration(t *testing.T) {
var content string
for i := 0; i < 30; i++ {
content += "Line " + string(rune('A'+i%26)) + "\n"
}
editor := NewEditor(WithContent(content))
model := editor.(*editorModel)
model.width = 80
model.height = 20
model.viewport.Width = 80
model.viewport.Height = 20
model.cursor = newCursor(25, 0)
model.ensureCursorVisible()
assert.GreaterOrEqual(t, model.cursor.Row, model.viewport.YOffset,
"Cursor row should be within or after viewport start")
assert.Less(t, model.cursor.Row, model.viewport.YOffset+model.viewport.Height,
"Cursor row should be within viewport end")
}
func TestKeyBindingsIntegration(t *testing.T) {
editor := NewEditor()
model := editor.(*editorModel)
testBindingCalled := false
editor.AddBinding(KeyBinding{
Key: "ctrl+t",
Mode: ModeNormal,
Description: "Test binding",
Handler: func(b Buffer) tea.Cmd {
testBindingCalled = true
return nil
},
})
iBinding := model.registry.FindExact("i", ModeNormal)
assert.NotNil(t, iBinding, "Default binding for 'i' should exist")
ctrlTBinding := model.registry.FindExact("ctrl+t", ModeNormal)
require.NotNil(t, ctrlTBinding, "Custom binding for 'ctrl+t' should exist")
_ = ctrlTBinding.Command(model)
assert.True(t, testBindingCalled, "Custom binding command should have been executed")
}
func TestCommandsIntegration(t *testing.T) {
editor := NewEditor()
model := editor.(*editorModel)
commandCalled := false
editor.AddCommand("test", func(b Buffer, args []string) tea.Cmd {
commandCalled = true
if len(args) > 0 && args[0] == "arg" {
return nil
}
return nil
})
model.commandBuffer = "test arg"
model.Update(CommandMsg{Command: "test"})
assert.True(t, commandCalled, "Command should have been executed")
}
func TestClearIntegration(t *testing.T) {
// Create editor with initial content
initialContent := "Line 1\nLine 2\nLine 3"
editor := NewEditor(WithContent(initialContent))
buffer := editor.GetBuffer()
// Verify initial content
assert.Equal(t, initialContent, buffer.Text(), "Buffer should have initial content")
assert.Equal(t, 3, buffer.LineCount(), "Buffer should have 3 lines initially")
// Set cursor to a non-zero position
model := editor.(*editorModel)
model.cursor = newCursor(1, 3)
// Clear the buffer using the Clear method
clearCmd := buffer.Clear()
if clearCmd != nil {
clearCmd()
}
// After clearing, the buffer should have a single empty line and cursor at 0,0
assert.Equal(t, 1, buffer.LineCount(), "Buffer should have 1 line after clear")
assert.Equal(t, "", buffer.Text(), "Buffer text should be empty")
assert.Equal(t, 0, model.cursor.Row, "Cursor row should be reset to 0")
assert.Equal(t, 0, model.cursor.Col, "Cursor column should be reset to 0")
// Test undo functionality after clear
undoCmd := buffer.Undo()
undoResult := undoCmd().(UndoRedoMsg)
assert.True(t, undoResult.Success, "Undo after clear should succeed")
assert.Equal(t, initialContent, buffer.Text(), "Buffer should return to initial content after undo")
}
func TestResetIntegration(t *testing.T) {
// Create editor with initial content
initialContent := "Initial content"
editor := NewEditor(WithContent(initialContent))
buffer := editor.GetBuffer()
// Verify initial content
assert.Equal(t, initialContent, buffer.Text(), "Buffer should have initial content")
// Make changes to the editor
model := editor.(*editorModel)
buffer.InsertAt(0, 0, "Modified ") // Modify the content
model.cursor = newCursor(0, 9) // Move cursor after "Modified "
// Verify the changes were made
assert.Equal(t, "Modified Initial content", buffer.Text(), "Buffer content should be modified")
assert.Equal(t, 0, model.cursor.Row, "Cursor row should be 0")
assert.Equal(t, 9, model.cursor.Col, "Cursor column should be 9")
// Reset the editor
resetCmd := editor.Reset()
if resetCmd != nil {
resetCmd()
}
// Verify the editor has been reset to initial state
assert.Equal(t, initialContent, buffer.Text(), "Buffer should be reset to initial content")
assert.Equal(t, 0, model.cursor.Row, "Cursor row should be reset to 0")
assert.Equal(t, 0, model.cursor.Col, "Cursor column should be reset to 0")
assert.Equal(t, ModeNormal, model.mode, "Editor mode should be reset to Normal")
assert.Equal(t, "", model.yankBuffer, "Yank buffer should be empty")
// Make more changes after reset
buffer.InsertAt(0, 0, "New ")
assert.Equal(t, "New Initial content", buffer.Text(), "Buffer should accept changes after reset")
// Reset again
resetCmd = editor.Reset()
if resetCmd != nil {
resetCmd()
}
// Verify reset again
assert.Equal(t, initialContent, buffer.Text(), "Buffer should be reset to initial content again")
}