Skip to content

Add @autoclosure to message parameter, so it's constructed only if it's needed. #17

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

Merged
merged 1 commit into from
Jan 8, 2016

Conversation

kovpas
Copy link
Contributor

@kovpas kovpas commented Dec 18, 2015

Sometimes it's nice to have a complex "conditional" XCTAssert message.

XCTAssertEqual(a, b, "\(String(idx, radix: 16)) \(a ? "exists in" : "does not exist in") mutable and \(b ? "exists in" : "does not exist in") immutable character set")

This is already a small performance impact, but it's nothing too bad, unless it's in a loop. There was a test in NSCharacterSet, which used to take quite a lot of time: https://github.com/apple/swift-corelibs-foundation/pull/149/files

I think it would be nice if message param is only evaluated if it's needed.

The test, that used to take 116 sec on my linux machine, takes only 3.5 seconds with this change.

@modocache
Copy link
Contributor

The XCTAssert() functions in Objective-C XCTest do not wrap their message parameters in @autoclosure. As a result, I think this request may conflict with this project's mission statement:

This version of XCTest uses the same API as the XCTest you are familiar with from Xcode. Our goal is to enable your project's tests to run on all Swift platforms without having to rewrite them.

Here's the Swift XCTAssert() signature from Objective-C XCTest:

public func XCTAssert(@autoclosure expression: () -> BooleanType, _ message: String = default, file: String = default, line: UInt = default)

@kovpas
Copy link
Contributor Author

kovpas commented Dec 18, 2015

Oh, I see. This is an API change indeed. However, it doesn't lead to any changes to tests.

@phausler
Copy link
Contributor

There are already a few differentials and I think this might be something that would perhaps be an improvement to both; in that the messages could have side-effects that would not be intended unless in a failure case. e.g. calculating a debug description etc.

I would be interested to see how this change would work if it were in the XCTest overlay as well performance wise; faster tests usually mean people are more willing to have more tests execute for the one thing they are iterating on.

@kovpas
Copy link
Contributor Author

kovpas commented Dec 18, 2015

@modocache current implementation already have slightly different signature:

public func XCTAssert(@autoclosure expression: () -> BooleanType, _ message: String = "", file: StaticString = __FILE__, line: UInt = __LINE__)

@modocache
Copy link
Contributor

Hey yeah, look at that! What the heck is a StaticString?

@kovpas
Copy link
Contributor Author

kovpas commented Dec 19, 2015

How about "", __FILE__ and __LINE__?

What's the final goal? To make sure it's letter-by-letter the same as the previous declaration? It's already not. If the goal is

to enable your project's tests to run on all Swift platforms without having to rewrite them.

Then @autoclosure in message param doesn't go against that.

@phausler
Copy link
Contributor

@modocache StaticString is specifically anything written as "" instead of a variable. This was initially required for the linux implementation to even work (at least at the time of when I originally wrote that portion). It may no longer be needed

@gribozavr
Copy link
Contributor

@phausler No, it was added to the library for a different reason. It was added to represent a string that does not need to be dynamically allocated (like a file name in an assertion). It is cheaper than String.

@modocache
Copy link
Contributor

@kovpas I agree! If we're willing to deviate slightly from the function signatures in Objective-C XCTest, using an @autoclosure for the message parameter seems like a great idea! Might even be worth filing a radar for Objective-C XCTest 😉

How about "", __FILE__ and __LINE__?

I think these are the default parameters for Objective-C XCTest as well, they're just represented as default in the bridged Swift headers.

@eschaton
Copy link
Contributor

This is correct, "", FILE, and LINE are the default values in the Xcode XCTest assertions. They're implemented in the XCTest overlay in the Swift codebase, so you can see them there.

On Dec 19, 2015, at 10:15 AM, Brian Gesiak [email protected] wrote:

@kovpas I agree! If we're willing to deviate slightly from the function signatures in Objective-C XCTest, using an @autoclosure for the message parameter seems like a great idea! Might even be worth filing a radar for Objective-C XCTest

How about "", FILE and LINE?

I think these are the default parameters for Objective-C XCTest as well, they're just represented as default in the bridged Swift headers.


Reply to this email directly or view it on GitHub.

@mike-ferris
Copy link

I like this change. Could you make a similar pull request for the XCTest overlay in the swift project as well?

@kovpas
Copy link
Contributor Author

kovpas commented Jan 8, 2016

@mike-ferris-apple sure, will do today.

@mike-ferris
Copy link

Also, some of the other pull requests we've taken lately have caused this to have conflicts. Do you mind rebasing it?

@kovpas kovpas force-pushed the autoclosure_param branch from e3f2fcc to 5171abb Compare January 8, 2016 19:47
@kovpas
Copy link
Contributor Author

kovpas commented Jan 8, 2016

@mike-ferris-apple done.

@kovpas
Copy link
Contributor Author

kovpas commented Jan 8, 2016

@mike-ferris-apple also created a pull request in the swift project: swiftlang/swift#913

@mike-ferris
Copy link

Thanks!

mike-ferris pushed a commit that referenced this pull request Jan 8, 2016
Add @autoclosure to message parameter, so it's constructed only if it's needed.
@mike-ferris mike-ferris merged commit f4c567a into swiftlang:master Jan 8, 2016
@kovpas kovpas deleted the autoclosure_param branch March 9, 2016 11:55
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.

6 participants