Skip to content

TestCodable: Replace fatalError() with XCTFail #1285

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 2 commits into from
Nov 6, 2017

Conversation

spevans
Copy link
Contributor

@spevans spevans commented Oct 27, 2017

Having tests abort via fatalError() is not as useful as just failing the tests in situations where further tests can still be run.

@spevans
Copy link
Contributor Author

spevans commented Oct 27, 2017

@swift-ci please test

}

let decoded: T
do {
decoded = try decode(data)
} catch {
fatalError("Unable to decode \(T.self) <\(value)>: \(error)")
XCTFail("Unable to decode \(T.self) <\(value)>: \(error)")
Copy link
Contributor

@parkera parkera Oct 27, 2017

Choose a reason for hiding this comment

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

Should this just be returning false and leaving the XCTFail for the higher level call?

Copy link
Contributor Author

@spevans spevans Oct 27, 2017

Choose a reason for hiding this comment

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

I left it in because it acts as a handy debug message in the failure case and isn't any noisier if the decode works.

Copy link
Contributor

@alblue alblue left a comment

Choose a reason for hiding this comment

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

Think we can make this better.

let data: Data
do {
data = try encode(value)
} catch {
fatalError("Unable to encode \(T.self) <\(value)>: \(error)")
XCTFail("Unable to encode \(T.self) <\(value)>: \(error)")
return false
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I would prefer not to have a return value of false which may forget to be called by the calling site. Instead, you should pass in the message into the expect() call and then there's no need to have the XCTAssertTrue sprinkled throughout the code base.

In other words, I like the idea of fatalError -> XCTFail change, but I don't like the modification of return type to Bool or the other changes in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the return value isn't acted upon then a compiler warning is emitted.

The advantage of having the XCTAssert.. in the test_() function is that failing tests show up more easily when running the tests under Xcode, when run as normal unit tests and not via TestFoundation. This makes it quicker to know what is failing as Xcode will highlight the failing test rather than having to step though the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we're going to do something which requires the caller take action (as opposed to merely a warning when it doesn't work) then we should change this from returning a Bool to making it a throw, which will explicitly force unadorned calls to be handled. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, agreed a throw is better since it causes and error rather than a warning if not handled. I will add a commit to see what it looks like.

@spevans
Copy link
Contributor Author

spevans commented Nov 3, 2017

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor Author

spevans commented Nov 3, 2017

@swift-ci please test

Copy link
Contributor

@ianpartridge ianpartridge left a comment

Choose a reason for hiding this comment

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

I would like @itaiferber to comment on this PR. It's on my list to get JSONEncoder and its tests back in sync with the overlay, so I'd like to confirm that these changes could be made over at https://github.com/apple/swift/blob/master/test/stdlib/CodableTests.swift too 😀

@alblue
Copy link
Contributor

alblue commented Nov 6, 2017

Looks good to me. WDYT @itaiferber ?

@itaiferber
Copy link
Contributor

Sorry for the delay; will review today when I get a moment.

@itaiferber
Copy link
Contributor

Looks reasonable to me! The stdlib test runner runs every test in a separate process so failing via fatalError is okay over there; this is much better for XCTest infrastructure though.

@itaiferber itaiferber self-requested a review November 6, 2017 21:02
@ianpartridge
Copy link
Contributor

@swift-ci please test and merge

@swift-ci swift-ci merged commit 1a068fb into swiftlang:master Nov 6, 2017
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