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
Merged

Introduce Behavior<Context> #701

merged 5 commits into from May 9, 2017

Conversation

@mosamer
Copy link
Contributor

@mosamer mosamer 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)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Loading

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Loading

@@ -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)
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Loading

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Loading

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Loading

@@ -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)
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Loading

@QuickBot
Copy link

@QuickBot QuickBot commented Apr 4, 2017

1 Warning
⚠️ Big PR

Generated by 🚫 danger

Loading

@jeffh
Copy link
Member

@jeffh jeffh 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.

Loading

Add test cases for different versions of `itBehavesLike` func with `Behavior` and context parameters
@mosamer mosamer changed the title [WIP] Introduce Behavior<Context> Introduce Behavior<Context> May 3, 2017
@mosamer
Copy link
Contributor Author

@mosamer mosamer 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.

Loading

@modocache
Copy link
Member

@modocache modocache commented May 6, 2017

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

Loading

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

@modocache modocache May 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Loading

Copy link
Contributor Author

@mosamer mosamer May 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👍

Loading

@jeffh jeffh merged commit 813d9f5 into Quick:master May 9, 2017
2 checks passed
Loading
@jeffh
Copy link
Member

@jeffh jeffh commented May 9, 2017

Thanks @mosamer & @modocache!

Loading

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

Successfully merging this pull request may close these issues.

None yet

5 participants