All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.kotest.assertions.show.Show.kt Maven / Gradle / Ivy

@file:Suppress("UNCHECKED_CAST")

package io.kotest.assertions.show

import io.kotest.mpp.isDataClass
import kotlin.reflect.KClass

/**
 * The [Show] typeclass abstracts the ability to obtain a String representation of any object.
 * It is used as a replacement for Java's Object#toString so that custom representations of
 * the object can be printed.
 */
interface Show {

   /**
    * Returns a printable version of this object as an instance of [Printed]
    */
   fun show(a: A): Printed
}

data class Printed(val value: String)

fun String.printed() = Printed(this)

fun Any?.show(): Printed = if (this == null) DefaultShow.show(this) else showFor(this).show(this)

fun  showFor(t: T): Show = when (t) {
   is String -> StringShow as Show
   is Map<*, *> -> MapShow as Show
   is Long, t is Boolean, t is Int, t is Double, t is Float, t is Short, t is Byte -> DefaultShow
   is KClass<*> -> KClassShow as Show
   else -> when {
      // this won't work in JS or native, so they'll get the boring old toString version
      t::class.isDataClass ?: false -> dataClassShow()
      else -> DefaultShow
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy