mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 14:52:13 -04:00
added test cases
This commit is contained in:
parent
fe6ed3f62a
commit
0d001d61f2
@ -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("/", "")
|
||||
|
@ -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("")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user