From c39f270138e1347ca172b43beac5571997d6a443 Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Sun, 21 Jan 2024 22:00:10 +0100 Subject: [PATCH] add documentation to tests and refactor them --- .../searchfilesystem/BasicSearchTest.kt | 42 +++++++------------ .../searchfilesystem/DeepSearchTest.kt | 40 ++++++------------ .../searchfilesystem/FileSearchTest.kt | 12 ++++++ .../searchfilesystem/IndexedSearchTest.kt | 30 ++++++------- 4 files changed, 52 insertions(+), 72 deletions(-) diff --git a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/BasicSearchTest.kt b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/BasicSearchTest.kt index d20dbfbd6c..5a5702e3fa 100644 --- a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/BasicSearchTest.kt +++ b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/BasicSearchTest.kt @@ -49,6 +49,10 @@ class BasicSearchTest { @MockK(relaxed = true, relaxUnitFun = true) lateinit var foundFileMock: HybridFileParcelable + val filePath = "/test/abc.txt" + val fileName = "abc.txt" + + /** Set up all mocks */ @Before fun setup() { MockKAnnotations.init(this, relaxUnitFun = true) @@ -65,8 +69,11 @@ class BasicSearchTest { every { foundFileMock.isDirectory(any()) } returns false every { foundFileMock.isHidden } returns true + every { foundFileMock.path } returns filePath + every { foundFileMock.getName(any()) } returns fileName } + /** Clean up all mocks */ @After fun cleanup() { unmockkAll() @@ -78,12 +85,6 @@ class BasicSearchTest { */ @Test fun testSimpleSearchMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - - every { foundFileMock.path } returns filePath - every { foundFileMock.getName(any()) } returns fileName - val basicSearch = BasicSearch( "ab", filePath, @@ -110,12 +111,6 @@ class BasicSearchTest { */ @Test fun testSimpleSearchNotMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - - every { foundFileMock.path } returns filePath - every { foundFileMock.getName(any()) } returns fileName - val basicSearch = BasicSearch( "ba", filePath, @@ -126,7 +121,7 @@ class BasicSearchTest { basicSearch.foundFilesLiveData.observeForever { actualResults -> Assert.assertNotNull(actualResults) Assert.assertTrue( - "List was not empty as expected but had ${actualResults.size} elements", + listNotEmptyError(actualResults.size), actualResults.isEmpty() ) } @@ -142,12 +137,6 @@ class BasicSearchTest { */ @Test fun testSearchWithPathMatchButNameNotMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - - every { foundFileMock.path } returns filePath - every { foundFileMock.getName(any()) } returns fileName - val basicSearch = BasicSearch( "test", filePath, @@ -158,7 +147,7 @@ class BasicSearchTest { basicSearch.foundFilesLiveData.observeForever { actualResults -> Assert.assertNotNull(actualResults) Assert.assertTrue( - "List was not empty as expected but had ${actualResults.size} elements", + listNotEmptyError(actualResults.size), actualResults.isEmpty() ) } @@ -169,17 +158,11 @@ class BasicSearchTest { } /** - * If a file is hidden and hidden files should not be shown, it should not be added to + * If a file is hidden and hidden files should not be shown, it should not be added to * [FileSearch.foundFilesLiveData] */ @Test fun testMatchHiddenFile() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - - every { foundFileMock.path } returns filePath - every { foundFileMock.getName(any()) } returns fileName - val basicSearch = BasicSearch( "ab", filePath, @@ -190,7 +173,7 @@ class BasicSearchTest { basicSearch.foundFilesLiveData.observeForever { actualResults -> Assert.assertNotNull(actualResults) Assert.assertTrue( - "List was not empty as expected but had ${actualResults.size} elements", + listNotEmptyError(actualResults.size), actualResults.isEmpty() ) } @@ -199,4 +182,7 @@ class BasicSearchTest { basicSearch.search() } } + + private fun listNotEmptyError(size: Int) = + "List was not empty as expected but had $size elements" } diff --git a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/DeepSearchTest.kt b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/DeepSearchTest.kt index 0b667c25df..e13c7dc553 100644 --- a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/DeepSearchTest.kt +++ b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/DeepSearchTest.kt @@ -50,6 +50,10 @@ class DeepSearchTest { @MockK(relaxed = true, relaxUnitFun = true) lateinit var foundFileMock: HybridFileParcelable + val filePath = "/test/abc.txt" + val fileName = "abc.txt" + + /** Set up all mocks */ @Before fun setup() { MockKAnnotations.init(this, relaxUnitFun = true) @@ -66,8 +70,11 @@ class DeepSearchTest { every { foundFileMock.isDirectory(any()) } returns false every { foundFileMock.isHidden } returns true + every { foundFileMock.path } returns filePath + every { foundFileMock.getName(any()) } returns fileName } + /** Clean up all mocks */ @After fun cleanup() { unmockkAll() @@ -79,12 +86,6 @@ class DeepSearchTest { */ @Test fun testSimpleSearchMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - - every { foundFileMock.path } returns filePath - every { foundFileMock.getName(any()) } returns fileName - val deepSearch = DeepSearch( "ab", filePath, @@ -112,12 +113,6 @@ class DeepSearchTest { */ @Test fun testSimpleSearchNotMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - - every { foundFileMock.path } returns filePath - every { foundFileMock.getName(any()) } returns fileName - val deepSearch = DeepSearch( "ba", filePath, @@ -129,7 +124,7 @@ class DeepSearchTest { deepSearch.foundFilesLiveData.observeForever { actualResults -> Assert.assertNotNull(actualResults) Assert.assertTrue( - "List was not empty as expected but had ${actualResults.size} elements", + listNotEmptyError(actualResults.size), actualResults.isEmpty() ) } @@ -145,12 +140,6 @@ class DeepSearchTest { */ @Test fun testSearchWithPathMatchButNameNotMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - - every { foundFileMock.path } returns filePath - every { foundFileMock.getName(any()) } returns fileName - val deepSearch = DeepSearch( "test", filePath, @@ -162,7 +151,7 @@ class DeepSearchTest { deepSearch.foundFilesLiveData.observeForever { actualResults -> Assert.assertNotNull(actualResults) Assert.assertTrue( - "List was not empty as expected but had ${actualResults.size} elements", + listNotEmptyError(actualResults.size), actualResults.isEmpty() ) } @@ -178,12 +167,6 @@ class DeepSearchTest { */ @Test fun testMatchHiddenFile() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - - every { foundFileMock.path } returns filePath - every { foundFileMock.getName(any()) } returns fileName - val basicSearch = DeepSearch( "ab", filePath, @@ -195,7 +178,7 @@ class DeepSearchTest { basicSearch.foundFilesLiveData.observeForever { actualResults -> Assert.assertNotNull(actualResults) Assert.assertTrue( - "List was not empty as expected but had ${actualResults.size} elements", + listNotEmptyError(actualResults.size), actualResults.isEmpty() ) } @@ -204,4 +187,7 @@ class DeepSearchTest { basicSearch.search() } } + + private fun listNotEmptyError(size: Int) = + "List was not empty as expected but had $size elements" } diff --git a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/FileSearchTest.kt b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/FileSearchTest.kt index 43a035c961..57a8f61556 100644 --- a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/FileSearchTest.kt +++ b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/FileSearchTest.kt @@ -86,6 +86,7 @@ class FileSearchTest { } } + /** Test the simple filter with a path that matches the query */ @Test fun simpleFilterMatchTest() = runTest { getFileSearchMatch( @@ -95,6 +96,7 @@ class FileSearchTest { ).search() } + /** Test the simple filter with a path that does not match the query */ @Test fun simpleFilterNotMatchTest() = runTest { // There is no "e" @@ -105,6 +107,7 @@ class FileSearchTest { ).search() } + /** Test the regex filter with a path that matches the query. The query contains `*`. */ @Test fun regexFilterStarMatchTest() = runTest { getFileSearchMatch( @@ -114,6 +117,7 @@ class FileSearchTest { ).search() } + /** Test the regex filter with a path that does not match the query. The query contains `*`. */ @Test fun regexFilterStarNotMatchTest() = runTest { // There is no "e" @@ -124,6 +128,7 @@ class FileSearchTest { ).search() } + /** Test the regex filter with a path that matches the query. The query contains `?`. */ @Test fun regexFilterQuestionMarkMatchTest() = runTest { getFileSearchMatch( @@ -133,6 +138,7 @@ class FileSearchTest { ).search() } + /** Test the regex filter with a path that does not match the query. The query contains `?`. */ @Test fun regexFilterQuestionMarkNotMatchTest() = runTest { // There is one character missing between "a" and "e" @@ -143,6 +149,10 @@ class FileSearchTest { ).search() } + /** + * Test the regex filter with a path that does not match the query + * because `-` is not recognized by `?` or `*`. + */ @Test fun regexFilterNotMatchNonWordCharacterTest() = runTest { getFileSearchNotMatch( @@ -152,6 +162,7 @@ class FileSearchTest { ).search() } + /** Test the regex match filter with a path that completely matches the query */ @Test fun regexMatchFilterMatchTest() = runTest { getFileSearchRegexMatches( @@ -161,6 +172,7 @@ class FileSearchTest { ).search() } + /** Test the regex match filter with a path that does not completely match the query */ @Test fun regexMatchFilterNotMatchTest() = runTest { // Pattern does not match whole name diff --git a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/IndexedSearchTest.kt b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/IndexedSearchTest.kt index 7078a33a21..6f78265f4c 100644 --- a/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/IndexedSearchTest.kt +++ b/app/src/test/java/com/amaze/filemanager/asynchronous/asynctasks/searchfilesystem/IndexedSearchTest.kt @@ -45,6 +45,10 @@ class IndexedSearchTest { @RelaxedMockK lateinit var mockCursor: Cursor + val filePath = "/test/abc.txt" + val fileName = "abc.txt" + + /** Set up all mocks */ @Before fun setup() { MockKAnnotations.init(this) @@ -57,8 +61,12 @@ class IndexedSearchTest { every { mockCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME) } returns displayNameColumn + + every { mockCursor.getString(dataColumn) } returns filePath + every { mockCursor.getString(displayNameColumn) } returns fileName } + /** Clean up all mocks */ @After fun cleanup() { unmockkAll() @@ -70,11 +78,6 @@ class IndexedSearchTest { */ @Test fun testSimpleSearchMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - every { mockCursor.getString(dataColumn) } returns filePath - every { mockCursor.getString(displayNameColumn) } returns fileName - val expectedNames = listOf(fileName) val expectedPaths = listOf(filePath) val expectedRanges = listOf(0..1) @@ -102,11 +105,6 @@ class IndexedSearchTest { */ @Test fun testSimpleSearchNotMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - every { mockCursor.getString(dataColumn) } returns filePath - every { mockCursor.getString(displayNameColumn) } returns fileName - val indexedSearch = IndexedSearch( "ba", "/", @@ -116,7 +114,7 @@ class IndexedSearchTest { indexedSearch.foundFilesLiveData.observeForever { actualResults -> Assert.assertNotNull(actualResults) Assert.assertTrue( - "List was not empty as expected but had ${actualResults.size} elements", + listNotEmptyError(actualResults.size), actualResults.isEmpty() ) } @@ -131,11 +129,6 @@ class IndexedSearchTest { */ @Test fun testSearchWithPathMatchButNameNotMatch() { - val filePath = "/test/abc.txt" - val fileName = "abc.txt" - every { mockCursor.getString(dataColumn) } returns filePath - every { mockCursor.getString(displayNameColumn) } returns fileName - val indexedSearch = IndexedSearch( "te", "/", @@ -145,7 +138,7 @@ class IndexedSearchTest { indexedSearch.foundFilesLiveData.observeForever { actualResults -> Assert.assertNotNull(actualResults) Assert.assertTrue( - "List was not empty as expected but had ${actualResults.size} elements", + listNotEmptyError(actualResults.size), actualResults.isEmpty() ) } @@ -153,4 +146,7 @@ class IndexedSearchTest { indexedSearch.search() } } + + private fun listNotEmptyError(size: Int) = + "List was not empty as expected but had $size elements" }