Skip to content

Lenght & size operators #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
86c9048
Failure result types split
Apr 28, 2022
cdb4831
Tests refactoring
Apr 28, 2022
7404119
All green tests and more refactoring
Apr 28, 2022
b8f157b
Merge branch 'main' into failure-result-subtypes
Apr 28, 2022
f283aea
Updated with master, resolved one TODO issue
Apr 28, 2022
7d28a30
More descriptive test names
Apr 28, 2022
62c3dbe
Field rename
Apr 28, 2022
09229d4
Less equals tests to avoid stack overflow
Apr 28, 2022
7d67f0e
Equals tests revert
Apr 28, 2022
093a424
Larger heap
Apr 28, 2022
cb543a7
extracted resultValue to save memory
Apr 29, 2022
cd1de74
Incremental build = false
Apr 29, 2022
b28b2e8
Incremental build = true
Apr 29, 2022
1cbcb21
Removed unused dependency
Apr 29, 2022
c4cc9a1
No code reuse in equals test
Apr 29, 2022
a4bb640
Unit tests code generation fix
May 3, 2022
edd6bb4
lenght and size operators
mkrogulskiallegro2 May 5, 2022
2232ac1
lenght and size operators implemented, added tests for both
mkrogulskiallegro2 May 10, 2022
5cdc937
added more tests for size, updates some implementation to handle edge…
mkrogulskiallegro2 May 11, 2022
d193bdb
Merge remote-tracking branch 'origin/main' into lenght_size_operators
mkrogulskiallegro2 May 11, 2022
0fbea8c
merge with main
mkrogulskiallegro2 May 11, 2022
3700445
merge with main
mkrogulskiallegro2 May 11, 2022
7fc0ec8
cr changes
mkrogulskiallegro2 May 11, 2022
c3695c6
cr changes
mkrogulskiallegro2 May 11, 2022
b730550
cr changes
mkrogulskiallegro2 May 11, 2022
6a5d1f2
cr changes
mkrogulskiallegro2 May 11, 2022
ef656ea
Merge remote-tracking branch 'origin/main' into lenght_size_operators
mkrogulskiallegro2 May 12, 2022
ba412e0
moved tests, updated with main, resolved conflict
mkrogulskiallegro2 May 12, 2022
a82c92f
detekt fix
mkrogulskiallegro2 May 13, 2022
4b14864
detekt fix
mkrogulskiallegro2 May 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package array

import LogicEvaluator
import operation.FunctionalLogicOperation
import unwrap.EvaluatingUnwrapper
import utils.asList
Expand Down
16 changes: 16 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/array/Size.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package array

import operation.StandardLogicOperation
import utils.asList
import utils.isSingleNullList

object Size : StandardLogicOperation {
override fun evaluateLogic(expression: Any?, data: Any?): Any? {
return when (val firstItem = expression.asList.firstOrNull()) {
firstItem.isSingleNullList() -> null
is Map<*, *> -> firstItem.size
is List<*> -> firstItem.size
else -> null
}
}
}
7 changes: 7 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/string/Length.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package string

import operation.StandardLogicOperation

object Length : StandardLogicOperation, StringUnwrapStrategy {
override fun evaluateLogic(expression: Any?, data: Any?): Any? = unwrapValueAsString(expression)?.length
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package string

import operation.StandardLogicOperation
import utils.asList


internal interface StringUnwrapStrategy {
fun unwrapValueAsString(wrappedValue: Any?): String? =
with(wrappedValue.asList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
import JsonLogicResult.Failure
import JsonLogicResult.Success
package array

import JsonLogicEngine
import TestInput
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe

class FindTest : FunSpec({
val logicEngine = JsonLogicEngine.Builder().addFunctionalOperation("find", Find).build()

withData(
nameFn = { input -> "Should evaluated ${input.expression} with given ${input.data} result in ${input.result}" },
ts = listOf(
TestInput(
expression = mapOf("find" to 1.3),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("find" to "banana"),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("find" to null),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("find" to true),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("find" to emptyList<Any>()),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("find" to listOf(null, mapOf("var" to ""))),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("find" to listOf(emptyList<String>(), mapOf("var" to ""))),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("find" to listOf(listOf(false, false), mapOf("var" to ""))),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("find" to listOf(listOf(true, false), mapOf("var" to ""))),
result = Success(true)
result = JsonLogicResult.Success(true)
),
TestInput(
expression = mapOf("find" to listOf(listOf(true, false))),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf(
Expand All @@ -62,23 +64,23 @@ class FindTest : FunSpec({
)
),
data = mapOf("integers" to listOf(1, 2, 3, 4, 5)),
result = Success(3)
result = JsonLogicResult.Success(3)
),
TestInput(
expression = mapOf("find" to listOf(listOf(-1, 1, 2, 3), mapOf(">" to listOf(mapOf("var" to ""), 0)))),
result = Success(1)
result = JsonLogicResult.Success(1)
),
TestInput(
expression = mapOf("find" to listOf(listOf(0, 0, 0, 0), mapOf("==" to listOf(mapOf("var" to ""), 0)))),
result = Success(0)
result = JsonLogicResult.Success(0)
),
TestInput(
expression = mapOf("find" to listOf(listOf(0, 0, 0, 0), mapOf("===" to listOf(mapOf("var" to ""), 0)))),
result = Success(0)
result = JsonLogicResult.Success(0)
),
TestInput(
expression = mapOf("find" to listOf(listOf(0, 0, 0, 0), mapOf("!=" to listOf(mapOf("var" to ""), 0)))),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf(
Expand All @@ -87,11 +89,11 @@ class FindTest : FunSpec({
mapOf(">" to listOf(mapOf("var" to ""), 0))
)
),
result = Success(3)
result = JsonLogicResult.Success(3)
),
TestInput(
expression = mapOf("find" to listOf(listOf(-1, 1, 2, 3), mapOf("<" to listOf(mapOf("var" to ""), 0)))),
result = Success(-1)
result = JsonLogicResult.Success(-1)
),
TestInput(
expression = mapOf(
Expand All @@ -100,7 +102,7 @@ class FindTest : FunSpec({
mapOf("==" to listOf(mapOf("var" to ""), "apple"))
)
),
result = Success("apple")
result = JsonLogicResult.Success("apple")
),
TestInput(
expression = mapOf(
Expand All @@ -110,7 +112,7 @@ class FindTest : FunSpec({
)
),
data = mapOf("fruits" to listOf("apple", "banana", "pineapple")),
result = Success("pineapple")
result = JsonLogicResult.Success("pineapple")
),
TestInput(
expression = mapOf(
Expand All @@ -119,7 +121,7 @@ class FindTest : FunSpec({
mapOf("<" to listOf(mapOf("var" to ""), 0))
)
),
result = Failure.NullResult
result = JsonLogicResult.Failure.NullResult
),
)
// given
Expand Down
105 changes: 105 additions & 0 deletions operations-stdlib/src/commonTest/kotlin/array/SizeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package array

import JsonLogicEngine
import TestInput
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe

class SizeTest : FunSpec({
val logicEngine = JsonLogicEngine.Builder().addStandardOperation("size", Size).build()

withData(
nameFn = { input -> "Should evaluated ${input.expression} with given ${input.data} result in ${input.result}" },
ts = listOf(
TestInput(
expression = mapOf("size" to listOf(listOf("test"))),
result = JsonLogicResult.Success(1)
),
TestInput(
expression = mapOf("size" to listOf(listOf("test", "test2"), "a")),
result = JsonLogicResult.Success(2)
),
TestInput(
expression = mapOf(
"size" to listOf(
mapOf("var" to "fruits"),
mapOf("var" to "fruits2")
)
),
data = mapOf("fruits" to listOf(listOf("apple"), "banana", "pineapple"), "fruits2" to "testFruit"),
result = JsonLogicResult.Success(3)
),
TestInput(
expression = mapOf(
"size" to mapOf("var" to "fruits")
),
data = mapOf("fruits" to listOf(listOf(listOf("apple"), "banana", "pineapple"))),
result = JsonLogicResult.Success(3)
),
TestInput(
expression = mapOf(
"size" to mapOf(
"if" to listOf(
mapOf("<" to listOf(mapOf("var" to "temp"), 0)),
"freezing",
mapOf("<" to listOf(mapOf("var" to "temp"), 100)),
listOf(listOf("liquid", "test", "b", "c")),
"gas"
)
)
),
data = mapOf("temp" to 55),
result = JsonLogicResult.Success(4)
),
TestInput(
expression = mapOf("size" to listOf<Any>(listOf<Any>())),
result = JsonLogicResult.Success(0)
),
TestInput(
expression = mapOf(
"size" to mapOf(
"if" to listOf(
mapOf("<" to listOf(mapOf("var" to "temp"), 100)),
listOf("liquid", "test", "b", "c"),
"gas"
)
)
),
data = mapOf("temp" to 55),
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf(
"size" to 1
),
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf(
"size" to 1521414312
),
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf(
"size" to null
),
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf(
"size" to "aa"
),
result = JsonLogicResult.Failure.NullResult
),
)
// given
) { testInput: TestInput ->
// when
val evaluationResult = logicEngine.evaluate(testInput.expression, testInput.data)

// then
evaluationResult shouldBe testInput.result
}
})
96 changes: 96 additions & 0 deletions operations-stdlib/src/commonTest/kotlin/string/LengthTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package string

import JsonLogicEngine
import TestInput
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe

class LengthTest : FunSpec({
val logicEngine = JsonLogicEngine.Builder()
.addStandardOperation("length", Length).build()
// given
withData(
nameFn = { input -> "Should evaluated ${input.expression} with given ${input.data} result in ${input.result}" },
ts = listOf(
TestInput(
expression = mapOf("length" to "test"),
result = JsonLogicResult.Success(4)
),
TestInput(
expression = mapOf(
"length" to mapOf("var" to "fruits")
),
data = mapOf("fruits" to listOf("apple")),
result = JsonLogicResult.Success(5)
),
TestInput(
expression = mapOf("length" to ""),
result = JsonLogicResult.Success(0)
),
TestInput(
expression = mapOf(
"length" to "this is very very very very very very very very very very very " +
" very very very very very very very very very very very very very very very very very" +
" very very very very very very very very very very very very very very very very very" +
" very very very very very very very very very very very very very very very long text"
),
result = JsonLogicResult.Success(336)
),
TestInput(
expression = mapOf(
"length" to mapOf(
"if" to listOf(
mapOf("<" to listOf(mapOf("var" to "temp"), 0)), "freezing"
)
)
),
data = mapOf("temp" to -20),
result = JsonLogicResult.Success(8)
),
TestInput(
expression = mapOf(
"length" to mapOf(
"if" to listOf(
mapOf("<" to listOf(mapOf("var" to "temp"), 0)), "freezing",
mapOf("<" to listOf(mapOf("var" to "temp"), 100)), "liquid",
"gas"
)
)
),
data = mapOf("temp" to 55),
result = JsonLogicResult.Success(6)
),
TestInput(
expression = mapOf("length" to 123455),
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("length" to -123455),
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("length" to 0),
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("length" to 10.0),
result = JsonLogicResult.Failure.NullResult
),

TestInput(
expression = mapOf("length" to listOf("test", "test2")),
result = JsonLogicResult.Failure.NullResult
),
TestInput(
expression = mapOf("length" to mapOf("test" to "test2")),
result = JsonLogicResult.Failure.MissingOperation
),
)
) { testInput: TestInput ->
// when
val evaluationResult = logicEngine.evaluate(testInput.expression, testInput.data)
// then
evaluationResult shouldBe testInput.result
}
})