Skip to content

Commit

Permalink
show bytes as hex on all supported platforms (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
evant authored Dec 5, 2023
1 parent 0c0438f commit 9470bb6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package assertk.assertions.support

internal actual fun displayPlatformSpecific(value: Any?): String {
return value.toString()
return when (value) {
is Byte -> formatHex(value)
else -> value.toString()
}
}

//TODO: replace with HexFormat when stable
private fun formatHex(byte: Byte) : String {
val topNibble = (byte.toInt() and 0xF0) ushr 4
val bottomNibble = byte.toInt() and 0x0F
return "0x${topNibble.toString(16).uppercase()}${bottomNibble.toString(16).uppercase()}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package test.assertk.assertions.support

import assertk.assertions.support.show
import kotlin.test.Test
import kotlin.test.assertEquals

class NativeSupportTest {

@Test fun show_byte_array() {
assertEquals("<[0x0A, 0x0F]>", show(byteArrayOf(10, 15)))
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package assertk.assertions.support

internal actual fun displayPlatformSpecific(value: Any?): String {
return value.toString()
return when (value) {
is Byte -> formatHex(value)
else -> value.toString()
}
}

//TODO: replace with HexFormat when stable
private fun formatHex(byte: Byte) : String {
val topNibble = (byte.toInt() and 0xF0) ushr 4
val bottomNibble = byte.toInt() and 0x0F
return "0x${topNibble.toString(16).uppercase()}${bottomNibble.toString(16).uppercase()}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.assertk.assertions.support

import assertk.assertions.support.show
import kotlin.test.Test
import kotlin.test.assertEquals

class WasmJsSupportTest {

@Test fun show_byte_array() {
assertEquals("<[0x0A, 0x0F]>", show(byteArrayOf(10, 15)))
}
}

0 comments on commit 9470bb6

Please sign in to comment.