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 lambda constructor parameter to shorten specification. #23

Open
hastebrot opened this issue Apr 10, 2016 · 1 comment
Open

Comments

@hastebrot
Copy link

Here is a small sample that introduces an intermediate class that shortens specification classes. We lose flexibility a bit (no way to change the runner via @RunWith) and it also clashes the name with the Spec interface. This was inspired by spekframework/spek#71.

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import io.polymorphicpanda.kspec.KSpec
import io.polymorphicpanda.kspec.describe
import io.polymorphicpanda.kspec.it
import io.polymorphicpanda.kspec.junit.JUnitKSpecRunner
import org.junit.runner.RunWith

@RunWith(JUnitKSpecRunner::class)
open class Spec(private val definition: Spec.() -> Unit) : KSpec() {
    override fun spec() {
        definition()
    }
}

class ExampleSpec : Spec({
    describe("a group") {
        it("example") {
            assertThat(2 + 3, equalTo(5))
        }
    }
})
@raniejade
Copy link
Owner

It does shorten it a bit but looks weird if you do shared examples like:

class CalculatorSpec: Specks({
    describe(Calculator::class) {
        itBehavesLike(calculator())
    } }) {
    companion object {
        fun calculator() = sharedExample<Calculator> {
            describe("add") {
                it("1 + 1 = 2") {
                    assertThat(subject.add(1, 1), equalTo(2))
                }
            }

            describe("minus") {
                it("1 - 1 = 0") {
                    assertThat(subject.minus(1, 1), equalTo(0))
                }
            }

            describe("multiply") {
                it("1 * 2 = 2") {
                    assertThat(subject.multiply(1, 2), equalTo(2))
                }
            }

            describe("divide") {
                it("10 / 5 = 2") {
                    assertThat(subject.divide(10, 5), equalTo(2))
                }
            }
        }
    }
}

Declaring it somewhere else would fix it. #17 added support for filtering based on tags (another new feature) which can be configured by overriding KSpec.configure. This will add to the weirdness again.

class TestSpec: KSpec() {
    override fun configure(config: KSpecConfig) {
        ...
    }

   ...
}

Personally I still prefer the current approach, instance properties and methods might come in handy in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants