Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

LiteralBoolean and LiteralFormat #36

Closed
SDelgado-21 opened this issue Mar 14, 2024 · 0 comments
Closed

LiteralBoolean and LiteralFormat #36

SDelgado-21 opened this issue Mar 14, 2024 · 0 comments
Assignees

Comments

@SDelgado-21
Copy link
Collaborator

Epic: inline Snapshot #8

Implementations: https://github.com/diffplug/selfie-python-wip/blob/main/jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/guts/Literals.kt

abstract class LiteralFormat<T : Any> {
internal abstract fun encode(
value: T,
language: Language,
encodingPolicy: EscapeLeadingWhitespace
): String
internal abstract fun parse(str: String, language: Language): T
}

internal object LiteralBoolean : LiteralFormat<Boolean>() {
override fun encode(
value: Boolean,
language: Language,
encodingPolicy: EscapeLeadingWhitespace
): String {
return value.toString()
}
override fun parse(str: String, language: Language): Boolean {
return str.toBooleanStrict()
}
}

Tests:https://github.com/diffplug/selfie-python-wip/blob/main/jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/guts/LiteralBooleanTest.kt

package com.diffplug.selfie.guts
import io.kotest.matchers.shouldBe
import kotlin.test.Test
class LiteralBooleanTest {
@Test
fun encode() {
encode(true, "true")
encode(false, "false")
}
private fun encode(value: Boolean, expected: String) {
val actual = LiteralBoolean.encode(value, Language.JAVA, EscapeLeadingWhitespace.ALWAYS)
actual shouldBe expected
}
@Test
fun decode() {
decode("true", true)
decode("false", false)
}
private fun decode(value: String, expected: Boolean) {
val actual = LiteralBoolean.parse(value, Language.JAVA)
actual shouldBe expected
}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant