-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to CONTAINS operations (#20)
This change adds a new operator `CONTAINS` that can be used to assert data by checking part of its content. When the `left` operand is a `string` it will compare if any part of the given string contains the given `right` operand. When the `left`operand is an `array` it will compare if any elements of the given array contains the given `right` operand.
- Loading branch information
Showing
10 changed files
with
239 additions
and
40 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
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
53 changes: 41 additions & 12 deletions
53
src/main/kotlin/com/rapatao/projects/ruleset/engine/types/Operator.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 |
---|---|---|
@@ -1,26 +1,55 @@ | ||
package com.rapatao.projects.ruleset.engine.types | ||
|
||
/** | ||
* Enum class for different operators. | ||
* | ||
* This enum class represents various operators that can be used for comparison or evaluation operations. | ||
* The available operators are: | ||
* - EQUALS: Represents the equals operator (==). | ||
* - NOT_EQUALS: Represents the not equals operator (!=). | ||
* - GREATER_THAN: Represents the greater than operator (>). | ||
* - GREATER_OR_EQUAL_THAN: Represents the greater than or equal to operator (>=). | ||
* - LESS_THAN: Represents the less than operator (<). | ||
* - LESS_OR_EQUAL_THAN: Represents the less than or equal to operator (<=). | ||
* - STARTS_WITH: Represents the startsWith operation. | ||
* - ENDS_WITH: Represents the endsWith operation. | ||
* This enum class defines a set of operators that can be utilized in different contexts such as string comparison, | ||
* numerical comparison, or array element verification. | ||
*/ | ||
enum class Operator { | ||
/** | ||
* Represents the equality operator (==), used to check if two values are equal. | ||
*/ | ||
EQUALS, | ||
|
||
/** | ||
* Represents the inequality operator (!=), used to check if two values are not equal. | ||
*/ | ||
NOT_EQUALS, | ||
|
||
/** | ||
* Represents the greater than operator (>), used to compare if one value is greater than another. | ||
*/ | ||
GREATER_THAN, | ||
|
||
/** | ||
* Represents the greater than or equal to operator (>=), used to compare if one value is greater than or equal to | ||
* another. | ||
*/ | ||
GREATER_OR_EQUAL_THAN, | ||
|
||
/** | ||
* Represents the less than operator (<), used to compare if one value is less than another. | ||
*/ | ||
LESS_THAN, | ||
|
||
/** | ||
* Represents the less than or equal to operator (<=), used to compare if one value is less than or equal to | ||
* another. | ||
*/ | ||
LESS_OR_EQUAL_THAN, | ||
|
||
/** | ||
* Represents the operation to check if a string starts with a specified sequence of characters. | ||
*/ | ||
STARTS_WITH, | ||
|
||
/** | ||
* Represents the operation to check if a string ends with a specified sequence of characters. | ||
*/ | ||
ENDS_WITH, | ||
|
||
/** | ||
* Represents the operation to check if a string contains a specified sequence of characters or if an array contains | ||
* a particular element. | ||
*/ | ||
CONTAINS, | ||
} |
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
22 changes: 22 additions & 0 deletions
22
...kotlin/com/rapatao/projects/ruleset/engine/types/builder/extensions/ContainsExtensions.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,22 @@ | ||
package com.rapatao.projects.ruleset.engine.types.builder.extensions | ||
|
||
import com.rapatao.projects.ruleset.engine.types.Expression | ||
import com.rapatao.projects.ruleset.engine.types.builder.ExpressionBuilder | ||
|
||
/** | ||
* Creates an expression to check if the current string contains the specified substring. | ||
* | ||
* @receiver The string in which to check for the presence of the substring. | ||
* @param right The substring to search for within the current string. | ||
* @return An [Expression] object representing the containment check, with the operator set to CONTAINS. | ||
*/ | ||
infix fun String.expContains(right: String): Expression = ExpressionBuilder.left(this).contains(right) | ||
|
||
/** | ||
* Creates an expression to check if the current list contains the specified element. | ||
* | ||
* @receiver The list in which to check for the presence of the element. | ||
* @param right The element to search for within the current list. | ||
* @return An [Expression] object representing the containment check, with the operator set to CONTAINS. | ||
*/ | ||
infix fun List<Any>.expContains(right: Any): Expression = ExpressionBuilder.left(this).contains(right) |
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
25 changes: 25 additions & 0 deletions
25
src/test/kotlin/com/rapatao/projects/ruleset/engine/cases/ContainsCases.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,25 @@ | ||
package com.rapatao.projects.ruleset.engine.cases | ||
|
||
import com.rapatao.projects.ruleset.engine.types.builder.extensions.expContains | ||
import org.junit.jupiter.params.provider.Arguments | ||
|
||
object ContainsCases { | ||
fun cases(): List<Arguments> = listOf( | ||
Arguments.of( | ||
"item.name" expContains "\"duct\"", | ||
true, | ||
), | ||
Arguments.of( | ||
"item.name" expContains "\"different value\"", | ||
false, | ||
), | ||
Arguments.of( | ||
"item.tags" expContains "\"test\"", | ||
true, | ||
), | ||
Arguments.of( | ||
"item.tags" expContains "\"different value\"", | ||
false, | ||
), | ||
) | ||
} |
Oops, something went wrong.