mirror of
https://github.com/apple/swift.git
synced 2026-06-27 12:25:55 +02:00
ebb8d7392c
The error is passed in to the catch block as a +1 value but we were just dropping it on the floor. Fixes https://bugs.swift.org/browse/SR-972
55 lines
1.2 KiB
Swift
55 lines
1.2 KiB
Swift
// RUN: %target-run-simple-swift
|
|
// REQUIRES: executable_test
|
|
|
|
//
|
|
// Tests for error handling.
|
|
//
|
|
|
|
import StdlibUnittest
|
|
|
|
// Also import modules which are used by StdlibUnittest internally. This
|
|
// workaround is needed to link all required libraries in case we compile
|
|
// StdlibUnittest with -sil-serialize-all.
|
|
import SwiftPrivate
|
|
#if _runtime(_ObjC)
|
|
import ObjectiveC
|
|
#endif
|
|
|
|
enum Excuse : ErrorProtocol { case CatAteHomework(LifetimeTracked) }
|
|
|
|
var ErrorHandlingTests = TestSuite("ErrorHandling")
|
|
|
|
func furball(b: Bool) throws -> LifetimeTracked {
|
|
if b {
|
|
throw Excuse.CatAteHomework(LifetimeTracked(0))
|
|
} else {
|
|
return LifetimeTracked(1)
|
|
}
|
|
}
|
|
|
|
ErrorHandlingTests.test("tryCatch") {
|
|
do {
|
|
try expectEqual(furball(false), LifetimeTracked(1))
|
|
} catch {
|
|
expectUnreachable()
|
|
}
|
|
|
|
do {
|
|
try furball(true)
|
|
expectUnreachable()
|
|
} catch let e {
|
|
if case Excuse.CatAteHomework(let c) = e {
|
|
expectEqual(c, LifetimeTracked(0))
|
|
} else {
|
|
expectUnreachable()
|
|
}
|
|
}
|
|
}
|
|
|
|
ErrorHandlingTests.test("tryOptional") {
|
|
expectEqual(LifetimeTracked(1), try? furball(false))
|
|
expectEqual(Optional<LifetimeTracked>.none, try? furball(true))
|
|
}
|
|
|
|
runAllTests()
|