Skip to content

Commit ab6f20a

Browse files
committed
Add library evolution tests for re-ordering protocol requirements
1 parent 69583c7 commit ab6f20a

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
public protocol Bed {
2+
func squiggle()
3+
}
4+
5+
public protocol Outfit {
6+
var size: Int { get }
7+
}
8+
9+
public protocol Eater {
10+
#if BEFORE
11+
func eat()
12+
func poop()
13+
#else
14+
func poop()
15+
func eat()
16+
#endif
17+
}
18+
19+
public protocol Wiggler {
20+
#if BEFORE
21+
func wiggle()
22+
func cry()
23+
#else
24+
func cry()
25+
func wiggle()
26+
#endif
27+
}
28+
29+
#if BEFORE
30+
31+
public protocol Baby : Eater, Wiggler {
32+
associatedtype Bassinet : Bed
33+
associatedtype Onesie : Outfit
34+
35+
var outfitSize: Int { get }
36+
37+
func sleep(in: Bassinet)
38+
func wear(outfit: Onesie)
39+
}
40+
41+
#else
42+
43+
public protocol Baby : Wiggler, Eater {
44+
associatedtype Onesie : Outfit
45+
associatedtype Bassinet : Bed
46+
47+
var outfitSize: Int { get }
48+
49+
func wear(outfit: Onesie)
50+
func sleep(in: Bassinet)
51+
}
52+
53+
#endif
54+
55+
public func goodDay<B : Baby>(for baby: B,
56+
sleepingIn bed: B.Bassinet,
57+
wearing outfit: B.Onesie) {
58+
if baby.outfitSize != outfit.size {
59+
fatalError("I grew too much!")
60+
}
61+
62+
baby.wear(outfit: outfit)
63+
baby.sleep(in: bed)
64+
baby.poop()
65+
baby.sleep(in: bed)
66+
baby.eat()
67+
baby.sleep(in: bed)
68+
baby.wiggle()
69+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// RUN: %target-resilience-test --no-backward-deployment
2+
// REQUIRES: executable_test
3+
4+
import StdlibUnittest
5+
import protocol_reorder_requirements
6+
7+
8+
var ProtocolReorderRequirementsTest = TestSuite("ProtocolReorderRequirements")
9+
10+
var log = [String]()
11+
12+
struct MyBassinet : Bed {
13+
func squiggle() {
14+
log.append("nap time")
15+
}
16+
}
17+
18+
struct MyOnesie : Outfit {
19+
let size = 3
20+
}
21+
22+
struct SillyBaby : Baby {
23+
func eat() {
24+
log.append("hangry!")
25+
}
26+
27+
func sleep(in bassinet: MyBassinet) {
28+
bassinet.squiggle()
29+
}
30+
31+
func wear(outfit: MyOnesie) {
32+
log.append("wearing outfit size \(outfit.size)")
33+
}
34+
35+
func poop() {
36+
log.append("change the diaper")
37+
}
38+
39+
func cry() {
40+
log.append("waaaaah!")
41+
}
42+
43+
func wiggle() {
44+
log.append("time to wiggle!")
45+
}
46+
47+
let outfitSize = 3
48+
}
49+
50+
func typicalDay<B : Baby>(for baby: B,
51+
sleepingIn bed: B.Bassinet,
52+
wearing outfit: B.Onesie) {
53+
baby.wear(outfit: outfit)
54+
baby.sleep(in: bed)
55+
baby.cry()
56+
baby.poop()
57+
baby.cry()
58+
baby.sleep(in: bed)
59+
baby.eat()
60+
baby.cry()
61+
}
62+
63+
ProtocolReorderRequirementsTest.test("ReorderProtocolRequirements") {
64+
let baby = SillyBaby()
65+
let bed = MyBassinet()
66+
let outfit = MyOnesie()
67+
68+
typicalDay(for: baby, sleepingIn: bed, wearing: outfit)
69+
expectEqual(log, [
70+
"wearing outfit size 3",
71+
"nap time",
72+
"waaaaah!",
73+
"change the diaper",
74+
"waaaaah!",
75+
"nap time",
76+
"hangry!",
77+
"waaaaah!"
78+
])
79+
log = []
80+
81+
goodDay(for: baby, sleepingIn: bed, wearing: outfit)
82+
expectEqual(log, [
83+
"wearing outfit size 3",
84+
"nap time",
85+
"change the diaper",
86+
"nap time",
87+
"hangry!",
88+
"nap time",
89+
"time to wiggle!"
90+
])
91+
}
92+
93+
runAllTests()
94+

0 commit comments

Comments
 (0)