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

Add Spek integration #107

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ ext {
// Spark
spark_kotlin_version = '1.0.0-alpha'

// Spek
spek_version = '1.1.5'

// Test
junit_version = "4.12"
mockito_version = "2.8.47"
Expand Down
43 changes: 43 additions & 0 deletions koin-spek/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

archivesBaseName = 'koin-spek'
description = 'Koin - simple dependency injection for Kotlin - ' + archivesBaseName

buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
}
}

apply plugin: 'org.junit.platform.gradle.plugin'

junitPlatform {
filters {
engines {
include 'spek'
}
}
}

repositories {
maven { url "http://dl.bintray.com/jetbrains/spek" }
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

// Koin
compile project(":koin-core")

compile "junit:junit:$junit_version"
testCompile "org.mockito:mockito-core:$mockito_version"

// Spek
compile "org.jetbrains.spek:spek-api:$spek_version"
testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testCompile "org.jetbrains.spek:spek-api:$spek_version"
testRuntime "org.jetbrains.spek:spek-junit-platform-engine:$spek_version"
}
31 changes: 31 additions & 0 deletions koin-spek/src/main/kotlin/org/koin/spek/KoinSpek.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.koin.spek

import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.Spec
import org.jetbrains.spek.api.dsl.TestBody
import org.jetbrains.spek.api.lifecycle.GroupScope
import org.jetbrains.spek.api.lifecycle.LifecycleListener
import org.koin.KoinContext
import org.koin.core.parameter.Parameters
import org.koin.standalone.KoinComponent
import org.koin.standalone.StandAloneContext

class KoinSpec(val root: Spec) : KoinComponent, Spec by root

abstract class KoinSpek(koinSpec: KoinSpec.() -> Unit): Spek({
koinSpec.invoke(KoinSpec(this))
})

abstract class AutoCloseKoinSpek(koinSpec: KoinSpec.() -> Unit): KoinSpek({
registerListener(object: LifecycleListener {
override fun afterExecuteGroup(group: GroupScope) {
StandAloneContext.closeKoin()
}
})

koinSpec.invoke(this)
})

fun TestBody.dryRun(defaultParameters: Parameters = { kotlin.collections.emptyMap() }) {
(org.koin.standalone.StandAloneContext.koinContext as KoinContext).dryRun(defaultParameters)
}
31 changes: 31 additions & 0 deletions koin-spek/src/test/kotlin/org/koin/spek/KoinDSLTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.koin.spek

import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import org.junit.Assert
import org.koin.dsl.module.applicationContext
import org.koin.standalone.StandAloneContext
import org.koin.standalone.inject

class HelloService
class WorldService(val service: HelloService)

val module = applicationContext {
factory { HelloService() }
bean { WorldService(get()) }
}

class KoinDSLTest : AutoCloseKoinSpek({
describe("DSL") {
it("is possible to use inject") {
StandAloneContext.startKoin(listOf(module))

val worldService: WorldService by inject()
Assert.assertNotNull(worldService.service)
}

it("is possible to use dryRun") {
dryRun()
}
}
})
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':koin-core', ':koin-test', 'koin-ktor', 'koin-spark'
include ':koin-core', ':koin-test', 'koin-ktor', 'koin-spark', 'koin-spek'