Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
uniumuniu committed Oct 11, 2023
1 parent 5e59ff0 commit e64b04d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 45 deletions.
13 changes: 4 additions & 9 deletions src/main/kotlin/com/featurevisor/sdk/Conditions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ object Conditions {
EQUALS -> attributeValue.value == conditionValue.value
NOT_EQUALS -> attributeValue.value != conditionValue.value
CONTAINS -> attributeValue.value.contains(conditionValue.value)
NOT_CONTAINS ->
attributeValue.value.contains(conditionValue.value).not()

STARTS_WITH ->
attributeValue.value.startsWith(conditionValue.value)

NOT_CONTAINS -> attributeValue.value.contains(conditionValue.value).not()
STARTS_WITH -> attributeValue.value.startsWith(conditionValue.value)
ENDS_WITH -> attributeValue.value.endsWith(conditionValue.value)
SEMVER_EQUALS -> compareVersions(attributeValue.value, conditionValue.value) == 0
SEMVER_NOT_EQUALS -> compareVersions(attributeValue.value, conditionValue.value) != 0
Expand Down Expand Up @@ -111,8 +107,8 @@ object Conditions {
}
}

fun allConditionsAreMatched(condition: Condition, context: Context): Boolean {
return when (condition) {
fun allConditionsAreMatched(condition: Condition, context: Context): Boolean =
when (condition) {
is Plain -> conditionIsMatched(condition, context)

is And -> condition.and.all {
Expand All @@ -127,7 +123,6 @@ object Conditions {
allConditionsAreMatched(it, context)
}.not()
}
}

private fun compareVersions(actual: String, condition: String): Int =
SemVer.parse(actual).compareTo(SemVer.parse(condition))
Expand Down
70 changes: 44 additions & 26 deletions src/test/kotlin/com/featurevisor/sdk/ConditionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ package com.featurevisor.sdk
import com.featurevisor.types.AttributeValue
import com.featurevisor.types.Condition
import com.featurevisor.types.ConditionValue
import com.featurevisor.types.Operator
import com.featurevisor.types.Operator.AFTER
import com.featurevisor.types.Operator.BEFORE
import com.featurevisor.types.Operator.CONTAINS
import com.featurevisor.types.Operator.ENDS_WITH
import com.featurevisor.types.Operator.EQUALS
import com.featurevisor.types.Operator.GREATER_THAN
import com.featurevisor.types.Operator.GREATER_THAN_OR_EQUAL
import com.featurevisor.types.Operator.IN_ARRAY
import com.featurevisor.types.Operator.LESS_THAN
import com.featurevisor.types.Operator.LESS_THAN_OR_EQUAL
import com.featurevisor.types.Operator.NOT_EQUALS
import com.featurevisor.types.Operator.NOT_IN_ARRAY
import com.featurevisor.types.Operator.SEMVER_EQUALS
import com.featurevisor.types.Operator.SEMVER_GREATER_THAN
import com.featurevisor.types.Operator.SEMVER_GREATER_THAN_OR_EQUAL
import com.featurevisor.types.Operator.SEMVER_LESS_THAN
import com.featurevisor.types.Operator.SEMVER_LESS_THAN_OR_EQUAL
import com.featurevisor.types.Operator.SEMVER_NOT_EQUALS
import com.featurevisor.types.Operator.STARTS_WITH
import io.kotest.matchers.shouldBe
import java.time.LocalDate
import kotlin.test.Test
Expand All @@ -14,7 +32,7 @@ class ConditionsTest {
val condition =
Condition.Plain(
attributeKey = "browser_type",
operator = Operator.EQUALS,
operator = EQUALS,
value = ConditionValue.StringValue("chrome")
)

Expand All @@ -34,7 +52,7 @@ class ConditionsTest {
val condition =
Condition.Plain(
attributeKey = "browser_type",
operator = Operator.NOT_EQUALS,
operator = NOT_EQUALS,
value = ConditionValue.StringValue("chrome")
)

Expand All @@ -54,7 +72,7 @@ class ConditionsTest {
val condition =
Condition.Plain(
attributeKey = "age",
operator = Operator.GREATER_THAN,
operator = GREATER_THAN,
value = ConditionValue.IntValue(18)
)

Expand All @@ -74,7 +92,7 @@ class ConditionsTest {
val condition =
Condition.Plain(
attributeKey = "age",
operator = Operator.LESS_THAN,
operator = LESS_THAN,
value = ConditionValue.IntValue(18)
)

Expand All @@ -94,7 +112,7 @@ class ConditionsTest {
val condition =
Condition.Plain(
attributeKey = "age",
operator = Operator.GREATER_THAN_OR_EQUAL,
operator = GREATER_THAN_OR_EQUAL,
value = ConditionValue.IntValue(18)
)

Expand All @@ -119,7 +137,7 @@ class ConditionsTest {
val condition =
Condition.Plain(
attributeKey = "age",
operator = Operator.LESS_THAN_OR_EQUAL,
operator = LESS_THAN_OR_EQUAL,
value = ConditionValue.IntValue(18)
)

Expand All @@ -143,7 +161,7 @@ class ConditionsTest {
fun `CONTAINS operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "browser_type",
operator = Operator.CONTAINS,
operator = CONTAINS,
value = ConditionValue.StringValue("hro"),
)

Expand All @@ -162,7 +180,7 @@ class ConditionsTest {
fun `NOT_CONTAINS operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "browser_type",
operator = Operator.CONTAINS,
operator = CONTAINS,
value = ConditionValue.StringValue("hro"),
)

Expand All @@ -181,7 +199,7 @@ class ConditionsTest {
fun `STARTS_WITH operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "browser_type",
operator = Operator.STARTS_WITH,
operator = STARTS_WITH,
value = ConditionValue.StringValue("chr"),
)

Expand All @@ -200,7 +218,7 @@ class ConditionsTest {
fun `ENDS_WITH operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "browser_type",
operator = Operator.ENDS_WITH,
operator = ENDS_WITH,
value = ConditionValue.StringValue("ome"),
)

Expand All @@ -219,7 +237,7 @@ class ConditionsTest {
fun `SEMVER_EQUALS operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "version",
operator = Operator.SEMVER_EQUALS,
operator = SEMVER_EQUALS,
value = ConditionValue.StringValue("1.2.3")
)

Expand All @@ -238,7 +256,7 @@ class ConditionsTest {
fun `SEMVER_NOT_EQUALS operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "version",
operator = Operator.SEMVER_NOT_EQUALS,
operator = SEMVER_NOT_EQUALS,
value = ConditionValue.StringValue("1.2.3")
)

Expand All @@ -257,7 +275,7 @@ class ConditionsTest {
fun `SEMVER_GREATER_THAN operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "version",
operator = Operator.SEMVER_GREATER_THAN,
operator = SEMVER_GREATER_THAN,
value = ConditionValue.StringValue("1.2.3")
)

Expand All @@ -281,7 +299,7 @@ class ConditionsTest {
fun `SEMVER_GREATER_THAN_OR_EQUAL operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "version",
operator = Operator.SEMVER_GREATER_THAN_OR_EQUAL,
operator = SEMVER_GREATER_THAN_OR_EQUAL,
value = ConditionValue.StringValue("1.2.3")
)

Expand All @@ -305,7 +323,7 @@ class ConditionsTest {
fun `SEMVER_LESS_THAN operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "version",
operator = Operator.SEMVER_LESS_THAN,
operator = SEMVER_LESS_THAN,
value = ConditionValue.StringValue("1.2.3")
)

Expand All @@ -329,7 +347,7 @@ class ConditionsTest {
fun `SEMVER_LESS_THAN_OR_EQUAL operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "version",
operator = Operator.SEMVER_LESS_THAN_OR_EQUAL,
operator = SEMVER_LESS_THAN_OR_EQUAL,
value = ConditionValue.StringValue("1.2.3")
)

Expand All @@ -353,7 +371,7 @@ class ConditionsTest {
fun `BEFORE operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "date",
operator = Operator.BEFORE,
operator = BEFORE,
value = ConditionValue.DateTimeValue(LocalDate.of(2023, 10, 5)),
)

Expand All @@ -377,7 +395,7 @@ class ConditionsTest {
fun `AFTER operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "date",
operator = Operator.AFTER,
operator = AFTER,
value = ConditionValue.DateTimeValue(LocalDate.of(2023, 10, 5)),
)

Expand All @@ -401,7 +419,7 @@ class ConditionsTest {
fun `IN_ARRAY operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "letter",
operator = Operator.IN_ARRAY,
operator = IN_ARRAY,
value = ConditionValue.ArrayValue(listOf("a", "b", "c")),
)

Expand All @@ -420,7 +438,7 @@ class ConditionsTest {
fun `NOT_IN_ARRAY operator works for strings`() {
val condition = Condition.Plain(
attributeKey = "letter",
operator = Operator.NOT_IN_ARRAY,
operator = NOT_IN_ARRAY,
value = ConditionValue.ArrayValue(listOf("a", "b", "c")),
)

Expand All @@ -439,32 +457,32 @@ class ConditionsTest {
fun `multiple conditions work`() {
val startsWithCondition = Condition.Plain(
attributeKey = "browser_type",
operator = Operator.STARTS_WITH,
operator = STARTS_WITH,
value = ConditionValue.StringValue("chr"),
)

val semVerCondition = Condition.Plain(
attributeKey = "version",
operator = Operator.SEMVER_GREATER_THAN,
operator = SEMVER_GREATER_THAN,
value = ConditionValue.StringValue("1.2.3")
)

val ageCondition =
Condition.Plain(
attributeKey = "age",
operator = Operator.GREATER_THAN,
operator = GREATER_THAN,
value = ConditionValue.IntValue(18)
)

val beforeCondition = Condition.Plain(
attributeKey = "date",
operator = Operator.BEFORE,
operator = BEFORE,
value = ConditionValue.DateTimeValue(LocalDate.of(2023, 10, 5)),
)

val inArrayCondition = Condition.Plain(
attributeKey = "letter",
operator = Operator.IN_ARRAY,
operator = IN_ARRAY,
value = ConditionValue.ArrayValue(listOf("a", "b", "c")),
)

Expand Down
12 changes: 6 additions & 6 deletions src/test/kotlin/com/featurevisor/sdk/DataFileReaderTest.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.featurevisor.sdk

import com.featurevisor.sdk.factory.MockDatafileContentFactory
import com.featurevisor.sdk.factory.DatafileContentFactory
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class DataFileReaderTest {

private val systemUnderTest = DataFileReader(
datafileJson = MockDatafileContentFactory.get()
datafileJson = DatafileContentFactory.get()
)

@Test
Expand All @@ -22,22 +22,22 @@ class DataFileReaderTest {

@Test
fun `getAllAttributes() returns correct list`() {
systemUnderTest.getAllAttributes() shouldBe MockDatafileContentFactory.getAttributes()
systemUnderTest.getAllAttributes() shouldBe DatafileContentFactory.getAttributes()
}

@Test
fun `getAttribute() returns correct value`() {
systemUnderTest.getAttribute("browser_type") shouldBe MockDatafileContentFactory.getAttributes().first()
systemUnderTest.getAttribute("browser_type") shouldBe DatafileContentFactory.getAttributes().first()
}

@Test
fun `getSegment() returns correct value`() {
systemUnderTest.getSegment("netherlands") shouldBe MockDatafileContentFactory.getSegments().first()
systemUnderTest.getSegment("netherlands") shouldBe DatafileContentFactory.getSegments().first()
}

@Test
fun `getFeature() returns correct value`() {
systemUnderTest.getFeature("landing_page") shouldBe MockDatafileContentFactory.getFeatures().first()
systemUnderTest.getFeature("landing_page") shouldBe DatafileContentFactory.getFeatures().first()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import com.featurevisor.types.Condition
import com.featurevisor.types.ConditionValue
import com.featurevisor.types.DatafileContent
import com.featurevisor.types.Feature
import com.featurevisor.types.Operator
import com.featurevisor.types.Operator.EQUALS
import com.featurevisor.types.Operator.NOT_EQUALS
import com.featurevisor.types.Segment

object MockDatafileContentFactory {
object DatafileContentFactory {

fun get() = DatafileContent(
schemaVersion = "schemaVersion",
Expand Down Expand Up @@ -45,12 +46,12 @@ object MockDatafileContentFactory {
listOf(
Condition.Plain(
attributeKey = "browser_type",
operator = Operator.EQUALS,
operator = EQUALS,
value = ConditionValue.StringValue("chrome"),
),
Condition.Plain(
attributeKey = "device",
operator = Operator.NOT_EQUALS,
operator = NOT_EQUALS,
value = ConditionValue.StringValue("tablet"),
)
),
Expand Down

0 comments on commit e64b04d

Please sign in to comment.