From 4213aefb314a1430959d0d376f5944afa4a546e2 Mon Sep 17 00:00:00 2001 From: Jan Galinski Date: Fri, 10 May 2019 11:36:56 +0200 Subject: [PATCH] introduce jgiven-kotlin --- jgiven-kotlin/build.gradle.kts | 28 +++++++++ jgiven-kotlin/src/main/kotlin/JGivenKotlin.kt | 23 +++++++ .../test/kotlin/JGivenKotlinExtensionTest.kt | 62 +++++++++++++++++++ .../src/test/resources/log4j.properties | 4 ++ settings.gradle | 3 +- 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 jgiven-kotlin/build.gradle.kts create mode 100644 jgiven-kotlin/src/main/kotlin/JGivenKotlin.kt create mode 100644 jgiven-kotlin/src/test/kotlin/JGivenKotlinExtensionTest.kt create mode 100644 jgiven-kotlin/src/test/resources/log4j.properties diff --git a/jgiven-kotlin/build.gradle.kts b/jgiven-kotlin/build.gradle.kts new file mode 100644 index 00000000000..382c41633ba --- /dev/null +++ b/jgiven-kotlin/build.gradle.kts @@ -0,0 +1,28 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +plugins { + kotlin("jvm") version "1.3.21" + id("org.jetbrains.kotlin.plugin.allopen") version "1.3.21" +} + +dependencies { + api(project(":jgiven-core")) + + implementation(kotlin("stdlib-jdk8")) + + testImplementation(project(":jgiven-junit")) +} + +allOpen { + annotation("com.tngtech.jgiven.kotlin.JGivenStage") +} + + +val compileKotlin: KotlinCompile by tasks +compileKotlin.kotlinOptions { + jvmTarget = "1.8" +} +val compileTestKotlin: KotlinCompile by tasks +compileTestKotlin.kotlinOptions { + jvmTarget = "1.8" +} \ No newline at end of file diff --git a/jgiven-kotlin/src/main/kotlin/JGivenKotlin.kt b/jgiven-kotlin/src/main/kotlin/JGivenKotlin.kt new file mode 100644 index 00000000000..2a170412ef2 --- /dev/null +++ b/jgiven-kotlin/src/main/kotlin/JGivenKotlin.kt @@ -0,0 +1,23 @@ +package com.tngtech.jgiven.kotlin + +import com.tngtech.jgiven.Stage +import com.tngtech.jgiven.base.ScenarioTestBase + +/** + * Annotation that can be used in a non spring kotlin project to mark stages and + * make use of the `all-open` compiler plugin. + */ +annotation class JGivenStage + +// extension attributes on testBase + +val , W : Stage, T : Stage> ScenarioTestBase.GIVEN: G get() = given() +val , W : Stage, T : Stage> ScenarioTestBase.WHEN: W get() = `when`() +val , W : Stage, T : Stage> ScenarioTestBase.THEN: T get() = then() + +// extension attributes on stage + +val > Stage.AND: X get() = and() +val > Stage.WITH: X get() = with() +val > Stage.BUT: X get() = but() +val > Stage.SELF: X get() = self()!! diff --git a/jgiven-kotlin/src/test/kotlin/JGivenKotlinExtensionTest.kt b/jgiven-kotlin/src/test/kotlin/JGivenKotlinExtensionTest.kt new file mode 100644 index 00000000000..14eb2e9ff22 --- /dev/null +++ b/jgiven-kotlin/src/test/kotlin/JGivenKotlinExtensionTest.kt @@ -0,0 +1,62 @@ +package com.tngtech.jgiven.kotlin + +import com.tngtech.jgiven.Stage +import com.tngtech.jgiven.annotation.ExpectedScenarioState +import com.tngtech.jgiven.annotation.ProvidedScenarioState +import com.tngtech.jgiven.annotation.ScenarioState.Resolution.NAME +import com.tngtech.jgiven.junit.ScenarioTest +import org.junit.Assert +import org.junit.Test + +/** + * Simple Test that just checks the sum of two numbers, displaying how the allopen plugin + * works together with JGivenStage. + * + * given(), when(), then(), ... are replaced by extension attributes for increased readability. + */ +class JGivenKotlinExtensionTest : ScenarioTest() { + + @Test + fun `add two numbers`() { + GIVEN + .number(5) + + WHEN + .we_add_number(7) + + THEN + .the_result_is(12) + } +} + + +@JGivenStage +class JGivenKotlinGiven : Stage() { + + @ProvidedScenarioState private var firstNumber : Int = 0 + + fun number(number : Int) : JGivenKotlinGiven = SELF.apply { firstNumber = number } +} + +@JGivenStage +class JGivenKotlinWhen : Stage() { + + @ExpectedScenarioState private var firstNumber : Int = 0 + @ProvidedScenarioState private var secondNumber : Int = 0 + + fun we_add_number(number: Int) = SELF.apply { secondNumber = number } + +} + +@JGivenStage +class JGivenKotlinThen : Stage() { + + @ProvidedScenarioState(resolution = NAME) private var firstNumber : Int = 0 + @ProvidedScenarioState(resolution = NAME) private var secondNumber : Int = 0 + + fun the_result_is(expected: Int) = SELF.apply { + Assert.assertEquals(expected, firstNumber + secondNumber) + } + +} + diff --git a/jgiven-kotlin/src/test/resources/log4j.properties b/jgiven-kotlin/src/test/resources/log4j.properties new file mode 100644 index 00000000000..b1f53f87040 --- /dev/null +++ b/jgiven-kotlin/src/test/resources/log4j.properties @@ -0,0 +1,4 @@ +log4j.rootLogger=ERROR, stdout +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n diff --git a/settings.gradle b/settings.gradle index 3f27d1996a6..1c8d9b28635 100644 --- a/settings.gradle +++ b/settings.gradle @@ -7,7 +7,8 @@ include ':jgiven-core', ':jgiven-testng', ':jgiven-spring', ':jgiven-html-app', - ':jgiven-html5-report' + ':jgiven-html5-report', + ':jgiven-kotlin' def release = System.env.RELEASE == "true" def android = System.env.ANDROID == "true"