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 `Behavior<Context>` #701

Merged
merged 5 commits into from May 9, 2017

Conversation

@mosamer
Copy link
Contributor

commented Apr 4, 2017

This introduces a base generic behavior definition which can be extended with a specified context type.

Currently, shared assertions implementation using "shared examples" suffers from two drawbacks:

  • Defined in QuickConfiguration subclass. A file that initially intended for configuration how Quick behaves, "shared examples" are added here to allow their discovery during setup time.
  • Receives a context of [String: Any], which is string-typed and erase any type information of the context elements.

The proposed solution is to introduce Behavior<Context> which will address these issues by allowing user to define subclasses for each "shared example" with a specific context type and a similar structure to QuickSpec

The example below demonstrates how it might be used:

import Quick
import Nimble

struct EdibleContext {
   let edible: Edible
}

class EdibleBehavior: Behavior<EdibleContext> {
   override class func spec(_ aContext: @escaping () -> EdibleContext) {
      var sut: Edible!
      beforeEach {
         sut = aContext().edible
      }
      beforeEach {
         sut = nil
      }
      it("makes dolphins happy") {
        let dolphin = Dolphin(happy: false)
        dolphin.eat(sut)
        expect(dolphin.isHappy).to(beTruthy())
      }
   } 
}

class MackerelSpec: QuickSpec {
  override func spec() {
    var mackerel: Mackerel!
    beforeEach {
      mackerel = Mackerel()
    }

    itBehavesLike(EdibleBehavior.self) { EdibleContext(edible: mackerel) }
  }
}

Checklist - While not every PR needs it, new features should consider this list:

  • Does this have tests? Yes
  • Does this have documentation? Yes
  • Does this break the public API (Requires major version bump)? No
  • Is this a new feature (Requires minor version bump)? Yes
pendingFlags[Filter.pending] = true
self.itBehavesLike(behavior, context: context, flags: pendingFlags, file: file, line: line)
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Apr 4, 2017

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let closure = behavior.spec
let group = ExampleGroup(description: behavior.name, flags: flags)
currentExampleGroup.appendExampleGroup(group)
performWithCurrentExampleGroup(group) {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Apr 4, 2017

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

@@ -239,3 +251,5 @@ public func fitBehavesLike(_ name: String, flags: FilterFlags = [:], file: Strin
public func fitBehavesLike(_ name: String, flags: FilterFlags = [:], file: String = #file, line: UInt = #line, sharedExampleContext: @escaping SharedExampleContext) {
World.sharedWorld.fitBehavesLike(name, sharedExampleContext: sharedExampleContext, flags: flags, file: file, line: line)
}


This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Apr 4, 2017

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line. Currently 2. (vertical_whitespace)
Trailing Newline Violation: Files should have a single trailing newline. (trailing_newline)

pendingFlags[Filter.pending] = true
self.itBehavesLike(behavior, context: context, flags: pendingFlags, file: file, line: line)
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Apr 4, 2017

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let closure = behavior.spec
let group = ExampleGroup(description: behavior.name, flags: flags)
currentExampleGroup.appendExampleGroup(group)
performWithCurrentExampleGroup(group) {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Apr 4, 2017

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

@@ -239,3 +251,5 @@ public func fitBehavesLike(_ name: String, flags: FilterFlags = [:], file: Strin
public func fitBehavesLike(_ name: String, flags: FilterFlags = [:], file: String = #file, line: UInt = #line, sharedExampleContext: @escaping SharedExampleContext) {
World.sharedWorld.fitBehavesLike(name, sharedExampleContext: sharedExampleContext, flags: flags, file: file, line: line)
}


This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Apr 4, 2017

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line. Currently 2. (vertical_whitespace)
Trailing Newline Violation: Files should have a single trailing newline. (trailing_newline)

@QuickBot

This comment has been minimized.

Copy link

commented Apr 4, 2017

1 Warning
⚠️ Big PR

Generated by 🚫 danger

@mosamer mosamer force-pushed the mosamer:master branch from e8f07bc to c992b57 Apr 4, 2017

@jeffh

This comment has been minimized.

Copy link
Member

commented Apr 11, 2017

Hey @mosamer,

Thanks for opening a pull request. I do like the idea a lot. I'm just wondering how it compares to using plain old functions roughly like:

func itIsEditible(sut: inout Editble) {
   context('behaves like editble') {
      it("makes dolphins happy") {
        let dolphin = Dolphin(happy: false)
        dolphin.eat(sut)
        expect(dolphin.isHappy).to(beTruthy())
      }
   }
}

class MackerelSpec: QuickSpec {
  override func spec() {
    var mackerel: Mackerel!
    beforeEach {
      mackerel = Mackerel()
    }

    itIsEditible(&mackerel)
  }
}

But I digress, I think this is useful regardless. Although there needs to be tests and documentation before I can merge this PR.

Add unit tests for `itBehavesLike`
Add test cases for different versions of `itBehavesLike` func with `Behavior` and context parameters

@mosamer mosamer force-pushed the mosamer:master branch from 6be5f85 to ffb8923 May 3, 2017

@mosamer mosamer changed the title [WIP] Introduce `Behavior<Context>` Introduce `Behavior<Context>` May 3, 2017

@mosamer

This comment has been minimized.

Copy link
Contributor Author

commented May 3, 2017

Thanks @jeffh for your feedback, I think what plain functions are missing is the handling of call sites. Of course this can be added to the function arguments but will be impractical to scale and share same assertions across different files. I think this is probably the reason itBehavesLike was introduced in the first place.

I have added unit tests and documentation, following those already available for shared examples, but please feel free to let me know if any additional work is needed.

@modocache

This comment has been minimized.

Copy link
Member

commented May 6, 2017

This is a really awesome improvement! Thanks, @mosamer! ❤️

//
// Created by Mostafa Amer on 19.04.17.
// Copyright © 2017 Brian Ivan Gesiak. All rights reserved.
//

This comment has been minimized.

Copy link
@modocache

modocache May 6, 2017

Member

You can remove this header and the ones below. Most of the files in this project don't include header comments.

This comment has been minimized.

Copy link
@mosamer

mosamer May 8, 2017

Author Contributor

done 👍

@jeffh jeffh merged commit 813d9f5 into Quick:master May 9, 2017

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
hound No violations found. Woof!
@jeffh

This comment has been minimized.

Copy link
Member

commented May 9, 2017

Thanks @mosamer & @modocache!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.