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

Swift support for the Cedar test runner #367

Merged
merged 14 commits into from
Jan 15, 2016

Conversation

briancroom
Copy link
Contributor

As more and more projects are expressing interest in using Cedar in pure-Swift and (especially) mixed Objective-C and Swift contexts, I've taken some steps towards a maintainable approach to making the Cedar test runner, including the describe, it, etc. DSL available from Swift. To be clear, Cedar matchers and doubles remain inaccessible from Swift due to their reliance on C++. (See discussion at #259)

I consider this to be an evolution of great proof-of-concept work done by @tjarratt and @Jdavid1001 on the swift-spec-runner branch a while back. The key difference is that this branch makes the existing Cedar framework targets importable from Swift, instead of introducing additional targets dedicated to the runner.

Have a look at the individual commit messages to see the whole story, but the highlights here are:

  • The Cedar framework now defines an Objective-C module on all 4 platforms, allowing it to be imported from Swift source files, although only the runner-related components are exposed. This is accomplished by removing the Objective-C++ requirement from the Cedar.h umbrella header, and instead conditionally-compiling individual headers that require C++.
  • When running within an XCTest bundle, translate XCTest assertion failures to be translated into Cedar spec failures. This allows using XCTAssert* functions from specs defined in Swift, as well as out-of-the-box support for other matcher frameworks (in particular Nimble) that report failures through XCTest.

With this PR, it is possible to add a Swift file like the following to an existing Cedar test suite or bundle:

import Cedar
import Nimble

class SwiftSpec: CDRSpec {
    override func declareBehaviors() {
        describe("running tests in Swift") {
            it("can use XCTest assertions") {
                XCTAssertEqual(1, 1)
            }

            it("can use Nimble matchers") {
                expect(1).to(equal(1));
            }
        }
    }
}

@briancroom briancroom mentioned this pull request Dec 29, 2015
@briancroom
Copy link
Contributor Author

There may be some dependency here on sorting out our Travis situation (#345), as Swift 2 is a requirement for some of this stuff to work properly.

@briancroom
Copy link
Contributor Author

Here's a full example of using this in a test target mixing Objective-C and Swift specs with Nimble matchers: https://github.com/briancroom/TastesOfCocoa/tree/master/CedarWithNimble

@tjarratt
Copy link
Contributor

This is truly awesome! I had wondered if it would be possible to conditionally compile in the C++ features of Cedar, but couldn't find a way to make it work because of the strict rule of the umbrella header importing each public header.

Conditionally bringing in the contents of the header though is quite clever. 🏆

@briancroom
Copy link
Contributor Author

To make progress on getting CI fixed up for this I'm intending to introduce an opt-out compile-time flag that can be included in rake's xcodebuild invocation to exclude the swift spec for older Xcode versions.

@tjarratt
Copy link
Contributor

tjarratt commented Jan 6, 2016

If there's anything we can do to help out, please let us know. There's a bit of a beach in SF and NY right now, and I'm really eager to help out myself as well.

@briancroom briancroom force-pushed the swift_module branch 4 times, most recently from be06518 to 05b5f6e Compare January 8, 2016 12:57
@briancroom
Copy link
Contributor Author

Travis is now passing and I have resolved the shared examples context issue as well. I'm looking forward to more feedback on this, but I believe it's about ready to merge!

This is a modernization bringing them in line with what our spec
template has had for a while. We also no longer need to conditional
importing with quotes or angle brackets depending on platform
* Remove requirement that Cedar.h itself only be imported by Objective-C++ files
* Conditionally compile C++ constructs in individual headers
* Cedar.h now only imports the various sub-umbrella headers for different
  subsystems
* Add CedarReporters.h so the reporter headers (already public!) get imported
* CDRSpec.h no longer imports headers related to matchers and doubles
* Properly copy the ContainSubset matcher header
This better advertises API intention, as well as improving the experience
of Swift users. Unfortunately this does require silencing Clang warnings
in a few `-dealloc` methods which set `nonnull` properties to nil during
cleanup.
This ensures that the context gets bridged into Swift as a reference type
instead of a value type.
@briancroom
Copy link
Contributor Author

I have updated this to include Xcode snippets for the standard Cedar BDD constructs in Swift, as well as added the option to the file template to generate a Swift spec. Thanks to @modocache for the Quick spec template which I used as a reference for how to add a language picker!

With this, I feel that this is ready to be merged into master to receive broader feedback.
/cc @tjarratt

@briancroom
Copy link
Contributor Author

(I'll defer to someone else for adding Swift snippets for AppCode when the need is felt for them!)

@tjarratt
Copy link
Contributor

I don't have nearly enough emoji to express my excitement right now.

This will have to suffice.
🌲 + 🐦 = 🎈 🎈 🎈

tjarratt added a commit that referenced this pull request Jan 15, 2016
Swift support for the Cedar test runner
@tjarratt tjarratt merged commit 18bdaf9 into cedarbdd:master Jan 15, 2016
@briancroom briancroom deleted the swift_module branch January 23, 2016 03:47
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.

2 participants