Skip to content

Instantly share code, notes, and snippets.

Avatar
💪
Attitute beats talent

Fernando Prieto Moyano ferPrieto

💪
Attitute beats talent
View GitHub Profile
View TestConfigurationBuilder.kt
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
View InstagramErrorContent.kt
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
View InstagramErrorType.kt
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()
View ErrorCode.kt
@InstagramErrorCode
enum class ErrorCode {
Declined,
OverLimit,
TimeOut
}
View InstagramErrorByClassTest.kt
class InstagramErrorByClassTest {
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class MixedConstructors {
private val instagramErrors = mutableListOf<String>()
@AfterAll
fun check() {
View InstagramErrorByTypeTest.kt
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,
View InstagramTypeFactory.kt
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`
View InstagramTypeFactory.kt
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
View InstagramTypeFactory.kt
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
View NumberSeries.kt
// 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] }