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

introduce jgiven-kotlin #407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions jgiven-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"
}
23 changes: 23 additions & 0 deletions jgiven-kotlin/src/main/kotlin/JGivenKotlin.kt
Original file line number Diff line number Diff line change
@@ -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 <G : Stage<G>, W : Stage<W>, T : Stage<T>> ScenarioTestBase<G, W, T>.GIVEN: G get() = given()
val <G : Stage<G>, W : Stage<W>, T : Stage<T>> ScenarioTestBase<G, W, T>.WHEN: W get() = `when`()
val <G : Stage<G>, W : Stage<W>, T : Stage<T>> ScenarioTestBase<G, W, T>.THEN: T get() = then()

// extension attributes on stage

val <X : Stage<X>> Stage<X>.AND: X get() = and()
val <X : Stage<X>> Stage<X>.WITH: X get() = with()
val <X : Stage<X>> Stage<X>.BUT: X get() = but()
val <X : Stage<X>> Stage<X>.SELF: X get() = self()!!
62 changes: 62 additions & 0 deletions jgiven-kotlin/src/test/kotlin/JGivenKotlinExtensionTest.kt
Original file line number Diff line number Diff line change
@@ -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<JGivenKotlinGiven, JGivenKotlinWhen, JGivenKotlinThen>() {

@Test
fun `add two numbers`() {
GIVEN
.number(5)

WHEN
.we_add_number(7)

THEN
.the_result_is(12)
}
}


@JGivenStage
class JGivenKotlinGiven : Stage<JGivenKotlinGiven>() {

@ProvidedScenarioState private var firstNumber : Int = 0

fun number(number : Int) : JGivenKotlinGiven = SELF.apply { firstNumber = number }
}

@JGivenStage
class JGivenKotlinWhen : Stage<JGivenKotlinWhen>() {

@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<JGivenKotlinThen>() {

@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)
}

}

4 changes: 4 additions & 0 deletions jgiven-kotlin/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down