Files
swift-mirror/test/Interpreter/SDK/objc_failable_initializers.swift
Slava Pestov 38fdb6b7ff Update various tests to not use Swift 3 code
Completely mechanical changes:

- Explicit @objc in a few places
- Some imported APIs changed
- For the mix-and-match tests, just test version 4/5 instead of 3/4
2018-10-26 20:15:01 -04:00

90 lines
1.4 KiB
Swift

// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop
import StdlibUnittest
import Foundation
var ObjCFailableInitTestSuite = TestSuite("ObjCFailableInit")
class Canary {
static var count: Int = 0
init() {
Canary.count += 1
}
deinit {
Canary.count -= 1
}
}
extension NSDate {
@objc convenience init?(b: Bool) {
guard b else { return nil }
self.init()
}
}
class MyDate : NSDate {
var derivedCanary = Canary()
static var count = 0
override init() {
MyDate.count += 1
super.init()
}
required convenience init(coder: NSCoder) {
fatalError("not implemented")
}
deinit {
MyDate.count -= 1
}
@objc convenience init?(b: Bool) {
guard b else { return nil }
self.init()
}
}
class MyDerivedDate : MyDate {
var canary = Canary()
static var derivedCount = 0
override init() {
MyDerivedDate.count += 1
}
deinit {
MyDerivedDate.count -= 1
}
}
func mustFail<T>(f: () -> T?) {
if f() != nil {
preconditionFailure("Didn't fail")
}
}
ObjCFailableInitTestSuite.test("InitFailure_Before") {
mustFail { NSDate(b: false) }
expectEqual(0, Canary.count)
mustFail { MyDate(b: false) }
expectEqual(0, Canary.count)
expectEqual(0, MyDate.count)
mustFail { MyDerivedDate(b: false) }
expectEqual(0, Canary.count)
expectEqual(0, MyDate.count)
expectEqual(0, MyDerivedDate.derivedCount)
}
runAllTests()