Skip to content

Commit 6d48925

Browse files
committed
Started writing a few tests for the find panel.
1 parent aa7829a commit 6d48925

File tree

1 file changed

+232
-0
lines changed

1 file changed

+232
-0
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
import Testing
2+
import AppKit
3+
import CodeEditTextView
4+
@testable import CodeEditSourceEditor
5+
6+
@MainActor
7+
struct FindPanelTests {
8+
class MockPanelTarget: FindPanelTarget {
9+
var emphasisManager: EmphasisManager?
10+
var findPanelTargetView: NSView
11+
var cursorPositions: [CursorPosition] = []
12+
var textView: TextView!
13+
var findPanelWillShowCalled = false
14+
var findPanelWillHideCalled = false
15+
var findPanelModeDidChangeCalled = false
16+
var lastMode: FindPanelMode?
17+
18+
@MainActor init(text: String = "") {
19+
findPanelTargetView = NSView()
20+
textView = TextView(string: text)
21+
}
22+
23+
func setCursorPositions(_ positions: [CursorPosition], scrollToVisible: Bool) { }
24+
func updateCursorPosition() { }
25+
func findPanelWillShow(panelHeight: CGFloat) {
26+
findPanelWillShowCalled = true
27+
}
28+
func findPanelWillHide(panelHeight: CGFloat) {
29+
findPanelWillHideCalled = true
30+
}
31+
func findPanelModeDidChange(to mode: FindPanelMode) {
32+
findPanelModeDidChangeCalled = true
33+
lastMode = mode
34+
}
35+
}
36+
37+
@Test func findPanelShowsOnCommandF() async throws {
38+
let target = MockPanelTarget()
39+
let viewModel = FindPanelViewModel(target: target)
40+
let viewController = FindViewController(target: target, childView: NSView())
41+
42+
// Show find panel
43+
viewController.showFindPanel()
44+
45+
// Verify panel is shown
46+
#expect(viewModel.isShowingFindPanel == true)
47+
#expect(target.findPanelWillShowCalled == true)
48+
49+
// Hide find panel
50+
viewController.hideFindPanel()
51+
52+
// Verify panel is hidden
53+
#expect(viewModel.isShowingFindPanel == false)
54+
#expect(target.findPanelWillHideCalled == true)
55+
}
56+
57+
@Test func replaceFieldShowsWhenReplaceModeSelected() async throws {
58+
let target = MockPanelTarget()
59+
let viewModel = FindPanelViewModel(target: target)
60+
61+
// Switch to replace mode
62+
viewModel.mode = .replace
63+
64+
// Verify mode change
65+
#expect(viewModel.mode == .replace)
66+
#expect(target.findPanelModeDidChangeCalled == true)
67+
#expect(target.lastMode == .replace)
68+
#expect(viewModel.panelHeight == 54) // Height should be larger in replace mode
69+
70+
// Switch back to find mode
71+
viewModel.mode = .find
72+
73+
// Verify mode change
74+
#expect(viewModel.mode == .find)
75+
#expect(viewModel.panelHeight == 28) // Height should be smaller in find mode
76+
}
77+
78+
@Test func wrapAroundEnabled() async throws {
79+
let target = MockPanelTarget(text: "test1\ntest2\ntest3")
80+
let viewModel = FindPanelViewModel(target: target)
81+
viewModel.findText = "test"
82+
viewModel.wrapAround = true
83+
84+
// Perform initial find
85+
viewModel.find()
86+
#expect(viewModel.findMatches.count == 3)
87+
88+
// Move to last match
89+
viewModel.currentFindMatchIndex = 2
90+
91+
// Move to next (should wrap to first)
92+
viewModel.moveToNextMatch()
93+
#expect(viewModel.currentFindMatchIndex == 0)
94+
95+
// Move to previous (should wrap to last)
96+
viewModel.moveToPreviousMatch()
97+
#expect(viewModel.currentFindMatchIndex == 2)
98+
}
99+
100+
@Test func wrapAroundDisabled() async throws {
101+
let target = MockPanelTarget(text: "test1\ntest2\ntest3")
102+
let viewModel = FindPanelViewModel(target: target)
103+
viewModel.findText = "test"
104+
viewModel.wrapAround = false
105+
106+
// Perform initial find
107+
viewModel.find()
108+
#expect(viewModel.findMatches.count == 3)
109+
110+
// Move to last match
111+
viewModel.currentFindMatchIndex = 2
112+
113+
// Move to next (should stay at last)
114+
viewModel.moveToNextMatch()
115+
#expect(viewModel.currentFindMatchIndex == 2)
116+
117+
// Move to first match
118+
viewModel.currentFindMatchIndex = 0
119+
120+
// Move to previous (should stay at first)
121+
viewModel.moveToPreviousMatch()
122+
#expect(viewModel.currentFindMatchIndex == 0)
123+
}
124+
125+
@Test func findMatches() async throws {
126+
let target = MockPanelTarget(text: "test1\ntest2\ntest3")
127+
let viewModel = FindPanelViewModel(target: target)
128+
viewModel.findText = "test"
129+
130+
viewModel.find()
131+
132+
#expect(viewModel.findMatches.count == 3)
133+
#expect(viewModel.findMatches[0].location == 0)
134+
#expect(viewModel.findMatches[1].location == 6)
135+
#expect(viewModel.findMatches[2].location == 12)
136+
}
137+
138+
@Test func noMatchesFound() async throws {
139+
let target = MockPanelTarget(text: "test1\ntest2\ntest3")
140+
let viewModel = FindPanelViewModel(target: target)
141+
viewModel.findText = "nonexistent"
142+
143+
viewModel.find()
144+
145+
#expect(viewModel.findMatches.isEmpty)
146+
#expect(viewModel.currentFindMatchIndex == nil)
147+
}
148+
149+
@Test func matchCaseToggle() async throws {
150+
let target = MockPanelTarget(text: "Test1\ntest2\nTEST3")
151+
let viewModel = FindPanelViewModel(target: target)
152+
153+
// Test case-sensitive
154+
viewModel.matchCase = true
155+
viewModel.findText = "Test"
156+
viewModel.find()
157+
#expect(viewModel.findMatches.count == 1)
158+
159+
// Test case-insensitive
160+
viewModel.matchCase = false
161+
viewModel.find()
162+
#expect(viewModel.findMatches.count == 3)
163+
}
164+
165+
@Test func findMethodPickerOptions() async throws {
166+
let target = MockPanelTarget(text: "test1 test2 test3")
167+
let viewModel = FindPanelViewModel(target: target)
168+
169+
// Test contains
170+
viewModel.findMethod = .contains
171+
viewModel.findText = "test"
172+
viewModel.find()
173+
#expect(viewModel.findMatches.count == 3)
174+
175+
// Test matchesWord
176+
viewModel.findMethod = .matchesWord
177+
viewModel.find()
178+
#expect(viewModel.findMatches.count == 3)
179+
180+
// Test startsWith
181+
viewModel.findMethod = .startsWith
182+
viewModel.findText = "test"
183+
viewModel.find()
184+
#expect(viewModel.findMatches.count == 3)
185+
186+
// Test endsWith
187+
viewModel.findMethod = .endsWith
188+
viewModel.findText = "test3"
189+
viewModel.find()
190+
#expect(viewModel.findMatches.count == 1)
191+
192+
// Test regularExpression
193+
viewModel.findMethod = .regularExpression
194+
viewModel.findText = "test\\d"
195+
viewModel.find()
196+
#expect(viewModel.findMatches.count == 3)
197+
}
198+
199+
@Test func findMethodPickerOptionsWithComplexText() async throws {
200+
let target = MockPanelTarget(text: "test1 test2 test3\nprefix_test suffix_test\nword_test_word")
201+
let viewModel = FindPanelViewModel(target: target)
202+
203+
// Test contains with partial matches
204+
viewModel.findMethod = .contains
205+
viewModel.findText = "test"
206+
viewModel.find()
207+
#expect(viewModel.findMatches.count == 6)
208+
209+
// Test matchesWord with word boundaries
210+
viewModel.findMethod = .matchesWord
211+
viewModel.find()
212+
#expect(viewModel.findMatches.count == 3)
213+
214+
// Test startsWith with prefixes
215+
viewModel.findMethod = .startsWith
216+
viewModel.findText = "prefix"
217+
viewModel.find()
218+
#expect(viewModel.findMatches.count == 1)
219+
220+
// Test endsWith with suffixes
221+
viewModel.findMethod = .endsWith
222+
viewModel.findText = "suffix"
223+
viewModel.find()
224+
#expect(viewModel.findMatches.count == 1)
225+
226+
// Test regularExpression with complex pattern
227+
viewModel.findMethod = .regularExpression
228+
viewModel.findText = "test\\d"
229+
viewModel.find()
230+
#expect(viewModel.findMatches.count == 3)
231+
}
232+
}

0 commit comments

Comments
 (0)