Skip to content

Commit

Permalink
Enhance table builder logic"
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelpasterz committed Jun 23, 2021
1 parent 2e96952 commit de07d2f
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package ftl.environment

import ftl.util.Alignment
import ftl.util.SystemOutColor
import ftl.util.TableColumn

typealias TestEnvironmentInfo = MutableMap<String, MutableList<String>>

internal fun TestEnvironmentInfo.getOrCreateList(key: String) = getOrPut(key) { mutableListOf() }

internal fun TestEnvironmentInfo.createTableColumnFor(key: String) = TableColumn(key, getValue(key))
internal fun TestEnvironmentInfo.createTableColumnFor(key: String, alignment: Alignment) = TableColumn(key, getValue(key), alignment = alignment)

internal val tagToSystemOutColorMapper: (String) -> SystemOutColor = {
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import ftl.environment.createTableColumnFor
import ftl.environment.getOrCreateList
import ftl.environment.isValid
import ftl.environment.tagToSystemOutColorMapper
import ftl.util.Alignment
import ftl.util.SystemOutColor
import ftl.util.alignToTheXMark
import ftl.util.applyColorsUsing
import ftl.util.buildTable

Expand All @@ -38,13 +40,13 @@ private fun List<DeviceModel.Android>.createTestEnvironmentInfo() =
}

private fun TestEnvironmentInfo.createAndroidDevicesTable() = buildTable(
createTableColumnFor(MODEL_ID),
createTableColumnFor(MAKE),
createTableColumnFor(MODEL_NAME),
createTableColumnFor(FORM).applyColorsUsing(formToSystemOutColorMapper),
createTableColumnFor(RESOLUTION),
createTableColumnFor(OS_VERSION_IDS),
createTableColumnFor(TAGS).applyColorsUsing(tagToSystemOutColorMapper)
createTableColumnFor(MODEL_ID, Alignment.LEFT),
createTableColumnFor(MAKE, Alignment.LEFT),
createTableColumnFor(MODEL_NAME, Alignment.LEFT),
createTableColumnFor(FORM, Alignment.LEFT).applyColorsUsing(formToSystemOutColorMapper),
createTableColumnFor(RESOLUTION, Alignment.CENTER).alignToTheXMark(),
createTableColumnFor(OS_VERSION_IDS, Alignment.LEFT),
createTableColumnFor(TAGS, Alignment.LEFT).applyColorsUsing(tagToSystemOutColorMapper)
)

private val DeviceModel.Android.resolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import ftl.environment.createTableColumnFor
import ftl.environment.getOrCreateList
import ftl.environment.isValid
import ftl.environment.tagToSystemOutColorMapper
import ftl.util.Alignment
import ftl.util.alignToTheXMark
import ftl.util.applyColorsUsing
import ftl.util.buildTable

Expand All @@ -33,9 +35,9 @@ private val DeviceModel.Ios.resolution
get() = if (screenX.isValid() && screenY.isValid()) "$screenY x $screenX" else "UNKNOWN"

private fun TestEnvironmentInfo.createIoDevicesTable() = buildTable(
createTableColumnFor(MODEL_ID),
createTableColumnFor(MODEL_NAME),
createTableColumnFor(RESOLUTION),
createTableColumnFor(OS_VERSION_IDS),
createTableColumnFor(TAGS).applyColorsUsing(tagToSystemOutColorMapper)
createTableColumnFor(MODEL_ID, Alignment.LEFT),
createTableColumnFor(MODEL_NAME, Alignment.LEFT),
createTableColumnFor(RESOLUTION, Alignment.CENTER).alignToTheXMark(),
createTableColumnFor(OS_VERSION_IDS, Alignment.LEFT),
createTableColumnFor(TAGS, Alignment.LEFT).applyColorsUsing(tagToSystemOutColorMapper)
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ftl.environment.createTableColumnFor
import ftl.environment.getOrCreateList
import ftl.environment.orUnknown
import ftl.environment.tagToSystemOutColorMapper
import ftl.util.Alignment
import ftl.util.applyColorsUsing
import ftl.util.buildTable

Expand Down Expand Up @@ -42,11 +43,11 @@ private fun List<IosVersion>.createTestEnvironmentInfoFromIosVersions() =
}

private fun TestEnvironmentInfo.createIOsSoftwareVersionsTable() = buildTable(
createTableColumnFor(OS_VERSION_ID),
createTableColumnFor(MAJOR_VERSION),
createTableColumnFor(MINOR_VERSION),
createTableColumnFor(TAGS).applyColorsUsing(tagToSystemOutColorMapper),
createTableColumnFor(SUPPORTED_XCODE_VERSION_IDS)
createTableColumnFor(OS_VERSION_ID, Alignment.CENTER),
createTableColumnFor(MAJOR_VERSION, Alignment.CENTER),
createTableColumnFor(MINOR_VERSION, Alignment.CENTER),
createTableColumnFor(TAGS, Alignment.CENTER).applyColorsUsing(tagToSystemOutColorMapper),
createTableColumnFor(SUPPORTED_XCODE_VERSION_IDS, Alignment.LEFT)
)

private const val MAJOR_VERSION = "MAJOR_VERSION"
Expand Down
19 changes: 13 additions & 6 deletions test_runner/src/main/kotlin/ftl/json/SavedMatrixTableUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ftl.json

import ftl.api.TestMatrix
import ftl.util.Alignment
import ftl.util.StepOutcome.failure
import ftl.util.StepOutcome.flaky
import ftl.util.StepOutcome.success
Expand All @@ -14,27 +15,33 @@ fun List<TestMatrix.Data>.asPrintableTable(): String = buildTable(
TableColumn(
header = OUTCOME_COLUMN_HEADER,
data = flatMapTestAxis { outcome },
dataColor = flatMapTestAxis { outcomeColor }
dataColor = flatMapTestAxis { outcomeColor },
alignment = Alignment.LEFT
),
TableColumn(
header = MATRIX_ID_COLUMN_HEADER,
data = flatMapTestAxis { matrix -> matrix.matrixId }
data = flatMapTestAxis { matrix -> matrix.matrixId },
alignment = Alignment.LEFT
),
TableColumn(
header = APP_NAME_COLUMN_HEADER,
data = flatMapTestAxis { matrix -> matrix.appFileName }
data = flatMapTestAxis { matrix -> matrix.appFileName },
alignment = Alignment.LEFT
),
TableColumn(
header = TEST_APP_NAME_COLUMN_HEADER,
data = flatMapTestAxis { matrix -> matrix.testFileName }
data = flatMapTestAxis { matrix -> matrix.testFileName },
alignment = Alignment.LEFT
),
TableColumn(
header = TEST_AXIS_VALUE_HEADER,
data = flatMapTestAxis { device }
data = flatMapTestAxis { device },
alignment = Alignment.LEFT
),
TableColumn(
header = OUTCOME_DETAILS_COLUMN_HEADER,
data = flatMapTestAxis { details }
data = flatMapTestAxis { details },
alignment = Alignment.LEFT
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ftl.environment.TestEnvironmentInfo
import ftl.environment.createTableColumnFor
import ftl.environment.getOrCreateList
import ftl.environment.tagToSystemOutColorMapper
import ftl.util.Alignment
import ftl.util.applyColorsUsing
import ftl.util.buildTable

Expand All @@ -19,9 +20,9 @@ private fun List<Orientation>.createOrientationsDetails() = fold(mutableMapOf<St
}

private fun TestEnvironmentInfo.createOrientationsTable() = buildTable(
createTableColumnFor(ORIENTATION_ID),
createTableColumnFor(NAME),
createTableColumnFor(TAG).applyColorsUsing(tagToSystemOutColorMapper)
createTableColumnFor(ORIENTATION_ID, Alignment.LEFT),
createTableColumnFor(NAME, Alignment.LEFT),
createTableColumnFor(TAG, Alignment.CENTER).applyColorsUsing(tagToSystemOutColorMapper)
)

private const val ORIENTATION_ID = "ORIENTATION_ID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ftl.environment.getOrCreateList
import ftl.environment.orUnknown
import ftl.environment.tagToSystemOutColorMapper
import ftl.reports.api.twoDigitString
import ftl.util.Alignment
import ftl.util.applyColorsUsing
import ftl.util.buildTable

Expand All @@ -31,12 +32,12 @@ private fun OsVersion.Date?.printableReleaseDate() =
else "$year-${month.twoDigitString()}-${day.twoDigitString()}"

private fun TestEnvironmentInfo.createAndroidSoftwareVersionsTable() = buildTable(
createTableColumnFor(OS_VERSION_ID),
createTableColumnFor(VERSION),
createTableColumnFor(CODE_NAME),
createTableColumnFor(API_LEVEL),
createTableColumnFor(RELEASE_DATE),
createTableColumnFor(TAGS).applyColorsUsing(tagToSystemOutColorMapper)
createTableColumnFor(OS_VERSION_ID, Alignment.CENTER),
createTableColumnFor(VERSION, Alignment.CENTER),
createTableColumnFor(CODE_NAME, Alignment.LEFT),
createTableColumnFor(API_LEVEL, Alignment.CENTER),
createTableColumnFor(RELEASE_DATE, Alignment.CENTER),
createTableColumnFor(TAGS, Alignment.CENTER).applyColorsUsing(tagToSystemOutColorMapper)
)

const val VERSION = "VERSION"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl.presentation.cli.firebase.test.ipblocks

import ftl.api.IpBlockList
import ftl.environment.getOrCreateList
import ftl.util.Alignment
import ftl.util.TableColumn
import ftl.util.TableStyle

Expand All @@ -18,7 +19,8 @@ private fun IpBlockList.createDataMap() = blocks
}
}

private fun Map<String, List<String>>.collectDataPerColumn() = map { (header, data) -> TableColumn(header, data) }
private fun Map<String, List<String>>.collectDataPerColumn() =
map { (header, data) -> TableColumn(header = header, data = data, alignment = Alignment.LEFT) }

private fun List<TableColumn>.buildTable() =
if (isNotEmpty()) ftl.util.buildTable(*toTypedArray(), tableStyle = TableStyle.DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ftl.environment.createTableColumnFor
import ftl.environment.getOrCreateList
import ftl.environment.tagToSystemOutColorMapper
import ftl.run.exception.FlankGeneralError
import ftl.util.Alignment
import ftl.util.applyColorsUsing
import ftl.util.buildTable

Expand All @@ -23,10 +24,10 @@ fun List<Locale>.createTestEnvironment() =
}

fun TestEnvironmentInfo.createLocalesTable() = buildTable(
createTableColumnFor(LOCALE),
createTableColumnFor(NAME),
createTableColumnFor(REGION),
createTableColumnFor(TAGS).applyColorsUsing(tagToSystemOutColorMapper)
createTableColumnFor(LOCALE, Alignment.LEFT),
createTableColumnFor(NAME, Alignment.LEFT),
createTableColumnFor(REGION, Alignment.LEFT),
createTableColumnFor(TAGS, Alignment.CENTER).applyColorsUsing(tagToSystemOutColorMapper)
)

private const val LOCALE = "LOCALE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ftl.api.NetworkProfile
import ftl.environment.TestEnvironmentInfo
import ftl.environment.createTableColumnFor
import ftl.environment.getOrCreateList
import ftl.util.Alignment
import ftl.util.TableStyle
import ftl.util.buildTable

Expand Down Expand Up @@ -35,13 +36,13 @@ private fun List<NetworkProfile>.createConfigurationDetails() = fold(mutableMapO
}

private fun TestEnvironmentInfo.createConfigurationsTable() = buildTable(
createTableColumnFor(PROFILE_ID),
createTableColumnFor(RULE),
createTableColumnFor(DELAY),
createTableColumnFor(LOSS_RATION),
createTableColumnFor(DUPLICATION_RATION),
createTableColumnFor(BANDWIDTH),
createTableColumnFor(BURST),
createTableColumnFor(PROFILE_ID, Alignment.LEFT),
createTableColumnFor(RULE, Alignment.LEFT),
createTableColumnFor(DELAY, Alignment.CENTER),
createTableColumnFor(LOSS_RATION, Alignment.CENTER),
createTableColumnFor(DUPLICATION_RATION, Alignment.CENTER),
createTableColumnFor(BANDWIDTH, Alignment.CENTER),
createTableColumnFor(BURST, Alignment.CENTER),
tableStyle = TableStyle.ROW_SEPARATOR
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ftl.presentation.cli.firebase.test.providedsoftware

import com.google.testing.model.ProvidedSoftwareCatalog
import ftl.util.Alignment
import ftl.util.TableColumn
import ftl.util.buildTable

fun ProvidedSoftwareCatalog.toCliTable() = buildTable(
TableColumn(
ORCHESTRATOR_VERSION,
listOf(orchestratorVersion)
listOf(orchestratorVersion),
alignment = Alignment.LEFT
)
)

Expand Down
40 changes: 30 additions & 10 deletions test_runner/src/main/kotlin/ftl/util/LogTableBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ data class TableColumn(
val header: String,
val data: List<String>,
val dataColor: List<SystemOutColor> = listOf(),
val columnSize: Int = ((data + header).maxByOrNull { it.length }?.length ?: 0) + DEFAULT_COLUMN_PADDING
val columnSize: Int = ((data + header).maxByOrNull { it.length }?.length ?: 0) + DEFAULT_COLUMN_PADDING,
val alignment: Alignment
)

enum class Alignment {
LEFT, CENTER
}

private data class DataWithSize(
val data: String,
val columnSize: Int,
val centered: Boolean
) {
constructor(data: String, columnSize: Int) : this(data, columnSize, data.matches("-?\\d+(\\.\\d+)?".toRegex()))
}
val alignment: Alignment
)

private const val DEFAULT_COLUMN_PADDING = 2

Expand Down Expand Up @@ -63,7 +66,7 @@ fun buildTable(vararg tableColumns: TableColumn, tableStyle: TableStyle = TableS
val builder = StringBuilder().apply {
startTable(rowSizes)
tableColumns
.map { DataWithSize(data = it.header, columnSize = it.columnSize, centered = true) }
.map { DataWithSize(data = it.header, columnSize = it.columnSize, alignment = Alignment.CENTER) }
.apply { appendDataRow(this) }
rowSeparator(rowSizes)
appendData(tableColumns, rowSizes, tableStyle)
Expand Down Expand Up @@ -92,16 +95,20 @@ private fun StringBuilder.rowSeparator(rowSizes: List<Int>) {
appendLine()
}

private fun StringBuilder.appendData(tableColumns: Array<out TableColumn>, rowSizes: List<Int>, tableStyle: TableStyle) {
private fun StringBuilder.appendData(
tableColumns: Array<out TableColumn>,
rowSizes: List<Int>,
tableStyle: TableStyle
) {
val rowCount = (tableColumns.maxByOrNull { it.data.size } ?: tableColumns.first()).data.size
(0 until rowCount)
.map { rowNumber ->
tableColumns.map {
val color = it.dataColor.getOrNull(rowNumber) ?: SystemOutColor.DEFAULT
val data = it.data.getOrNull(rowNumber).orEmpty()
val columnSize = it.columnSize
if (color == SystemOutColor.DEFAULT) DataWithSize(data, columnSize)
else DataWithSize(color.applyTo(data), columnSize + color.additionalLengthWhenApplied)
if (color == SystemOutColor.DEFAULT) DataWithSize(data, columnSize, it.alignment)
else DataWithSize(color.applyTo(data), columnSize + color.additionalLengthWhenApplied, it.alignment)
}
}
.forEachIndexed { index, list ->
Expand Down Expand Up @@ -131,7 +138,12 @@ private fun StringBuilder.appendTableSeparator(startChar: Char, middleChar: Char
private fun StringBuilder.appendDataRow(data: List<DataWithSize>) {
append(TABLE_VERTICAL_LINE)
data.forEach {
if (it.centered) append(it.center()) else append(it.leftAligned())
append(
when (it.alignment) {
Alignment.LEFT -> it.leftAligned()
Alignment.CENTER -> it.center()
}
)
append(TABLE_VERTICAL_LINE)
}
appendLine()
Expand All @@ -145,3 +157,11 @@ private fun DataWithSize.center() = String.format(
)

inline fun TableColumn.applyColorsUsing(mapper: (String) -> SystemOutColor) = copy(dataColor = data.map(mapper))

// RESOLUTION column needs dedicated alignment logic and data pre-processing
fun TableColumn.alignToTheXMark() = copy(
data = data.map {
val dimensions = it.split("x")
if (dimensions[0].length + 1 == dimensions[1].length) " $it" else it
}
)
8 changes: 4 additions & 4 deletions test_runner/src/test/kotlin/ftl/util/LogTableBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import org.junit.Test
internal class LogTableBuilderTest {

private val sampleColumns = arrayOf(
TableColumn("header1", listOf("value1"), columnSize = 10),
TableColumn("header2", listOf("value2"), columnSize = 15),
TableColumn("header3", listOf("value3"), columnSize = 20),
TableColumn("header4", listOf("value4"), columnSize = 21)
TableColumn("header1", listOf("value1"), columnSize = 10, alignment = Alignment.LEFT),
TableColumn("header2", listOf("value2"), columnSize = 15, alignment = Alignment.LEFT),
TableColumn("header3", listOf("value3"), columnSize = 20, alignment = Alignment.LEFT),
TableColumn("header4", listOf("value4"), columnSize = 21, alignment = Alignment.LEFT)
)

@Test
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/test/kotlin/ftl/util/TableColumnTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class TableColumnTest {
val expectedColumnSize = 8

// when
val tableColumn = TableColumn("55555", listOf("1", "22", "333", "4444", "55555", "666666"))
val tableColumn = TableColumn("55555", listOf("1", "22", "333", "4444", "55555", "666666"), alignment = Alignment.LEFT)

// then
assertThat(tableColumn.columnSize).isEqualTo(expectedColumnSize)
Expand All @@ -23,7 +23,7 @@ internal class TableColumnTest {
val expectedColumnSize = 7

// when
val tableColumn = TableColumn("55555", listOf("1", "22", "333", "4444"))
val tableColumn = TableColumn("55555", listOf("1", "22", "333", "4444"), alignment = Alignment.LEFT)

// then
assertThat(tableColumn.columnSize).isEqualTo(expectedColumnSize)
Expand Down

0 comments on commit de07d2f

Please sign in to comment.