Files
swift-mirror/test/Interpreter/lazy_properties.swift
Slava Pestov be78626ea8 Sema: Add test case for lazy property 'resetting' that no longer works
Due to a serendipitous confluence of interesting behaviors,
Swift 3 allowed you to define a lazy property with an IUO
type, and setting the property to nil would 'reset' it,
re-evaluating the lazy getter the next time the property
was accessed.

This was not intended behavior and it no longer works,
so add a test that it no longer works.

Fixes <rdar://problem/32687168> and
<https://bugs.swift.org/browse/SR-5172>.
2017-07-11 17:00:36 -07:00

73 lines
1.4 KiB
Swift

// RUN: %target-run-simple-swift
// REQUIRES: executable_test
import StdlibUnittest
var LazyPropertyTestSuite = TestSuite("LazyProperty")
var lazyPropertyInitialized = 0
var lazyPropertyInitialized2 = 0
var lazyPropertyInitialized3 = 0
func lazyInitFunction() -> Int {
lazyPropertyInitialized += 1
return 0
}
class LazyPropertyClass {
var id : Int
lazy var lazyProperty = lazyInitFunction()
lazy var lazyProperty2: Int = {
lazyPropertyInitialized2 += 1
return 0
}()
lazy var lazyProperty3: Int! = {
lazyPropertyInitialized3 += 1
return 0
}()
init(_ ident : Int) {
id = ident
}
}
LazyPropertyTestSuite.test("Basic") {
var a = LazyPropertyClass(1)
expectEqual(0, lazyPropertyInitialized)
_ = a.lazyProperty
expectEqual(1, lazyPropertyInitialized)
_ = a.lazyProperty
a.lazyProperty = 42 // nothing interesting happens
expectEqual(0, lazyPropertyInitialized2)
_ = a.lazyProperty2
expectEqual(1, lazyPropertyInitialized2)
a = LazyPropertyClass(2)
a = LazyPropertyClass(3)
a.lazyProperty = 42
expectEqual(1, lazyPropertyInitialized)
expectEqual(0, lazyPropertyInitialized3)
expectEqual(0, a.lazyProperty3)
expectEqual(1, lazyPropertyInitialized3)
a.lazyProperty3 = nil
expectEqual(nil, a.lazyProperty3)
expectEqual(1, lazyPropertyInitialized3)
}
// Swift 3 had a bogus 'property resetting' behavior,
// but we don't allow that anymore.
LazyPropertyTestSuite.test("Reset") {
}
runAllTests()