Skip to content

Commit 57d4280

Browse files
committed
Move property_behavior tests into "Prototypes" dir.
The "lazy" and "delayed" test cases reflect things we could conceivably bring into the standard library at some point, and the separate files make it more clear to experimenters how the partially-implemented bits of the feature manage to work.
1 parent 0ce9aef commit 57d4280

File tree

2 files changed

+81
-62
lines changed

2 files changed

+81
-62
lines changed

test/Interpreter/property_behavior.swift renamed to test/Prototypes/property_behaviors/delayed.swift

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import StdlibUnittest
88

9+
/// An immutable property with "delayed" initialization semantics. The property
10+
/// may be set at most once, after which it is not allowed to be mutated.
11+
/// The property must also be set before it is ever read.
912
protocol delayedImmutable {
1013
associatedtype Value
1114
var storage: Value? { get set }
@@ -34,41 +37,8 @@ extension delayedImmutable {
3437
}
3538
}
3639

37-
protocol lazy {
38-
associatedtype Value
39-
var storage: Value? { get set }
40-
func parameter() -> Value
41-
}
42-
extension lazy {
43-
var value: Value {
44-
mutating get {
45-
if let existing = storage {
46-
return existing
47-
}
48-
let value = parameter()
49-
storage = value
50-
return value
51-
}
52-
53-
set {
54-
storage = newValue
55-
}
56-
}
57-
58-
static func initStorage() -> Value? {
59-
return nil
60-
}
61-
}
62-
63-
var lazyEvaluated = false
64-
func evaluateLazy() -> Int {
65-
lazyEvaluated = true
66-
return 1738
67-
}
68-
6940
class Foo {
7041
var x: Int __behavior delayedImmutable
71-
var y: Int __behavior lazy { evaluateLazy() }
7242
}
7343

7444
var DelayedImmutable = TestSuite("DelayedImmutable")
@@ -92,33 +62,4 @@ DelayedImmutable.test("write after initialization") {
9262
foo.x = 680
9363
}
9464

95-
var Lazy = TestSuite("Lazy")
96-
97-
Lazy.test("usage") {
98-
let foo = Foo()
99-
100-
expectFalse(lazyEvaluated)
101-
expectEqual(foo.y, 1738)
102-
expectTrue(lazyEvaluated)
103-
104-
lazyEvaluated = false
105-
expectEqual(foo.y, 1738)
106-
expectFalse(lazyEvaluated)
107-
108-
foo.y = 36
109-
expectEqual(foo.y, 36)
110-
expectFalse(lazyEvaluated)
111-
112-
let foo2 = Foo()
113-
expectFalse(lazyEvaluated)
114-
foo2.y = 36
115-
expectEqual(foo2.y, 36)
116-
expectFalse(lazyEvaluated)
117-
118-
let foo3 = Foo()
119-
expectFalse(lazyEvaluated)
120-
expectEqual(foo3.y, 1738)
121-
expectTrue(lazyEvaluated)
122-
}
123-
12465
runAllTests()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: %target-build-swift -Xfrontend -enable-experimental-property-behaviors %s -o %t/a.out
4+
// RUN: %target-run %t/a.out
5+
// REQUIRES: executable_test
6+
7+
import StdlibUnittest
8+
9+
/// A lazily-initialized, mutable, unsynchronized property. The property's
10+
/// parameter closure is evaluated the first time the property is read, if
11+
/// it has not been written to beforehand. No synchronization is provided
12+
/// for the lazy initialization.
13+
protocol lazy {
14+
associatedtype Value
15+
var storage: Value? { get set }
16+
func parameter() -> Value
17+
}
18+
extension lazy {
19+
var value: Value {
20+
mutating get {
21+
if let existing = storage {
22+
return existing
23+
}
24+
let value = parameter()
25+
storage = value
26+
return value
27+
}
28+
29+
set {
30+
storage = newValue
31+
}
32+
}
33+
34+
static func initStorage() -> Value? {
35+
return nil
36+
}
37+
}
38+
39+
var lazyEvaluated = false
40+
func evaluateLazy() -> Int {
41+
lazyEvaluated = true
42+
return 1738
43+
}
44+
45+
class Foo {
46+
var y: Int __behavior lazy { evaluateLazy() }
47+
}
48+
49+
var Lazy = TestSuite("Lazy")
50+
51+
Lazy.test("usage") {
52+
let foo = Foo()
53+
54+
expectFalse(lazyEvaluated)
55+
expectEqual(foo.y, 1738)
56+
expectTrue(lazyEvaluated)
57+
58+
lazyEvaluated = false
59+
expectEqual(foo.y, 1738)
60+
expectFalse(lazyEvaluated)
61+
62+
foo.y = 36
63+
expectEqual(foo.y, 36)
64+
expectFalse(lazyEvaluated)
65+
66+
let foo2 = Foo()
67+
expectFalse(lazyEvaluated)
68+
foo2.y = 36
69+
expectEqual(foo2.y, 36)
70+
expectFalse(lazyEvaluated)
71+
72+
let foo3 = Foo()
73+
expectFalse(lazyEvaluated)
74+
expectEqual(foo3.y, 1738)
75+
expectTrue(lazyEvaluated)
76+
}
77+
78+
runAllTests()

0 commit comments

Comments
 (0)