-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathtest_protocol_reorder_requirements.swift
151 lines (118 loc) · 2.7 KB
/
test_protocol_reorder_requirements.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// RUN: %target-resilience-test
// REQUIRES: executable_test
import StdlibUnittest
import protocol_reorder_requirements
var ProtocolReorderRequirementsTest = TestSuite("ProtocolReorderRequirements")
var log = [String]()
struct MyBassinet : Bed {
func squiggle() {
log.append("nap time")
}
}
struct MyOnesie : Outfit {
let size = 3
}
struct SillyBaby : Baby {
func eat() {
log.append("hangry!")
}
func sleep(in bassinet: MyBassinet) {
bassinet.squiggle()
}
func wear(outfit: MyOnesie) {
log.append("wearing outfit size \(outfit.size)")
}
func poop() {
log.append("change the diaper")
}
func cry() {
log.append("waaaaah!")
}
func wiggle() {
log.append("time to wiggle!")
}
let outfitSize = 3
}
func typicalDay<B : Baby>(for baby: B,
sleepingIn bed: B.Bassinet,
wearing outfit: B.Onesie) {
baby.wear(outfit: outfit)
baby.sleep(in: bed)
baby.cry()
baby.poop()
baby.cry()
baby.sleep(in: bed)
baby.eat()
baby.cry()
}
ProtocolReorderRequirementsTest.test("ReorderProtocolRequirements") {
let baby = SillyBaby()
let bed = MyBassinet()
let outfit = MyOnesie()
typicalDay(for: baby, sleepingIn: bed, wearing: outfit)
expectEqual(log, [
"wearing outfit size 3",
"nap time",
"waaaaah!",
"change the diaper",
"waaaaah!",
"nap time",
"hangry!",
"waaaaah!"
])
log = []
goodDay(for: baby, sleepingIn: bed, wearing: outfit)
expectEqual(log, [
"wearing outfit size 3",
"nap time",
"change the diaper",
"nap time",
"hangry!",
"nap time",
"time to wiggle!"
])
}
struct Adult<Child: Baby> { }
extension Adult: Adoring
where Child.Onesie == MyOnesie, Child.Bassinet == MyBassinet {
func adore() -> String { return "awwwwwww" }
}
struct MyDiaper : Outfit {
let size = 2
}
struct GrumpyBaby : Baby {
func eat() {
log.append("waaaaaa!")
}
func sleep(in bassinet: MyBassinet) {
bassinet.squiggle()
}
func wear(outfit: MyDiaper) {
log.append("waaaaaa!")
}
func poop() {
log.append("waaaaah!")
}
func cry() {
log.append("waaaaah!")
}
func wiggle() {
log.append("waaaaah!")
}
let outfitSize = 2
}
func adoreIfYouCan(_ value: Any) -> String {
if let adoring = value as? Adoring {
return adoring.adore()
}
return "bah humbug"
}
ProtocolReorderRequirementsTest.test("ReorderProtocolRequirements") {
let adult1 = Adult<SillyBaby>()
let exclamation1 = adoreIfYouCan(adult1)
expectEqual(exclamation1, "awwwwwww")
let adult2 = Adult<GrumpyBaby>()
let exclamation2 = adoreIfYouCan(adult2)
expectEqual(exclamation2, "bah humbug")
}
runAllTests()