Skip to content

Commit

Permalink
refactor: Structural output iOS models describe (#1895)
Browse files Browse the repository at this point in the history
Fixes #1850

## Test Plan
> How do we know the code works?

- Code is refactored according to #1850
- iOS describe models command works like previously

## Checklist

- [x] Unit tested
  • Loading branch information
piotradamczyk5 authored May 6, 2021
1 parent 944fa26 commit 307b583
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
12 changes: 7 additions & 5 deletions test_runner/src/main/kotlin/ftl/domain/DescribeIosModels.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package ftl.domain

import flank.common.logLn
import ftl.api.fetchDeviceModelIos
import ftl.args.IosArgs
import ftl.environment.ios.getDescription
import ftl.presentation.Output
import ftl.run.exception.FlankConfigurationError
import ftl.run.exception.FlankGeneralError
import java.nio.file.Paths

interface DescribeIosModels {
interface DescribeIosModels : Output {
val modelId: String
val configPath: String
}

operator fun DescribeIosModels.invoke() {
if (modelId.isBlank()) throw FlankConfigurationError("Argument MODEL_ID must be specified.")
// TODO move getDescription() and printing presentation layer during refactor of presentation after #1728
logLn(fetchDeviceModelIos(IosArgs.loadOrDefault(Paths.get(configPath)).project).getDescription(modelId))
fetchDeviceModelIos(IosArgs.loadOrDefault(Paths.get(configPath)).project)
.find { it.id == modelId }
?.out()
?: throw FlankGeneralError("ERROR: '$modelId' is not a valid model")
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package ftl.presentation.cli.firebase.test.ios.models

import ftl.api.DeviceModel
import ftl.config.FtlConstants
import ftl.domain.DescribeIosModels
import ftl.domain.invoke
import ftl.presentation.cli.firebase.test.ios.models.describe.prepareDescription
import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import picocli.CommandLine

@CommandLine.Command(
Expand Down Expand Up @@ -45,4 +49,11 @@ class IosModelDescribeCommand :
var usageHelpRequested: Boolean = false

override fun run() = invoke()

override val out = outputLogger {
when (this) {
is DeviceModel.Ios -> prepareDescription()
else -> throwUnknownType()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package ftl.environment.ios
package ftl.presentation.cli.firebase.test.ios.models.describe

import ftl.api.DeviceModel
import ftl.run.exception.FlankGeneralError

fun List<DeviceModel.Ios>.getDescription(modelId: String) = findModel(modelId)?.prepareDescription().orErrorMessage(modelId)

private fun List<DeviceModel.Ios>.findModel(modelId: String) = firstOrNull { it.id == modelId }

private fun DeviceModel.Ios.prepareDescription() = "".appendList(DEVICE_CAPABILITIES_HEADER, deviceCapabilities)
.appendModelBasicData(this).appendList(SUPPORTED_VERSIONS_HEADER, supportedVersionIds).appendList(TAGS_HEADER, tags).trim()
fun DeviceModel.Ios.prepareDescription() = "".appendList(DEVICE_CAPABILITIES_HEADER, deviceCapabilities)
.appendModelBasicData(this)
.appendList(SUPPORTED_VERSIONS_HEADER, supportedVersionIds)
.appendList(TAGS_HEADER, tags)
.trim()

private fun String.appendList(header: String, items: List<String>?) =
if (!items.isNullOrEmpty()) StringBuilder(this).appendLine(header).appendItems(items).toString()
Expand All @@ -29,7 +28,8 @@ screenY: ${model.screenY}
""".trimIndent()
).toString()

private fun String?.orErrorMessage(modelId: String) = this ?: throw FlankGeneralError("ERROR: '$modelId' is not a valid model")
private fun String?.orErrorMessage(modelId: String) =
this ?: throw FlankGeneralError("ERROR: '$modelId' is not a valid model")

private const val DEVICE_CAPABILITIES_HEADER = "deviceCapabilities:"
private const val SUPPORTED_VERSIONS_HEADER = "supportedVersionIds:"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ftl.environment.ios

import ftl.api.DeviceModel
import ftl.run.exception.FlankGeneralError
import ftl.test.util.TestHelper.getThrowable
import org.junit.Assert
import ftl.presentation.cli.firebase.test.ios.models.describe.prepareDescription
import org.junit.Assert.assertEquals
import org.junit.Test

class IosModelDescriptionTest {
Expand All @@ -23,7 +22,7 @@ class IosModelDescriptionTest {
)
)

val modelDescription = models.getDescription("iphone6s")
val modelDescription = models.find { it.id == "iphone6s" }?.prepareDescription()
val expected = """
deviceCapabilities:
- accelerometer
Expand All @@ -41,7 +40,7 @@ class IosModelDescriptionTest {
- deprecated=10.3
- deprecated=11.2
""".trimIndent()
Assert.assertEquals(expected, modelDescription)
assertEquals(expected, modelDescription)
}

@Test
Expand All @@ -60,7 +59,7 @@ class IosModelDescriptionTest {
)
)

val modelDescription = models.getDescription("iphone6s")
val modelDescription = models.find { it.id == "iphone6s" }?.prepareDescription()
val expected = """
deviceCapabilities:
- accelerometer
Expand All @@ -75,16 +74,6 @@ class IosModelDescriptionTest {
- 10.3
- 11.2
""".trimIndent()
Assert.assertEquals(expected, modelDescription)
}

@Test(expected = FlankGeneralError::class)
fun `should return error message if model not found and throw FlankGeneralError`() {
val versions = listOf<DeviceModel.Ios>()
val versionName = "test"
val localesDescription = getThrowable { versions.getDescription(versionName) }
val expected = "ERROR: '$versionName' is not a valid model"
Assert.assertEquals(expected, localesDescription.message)
throw localesDescription
assertEquals(expected, modelDescription)
}
}

0 comments on commit 307b583

Please sign in to comment.