Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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)

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)

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)

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)

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)

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)

@QuickBot
Copy link

QuickBot commented Apr 4, 2017

1 Warning
⚠️ Big PR

Generated by 🚫 danger

@jeffh
Copy link
Member

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.

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 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
Copy link
Member

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

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done 👍

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

jeffh 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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants