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

Utilize multiline string literals #544

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Sources/Nimble/Adapters/NimbleXCTestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ private func recordFailure(_ message: String, location: SourceLocation) {
let line = Int(location.line)
testCase.recordFailure(withDescription: message, inFile: location.file, atLine: line, expected: true)
} else {
let msg = "Attempted to report a test failure to XCTest while no test case was running. " +
"The failure was:\n\"\(message)\"\nIt occurred at: \(location.file):\(location.line)"
let msg = """
Attempted to report a test failure to XCTest while no test case was running. The failure was:
\"\(message)\"
It occurred at: \(location.file):\(location.line)
"""
NSException(name: .internalInconsistencyException, reason: msg, userInfo: nil).raise()
}
#endif
Expand Down
9 changes: 6 additions & 3 deletions Sources/Nimble/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ internal func nimblePrecondition(
}

internal func internalError(_ msg: String, file: FileString = #file, line: UInt = #line) -> Never {
// swiftlint:disable line_length
fatalError(
"Nimble Bug Found: \(msg) at \(file):\(line).\n" +
"Please file a bug to Nimble: https://github.com/Quick/Nimble/issues with the " +
"code snippet that caused this error."
"""
Nimble Bug Found: \(msg) at \(file):\(line).
Please file a bug to Nimble: https://github.com/Quick/Nimble/issues with the code snippet that caused this error.
"""
)
// swiftlint:enable line_length
}
6 changes: 4 additions & 2 deletions Sources/Nimble/Matchers/AsyncMatcherWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ internal struct AsyncMatcherWrapper<T, U>: Matcher
}

private let toEventuallyRequiresClosureError = FailureMessage(
// swiftlint:disable:next line_length
stringValue: "expect(...).toEventually(...) requires an explicit closure (eg - expect { ... }.toEventually(...) )\nSwift 1.2 @autoclosure behavior has changed in an incompatible way for Nimble to function"
stringValue: """
expect(...).toEventually(...) requires an explicit closure (eg - expect { ... }.toEventually(...) )
Swift 1.2 @autoclosure behavior has changed in an incompatible way for Nimble to function
"""
)

extension Expectation {
Expand Down
13 changes: 9 additions & 4 deletions Sources/Nimble/Utils/Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ internal class AssertionWaitLock: WaitLock {
nimblePrecondition(
currentWaiter == nil,
"InvalidNimbleAPIUsage",
"Nested async expectations are not allowed to avoid creating flaky tests.\n\n" +
"The call to\n\t\(info)\n" +
"triggered this exception because\n\t\(currentWaiter!)\n" +
"is currently managing the main run loop."
"""
Nested async expectations are not allowed to avoid creating flaky tests.

The call to
\t\(info)
triggered this exception because
\t\(currentWaiter!)
is currently managing the main run loop.
"""
)
currentWaiter = info
}
Expand Down
19 changes: 10 additions & 9 deletions Tests/NimbleTests/AsynchronousTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,16 @@ final class AsyncTest: XCTestCase, XCTestCaseProvider {
func testCombiningAsyncWaitUntilAndToEventuallyIsNotAllowed() {
// Currently we are unable to catch Objective-C exceptions when built by the Swift Package Manager
#if !SWIFT_PACKAGE
let referenceLine = #line + 9
var msg = "Unexpected exception raised: Nested async expectations are not allowed "
msg += "to avoid creating flaky tests."
msg += "\n\n"
msg += "The call to\n\t"
msg += "expect(...).toEventually(...) at \(#file):\(referenceLine + 7)\n"
msg += "triggered this exception because\n\t"
msg += "waitUntil(...) at \(#file):\(referenceLine + 1)\n"
msg += "is currently managing the main run loop."
let referenceLine = #line + 10
let msg = """
Unexpected exception raised: Nested async expectations are not allowed to avoid creating flaky tests.

The call to
\texpect(...).toEventually(...) at \(#file):\(referenceLine + 7)
triggered this exception because
\twaitUntil(...) at \(#file):\(referenceLine + 1)
is currently managing the main run loop.
"""
failsWithErrorMessage(msg) { // reference line
waitUntil(timeout: 2.0) { done in
var protected: Int = 0
Expand Down
7 changes: 6 additions & 1 deletion Tests/NimbleTests/Helpers/utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ func failsWithErrorMessage(_ messages: [String], file: FileString = #file, line:
} else {
let knownFailures = recorder.assertions.filter { !$0.success }.map { $0.message.stringValue }
let knownFailuresJoined = knownFailures.joined(separator: ", ")
message = "Expected error message (\(msg)), got (\(knownFailuresJoined))\n\nAssertions Received:\n\(recorder.assertions)"
message = """
Expected error message (\(msg)), got (\(knownFailuresJoined))

Assertions Received:
\(recorder.assertions)
"""
}
NimbleAssertionHandler.assert(false,
message: FailureMessage(stringValue: message),
Expand Down
51 changes: 39 additions & 12 deletions Tests/NimbleTests/Matchers/HaveCountTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ final class HaveCountTest: XCTestCase, XCTestCaseProvider {
expect([1, 2, 3]).to(haveCount(3))
expect([1, 2, 3]).notTo(haveCount(1))

failsWithErrorMessage("expected to have Array<Int> with count 1, got 3\nActual Value: [1, 2, 3]") {
failsWithErrorMessage(
"""
expected to have Array<Int> with count 1, got 3

Choose a reason for hiding this comment

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

Syntactic Sugar Violation: Shorthand syntactic sugar should be used, i.e. [Int] instead of Array. (syntactic_sugar)

Actual Value: [1, 2, 3]
"""
) {
expect([1, 2, 3]).to(haveCount(1))
}

failsWithErrorMessage("expected to not have Array<Int> with count 3, got 3\nActual Value: [1, 2, 3]") {
failsWithErrorMessage(
"""
expected to not have Array<Int> with count 3, got 3

Choose a reason for hiding this comment

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

Syntactic Sugar Violation: Shorthand syntactic sugar should be used, i.e. [Int] instead of Array. (syntactic_sugar)

Actual Value: [1, 2, 3]
"""
) {
expect([1, 2, 3]).notTo(haveCount(3))
}
}
Expand All @@ -20,13 +30,22 @@ final class HaveCountTest: XCTestCase, XCTestCaseProvider {
expect(dictionary).to(haveCount(3))
expect(dictionary).notTo(haveCount(1))

failsWithErrorMessage("expected to have Dictionary<String, Int> with count 1, got 3\nActual Value: \(stringify(dictionary))") {
failsWithErrorMessage(
"""
expected to have Dictionary<String, Int> with count 1, got 3

Choose a reason for hiding this comment

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

Syntactic Sugar Violation: Shorthand syntactic sugar should be used, i.e. [String: Int] instead of Dictionary<String, Int>. (syntactic_sugar)

Actual Value: \(stringify(dictionary))
"""
) {
expect(dictionary).to(haveCount(1))
}

failsWithErrorMessage("expected to not have Dictionary<String, Int> with count 3, got 3" +
"\nActual Value: \(stringify(dictionary))") {
expect(dictionary).notTo(haveCount(3))
failsWithErrorMessage(
"""
expected to not have Dictionary<String, Int> with count 3, got 3

Choose a reason for hiding this comment

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

Syntactic Sugar Violation: Shorthand syntactic sugar should be used, i.e. [String: Int] instead of Dictionary<String, Int>. (syntactic_sugar)

Copy link
Member Author

Choose a reason for hiding this comment

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

Those are written in string literals, so should be ignored.

Actual Value: \(stringify(dictionary))
"""
) {
expect(dictionary).notTo(haveCount(3))
}
}

Expand All @@ -35,14 +54,22 @@ final class HaveCountTest: XCTestCase, XCTestCaseProvider {
expect(set).to(haveCount(3))
expect(set).notTo(haveCount(1))

failsWithErrorMessage("expected to have Set<Int> with count 1, got 3" +
"\nActual Value: \(stringify(set))") {
expect(set).to(haveCount(1))
failsWithErrorMessage(
"""
expected to have Set<Int> with count 1, got 3
Actual Value: \(stringify(set))
"""
) {
expect(set).to(haveCount(1))
}

failsWithErrorMessage("expected to not have Set<Int> with count 3, got 3" +
"\nActual Value: \(stringify(set))") {
expect(set).notTo(haveCount(3))
failsWithErrorMessage(
"""
expected to not have Set<Int> with count 3, got 3
Actual Value: \(stringify(set))
"""
) {
expect(set).notTo(haveCount(3))
}
}
}
54 changes: 36 additions & 18 deletions Tests/NimbleTests/UserDescriptionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,67 @@ import Nimble
final class UserDescriptionTest: XCTestCase, XCTestCaseProvider {
func testToMatcher_CustomFailureMessage() {
failsWithErrorMessage(
"These aren't equal!\n" +
"expected to match, got <1>") {
expect(1).to(MatcherFunc { _, _ in false }, description: "These aren't equal!")
"""
These aren't equal!
expected to match, got <1>
"""
) {
expect(1).to(MatcherFunc { _, _ in false }, description: "These aren't equal!")
}
}

func testNotToMatcher_CustomFailureMessage() {
failsWithErrorMessage(
"These aren't equal!\n" +
"expected to not match, got <1>") {
expect(1).notTo(MatcherFunc { _, _ in true }, description: "These aren't equal!")
"""
These aren't equal!
expected to not match, got <1>
"""
) {
expect(1).notTo(MatcherFunc { _, _ in true }, description: "These aren't equal!")
}
}

func testToNotMatcher_CustomFailureMessage() {
failsWithErrorMessage(
"These aren't equal!\n" +
"expected to not match, got <1>") {
expect(1).toNot(MatcherFunc { _, _ in true }, description: "These aren't equal!")
"""
These aren't equal!
expected to not match, got <1>
"""
) {
expect(1).toNot(MatcherFunc { _, _ in true }, description: "These aren't equal!")
}
}

func testToEventuallyMatch_CustomFailureMessage() {
failsWithErrorMessage(
"These aren't eventually equal!\n" +
"expected to eventually equal <1>, got <0>") {
expect { 0 }.toEventually(equal(1), description: "These aren't eventually equal!")
"""
These aren't eventually equal!
expected to eventually equal <1>, got <0>
"""
) {
expect { 0 }.toEventually(equal(1), description: "These aren't eventually equal!")
}
}

func testToEventuallyNotMatch_CustomFailureMessage() {
failsWithErrorMessage(
"These are eventually equal!\n" +
"expected to eventually not equal <1>, got <1>") {
expect { 1 }.toEventuallyNot(equal(1), description: "These are eventually equal!")
"""
These are eventually equal!
expected to eventually not equal <1>, got <1>
"""
) {
expect { 1 }.toEventuallyNot(equal(1), description: "These are eventually equal!")
}
}

func testToNotEventuallyMatch_CustomFailureMessage() {
failsWithErrorMessage(
"These are eventually equal!\n" +
"expected to eventually not equal <1>, got <1>") {
expect { 1 }.toEventuallyNot(equal(1), description: "These are eventually equal!")
"""
These are eventually equal!
expected to eventually not equal <1>, got <1>
"""
) {
expect { 1 }.toEventuallyNot(equal(1), description: "These are eventually equal!")
}
}

Expand Down