Skip to content

Commit

Permalink
Extension support
Browse files Browse the repository at this point in the history
This introduces extensions in Spek, see #115 on how to implement one.

It also includes the following changes:

- Renamed several DSL classes (breaking change)
- Reworked subject support as an extension
- Rebrand lazy groups to action groups
  • Loading branch information
raniejade committed Jan 1, 2017
1 parent c15c7df commit b9967f3
Show file tree
Hide file tree
Showing 75 changed files with 883 additions and 1,172 deletions.
11 changes: 11 additions & 0 deletions documentation/src/docs/latest/changes.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
== Changelog

Releases::
1.1.0:::
* Renamed `Dsl` to `SpecBody`.
* Added support for extensions.
* Reworked subjects as an extension.
* Support specs defined as objects.

1.0.0:::
* Initial release of Spek.
2 changes: 2 additions & 0 deletions documentation/src/docs/latest/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ include::writing-specifications.adoc[]

include::faq.adoc[]

include::changes.adoc[]

include::contributors.adoc[]

include::other.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.platform.engine.EngineExecutionListener
import org.junit.platform.engine.TestDescriptor
import org.junit.platform.engine.TestExecutionResult
import org.junit.platform.engine.reporting.ReportEntry
import java.util.*
import java.util.LinkedList

/**
* @author Ranie Jade Ramiso
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
rootProject.name = 'spek'

include 'spek-api'
include 'spek-subject-extension'
include 'junit-platform-engine-test-support'
include 'spek-junit-platform-engine'
include 'spek-dist'
Expand Down
14 changes: 14 additions & 0 deletions spek-api/src/main/kotlin/org/jetbrains/spek/api/CreateWith.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jetbrains.spek.api

import org.jetbrains.spek.api.lifecycle.InstanceFactory
import java.lang.annotation.Inherited
import kotlin.reflect.KClass

/**
* @author Ranie Jade Ramiso
* @since 1.1
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@Inherited
annotation class CreateWith(val factory: KClass<out InstanceFactory>)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.jetbrains.spek.api

import org.jetbrains.spek.api.dsl.SubjectDsl
import org.jetbrains.spek.api.dsl.Spec
import org.jetbrains.spek.meta.Experimental

/**
* @author Ranie Jade Ramiso
* @since 1.1
*/
@Experimental
abstract class SubjectSpek<T>(val spec: SubjectDsl<T>.() -> Unit)
fun Spec.include(spec: Spek) {
spec.spec(this)
}
8 changes: 6 additions & 2 deletions spek-api/src/main/kotlin/org/jetbrains/spek/api/Spek.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.jetbrains.spek.api

import org.jetbrains.spek.api.dsl.Dsl
import org.jetbrains.spek.api.dsl.Spec

/**
* @author Ranie Jade Ramiso
* @since 1.0
*/
abstract class Spek(val spec: Dsl.() -> Unit)
abstract class Spek(val spec: Spec.() -> Unit) {
companion object {
fun wrap(spec: Spec.() -> Unit) = object: Spek(spec) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jetbrains.spek.api.dsl

import org.jetbrains.spek.meta.SpekDsl

/**
* @author Ranie Jade Ramiso
*/
@SpekDsl
interface ActionBody: TestContainer
14 changes: 0 additions & 14 deletions spek-api/src/main/kotlin/org/jetbrains/spek/api/dsl/Dsl.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package org.jetbrains.spek.api.dsl

/**
* @author Ranie Jade Ramiso
* @since 1.0
*/
sealed class Pending private constructor(val pending: Boolean) {
sealed class Pending constructor(val pending: Boolean) {
class Yes(val reason: String? = null): Pending(true)
object No: Pending(false)
}
13 changes: 13 additions & 0 deletions spek-api/src/main/kotlin/org/jetbrains/spek/api/dsl/Spec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.jetbrains.spek.api.dsl

import org.jetbrains.spek.api.lifecycle.LifecycleListener
import org.jetbrains.spek.meta.Experimental

/**
* @author Ranie Jade Ramiso
* @since 1.1
*/
@Experimental
interface Spec: SpecBody {
fun registerListener(listener: LifecycleListener)
}
19 changes: 19 additions & 0 deletions spek-api/src/main/kotlin/org/jetbrains/spek/api/dsl/SpecBody.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jetbrains.spek.api.dsl

import org.jetbrains.spek.api.lifecycle.CachingMode
import org.jetbrains.spek.api.lifecycle.LifecycleAware
import org.jetbrains.spek.meta.SpekDsl

/**
* @author Ranie Jade Ramiso
* @since 1.0
*/
@SpekDsl
interface SpecBody: TestContainer {
fun group(description: String, pending: Pending = Pending.No, body: SpecBody.() -> Unit)
fun action(description: String, pending: Pending = Pending.No, body: ActionBody.() -> Unit)
fun <T> memoized(mode: CachingMode = CachingMode.TEST, factory: () -> T): LifecycleAware<T>

fun beforeEachTest(callback: () -> Unit)
fun afterEachTest(callback: () -> Unit)
}
57 changes: 22 additions & 35 deletions spek-api/src/main/kotlin/org/jetbrains/spek/api/dsl/Standard.kt
Original file line number Diff line number Diff line change
@@ -1,114 +1,101 @@
package org.jetbrains.spek.api.dsl

import org.jetbrains.spek.api.SubjectSpek
import kotlin.reflect.KClass

/**
* Creates a [group][Dsl.group].
* Creates a [group][SpecBody.group].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.describe(description: String, body: Dsl.() -> Unit) {
fun SpecBody.describe(description: String, body: SpecBody.() -> Unit) {
group("describe $description", body = body)
}

/**
* Creates a [group][Dsl.group].
* Creates a [group][SpecBody.group].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.context(description: String, body: Dsl.() -> Unit) {
fun SpecBody.context(description: String, body: SpecBody.() -> Unit) {
group("context $description", body = body)
}

/**
* Creates a [group][Dsl.group].
* Creates a [group][SpecBody.group].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.given(description: String, body: Dsl.() -> Unit) {
fun SpecBody.given(description: String, body: SpecBody.() -> Unit) {
group("given $description", body = body)
}

/**
* Creates a [group][Dsl.group].
* Creates a [group][SpecBody.group].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.on(description: String, body: Dsl.() -> Unit) {
group("on $description", lazy = true, body = body)
fun SpecBody.on(description: String, body: ActionBody.() -> Unit) {
action("on $description", body = body)
}

/**
* Creates a [test][Dsl.test].
* Creates a [test][SpecBody.test].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.it(description: String, body: () -> Unit) {
fun TestContainer.it(description: String, body: TestBody.() -> Unit) {
test("it $description", body = body)
}

/**
* Creates a [group][Dsl.group].
* Creates a [group][SpecBody.group].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.xdescribe(description: String, reason: String? = null, body: Dsl.() -> Unit) {
fun SpecBody.xdescribe(description: String, reason: String? = null, body: SpecBody.() -> Unit) {
group("describe $description", Pending.Yes(reason), body = body)
}

/**
* Creates a [group][Dsl.group].
* Creates a [group][SpecBody.group].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.xcontext(description: String, reason: String? = null, body: Dsl.() -> Unit) {
fun SpecBody.xcontext(description: String, reason: String? = null, body: SpecBody.() -> Unit) {
group("context $description", Pending.Yes(reason), body = body)
}

/**
* Creates a [group][Dsl.group].
* Creates a [group][SpecBody.group].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.xgiven(description: String, reason: String? = null, body: Dsl.() -> Unit) {
fun SpecBody.xgiven(description: String, reason: String? = null, body: SpecBody.() -> Unit) {
group("given $description", Pending.Yes(reason), body = body)
}

/**
* Creates a pending [group][Dsl.group].
* Creates a pending [group][SpecBody.group].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.xon(description: String, reason: String? = null, body: Dsl.() -> Unit = {}) {
group("on $description", Pending.Yes(reason), lazy = true, body = body)
fun SpecBody.xon(description: String, reason: String? = null, body: ActionBody.() -> Unit = {}) {
action("on $description", Pending.Yes(reason), body = body)
}

/**
* Creates a pending [test][Dsl.test].
* Creates a pending [test][SpecBody.test].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun Dsl.xit(description: String, reason: String? = null, body: () -> Unit = {}) {
fun TestContainer.xit(description: String, reason: String? = null, body: TestBody.() -> Unit = {}) {
test("it $description", Pending.Yes(reason), body)
}

/**
* Alias for [SubjectDsl.includeSubjectSpec].
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
fun <T, K: SubjectSpek<T>> SubjectDsl<*>.itBehavesLike(spec: KClass<K>) {
includeSubjectSpec(spec)
}
19 changes: 0 additions & 19 deletions spek-api/src/main/kotlin/org/jetbrains/spek/api/dsl/SubjectDsl.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jetbrains.spek.api.dsl

import org.jetbrains.spek.meta.SpekDsl

/**
* @author Ranie Jade Ramiso
*/
@SpekDsl
interface TestBody
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jetbrains.spek.api.dsl

import org.jetbrains.spek.meta.SpekDsl

/**
* @author Ranie Jade Ramiso
*/
@SpekDsl
interface TestContainer {
fun test(description: String, pending: Pending = Pending.No, body: TestBody.() -> Unit)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.jetbrains.spek.api.lifecycle

/**
* @author Ranie Jade Ramiso
*/
interface ActionScope: GroupScope
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.jetbrains.spek.api.memoized
package org.jetbrains.spek.api.lifecycle

import org.jetbrains.spek.meta.Experimental

/**
* Specifies how [subjects][Subject] are cached.
* Specifies how [lifecycle aware objects][LifecycleAware] are cached.
*
* @author Ranie Jade Ramiso
* @since 1.0
*/
@Experimental
enum class CachingMode {
/**
* Subjects will be shared throughout the group which it was declared.
* Instance will be shared within the group it was declared.
*/
GROUP,
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jetbrains.spek.extension
package org.jetbrains.spek.api.lifecycle

import org.jetbrains.spek.meta.Experimental

/**
* @author Ranie Jade Ramiso
* @since 1.1
*/
@Experimental
interface ExtensionContext {
}
interface GroupScope: Scope
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jetbrains.spek.api.lifecycle

import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.meta.Experimental
import kotlin.reflect.KClass

/**
* @author Ranie Jade Ramiso
* @since 1.1
*/
@Experimental
interface InstanceFactory {
fun <T: Spek> create(spek: KClass<T>): T
}
Loading

0 comments on commit b9967f3

Please sign in to comment.