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

[XCTestExpectation] Adding expectationForNotification #85

Merged
merged 1 commit into from Mar 30, 2016

Conversation

kylesyoon
Copy link
Contributor

What's in this pull request?

This pull request adds the XCTestCase convenience constructor for XCTestExpectation expectationForNotification:object:handler:.

It also includes the XCNotificationExpectationHandler typealias file as well as tests to check expectationForNotification:object:handler: behavior.

https://bugs.swift.org/browse/SR-907
However, this pull request does not include the convenience constructor expectationForPredicate:evaluatedWithObject:handler: described in the ticket, as NSPredicate initializers have yet to be implemented.

Why merge this pull request?

This will provide a full Swift implementation of expectationForNotification:object:handler: that allows asynchronous testing for NSNotification.

// CHECK: .*/Tests/Functional/Asynchronous/Expectations/main.swift:125: error: ExpectationsTestCase.test_observeNotificationWithIncorrectObject_fails : Asynchronous wait failed - Exceeded timeout of 0.1 seconds, with unfulfilled expectations: Expect notification 'notificationWithIncorrectObjectTest' from dummyObject
// CHECK: Test Case 'ExpectationsTestCase.test_observeNotificationWithIncorrectObject_fails' failed \(\d+\.\d+ seconds\).
func test_observeNotificationWithIncorrectObject_fails() {
let notificationNamme = "notificationWithIncorrectObjectTest"

Choose a reason for hiding this comment

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

“notificationName”

Choose a reason for hiding this comment

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

🚗 💨

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😂 You got me
🙏 Namaste

@modocache
Copy link
Collaborator

This is awesome, thanks @yoonapps!! 💯 I have a few nitpicks, but other than those this looks great to me!

objectDescription = "\(object)"
} else {
objectDescription = "any object"
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit-pick: Perhaps use a ternary operator here?

let objectDescription = object == nil ? "any object" : "\(object)"

I don't have a strong opinion either way, but a ternary operator would only take one line, whereas this approach uses six.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about the ternary operator but if I don't unwrap the optional, then the object description comes with Optional(description). This led to using the if statement. I can change it to the ternary operator as long as it's cool to do "\(object!)" (force unwrap).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or is it better to print in the description that the argument is an optional? Maybe it's something to consider since the description is following the Obj C style.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, good point! Perhaps this, then?

let objectDescription = object == nil ? "any object" : "\(object!)"

Or we could leave this as-is. Again, I don't have a strong opinion here. :)

@modocache
Copy link
Collaborator

@yoonapps This is fantastic! 😍 Could you squash these three commits into one?

@mike-ferris-apple Could you ask @swift-ci to please test?

@kylesyoon
Copy link
Contributor Author

Awesome, yes I will address these reviews and squash the commits this evening! Thanks for the reviews.

/// allows a nil notification name for its criteria of observing
/// notifications, the Objective-C implementation of this function
/// requires a notification name.
public func expectationForNotification(notificationName: String, object: AnyObject?, handler: XCNotificationExpectationHandler?) -> XCTestExpectation {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Super minor nit-pick: Technically the Apple XCTest signature for this method is:

public func expectationForNotification(notificationName: String, object objectToObserve: AnyObject?, handler: XCNotificationExpectationHandler?) -> XCTestExpectation

I don't think the parameter name is important, but I'd personally prefer to be 100% consistent. 😛

@mike-ferris
Copy link

@swift-ci please test

@kylesyoon kylesyoon force-pushed the master branch 2 times, most recently from c70c5f4 to adae1b2 Compare March 28, 2016 20:39
@kylesyoon
Copy link
Contributor Author

@modocache addressed issues

XCTMain([testCase(NotificationExpectationsTestCase.allTests)])

// CHECK: Executed 5 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Total executed 5 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit-pick: Add a new line at the end of this file so Git won't complain 😛

@kylesyoon
Copy link
Contributor Author

@modocache done ✔️ hmm actually not

@kylesyoon
Copy link
Contributor Author

Ok well Github is not showing the extra line... but there is one 😕

@modocache
Copy link
Collaborator

Yup, it's there! If no new line is present at the bottom of a file GitHub shows a symbol that looks like 🚫.

Sorry @mike-ferris-apple, but could you ask @swift-ci to please test again? I think the updates to the pull request cancelled the previous CI job.

If CI passes on Linux (OS X is still broken, see #77), then I'll merge. 😄

@mike-ferris
Copy link

@swift-ci please test

@kylesyoon
Copy link
Contributor Author

😖
@modocache The SwiftXCTestFunctionalTests build is passing for me locally. Although, I've seen the test fail once, and it was because the test expected // CHECK: in the new handler test main.swift to be the // CHECK: in the existing handler test main.swift. But I ran the test again and it was fine. Do you think this could be a file path issue?

// RUN: %{swiftc} %s -o %{built_tests_dir}/Handler
// RUN: %{built_tests_dir}/Handler > %t || true
// RUN: %{xctest_checker} %t %s

@modocache
Copy link
Collaborator

@yoonapps Nice catch!! It most certainly is a file path issue, one I've seen before--it's pretty nasty, but we need to use a unique file name for each test. You should be fine if you "namespace" your test file name, like so:

// RUN: %{swiftc} %s -o %{built_tests_dir}/Aynschronous-Notifications-Handler
// RUN: %{built_tests_dir}/Aynschronous-Notifications-Handler > %t || true
// RUN: %{xctest_checker} %t %s

Sorry about that. I'll create a JIRA issue so we can track fixing this.

@kylesyoon
Copy link
Contributor Author

@modocache Ok! I updated the paths for both the Notification Expectations and Handler main.swift files. Please work! 😭

@modocache
Copy link
Collaborator

@mike-ferris-apple Mind asking @swift-ci to please test again? 😅

I wonder when CI will be opened up to all committers... 💭

@kylesyoon
Copy link
Contributor Author

Sorry!

@modocache
Copy link
Collaborator

Haha, don't be! I'm actually fine with merging this without testing again, but better safe than sorry.

- Prints the same description as Obj C
- Uses the default notification center to observe for notifications
- Optional handler returns true or false of whether the expectation was fulfilled
- Tests check comparing the notification and object arguments for the expectation and notification
- Tests check returning true or false in handler
@parkera
Copy link
Member

parkera commented Mar 30, 2016

@swift-ci please test

@modocache modocache merged commit db3643a into apple:master Mar 30, 2016
@modocache
Copy link
Collaborator

Awesome, thanks @yoonapps! Congratulations on completing your first starter task! 💐

@kylesyoon
Copy link
Contributor Author

AWWWW YEEEEE
@modocache Thank you so much for all your help! Can't wait to do more. 🔥

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.

None yet

5 participants