From 4e5fa4d84a3320540a2db7fa1bec7b44bbccd7de Mon Sep 17 00:00:00 2001 From: Marcin Kuszczak <1508798+aartiPl@users.noreply.github.com> Date: Wed, 19 Jul 2023 21:47:04 +0200 Subject: [PATCH] Added tests --- build.gradle.kts | 4 +- .../net/igsoft/typeutils/pipeline/Context.kt | 1 - .../ImmutableTypedPropertiesBuilder.kt | 14 +++++ .../typeutils/property/TypedProperties.kt | 19 ++++-- .../globalcontext/GlobalContextTest.kt | 20 +++++++ .../typeutils/marker/TypedMarkerTest.kt | 30 ++++++++++ .../igsoft/typeutils/pipeline/PipelineTest.kt | 49 +++++++++++++++ .../typeutils/property/TypedPropertiesTest.kt | 59 +++++++++++++++++++ 8 files changed, 188 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/net/igsoft/typeutils/property/ImmutableTypedPropertiesBuilder.kt create mode 100644 src/test/kotlin/net/igsoft/typeutils/globalcontext/GlobalContextTest.kt create mode 100644 src/test/kotlin/net/igsoft/typeutils/marker/TypedMarkerTest.kt create mode 100644 src/test/kotlin/net/igsoft/typeutils/pipeline/PipelineTest.kt create mode 100644 src/test/kotlin/net/igsoft/typeutils/property/TypedPropertiesTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index fef0ac9..12f56fb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "net.igsoft" -version = "0.5.0-SNAPSHOT" +version = "0.5.0" repositories { mavenCentral() @@ -97,6 +97,6 @@ dependencies { testImplementation("org.junit.platform:junit-platform-suite-commons:1.9.0") testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.0") testImplementation("org.junit.jupiter:junit-jupiter-params:5.9.0") - testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.25") + testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.26.1") testImplementation("io.mockk:mockk:1.13.2") } diff --git a/src/main/kotlin/net/igsoft/typeutils/pipeline/Context.kt b/src/main/kotlin/net/igsoft/typeutils/pipeline/Context.kt index d17f37b..a56d6a4 100644 --- a/src/main/kotlin/net/igsoft/typeutils/pipeline/Context.kt +++ b/src/main/kotlin/net/igsoft/typeutils/pipeline/Context.kt @@ -9,5 +9,4 @@ class Context : MutableTypedProperties by TypedProperties(mutableMapOf()) { fun invokeNextProcessor() { pipeline?.process(this) } - } diff --git a/src/main/kotlin/net/igsoft/typeutils/property/ImmutableTypedPropertiesBuilder.kt b/src/main/kotlin/net/igsoft/typeutils/property/ImmutableTypedPropertiesBuilder.kt new file mode 100644 index 0000000..4f31850 --- /dev/null +++ b/src/main/kotlin/net/igsoft/typeutils/property/ImmutableTypedPropertiesBuilder.kt @@ -0,0 +1,14 @@ +package net.igsoft.typeutils.property + +import net.igsoft.typeutils.marker.TypedMarker + +@Suppress("unused") +class ImmutableTypedPropertiesBuilder { + private val properties = TypedProperties(mutableMapOf()) + + fun putProperty(key: TypedMarker, value: T) = apply { + properties[key] = value + } + + fun build(): ImmutableTypedProperties = properties +} diff --git a/src/main/kotlin/net/igsoft/typeutils/property/TypedProperties.kt b/src/main/kotlin/net/igsoft/typeutils/property/TypedProperties.kt index c2ee463..6d62c51 100644 --- a/src/main/kotlin/net/igsoft/typeutils/property/TypedProperties.kt +++ b/src/main/kotlin/net/igsoft/typeutils/property/TypedProperties.kt @@ -3,10 +3,10 @@ package net.igsoft.typeutils.property import net.igsoft.typeutils.marker.Marker import net.igsoft.typeutils.marker.TypedMarker -@Suppress("UNCHECKED_CAST") +@Suppress("UNCHECKED_CAST", "unused") class TypedProperties(private val map: MutableMap) : MutableTypedProperties { - override fun set(marker: TypedMarker, value: T?) { + override operator fun set(marker: TypedMarker, value: T?) { map[marker] = value } @@ -60,13 +60,13 @@ class TypedProperties(private val map: MutableMap) : MutableTypedP override val size: Int get() = map.size - override val entries: Set> + override val entries: MutableSet> get() = map.entries - override val keys: Set + override val keys: MutableSet get() = map.keys - override val values: Collection + override val values: MutableCollection get() = map.values override fun isEmpty(): Boolean = map.isEmpty() @@ -77,6 +77,15 @@ class TypedProperties(private val map: MutableMap) : MutableTypedP override fun iterator(): Iterator> = map.iterator() + fun clear() { + map.clear() + } + + fun put(key: TypedMarker, value: T?): Any? = map.put(key, value) + + fun putAll(from: Map) = map.putAll(from) + + fun remove(key: Marker): Any? = map.remove(key) private fun isPropertyKeyMissing(any: Any?, marker: Marker) = any == null && !map.containsKey(marker) diff --git a/src/test/kotlin/net/igsoft/typeutils/globalcontext/GlobalContextTest.kt b/src/test/kotlin/net/igsoft/typeutils/globalcontext/GlobalContextTest.kt new file mode 100644 index 0000000..fe1f2e6 --- /dev/null +++ b/src/test/kotlin/net/igsoft/typeutils/globalcontext/GlobalContextTest.kt @@ -0,0 +1,20 @@ +package net.igsoft.typeutils.globalcontext + +import assertk.assertThat +import assertk.assertions.isEqualTo +import net.igsoft.typeutils.marker.TypedMarker +import org.junit.jupiter.api.Test + +data class Person(val firstName: String, val lastName: String, val age: Int) + +class GlobalContextTest { + private val person by TypedMarker.create() + + @Test + fun `Assert that we can save and read objects on GlobalContext`() { + val personEntity = Person("Marcin", "Iksiński", 28) + GlobalContext.register(person, personEntity) + + assertThat(GlobalContext[person]).isEqualTo(personEntity) + } +} diff --git a/src/test/kotlin/net/igsoft/typeutils/marker/TypedMarkerTest.kt b/src/test/kotlin/net/igsoft/typeutils/marker/TypedMarkerTest.kt new file mode 100644 index 0000000..f59d454 --- /dev/null +++ b/src/test/kotlin/net/igsoft/typeutils/marker/TypedMarkerTest.kt @@ -0,0 +1,30 @@ +package net.igsoft.typeutils.marker + +import assertk.assertThat +import assertk.assertions.isEqualTo +import assertk.assertions.prop +import org.junit.jupiter.api.Test + +class TypedMarkerTest { + @Test + fun `Assert that TypedMarker can be created with property syntax`() { + val someProperty by TypedMarker.create() + assertThat(someProperty).apply { + prop(TypedMarker::clazz).isEqualTo(String::class.java) + prop(TypedMarker::id).isEqualTo("someProperty") + } + } + + @Test + fun `Assert that TypedMarker can be created manually`() { + assertThat(TypedMarker.create(String::class.java, "s1")).apply { + prop(TypedMarker::clazz).isEqualTo(String::class.java) + prop(TypedMarker::id).isEqualTo("s1") + } + + assertThat(TypedMarker.create("s2")).apply { + prop(TypedMarker::clazz).isEqualTo(Integer::class.java) + prop(TypedMarker::id).isEqualTo("s2") + } + } +} diff --git a/src/test/kotlin/net/igsoft/typeutils/pipeline/PipelineTest.kt b/src/test/kotlin/net/igsoft/typeutils/pipeline/PipelineTest.kt new file mode 100644 index 0000000..a39f9df --- /dev/null +++ b/src/test/kotlin/net/igsoft/typeutils/pipeline/PipelineTest.kt @@ -0,0 +1,49 @@ +package net.igsoft.typeutils.pipeline + +import assertk.assertThat +import assertk.assertions.isEqualTo +import net.igsoft.typeutils.marker.TypedMarker +import org.junit.jupiter.api.Test + +class PipelineTest { + private val firstname by TypedMarker.create() + private val lastname by TypedMarker.create() + private val age by TypedMarker.create() + private val shoeSize by TypedMarker.create() + private val helloMessage by TypedMarker.create() + + private val p1: Processor = object: Processor { + override fun process(context: Context) { + context[helloMessage] = context[firstname] + " " + context[lastname] + context.invokeNextProcessor() + } + } + + private val p2 = object: Processor { + override fun process(context: Context) { + val shoeSize = context[shoeSize] + val details = "age: " + context[age] + if (shoeSize != null) ", shoeSize: $shoeSize" else "" + context[helloMessage] = context[helloMessage] + " [$details]" + context.invokeNextProcessor() + } + } + + @Test + fun `Create simple pipeline`() { + val context = Context() + context[firstname] = "Marcin" + context[lastname] = "Iksiński" + context[age] = 28 + + val pipeline = Pipeline(p1, p2) + + pipeline.process(context) + + assertThat(context[helloMessage]).isEqualTo("Marcin Iksiński [age: 28]") + + context[shoeSize] = 32 + pipeline.process(context) + + assertThat(context[helloMessage]).isEqualTo("Marcin Iksiński [age: 28, shoeSize: 32]") + } +} \ No newline at end of file diff --git a/src/test/kotlin/net/igsoft/typeutils/property/TypedPropertiesTest.kt b/src/test/kotlin/net/igsoft/typeutils/property/TypedPropertiesTest.kt new file mode 100644 index 0000000..947defb --- /dev/null +++ b/src/test/kotlin/net/igsoft/typeutils/property/TypedPropertiesTest.kt @@ -0,0 +1,59 @@ +package net.igsoft.typeutils.property + +import assertk.assertThat +import assertk.assertions.* +import net.igsoft.typeutils.marker.TypedMarker +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test + +class TypedPropertiesTest { + private lateinit var properties: TypedProperties + private val firstname by TypedMarker.create() + private val surname by TypedMarker.create() + private val age by TypedMarker.create() + private val shoeSize by TypedMarker.create() + + @BeforeEach + fun setUp() { + properties = TypedProperties(mutableMapOf()) + + properties[firstname] = "Gregory" + properties[surname] = "Iksiński" + properties[age] = 28 + } + + @Test + fun `Assert that creation is possible`() { + val properties = TypedProperties(mutableMapOf()) + + assertThat(properties).isNotNull() + assertThat(properties).apply { + prop(TypedProperties::size).isEqualTo(0) + prop(TypedProperties::keys).isEmpty() + prop(TypedProperties::values).isEmpty() + prop(TypedProperties::entries).isEmpty() + } + } + + @Test + fun `Assert that putting new properties is possible`() { + properties[shoeSize] = 32 + + assertThat(properties).apply { + prop(TypedProperties::size).isEqualTo(4) + prop(TypedProperties::keys).isEqualTo(setOf(firstname, surname, age, shoeSize)) + prop(TypedProperties::values).containsExactlyInAnyOrder("Gregory", "Iksiński", 28, 32) + } + } + + @Test + fun `Assert that removing properties is possible`() { + properties.remove(age) + + assertThat(properties).apply { + prop(TypedProperties::size).isEqualTo(2) + prop(TypedProperties::keys).isEqualTo(setOf(firstname, surname)) + prop(TypedProperties::values).containsExactlyInAnyOrder("Gregory", "Iksiński") + } + } +}