Skip to content

Commit

Permalink
feat: improved project coverage and minor code fixes (#36)
Browse files Browse the repository at this point in the history
* branch coverage is now at 90% for all project
* invalid `expression` was returning `true` instead of `failing`
(`onFailure` is used when evaluating invalid expressions)
* `Kotlin` evaluator was not considering unprotected string `"null"` as
`null` value, `"\"null\""` is still considered as `string` with value
`null`
  • Loading branch information
rapatao authored Jul 27, 2024
1 parent 8d19c9a commit 22a68f9
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 121 deletions.
77 changes: 76 additions & 1 deletion JSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,66 @@ To see more details, check its source: [here](src/test/kotlin/com/rapatao/projec
}
```

```json
{
"left" : "\"null\"",
"operator" : "equals",
"right" : "\"null\""
}
```

```json
{
"left" : 10.2,
"operator" : "equals",
"right" : 10.2
}
```

```json
{
"left" : 10,
"operator" : "equals",
"right" : 10
}
```

```json
{
"left" : "item.price",
"operator" : "not_equals",
"right" : 0
"right" : 10
}
```

```json
{
"left" : "item.price",
"operator" : "not_equals",
"right" : 0
}
```

```json
{
"left" : "\"null\"",
"operator" : "not_equals",
"right" : "\"null\""
}
```

```json
{
"left" : 10.2,
"operator" : "not_equals",
"right" : 10.2
}
```

```json
{
"left" : 10,
"operator" : "not_equals",
"right" : 10
}
```
Expand Down Expand Up @@ -1907,6 +1955,33 @@ To see more details, check its source: [here](src/test/kotlin/com/rapatao/projec
}
```

```json
{
"left" : "10",
"operator" : "contains",
"right" : "\"different value\"",
"onFailure" : "FALSE"
}
```

```json
{
"left" : "item.name",
"operator" : "contains",
"right" : "10",
"onFailure" : "FALSE"
}
```

```json
{
"left" : "null",
"operator" : "contains",
"right" : "10",
"onFailure" : "FALSE"
}
```

```json
{
"left" : "item.name",
Expand Down
36 changes: 36 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import kotlinx.kover.gradle.plugin.dsl.CoverageUnit

plugins {
id "java-library"
id "signing"
Expand All @@ -9,6 +11,40 @@ plugins {
id "com.github.ben-manes.versions" version "${BenManerVersionsVersion}"
}

kover {
reports {
total {
verify {
rule {
bound {
coverageUnits = CoverageUnit.BRANCH
it.minValue = 90
}
bound {
coverageUnits = CoverageUnit.INSTRUCTION
it.minValue = 90
}
bound {
coverageUnits = CoverageUnit.BRANCH
it.minValue = 90
}
}
}
it.html {
onCheck = true
}
}
}
}

dependencies {
kover(project(":core"))
kover(project(":graaljs-evaluator"))
kover(project(":kotlin-evaluator"))
kover(project(":rhino-evaluator"))
kover(project(":jackson"))
}

allprojects {
apply plugin: "java-library"
apply plugin: "signing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.rapatao.projects.ruleset.engine
import com.rapatao.projects.ruleset.engine.context.EvalContext
import com.rapatao.projects.ruleset.engine.types.Expression
import com.rapatao.projects.ruleset.engine.types.OnFailure
import com.rapatao.projects.ruleset.engine.types.errors.UnknownOperator
import com.rapatao.projects.ruleset.engine.types.operators.Operator

/**
Expand All @@ -24,6 +23,8 @@ abstract class Evaluator(
*/
fun evaluate(expression: Expression, inputData: Any): Boolean {
return usingFailureWrapper(expression.onFailure) {
require(expression.isValid(this)) { "expression $expression is not valid" }

call(inputData) {
expression.takeIf { v -> v.parseable() }?.processExpression(it) ?: true &&
expression.noneMatch?.processNoneMatch(it) ?: true &&
Expand Down Expand Up @@ -96,9 +97,10 @@ abstract class Evaluator(
return usingFailureWrapper(this.onFailure) {
requireNotNull(this.operator) { "expression operator must not be null" }

val operator = operator(this.operator) ?: throw UnknownOperator(this.operator)
val operator = operator(this.operator)
requireNotNull(operator) { "Unknown operator: $operator" }

context.process(this.left, operator, this.right)
context.process(left = this.left, operator = operator, right = this.right)
}
}

Expand Down

This file was deleted.

31 changes: 0 additions & 31 deletions graaljs-evaluator/build.gradle
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
import kotlinx.kover.gradle.plugin.dsl.MetricType

dependencies {
api project(":core")
api("org.graalvm.polyglot:polyglot:24.0.2")
api("org.graalvm.polyglot:js-community:24.0.2")

testImplementation project(":tests")
}

koverReport {
verify {
rule {
enabled = true
bound {
enabled = true
metric = MetricType.BRANCH
minValue = 80
}
bound {
enabled = true
metric = MetricType.LINE
minValue = 90
}
bound {
enabled = true
metric = MetricType.INSTRUCTION
minValue = 90
}
}
}

defaults {
html {
onCheck = true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.rapatao.projects.ruleset.engine.evaluator.graaljs

import com.rapatao.projects.ruleset.engine.BaseEvaluatorTest
import com.rapatao.projects.ruleset.engine.helper.ExposeEngineTestOperator

class GraalJSEvaluatorTest : BaseEvaluatorTest(GraalJSEvaluator())
class GraalJSEvaluatorTest : BaseEvaluatorTest(
GraalJSEvaluator(
operators = listOf(
ExposeEngineTestOperator(),
),
),
)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ jacocoVersion=0.8.7
javaVersion=17
junitVersion=5.10.2
kotlinVersion=1.9.22
koverVersion=0.7.6
koverVersion=0.8.3
publishPluginVersion=1.3.0
rhinoVersion=1.7.14
31 changes: 0 additions & 31 deletions kotlin-evaluator/build.gradle
Original file line number Diff line number Diff line change
@@ -1,36 +1,5 @@
import kotlinx.kover.gradle.plugin.dsl.MetricType

dependencies {
api project(":core")

testImplementation project(":tests")
}

koverReport {
verify {
rule {
enabled = true
bound {
enabled = true
metric = MetricType.BRANCH
minValue = 70
}
bound {
enabled = true
metric = MetricType.LINE
minValue = 90
}
bound {
enabled = true
metric = MetricType.INSTRUCTION
minValue = 90
}
}
}

defaults {
html {
onCheck = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class KotlinContext(

private fun Any?.asValue(): Any? {
val result = when {
this is String && this == "null" -> null
this is String && !this.trim().matches(Regex("^\".*\"$")) -> rawValue()
this is String && this.trim().matches(Regex("\".*\"")) -> this.unwrap()
else -> this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.rapatao.projects.ruleset.engine.evaluator.kotlin

import com.rapatao.projects.ruleset.engine.BaseEvaluatorTest
import com.rapatao.projects.ruleset.engine.helper.ExposeEngineTestOperator

class KotlinEvaluatorTest : BaseEvaluatorTest(KotlinEvaluator())
class KotlinEvaluatorTest : BaseEvaluatorTest(
KotlinEvaluator(
operators = listOf(
ExposeEngineTestOperator(),
),
),
)
31 changes: 0 additions & 31 deletions rhino-evaluator/build.gradle
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
import kotlinx.kover.gradle.plugin.dsl.MetricType

dependencies {
api project(":core")
api("org.mozilla:rhino:${rhinoVersion}")

testImplementation project(":tests")
}

koverReport {
verify {
rule {
enabled = true
bound {
enabled = true
metric = MetricType.BRANCH
minValue = 80
}
bound {
enabled = true
metric = MetricType.LINE
minValue = 90
}
bound {
enabled = true
metric = MetricType.INSTRUCTION
minValue = 90
}
}
}

defaults {
html {
onCheck = true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.rapatao.projects.ruleset.engine.evaluator.rhino

import com.rapatao.projects.ruleset.engine.BaseEvaluatorTest
import com.rapatao.projects.ruleset.engine.helper.ExposeEngineTestOperator

class RhinoEvaluatorTest : BaseEvaluatorTest(RhinoEvaluator())
class RhinoEvaluatorTest : BaseEvaluatorTest(
RhinoEvaluator(
operators = listOf(
ExposeEngineTestOperator(),
),
),
)
Loading

0 comments on commit 22a68f9

Please sign in to comment.