Skip to content

To array operation #74

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 2 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ plugins {
}

repositories {
// TODO remove before first public release
mavenLocal()
mavenCentral()
}

Expand Down
5 changes: 3 additions & 2 deletions operations-stdlib/src/commonMain/kotlin/OperationsProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import string.Capitalize
import string.IsBlank
import string.Length
import string.Lowercase
import string.ToArray
import string.Uppercase

object OperationsProvider {
Expand All @@ -22,8 +23,8 @@ object OperationsProvider {

// array
"size" to Size,

"reverse" to Reverse
"reverse" to Reverse,
"toArray" to ToArray
)

val functionalOperations: Map<String, FunctionalLogicOperation> = mutableMapOf(
Expand Down
9 changes: 9 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/string/ToArray.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package string

import operation.StandardLogicOperation
import utils.asList

object ToArray : StandardLogicOperation {
override fun evaluateLogic(expression: Any?, data: Any?): Any? =
(expression.asList.firstOrNull() as? String)?.split("")?.drop(1)?.dropLast(1)
}
99 changes: 99 additions & 0 deletions operations-stdlib/src/commonTest/kotlin/string/ToArrayTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package string

import TestInput
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
import JsonLogicResult.Success
import JsonLogicResult.Failure

class ToArrayTest : FunSpec({
val operatorName = "toArray"
val logicEngine = JsonLogicEngine.Builder().addStandardOperation(operatorName, ToArray).build()

withData(
nameFn = { input -> "Should evaluated ${input.expression} with given ${input.data} result in ${input.result}" },
ts = listOf(
TestInput(
expression = mapOf(operatorName to "banana"),
result = Success(listOf("b", "a", "n", "a", "n", "a"))
),
TestInput(
expression = mapOf(operatorName to listOf("banana")),
result = Success(listOf("b", "a", "n", "a", "n", "a"))
),
TestInput(
expression = mapOf(operatorName to ""),
result = Success(emptyList<String>())
),
TestInput(
expression = mapOf(operatorName to listOf("")),
result = Success(emptyList<String>())
),
TestInput(
expression = mapOf(operatorName to " "),
result = Success(listOf(" "))
),
TestInput(
expression = mapOf(operatorName to listOf(" ")),
result = Success(listOf(" "))
),
TestInput(
expression = mapOf(operatorName to "123"),
result = Success(listOf("1", "2", "3"))
),
TestInput(
expression = mapOf(operatorName to 123),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to mapOf("var" to "key")),
data = mapOf("key" to "APPLE"),
result = Success(listOf("A", "P", "P", "L", "E"))
),
TestInput(
expression = mapOf(operatorName to mapOf("var" to "key")),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to 1.3),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf("0.00001")),
result = Success(listOf("0", ".", "0", "0", "0", "0", "1"))
),
TestInput(
expression = mapOf(operatorName to null),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(null)),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to true),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(false)),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to emptyList<Any>()),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(emptyList<Any>())),
result = Failure.NullResult
),
)
// given
) { testInput: TestInput ->
// when
val evaluationResult = logicEngine.evaluate(testInput.expression, testInput.data)

// then
evaluationResult shouldBe testInput.result
}
})