@@ -30,30 +30,30 @@ import com.intellij.openapi.vfs.VirtualFile
3030import com.intellij.testFramework.TestApplicationManager
3131import com.mituuz.fuzzier.components.FuzzyFinderComponent
3232import com.mituuz.fuzzier.entities.CaseMode
33+ import com.mituuz.fuzzier.entities.FuzzyContainer
3334import com.mituuz.fuzzier.entities.GrepConfig
3435import com.mituuz.fuzzier.grep.backend.BackendStrategy
36+ import com.mituuz.fuzzier.runner.CommandRunner
3537import io.mockk.every
3638import io.mockk.mockk
3739import io.mockk.unmockkAll
3840import kotlinx.coroutines.runBlocking
3941import org.junit.jupiter.api.AfterEach
40- import org.junit.jupiter.api.Assertions.assertEquals
41- import org.junit.jupiter.api.Assertions.assertNotNull
42+ import org.junit.jupiter.api.Assertions.*
4243import org.junit.jupiter.api.BeforeEach
4344import org.junit.jupiter.api.Test
45+ import javax.swing.DefaultListModel
4446
4547class FuzzyGrepTest {
4648 private lateinit var fGrep: FuzzyGrep
4749
4850 private data class ValidVfContext (
49- val file : VirtualFile ,
50- val clm : ChangeListManager
51+ val file : VirtualFile , val clm : ChangeListManager
5152 )
5253
5354 private data class FindInFilesContext (
5455 val project : Project ,
5556 val component : FuzzyFinderComponent ,
56- val backend : BackendStrategy ,
5757 val clm : ChangeListManager
5858 )
5959
@@ -69,10 +69,7 @@ class FuzzyGrepTest {
6969 }
7070
7171 private fun createValidVfContext (
72- isDirectory : Boolean = false,
73- isBinary : Boolean = false,
74- isIgnored : Boolean = false,
75- extension : String? = null
72+ isDirectory : Boolean = false, isBinary : Boolean = false, isIgnored : Boolean = false, extension : String? = null
7673 ): ValidVfContext {
7774 val file = mockk<VirtualFile >()
7875 val clm = mockk<ChangeListManager >()
@@ -88,24 +85,21 @@ class FuzzyGrepTest {
8885 }
8986
9087 private fun createFindInFilesContext (
91- projectBasePath : String? = "/tmp/project",
92- secondaryText : String = "kt"
88+ projectBasePath : String? = "/tmp/project", secondaryText : String = "kt"
9389 ): FindInFilesContext {
9490 val project = mockk<Project >()
9591 val component = mockk<FuzzyFinderComponent >()
96- val backend = mockk<BackendStrategy >()
9792 val clm = mockk<ChangeListManager >()
9893
9994 every { project.basePath } returns projectBasePath
10095 every { component.getSecondaryText() } returns secondaryText
10196
10297 fGrep.component = component
103- fGrep.updateBackend(backend)
10498 fGrep.updateGrepConfig(
10599 GrepConfig (targets = null , caseMode = CaseMode .SENSITIVE , title = " Fuzzy Grep" )
106100 )
107101
108- return FindInFilesContext (project, component, backend, clm)
102+ return FindInFilesContext (project, component, clm)
109103 }
110104
111105 @Test
@@ -158,21 +152,63 @@ class FuzzyGrepTest {
158152
159153 @Test
160154 fun `findInFiles should skip backend when backend is null` () = runBlocking {
161- val (project, _, _ , clm) = createFindInFilesContext()
155+ val (project, component , clm) = createFindInFilesContext()
162156
163- val model = fGrep.findInFiles(" needle" , project, clm, null )
157+ val model = fGrep.findInFiles(" needle" , project, clm, null , component )
164158
165159 assertNotNull(model)
166160 assertEquals(0 , model.size)
167161 }
168162
169163 @Test
170164 fun `findInFiles should skip backend when project base path is null` () = runBlocking {
171- val (project, _, backend, clm) = createFindInFilesContext(projectBasePath = null )
165+ val (project, component, clm) = createFindInFilesContext(projectBasePath = null )
166+ val backend = MockBackend ()
172167
173- val model = fGrep.findInFiles(" needle" , project, clm, backend)
168+ val model = fGrep.findInFiles(" needle" , project, clm, backend, component )
174169
175170 assertNotNull(model)
176171 assertEquals(0 , model.size)
172+ assertFalse(backend.wasCalled)
173+ }
174+
175+ @Test
176+ fun `findInFiles calls backend when backend and project base path are available` () = runBlocking {
177+ val (project, component, clm) = createFindInFilesContext(projectBasePath = " not/null" )
178+ val backend = MockBackend ()
179+
180+ val model = fGrep.findInFiles(" needle" , project, clm, backend, component)
181+
182+ assertNotNull(model)
183+ assertEquals(0 , model.size)
184+ assertTrue(backend.wasCalled)
185+ assertEquals(" needle" , backend.receivedSearchString)
186+ assertEquals(" kt" , backend.receivedSecondarySearchString)
187+ assertEquals(" not/null" , backend.receivedProjectBasePath)
188+ }
189+
190+ private class MockBackend : BackendStrategy {
191+ override val name: String = " Mock"
192+
193+ var wasCalled = false
194+ var receivedSearchString: String? = null
195+ var receivedSecondarySearchString: String? = null
196+ var receivedProjectBasePath: String? = null
197+
198+ override suspend fun handleSearch (
199+ grepConfig : GrepConfig ,
200+ searchString : String ,
201+ secondarySearchString : String? ,
202+ commandRunner : CommandRunner ,
203+ listModel : DefaultListModel <FuzzyContainer >,
204+ projectBasePath : String ,
205+ project : Project ,
206+ fileFilter : (VirtualFile ) -> Boolean
207+ ) {
208+ wasCalled = true
209+ receivedSearchString = searchString
210+ receivedSecondarySearchString = secondarySearchString
211+ receivedProjectBasePath = projectBasePath
212+ }
177213 }
178214}
0 commit comments