Skip to content

Assert using message builder#1671

Draft
tokazio wants to merge 1 commit into
LemonAppDev:developfrom
tokazio:assert_using_message_builder
Draft

Assert using message builder#1671
tokazio wants to merge 1 commit into
LemonAppDev:developfrom
tokazio:assert_using_message_builder

Conversation

@tokazio

@tokazio tokazio commented Mar 11, 2025

Copy link
Copy Markdown

Allow using a StringBuilder as a message builder to dynamically build the assertion result message.

Konsist
        .scopeFromPackage("$packageName..")
        .classes()
        .assertTrue(testName="bla bla") { classDeclaration, messageBuilder ->
            val ctorProperties = classDeclaration.primaryConstructor!!.parameters.mapTo(mutableSetOf()) { param -> param.name }
            val actualSettableClassProperties = classDeclaration.properties()
            val missingProperties = actualSettableClassProperties - copyProperties
            if (missingProperties.isNotEmpty()) {
              messageBuilder?.appendLine(
                "${classDeclaration.name} has ${missingProperties.size} copiable properties outside of its ctor:\n ${
                missingProperties.joinToString("\n") { "\t* $it" }
              }"
              )
              false
            } else true
          }
        }

will produce a message like:

Assert 'bla bla' was violated (1 time).
MyClass has 2 copiable properties outside of its ctor:
 	* plip
	* plop

I've removed the 'additionalMessage' for these calls and kept retro compatibility to existing assertXXX calls

@tokazio tokazio marked this pull request as draft March 11, 2025 14:19
@tokazio tokazio changed the base branch from main to develop March 11, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants