-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from demndevel/main
feat: add ability to parse units without number
- Loading branch information
Showing
7 changed files
with
192 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: Library Release Deploy | ||
|
||
on: | ||
push: | ||
branches-ignore: [ "main" ] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
|
||
test-jvm: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Gradle Cache Setup | ||
uses: gradle/gradle-build-action@v2.4.2 | ||
- name: Gradle Check | ||
run: ./gradlew jvmTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,7 @@ kotlin { | |
iosX64() | ||
iosSimulatorArm64() | ||
} | ||
|
||
dependencies { | ||
commonTestImplementation(kotlin("test")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
units/src/commonTest/kotlin/me/y9san9/calkt/units/parse/UnitsMathParseOperand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package me.y9san9.calkt.units.parse | ||
|
||
import me.y9san9.calkt.math.MathExpression | ||
import me.y9san9.calkt.math.parse.DefaultMathParseOperand | ||
import me.y9san9.calkt.number.PreciseNumber | ||
import me.y9san9.calkt.parse.base.consume | ||
import me.y9san9.calkt.parse.getOrThrow | ||
import me.y9san9.calkt.parse.tryParse | ||
import me.y9san9.calkt.units.UnitKey | ||
import me.y9san9.calkt.units.UnitsExpression | ||
import me.y9san9.calkt.units.annotation.UnitKeySubclass | ||
import me.y9san9.calkt.units.parse.cause.ExpectedUnitsCause | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class UnitsMathParseOperandTest { | ||
|
||
@Test | ||
fun testCanParseNumber() { | ||
val operand = create() | ||
|
||
val result = tryParse("1") { context -> | ||
operand(context) | ||
}.getOrThrow() | ||
|
||
val expected = MathExpression.Number(number = PreciseNumber.of(int = 1)) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun testCanParseGroup() { | ||
val operand = create() | ||
|
||
val result = tryParse("(1 unit)") { context -> | ||
operand(context) | ||
}.getOrThrow() | ||
|
||
val expected = UnitsExpression.Conversion( | ||
value = MathExpression.Number( | ||
number = PreciseNumber.of(int = 1) | ||
), | ||
key = TestUnitKey | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun testCanUnitBefore() { | ||
val operand = create() | ||
|
||
val result = tryParse("unit 1") { context -> | ||
operand(context) | ||
}.getOrThrow() | ||
|
||
val expected = UnitsExpression.Conversion( | ||
value = MathExpression.Number( | ||
number = PreciseNumber.of(int = 1) | ||
), | ||
key = TestUnitKey | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun testCanParseUnitAfter() { | ||
val operand = create() | ||
|
||
val result = tryParse("1 unit") { context -> | ||
operand(context) | ||
}.getOrThrow() | ||
|
||
val expected = UnitsExpression.Conversion( | ||
value = MathExpression.Number( | ||
number = PreciseNumber.of(int = 1) | ||
), | ||
key = TestUnitKey | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun testCanParseUnitOnly() { | ||
val operand = create() | ||
|
||
val result = tryParse("unit") { context -> | ||
operand(context) | ||
}.getOrThrow() | ||
|
||
val expected = UnitsExpression.Conversion( | ||
value = MathExpression.Number( | ||
number = PreciseNumber.of(int = 1) | ||
), | ||
key = TestUnitKey | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
private fun create(): UnitsMathParseOperand { | ||
val parseUnitKey = UnitsParseUnitKeyFunction { context -> | ||
context.consume("unit") { ExpectedUnitsCause.of("unit") } | ||
TestUnitKey | ||
|
||
} | ||
val parseOperand = DefaultMathParseOperand | ||
return UnitsMathParseOperand(parseUnitKey, parseOperand) | ||
} | ||
|
||
@OptIn(UnitKeySubclass::class) | ||
private object TestUnitKey : UnitKey | ||
} |