All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rapatao.projects.ruleset.engine.helper.Helper.kt Maven / Gradle / Ivy

Go to download

Simple yet powerful rules engine that offers the flexibility of using the built-in engine and creating a custom one.

The newest version!
package com.rapatao.projects.ruleset.engine.helper

import com.rapatao.projects.ruleset.engine.Evaluator
import com.rapatao.projects.ruleset.engine.cases.TestData
import com.rapatao.projects.ruleset.engine.types.Expression
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo

object Helper {

    fun doEvaluationTest(evaluator: Evaluator, ruleSet: Expression, expected: Boolean) {
        assertThat(
            evaluator.evaluate(expression = ruleSet, inputData = TestData.inputData),
            equalTo(expected)
        )
    }

    fun compareMatcher(source: Expression, target: Expression) {
        target.takeIf { it.parseable() }?.run {
            assertThat(target.left, equalTo(source.left))
            assertThat(target.operator, equalTo(source.operator))
            assertThat(target.right, equalTo(source.right))
            assertThat(target.onFailure, equalTo(source.onFailure))
        }

        compareMatcherList(source.allMatch, target.allMatch)
        compareMatcherList(source.anyMatch, target.anyMatch)
        compareMatcherList(source.noneMatch, target.noneMatch)
    }

    private fun compareMatcherList(source: List?, target: List?) {
        assertThat(source?.size, equalTo(target?.size))

        source?.forEachIndexed { index, matcher ->
            compareMatcher(matcher, target!![index])
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy