Skip to content
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

Test Runner is failing in specific scenarios issue fixes #51

Merged
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
85 changes: 83 additions & 2 deletions src/main/kotlin/com/featurevisor/sdk/Conditions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object Conditions {
EQUALS -> attributeValue.value == conditionValue.value
NOT_EQUALS -> attributeValue.value != conditionValue.value
CONTAINS -> attributeValue.value?.contains(conditionValue.value.orEmpty()) ?: false
NOT_CONTAINS -> attributeValue.value?.contains(conditionValue.value.orEmpty())?.not() ?: false
NOT_CONTAINS -> attributeValue.value?.contains(conditionValue.value.orEmpty())?.not() ?: false
STARTS_WITH -> attributeValue.value?.startsWith(conditionValue.value.orEmpty()) ?: false
ENDS_WITH -> attributeValue.value?.endsWith(conditionValue.value.orEmpty()) ?: false
SEMVER_EQUALS -> compareVersions(
Expand Down Expand Up @@ -119,6 +119,65 @@ object Conditions {
}
}

attributeValue is AttributeValue.IntValue && conditionValue is ConditionValue.ArrayValue -> {
when (operator) {
IN_ARRAY -> attributeValue.value.toString() in conditionValue.values
NOT_IN_ARRAY -> (attributeValue.value.toString() !in conditionValue.values)
else -> false
}
}

attributeValue is AttributeValue.DoubleValue && conditionValue is ConditionValue.StringValue -> {
when (operator) {
EQUALS -> attributeValue.value.toString() == conditionValue.value
NOT_EQUALS -> attributeValue.value.toString() != conditionValue.value

SEMVER_EQUALS -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty(),
) == 0

SEMVER_NOT_EQUALS -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty(),
) != 0

SEMVER_GREATER_THAN -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty()
) == 1

SEMVER_GREATER_THAN_OR_EQUALS -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty()
) >= 0

SEMVER_LESS_THAN -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty()
) == -1

SEMVER_LESS_THAN_OR_EQUALS -> compareVersions(
attributeValue.value.toString(),
conditionValue.value.orEmpty()
) <= 0

else -> false
}
}

attributeValue is AttributeValue.StringValue && conditionValue is ConditionValue.IntValue -> {
when (operator) {
EQUALS -> attributeValue.value == conditionValue.value.toString()
NOT_EQUALS -> attributeValue.value != conditionValue.value.toString()
CONTAINS -> attributeValue.value?.contains(conditionValue.value.toString()) ?: false
NOT_CONTAINS -> attributeValue.value?.contains(conditionValue.value.toString())?.not() ?: false
STARTS_WITH -> attributeValue.value?.startsWith(conditionValue.value.toString()) ?: false
ENDS_WITH -> attributeValue.value?.endsWith(conditionValue.value.toString()) ?: false
else -> false
}
}

attributeValue is AttributeValue.DateValue && conditionValue is ConditionValue.DateTimeValue -> {
when (operator) {
EQUALS -> attributeValue.value == conditionValue.value
Expand All @@ -143,9 +202,31 @@ object Conditions {

private fun compareVersions(actual: String, condition: String): Int {
return try {
SemVer.parse(actual).compareTo(SemVer.parse(condition))
SemVer.parse(normalizeSemver(actual)).compareTo(SemVer.parse(normalizeSemver(condition)))
} catch (e: Exception) {
0
}
}

private fun normalizeSemver(version: String): String {
val parts = version.split("-", "+")
val mainParts = parts[0].split(".").map { it.toInt().toString() }
var normalizedVersion = mainParts.joinToString(".")

if (version.contains("-")) {
val preRelease = parts[1].split(".").joinToString(".") {
if (it.all { char -> char.isDigit() }) it.toInt().toString()
else it
}
normalizedVersion += "-$preRelease"
}

if (version.contains("+")) {
val buildMetadata = version.split("+")[1]
normalizedVersion += "+$buildMetadata"
}

return normalizedVersion
}

}
Loading
Loading