-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New ClassNameFormatter that handles formatting a valid class name (#851)
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
misk/src/main/kotlin/misk/web/formatter/ClassNameFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package misk.web.formatter | ||
|
||
import javax.inject.Inject | ||
import kotlin.reflect.KClass | ||
|
||
class ClassNameFormatter @Inject constructor() { | ||
fun <T : Any> format(kclass: KClass<T>): String { | ||
return when (kclass.qualifiedName) { | ||
null -> kclass.toString().split("class ").last() | ||
else -> kclass.qualifiedName.toString() | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
misk/src/test/kotlin/misk/web/formatter/ClassNameFormatterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package misk.web.formatter | ||
|
||
import com.google.common.base.CharMatcher | ||
import misk.testing.MiskTest | ||
import misk.web.NetworkChain | ||
import misk.web.NetworkInterceptor | ||
import misk.web.Response | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
@MiskTest(startService = false) | ||
class ClassNameFormatterTest { | ||
fun isValid(className: String) { | ||
assertTrue(className.isNotBlank()) | ||
assertNotNull(className) | ||
assertTrue( | ||
CharMatcher.inRange('A', 'Z').or(CharMatcher.inRange('a', 'z')).matchesAnyOf(className)) | ||
} | ||
|
||
@Test fun validQualifiedName() { | ||
val formatted = ClassNameFormatter().format(ValidQualifiedNameClass::class) | ||
isValid(formatted) | ||
assertEquals(formatted, ValidQualifiedNameClass::class.qualifiedName.toString()) | ||
} | ||
|
||
@Test fun missingQualifiedName() { | ||
val noQualifiedNameClass = NoQualifiedNameFactory().create() | ||
assertNull(noQualifiedNameClass::class.qualifiedName) | ||
|
||
val formatted = ClassNameFormatter().format(noQualifiedNameClass::class) | ||
isValid(formatted) | ||
assertEquals(formatted, noQualifiedNameClass::class.toString().split("class ").last()) | ||
} | ||
} | ||
|
||
class ValidQualifiedNameClass {} | ||
|
||
class NoQualifiedNameFactory { | ||
fun create(): NetworkInterceptor { | ||
return NoQualifiedNameClass | ||
} | ||
|
||
private companion object { | ||
val NoQualifiedNameClass = object : NetworkInterceptor { | ||
override fun intercept(chain: NetworkChain): Response<*> { | ||
return Response("False") | ||
} | ||
} | ||
} | ||
} |