Skip to content

Commit

Permalink
Merge pull request #6 from demndevel/main
Browse files Browse the repository at this point in the history
feat(units): add corner case test
  • Loading branch information
y9san9 authored Aug 24, 2024
2 parents 374319e + 44fa724 commit 06d1ecb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kotlin = "2.0.0"
bignum = "0.3.10"
maven-publish = "0.29.0"

calkt = "0.0.4"
calkt = "0.0.5"

[libraries]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class UnitsMathParseOperand(
val unitKey = parseUnitKey(context)

return UnitsExpression.Conversion(
value = MathExpression.Number(PreciseNumber.Companion.of(1)),
value = MathExpression.Number(PreciseNumber.of(1)),
key = unitKey
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public fun interface UnitsParseUnitKeyFunction {
context.token {
for (string in strings) {
if (context.startsWith(string)) {
val nextChar = context.string.getOrNull(index = context.position + string.length)
if (nextChar?.isLetter() == true) continue
context.drop(string.length)
return@UnitsParseUnitKeyFunction key
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.y9san9.calkt.units.parse

import me.y9san9.calkt.Expression
import me.y9san9.calkt.annotation.ExpressionSubclass
import me.y9san9.calkt.calculate.CalculateResult
import me.y9san9.calkt.parse.ParseContext
import me.y9san9.calkt.parse.ParseResult
import me.y9san9.calkt.parse.tryParse
import me.y9san9.calkt.units.UnitKey
import me.y9san9.calkt.units.annotation.UnitKeySubclass
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs

class UnitsParseUnitKeyFunctionTest {
@Test
fun testDefaultImplementationChecksForNextChar() {
val function = create()

val result = tryParse("prefixtest") { context ->
function(context) as Expression
}
assertIs<ParseResult.Success<TestKey>>(result)
assertEquals(TestKey, result.value)
}

private fun create(): UnitsParseUnitKeyFunction {
return UnitsParseUnitKeyFunction.ofStrings(TestKey, "prefix", "prefixtest")
}

@OptIn(UnitKeySubclass::class, ExpressionSubclass::class)
private data object TestKey : UnitKey, Expression
}

0 comments on commit 06d1ecb

Please sign in to comment.