Add method to map string type to AlbumListType enum.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2017-09-14 22:32:24 +02:00
parent 645728c0f7
commit 8e895685fc
2 changed files with 61 additions and 1 deletions

View File

@ -19,6 +19,25 @@ enum class AlbumListType(val typeName: String) {
BY_GENRE("byGenre");
override fun toString(): String {
return super.toString().toLowerCase()
return typeName
}
companion object {
@JvmStatic
fun fromName(typeName: String): AlbumListType = when (typeName) {
in RANDOM.typeName -> RANDOM
in NEWEST.typeName -> NEWEST
in HIGHEST.typeName -> HIGHEST
in FREQUENT.typeName -> FREQUENT
in RECENT.typeName -> RECENT
in SORTED_BY_NAME.typeName -> SORTED_BY_NAME
in SORTED_BY_ARTIST.typeName -> SORTED_BY_ARTIST
in STARRED.typeName -> STARRED
in BY_YEAR.typeName -> BY_YEAR
in BY_GENRE.typeName -> BY_GENRE
else -> throw IllegalArgumentException("Unknown type: $typeName")
}
private operator fun String.contains(other: String) = this.equals(other, true)
}
}

View File

@ -0,0 +1,41 @@
package org.moire.ultrasonic.api.subsonic.models
import org.amshove.kluent.`should equal to`
import org.amshove.kluent.`should equal`
import org.amshove.kluent.`should throw`
import org.junit.Test
/**
* Unit test for [AlbumListType] class.
*/
class AlbumListTypeTest {
@Test
fun `Should create type from string ignoring case`() {
val type = AlbumListType.SORTED_BY_NAME.typeName.toLowerCase()
val albumListType = AlbumListType.fromName(type)
albumListType `should equal` AlbumListType.SORTED_BY_NAME
}
@Test
fun `Should throw IllegalArgumentException for unknown type`() {
val failCall = {
AlbumListType.fromName("some-not-existing-type")
}
failCall `should throw` IllegalArgumentException::class
}
@Test
fun `Should convert type string to corresponding AlbumListType`() {
AlbumListType.values().forEach {
AlbumListType.fromName(it.typeName) `should equal` it
}
}
@Test
fun `Should return type name for toString call`() {
AlbumListType.STARRED.typeName `should equal to` AlbumListType.STARRED.toString()
}
}