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

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

package io.kotest.assertions.show

/**
 * A default implementation of [Show] that handles arrays, collections and primitives as well as nullable values.
 */
object DefaultShow : Show {
   override fun show(a: Any?): Printed = when (a) {
      null -> "".printed()
      is Boolean -> "$a".printed()
      is Float -> "${a}f".printed()
      is Long -> "${a}L".printed()
      is Char -> "'$a'".printed()
      is Int -> a.toString().printed()
      is Short -> a.toString().printed()
      is Byte -> a.toString().printed()
      is String -> "\"$a\"".printed()
      is Array<*> -> show(a.toList())
      is BooleanArray -> show(a.toList())
      is IntArray -> show(a.toList())
      is ShortArray -> show(a.toList())
      is FloatArray -> show(a.toList())
      is DoubleArray -> show(a.toList())
      is LongArray -> show(a.toList())
      is ByteArray -> show(a.toList())
      is CharArray -> show(a.toList())
      is List<*> -> a.getCollectionSnippet()
      is Iterable<*> -> show(a.toList())
      else -> a.toString().printed()
   }
}

private const val MaxCollectionSnippetSize = 20

/**
 * Returns the values in a Collection, up to a max size
 */
private fun Collection<*>.getCollectionSnippet(): Printed {
   val remainingItems = size - MaxCollectionSnippetSize

   val suffix = when {
      remainingItems <= 0 -> "]"
      else -> "] and $remainingItems more"
   }

   return joinToString(
      separator = ", ",
      prefix = "[",
      postfix = suffix,
      limit = MaxCollectionSnippetSize
   ) {
      recursiveRepr(this, it).value
   }.printed()
}

internal fun recursiveRepr(root: Any, node: Any?): Printed {
   return if (root == node) "(this ${root::class.simpleName})".printed() else node.show()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy