From e64b04d67585d1fff6ffe4fe0caffad9fb7829e5 Mon Sep 17 00:00:00 2001 From: uniumuniu Date: Wed, 11 Oct 2023 11:49:19 +0200 Subject: [PATCH] Fix formatting --- .../kotlin/com/featurevisor/sdk/Conditions.kt | 13 ++-- .../com/featurevisor/sdk/ConditionsTest.kt | 70 ++++++++++++------- .../featurevisor/sdk/DataFileReaderTest.kt | 12 ++-- ...ntFactory.kt => DatafileContentFactory.kt} | 9 +-- 4 files changed, 59 insertions(+), 45 deletions(-) rename src/test/kotlin/com/featurevisor/sdk/factory/{MockDatafileContentFactory.kt => DatafileContentFactory.kt} (91%) diff --git a/src/main/kotlin/com/featurevisor/sdk/Conditions.kt b/src/main/kotlin/com/featurevisor/sdk/Conditions.kt index 1e5bb75..b85e963 100644 --- a/src/main/kotlin/com/featurevisor/sdk/Conditions.kt +++ b/src/main/kotlin/com/featurevisor/sdk/Conditions.kt @@ -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 @@ -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 { @@ -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)) diff --git a/src/test/kotlin/com/featurevisor/sdk/ConditionsTest.kt b/src/test/kotlin/com/featurevisor/sdk/ConditionsTest.kt index 0d3732e..af5bf0a 100644 --- a/src/test/kotlin/com/featurevisor/sdk/ConditionsTest.kt +++ b/src/test/kotlin/com/featurevisor/sdk/ConditionsTest.kt @@ -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 @@ -14,7 +32,7 @@ class ConditionsTest { val condition = Condition.Plain( attributeKey = "browser_type", - operator = Operator.EQUALS, + operator = EQUALS, value = ConditionValue.StringValue("chrome") ) @@ -34,7 +52,7 @@ class ConditionsTest { val condition = Condition.Plain( attributeKey = "browser_type", - operator = Operator.NOT_EQUALS, + operator = NOT_EQUALS, value = ConditionValue.StringValue("chrome") ) @@ -54,7 +72,7 @@ class ConditionsTest { val condition = Condition.Plain( attributeKey = "age", - operator = Operator.GREATER_THAN, + operator = GREATER_THAN, value = ConditionValue.IntValue(18) ) @@ -74,7 +92,7 @@ class ConditionsTest { val condition = Condition.Plain( attributeKey = "age", - operator = Operator.LESS_THAN, + operator = LESS_THAN, value = ConditionValue.IntValue(18) ) @@ -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) ) @@ -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) ) @@ -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"), ) @@ -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"), ) @@ -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"), ) @@ -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"), ) @@ -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") ) @@ -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") ) @@ -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") ) @@ -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") ) @@ -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") ) @@ -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") ) @@ -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)), ) @@ -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)), ) @@ -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")), ) @@ -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")), ) @@ -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")), ) diff --git a/src/test/kotlin/com/featurevisor/sdk/DataFileReaderTest.kt b/src/test/kotlin/com/featurevisor/sdk/DataFileReaderTest.kt index eca7b63..dd3383e 100644 --- a/src/test/kotlin/com/featurevisor/sdk/DataFileReaderTest.kt +++ b/src/test/kotlin/com/featurevisor/sdk/DataFileReaderTest.kt @@ -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 @@ -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 diff --git a/src/test/kotlin/com/featurevisor/sdk/factory/MockDatafileContentFactory.kt b/src/test/kotlin/com/featurevisor/sdk/factory/DatafileContentFactory.kt similarity index 91% rename from src/test/kotlin/com/featurevisor/sdk/factory/MockDatafileContentFactory.kt rename to src/test/kotlin/com/featurevisor/sdk/factory/DatafileContentFactory.kt index 2e51a6e..7dc13a7 100644 --- a/src/test/kotlin/com/featurevisor/sdk/factory/MockDatafileContentFactory.kt +++ b/src/test/kotlin/com/featurevisor/sdk/factory/DatafileContentFactory.kt @@ -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", @@ -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"), ) ),