Skip to content

Commit

Permalink
add documentation to tests and refactor them
Browse files Browse the repository at this point in the history
  • Loading branch information
seelchen committed Jan 21, 2024
1 parent e649b7a commit c39f270
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
)
}
Expand All @@ -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,
Expand All @@ -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()
)
}
Expand All @@ -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,
Expand All @@ -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()
)
}
Expand All @@ -199,4 +182,7 @@ class BasicSearchTest {
basicSearch.search()
}
}

private fun listNotEmptyError(size: Int) =
"List was not empty as expected but had $size elements"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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()
)
}
Expand All @@ -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,
Expand All @@ -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()
)
}
Expand All @@ -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,
Expand All @@ -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()
)
}
Expand All @@ -204,4 +187,7 @@ class DeepSearchTest {
basicSearch.search()
}
}

private fun listNotEmptyError(size: Int) =
"List was not empty as expected but had $size elements"
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class FileSearchTest {
}
}

/** Test the simple filter with a path that matches the query */
@Test
fun simpleFilterMatchTest() = runTest {
getFileSearchMatch(
Expand All @@ -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"
Expand All @@ -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(
Expand All @@ -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"
Expand All @@ -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(
Expand All @@ -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"
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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<MatchRange>(0..1)
Expand Down Expand Up @@ -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",
"/",
Expand All @@ -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()
)
}
Expand All @@ -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",
"/",
Expand All @@ -145,12 +138,15 @@ 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()
)
}
runTest {
indexedSearch.search()
}
}

private fun listNotEmptyError(size: Int) =
"List was not empty as expected but had $size elements"
}

0 comments on commit c39f270

Please sign in to comment.