added test cases

This commit is contained in:
Gouri Panda 2022-05-14 14:48:00 +05:30 committed by MohitMaliFtechiz
parent fe6ed3f62a
commit 0d001d61f2
2 changed files with 46 additions and 1 deletions

View File

@ -291,7 +291,12 @@ object FileUtils {
return null
}
private fun getDecodedFileName(url: String?, src: String?): String =
/**
* Returns the file name from the url or src. In url it gets the file name from the last '/' and
* if it contains '.'. If the url is null then it'll get the file name from the last '/'.
* If the url and src doesn't exist it returns the empty string.
*/
fun getDecodedFileName(url: String?, src: String?): String =
url?.substringAfterLast("/", "")
?.takeIf { it.contains(".") }
?: src?.substringAfterLast("/", "")

View File

@ -88,4 +88,44 @@ class FileUtilsTest {
every { mockFile.path } returns "$fileName$extension"
every { mockFile.exists() } returns fileExists
}
@Test
fun `test decode file name`() {
val fileName =
FileUtils.getDecodedFileName(
url = "https://kiwix.org/contributors/contributors_list.pdf",
src = null
)
assertThat(fileName).isEqualTo("contributors_list.pdf")
}
@Test
fun `test file name if extension doesn't exist`() {
val fileName = FileUtils.getDecodedFileName(url = "https://kiwix.org/contributors/", src = null)
assertThat(fileName).isEqualTo("")
}
@Test
fun `test file name if the url and src doesn't exist`() {
val fileName = FileUtils.getDecodedFileName(url = null, src = null)
assertThat(fileName).isEqualTo("")
}
@Test
fun `test file name if only file name exist`() {
val fileName = FileUtils.getDecodedFileName(src = "android_tutorials.pdf", url = null)
assertThat(fileName).isEqualTo("")
}
@Test
fun `test file name if url doesn't exist`() {
val fileName = FileUtils.getDecodedFileName(url = null, src = "/html/images/test.png")
assertThat(fileName).isEqualTo("test.png")
}
@Test
fun `test file name if url and src's extension doesn't exist`() {
val fileName = FileUtils.getDecodedFileName(url = null, src = "/html/images/")
assertThat(fileName).isEqualTo("")
}
}