Quick: Syntactic sugar for `context`

Created on 13 Jun 2017  Â·  11Comments  Â·  Source: Quick/Quick

  • [x] I have read CONTRIBUTING and have done my best to follow them.

Just thinking of making tests that use Quick even more readable.
Similar to the it function, we could have when, if, while (and maybe more that I didn't think of) functions that work as context but adds that specific word (and a space) before the description.

Most helpful comment

Just went ahead and did it 😄
https://github.com/pedrovereza/Brisker

import Quick
import Nimble
import Brisk

class BriskTests: QuickSpec {
    override func spec() {

        describe("adding to an even number") {

            given("an even number") {
                let number = 2

                when("adding another even number") {
                    let result = number + 4

                    then("the result is also even") {
                        expect(result.isEven()).to(beTrue())
                    }
                }

                when("adding to an odd number") {
                    let result = number + 3

                    then("the result is odd") {
                        expect(result.isOdd()).to(beTrue())
                    }
                }
            }
        }
    }
}

extension Int {
    func isEven() -> Bool {
        return self % 2 == 0
    }

    func isOdd() -> Bool {
        return !isEven()
    }
}

All 11 comments

Thanks for the suggestion, @danielaRiesgo! What do you think of implementing this as a library on top of Quick, as opposed to including it in Quick itself? That is, you could create a Swift library that depends on Quick, and defines:

import Quick

public func when(description: String, closure: () -> ()) -> Void {
    context("when \(description)", closure)
}

This way, you could define the additional sugar you want, and Quick can remain small. What do you think?

@modocache I don't think I would create a whole library for just these small additions, but no problem !

I see. I made the suggestion because I like the idea of keeping Quick as small as possible. Even the fact that we define both describe and context -- which are identical -- bothers me a little, because I think it confuses users. (I can tolerate it, because testing frameworks like Quick but implemented in other languages tend to include these two, so I'm fine with including them both.)

Adding when, while, and if would, I think, give users even more functions to learn about, which I think is undesirable. So I would be opposed to adding these to Quick, but putting them into a separate library is, I think, a way everyone can be happy here. :)

I particularly don't see it as something to learn. You can have an explanation for context and at the very end explain that there are "aliases" for it like when, if, ... . But I get your point.

The great Jim Weirich advocated using the Cucumber given-when-then in RSpec - I’ve long wanted those synonyms in Quick (and Kiwi before it) as I think the specs read more clearly for non-technical people and the language is more intuitive. I don’t know if these requests combine to make a synonym enabler possible - kind of like that part in legal documents where we define the terms being used in the rest of the document. It might make Quick more localizable.

Best,

Daneil

On Jun 13, 2017, at 4:21 PM, Brian Gesiak notifications@github.com wrote:

I see. I made the suggestion because I like the idea of keeping Quick as small as possible. Even the fact that we define both describe and context -- which are identical -- bothers me a little, because I think it confuses users. (I can tolerate it, because testing frameworks like Quick but implemented in other languages tend to include these two, so I'm fine with including them both.)

Adding when, while, and if would, I think, give users even more functions to learn about, which I think is undesirable. So I would be opposed to adding these to Quick, but putting them into a separate library is, I think, a way everyone can be happy here. :)

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub https://github.com/Quick/Quick/issues/712#issuecomment-308236812, or mute the thread https://github.com/notifications/unsubscribe-auth/AAXdtBdfbVyjPY_Vzc_L5qno1FGii1GHks5sDu9jgaJpZM4N47QK.

Hmm... but do you both feel the synonyms need to be in Quick? I think I'd prefer to not include them, and instead allow others to distribute them as a separate package on top of Quick, but I could be convinced otherwise.

Do you think there's a significant downside to packaging separately? Users won't get them by default, but people who want them could opt-in.

I’m not trying to subvert your goals so I’m happy if they were separate.

Selfishly, I think I’d have an easier time convincing folks to use given/when/then if they were there in the framework but that is not a real justification for me asking for it.

On Jun 14, 2017, at 10:20 AM, Brian Gesiak notifications@github.com wrote:

Hmm... but do you both feel the synonyms need to be in Quick? I think I'd prefer to not include them, and instead allow others to distribute them as a separate package on top of Quick, but I could be convinced otherwise.

Do you think there's a significant downside to packaging separately? Users won't get them by default, but people who want them could opt-in.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/Quick/Quick/issues/712#issuecomment-308446429, or mute the thread https://github.com/notifications/unsubscribe-auth/AAXdtC88cKJyzC1Nfe0NVfMZWTaIxPUtks5sD-xLgaJpZM4N47QK.

Just went ahead and did it 😄
https://github.com/pedrovereza/Brisker

import Quick
import Nimble
import Brisk

class BriskTests: QuickSpec {
    override func spec() {

        describe("adding to an even number") {

            given("an even number") {
                let number = 2

                when("adding another even number") {
                    let result = number + 4

                    then("the result is also even") {
                        expect(result.isEven()).to(beTrue())
                    }
                }

                when("adding to an odd number") {
                    let result = number + 3

                    then("the result is odd") {
                        expect(result.isOdd()).to(beTrue())
                    }
                }
            }
        }
    }
}

extension Int {
    func isEven() -> Bool {
        return self % 2 == 0
    }

    func isOdd() -> Bool {
        return !isEven()
    }
}

Oh damn, Brisk is a great name 💯

Seeing the usages of given, when, and then actually make me feel even more strongly that I don't want to see these in Quick itself, sorry! I imagine reading these tests and wondering "what do all of these different functions do...?" Clearly my aesthetic is different from all of yours... so unless any other @Quick/core members feel strongly about this, I'd like to close this issue and leave things to libraries like Brisk.

@modocache Just sadly found out there is already a pod called Brisk 😄

Anyways, I agree with you on this: Quick should remain as is and things like given/when/then should be extensions (as library as I did on Brisk or inside the codebase using Quick)

Looks like this can be closed.

Was this page helpful?
0 / 5 - 0 ratings