Skip to content

Commit

Permalink
fix: operators now check that the next symbol is other than letter
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Aug 24, 2024
1 parent 53d877c commit 44fa724
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 183 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

This file was deleted.

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 44fa724

Please sign in to comment.