From da94e55e4e6acc1af8a2bed7e5d1c600e58c6fb9 Mon Sep 17 00:00:00 2001 From: Robert Stoll Date: Sun, 14 Mar 2021 21:41:49 +0100 Subject: [PATCH] useEmptyPrefix to be more flexible --- .../text/impl/DefaultTextReporter.kt | 80 ++++++++----------- .../atrium/reporting/text/impl/Main.kt | 41 +++++----- 2 files changed, 55 insertions(+), 66 deletions(-) diff --git a/core/api/atrium-core-api-jvm/src/main/kotlin/ch/tutteli/atrium/reporting/text/impl/DefaultTextReporter.kt b/core/api/atrium-core-api-jvm/src/main/kotlin/ch/tutteli/atrium/reporting/text/impl/DefaultTextReporter.kt index 628a5b2a1..7ef5f354e 100644 --- a/core/api/atrium-core-api-jvm/src/main/kotlin/ch/tutteli/atrium/reporting/text/impl/DefaultTextReporter.kt +++ b/core/api/atrium-core-api-jvm/src/main/kotlin/ch/tutteli/atrium/reporting/text/impl/DefaultTextReporter.kt @@ -53,16 +53,6 @@ class DefaultTextReporter( val sb = StringBuilder() format(rootNode, emptyList(), sb) return sb -// -// -// private fun noNeedToFormat(assertion: Assertion, parameterObject: AssertionFormatterParameterObject): Boolean { -// //assertionFilter only applies if: -// // - we are not in an assertion group which should not be filtered (e.g. explanatory or summary group) and -// // - if the given assertion is not an explanatory assertion group either. -// return parameterObject.isNotInDoNotFilterGroup() -// && !isExplanatoryAssertionGroup(assertion) -// && !parameterObject.assertionFilter(assertion) -// } } private fun format(outputNode: OutputNode, indentLevels: List, sb: StringBuilder) { @@ -159,8 +149,6 @@ interface TextPreRenderController { fun transform(reportable: Reportable): List fun transformChildren(children: List, controlObject: PreRenderControlObject): List fun transformChild(child: Reportable, controlObject: PreRenderControlObject): List - - fun indent(node: OutputNode, controlObject: PreRenderControlObject): OutputNode } class DefaultPreRenderController( @@ -183,13 +171,13 @@ class DefaultPreRenderController( .firstOrNull { it.canTransform(child) } ?: throw UnsupportedOperationException("no suitable ${PreRenderer::class.simpleName} found for the given reportable: $child") preRenderer.transform(child, controlObject).map { node -> - indent(node, controlObject) + indentAndPrefix(node, controlObject) } } else { emptyList() } - override fun indent( + private fun indentAndPrefix( node: OutputNode, controlObject: PreRenderControlObject ): OutputNode { @@ -199,7 +187,7 @@ class DefaultPreRenderController( } val iterator = node.columns.iterator() var index = controlObject.indentLevel - list.add(index++, iconStyler.style(controlObject.prefix)) + list.add(index++, if(node.useEmptyPrefix) StyledString.EMPTY_STRING else iconStyler.style(controlObject.prefix)) list.add(index++, iterator.next()) while (iterator.hasNext()) { list.add(index++, iterator.next()) @@ -221,7 +209,7 @@ data class OutputNode( val definesOwnLevel: Boolean, val indentLevel: Int = 0, val span: Int = 0, - val useIndentInsteadPrefix: Boolean = false + val useEmptyPrefix: Boolean = false ) { init { require(columns.isNotEmpty() || children.isNotEmpty()) { "at least one of columns/children needs to have elements" } @@ -272,17 +260,38 @@ enum class Style(val styleId: String) { } enum class Icon : Single { - INFORMATION_SOURCE, BULB, - DEBUG_INFO, BANGBANG, - SUCCESS, + DEBUG_INFO, + EMPTY_STRING, FAILURE, FEATURE, - EMPTY_STRING, - BULLET_POINT + FEATURE_BULLET_POINT, + INFORMATION_SOURCE, + ROOT_BULLET_POINT, + SUCCESS +} +interface IconStyler { + fun style(icon: Icon): StyledString } +class DefaultIconStyler(styler: Styler) : IconStyler { + private val icons = mapOf( + BULB to styler.style("💡 ", Style.BULB), + BANGBANG to styler.style("❗❗ ", Style.FAILURE), + DEBUG_INFO to "🔎 ".noStyle(), + EMPTY_STRING to StyledString.EMPTY_STRING, + FAILURE to styler.style("✘ ", Style.FAILURE), + FEATURE to "â–ļ ".noStyle(), + FEATURE_BULLET_POINT to "- ".noStyle(), + INFORMATION_SOURCE to styler.style("â„šī¸", Style.INFORMATION_SOURCE), + ROOT_BULLET_POINT to "◆".noStyle(), + SUCCESS to styler.style("✔ ", Style.SUCCESS) + ) + + override fun style(icon: Icon): StyledString = + icons[icon] ?: throw IllegalArgumentException("unsupported icon: $icon") +} class Ansi8Colours : ThemeProvider { override val styles = mapOf( @@ -372,9 +381,6 @@ private fun determineChildControlObject( ): PreRenderControlObject { val indentLevel = if (shallIndentChildren) controlObject.indentLevel + 1 else controlObject.indentLevel return when (child) { - // check syntax for fallthrough or | - is Feature -> controlObject.copy(prefix = EMPTY_STRING, indentLevel = indentLevel) - is Paragraph -> controlObject.copy(prefix = EMPTY_STRING, indentLevel = indentLevel) is Proof -> if (child.holds()) { controlObject.copy(prefix = SUCCESS, indentLevel = indentLevel) } else { @@ -400,7 +406,7 @@ class DefaultRootPreRenderer( ), reportable.children.flatMap { child -> val newControlObject = determineChildControlObject( - controlObject, child, EMPTY_STRING, shallIndentChildren = false + controlObject, child, ROOT_BULLET_POINT, shallIndentChildren = false ) controlObject.controller.transformChild(child, newControlObject) }, @@ -428,11 +434,12 @@ class DefaultFeaturePreRenderer( ), reportable.children.flatMap { child -> val newControlObject = determineChildControlObject( - controlObject, child, BULLET_POINT, shallIndentChildren = true + controlObject, child, FEATURE_BULLET_POINT, shallIndentChildren = true ) controlObject.controller.transformChild(child, newControlObject) }, definesOwnLevel = true, + useEmptyPrefix = true, span = 2 ) ) @@ -460,7 +467,7 @@ class DefaultParagraphPreRenderer : TypedPreRenderer(Paragraph::class paragraphColumns, emptyList(), definesOwnLevel = true, - // does not work either because + useEmptyPrefix = true, indentLevel = if (reportable.withIndent) 1 else 0 ) ) @@ -472,26 +479,7 @@ class DefaultTextPreRenderer(private val styler: Styler) : TypedPreRenderer(Icon::class) { diff --git a/core/api/atrium-core-api-jvm/src/main/kotlin/ch/tutteli/atrium/reporting/text/impl/Main.kt b/core/api/atrium-core-api-jvm/src/main/kotlin/ch/tutteli/atrium/reporting/text/impl/Main.kt index 8abca0e40..184453b6a 100644 --- a/core/api/atrium-core-api-jvm/src/main/kotlin/ch/tutteli/atrium/reporting/text/impl/Main.kt +++ b/core/api/atrium-core-api-jvm/src/main/kotlin/ch/tutteli/atrium/reporting/text/impl/Main.kt @@ -9,35 +9,36 @@ fun main(args: Array) { SimpleProof( Untranslatable("starts with"), "hello" ) { false }, -// SimpleProof(Untranslatable("ends with"), "test") { true }, -// Paragraph( -// Icon.INFORMATION_SOURCE, -// Text2("because, xyz"), -// withIndent = false -// ), -// Paragraph( -// Icon.BULB, -// Text2("notice, MyClass has not overridden equals\nbetter use toBeSameInstanceAs or implement equals/hashCode\n") -// ), -// Paragraph( -// Icon.DEBUG_INFO, -// Text2("the closest existing parent directory is /usr/bi") -// ), + SimpleProof(Untranslatable("ends with"), "test") { true }, + Paragraph( + Icon.DEBUG_INFO, + Text2("the closest existing parent directory is /usr/bin") + ), + Paragraph( + Icon.INFORMATION_SOURCE, + Text2("because, xyz"), + withIndent = false + ), + Feature( Untranslatable("name"), Text2("robert", Style.FAILURE), listOf( SimpleProof(Untranslatable("to equal"), 1) { false }, -// Paragraph( -// Icon.BANGBANG, -// Text2("failure at parent path"), -// withIndent = true -// ), + Paragraph( + Icon.BANGBANG, + Text2("failure at parent path"), + withIndent = true + ), Feature( Untranslatable("age"), 10, listOf( - SimpleProof(Untranslatable("to equal"), 2) { false } + SimpleProof(Untranslatable("to equal"), 2) { false }, + Paragraph( + Icon.BULB, + Text2("notice, MyClass has not overridden equals\nbetter use toBeSameInstanceAs or implement equals/hashCode\n") + ) ) ) )