Skip to content

Instantly share code, notes, and snippets.

View ferPrieto's full-sized avatar
💪
Attitute beats talent

Fernando Prieto Moyano ferPrieto

💪
Attitute beats talent
View GitHub Profile
const val BASE_URL = "http://127.0.0.1"
class TestConfigurationBuilder {
private lateinit var baseUrl: String
init {
testBaseUrl()
}
fun testBaseUrl() {
@ferPrieto
ferPrieto / InstagramErrorContent.kt
Created June 30, 2020 00:58
This is the interface that contains the common parameters the different InstagramErrors will have
interface InstagramErrorContent {
val photoVideoUrl: String?
val tags: List<String>?
val errorCode: ErrorCode?
}
@ferPrieto
ferPrieto / InstagramErrorType.kt
Created June 30, 2020 01:02
This class represents all of the different Instagram Errors as example
sealed class InstagramErrorType : InstagramErrorContent {
data class DeclinePost(
override val photoVideoUrl: String,
override val tags: List<String>,
override val errorCode: ErrorCode,
val textContent: String,
val location: String
) : InstagramErrorType()
@InstagramErrorCode
enum class ErrorCode {
Declined,
OverLimit,
TimeOut
}
class InstagramErrorByClassTest {
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class MixedConstructors {
private val instagramErrors = mutableListOf<String>()
@AfterAll
fun check() {
class InstagramErrorByTypeTest {
companion object {
@Suppress("unused")
@JvmStatic
private fun checkSealedClassesByType() = Stream.of(
Arguments.of(
InstagramErrorType.DeclinePost(
photoVideoUrl = "https://www.instagram.com/p/B_2B2nwpS5g/",
tags = listOf("pullup", "bodyweight"),
errorCode = ErrorCode.Declined,
internal class InstagramTypeFactory : DefaultTypeFactory() {
override fun KParameter.createArgument(): Any? =
when (type.classifier) {
Int::class -> 0
Byte::class -> 0.toByte()
Short::class -> 0.toShort()
String::class -> ""
Float::class -> 0f
Long::class -> 0L
List::class -> emptyList<String>()
@ferPrieto
ferPrieto / InstagramTypeFactory.kt
Created June 30, 2020 22:08
This is the second solution implemented using the annotation and the inline function `findAnnotation`
else -> if ((type.classifier as KClass<out Any>).findAnnotation<InstagramErrorCode>() != null) ErrorCode.TimeOut else null
@ferPrieto
ferPrieto / InstagramTypeFactory.kt
Last active June 30, 2020 22:09
This is the first solution implemented to find the ErrorCode by qualifiedName
else -> if ((type.classifier as KClass<out Any>).qualifiedName == "sctestingplayground.ErrorCode") ErrorCode.TimeOut else null
@ferPrieto
ferPrieto / NumberSeries.kt
Created August 5, 2020 10:17
Number-series naming example, with the correct and incorrect way to do it
// Noninformative way
fun combineLists(a1: List<Int>, a2: List<Int>): List<Int> = a1.mapIndexed { i, f -> f - a2[i] }
// Meaningful distinction
fun combinePrices(amazonPrices: List<Int>, ebayPrices: List<Int>): List<Int> =
amazonPrices.mapIndexed { index, amazonPrice -> amazonPrice - ebayPrices[index] }