com.github.mvysny.kaributesting.v10.Renderers.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of karibu-testing-v10 Show documentation
Show all versions of karibu-testing-v10 Show documentation
Karibu Testing, support for browserless Vaadin testing in Kotlin
package com.github.mvysny.kaributesting.v10
import com.github.mvysny.kaributools.template
import com.github.mvysny.kaributools.textRecursively
import com.github.mvysny.kaributools.valueProvider
import com.vaadin.flow.component.Component
import com.vaadin.flow.data.renderer.*
import com.vaadin.flow.function.ValueProvider
import org.jsoup.Jsoup
import java.lang.reflect.Method
private val _BasicRenderer_getFormattedValue: Method by lazy(LazyThreadSafetyMode.PUBLICATION) {
val getFormattedValueM: Method = BasicRenderer::class.java.declaredMethods
.first { it.name == "getFormattedValue" }
getFormattedValueM.isAccessible = true
getFormattedValueM
}
/**
* Returns the output of this renderer for given [rowObject] formatted as close as possible
* to the client-side output.
*/
public fun Renderer._getPresentationValue(rowObject: T): String? = when {
this is TemplateRenderer -> {
val renderedTemplateHtml: String = this.renderTemplate(rowObject)
Jsoup.parse(renderedTemplateHtml).textRecursively
}
this is BasicRenderer -> {
val value: Any? = this.valueProvider.apply(rowObject)
_BasicRenderer_getFormattedValue.invoke(this, value) as String?
}
this is TextRenderer -> {
renderText(rowObject)
}
this is ComponentRenderer<*, T> -> {
val component: Component = createComponent(rowObject)
component.toPrettyString()
}
this::class.simpleName == "LitRenderer" -> {
// LitRenderer re-declares private members
val templateProperty = this::class.java.getDeclaredField("templateExpression")
templateProperty.isAccessible = true
val templateExpression = templateProperty.get(this) as String
val valueProvidersProperty = this::class.java.getDeclaredField("valueProviders")
valueProvidersProperty.isAccessible = true
val valueProviders = valueProvidersProperty.get(this) as Map>
val renderedLitTemplateHtml: String = renderLitTemplate(templateExpression, valueProviders, rowObject)
Jsoup.parse(renderedLitTemplateHtml).textRecursively
}
else -> null
}
/**
* Renders the template for given [item]
*/
public fun TemplateRenderer.renderTemplate(item: T): String {
var template: String = this.template
this.valueProviders.forEach { (k: String, v: ValueProvider) ->
if (template.contains("[[item.$k]]")) {
template = template.replace("[[item.$k]]", v.apply(item).toString())
}
}
return template
}
public fun renderLitTemplate(template: String, valueProviders: Map>, item: T): String {
var renderedTemplate = template;
valueProviders.forEach { (k: String, v: ValueProvider) ->
if (renderedTemplate.contains("\${item.$k}")) {
renderedTemplate = renderedTemplate.replace("\${item.$k}", v.apply(item).toString())
}
}
return renderedTemplate
}
/**
* Returns the text rendered for given [item].
*/
@Suppress("UNCHECKED_CAST")
public fun TextRenderer.renderText(item: T): String =
createComponent(item).element.text
© 2015 - 2025 Weber Informatics LLC | Privacy Policy