-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathTestEvent.fs
More file actions
266 lines (218 loc) · 6.46 KB
/
Copy pathTestEvent.fs
File metadata and controls
266 lines (218 loc) · 6.46 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
module Fable.Tests.Event
open Util.Testing
type ClassWithCLIEvent() =
let event = new Event<_>()
[<CLIEvent>]
member _.Event = event.Publish
member this.TestEvent(arg) =
event.Trigger(this, arg)
type ClassWithNonCLIEvent() =
let event = new Event<_>()
member _.Event = event.Publish
member this.TestEvent(arg) =
event.Trigger(this, arg)
type InterfaceWithCLIEvent<'t> =
[<CLIEvent>]
abstract Event : IEvent<System.Action<obj,'t>,'t>
type ClassWithInterfaceWithCLIEvent<'t>() =
let event = new Event<_,_>()
member this.TestEvent(arg) =
event.Trigger(this, arg)
interface InterfaceWithCLIEvent<'t> with
[<CLIEvent>]
member _.Event = event.Publish
[<Fact>]
let ``test Event.add works`` () =
let source = Event<int>()
source.Publish |> Event.add (equal 10)
source.Trigger 10
[<Fact>]
let ``test Event.choose works`` () =
let mutable result = 0
let source = Event<_>()
source.Publish
|> Event.choose (function
| Choice1Of2 _ -> None
| Choice2Of2 x -> Some x)
|> Event.add (fun n -> result <- n)
source.Trigger (Choice1Of2 2)
source.Trigger (Choice2Of2 3)
equal 3 result
[<Fact>]
let ``test Event.filter works`` () =
let mutable result = 0
let source = Event<_>()
source.Publish
|> Event.filter ((>) 5)
|> Event.add (fun n -> result <- n )
source.Trigger 3
equal 3 result
source.Trigger 10
equal 3 result
[<Fact>]
let ``test Event.map works`` () =
let mutable result = 0
let source = Event<_>()
source.Publish
|> Event.map ((+) 3)
|> Event.add (fun n -> result <- n)
source.Trigger 10
equal 13 result
[<Fact>]
let ``test Event.merge works`` () =
let mutable result = 0
let source1 = Event<_>()
let source2 = Event<_>()
(source1.Publish, source2.Publish)
||> Event.merge
|> Event.add (fun n -> result <- n)
source2.Trigger 4
equal 4 result
source1.Trigger 3
equal 3 result
[<Fact>]
let ``test Event.pairwise works`` () =
let mutable result1 = 0
let mutable result2 = 0
let source = Event<_>()
source.Publish
|> Event.pairwise
|> Event.add (fun (x, y) ->
result1 <- x
result2 <- y)
source.Trigger 1
source.Trigger 2
equal 1 result1
equal 2 result2
[<Fact>]
let ``test Event.partition works`` () =
let mutable result1 = 0
let mutable result2 = 0
let source = Event<_>()
let source1, source2 =
source.Publish |> Event.partition ((>) 5)
Event.add (fun n -> result1 <- n) source1
Event.add (fun n -> result2 <- n) source2
source.Trigger 8
source.Trigger 3
equal 3 result1
equal 8 result2
[<Fact>]
let ``test Event.scan works`` () =
let mutable state = 5
let source =Event<_>()
(5, source.Publish)
||> Event.scan (+)
|> Event.add (fun x ->
state <- state + 1
)
source.Trigger 1
source.Trigger 1
equal state 7
[<Fact>]
let ``test Event.split works`` () =
let mutable result1 = 0
let mutable result2 = 0
let source = Event<_>()
let source1, source2 =
source.Publish |> Event.split (fun x ->
if 5 > x
then Choice1Of2 (x*3)
else Choice2Of2 (x*2))
Event.add (fun n -> result1 <- n) source1
Event.add (fun n -> result2 <- n) source2
source.Trigger 6
source.Trigger 2
equal 6 result1
equal 12 result2
[<Fact>]
let ``test IEvent.add works`` () =
let mutable result = 0
let source = Event<_> ()
source.Publish.Add(fun n -> result <- n)
source.Trigger 6
equal 6 result
[<Fact>]
let ``test IEvent.Subscribe works`` () =
let mutable result = 0
let source = Event<_> ()
source.Publish.Subscribe(fun n -> result <- n) |> ignore
source.Trigger 6
equal 6 result
[<Fact>]
let ``test IEvent.AddHandler works`` () =
let mutable result = 0
let source = Event<_> ()
source.Publish.AddHandler(new Handler<_>(fun sender n -> result <- n)) |> ignore
source.Trigger 6
equal 6 result
[<Fact>]
let ``test IEvent.RemoveHandler works`` () =
let mutable result = 0
let handler = new Handler<_>(fun sender n -> result <- n)
let source = Event<_> ()
source.Publish.AddHandler(handler) |> ignore
source.Publish.RemoveHandler(handler)
source.Trigger 6
equal 0 result
[<Fact>]
let ``test Classes can trigger CLI events`` () =
let mutable result = 0
let classWithEvent = new ClassWithCLIEvent()
classWithEvent.Event.Add(fun (sender, x) -> result <- x)
classWithEvent.TestEvent(5)
equal 5 result
[<Fact>]
let ``test Classes can trigger non-CLI events`` () =
let mutable result = ""
let classWithEvent = new ClassWithNonCLIEvent()
let disp = classWithEvent.Event.Subscribe(fun (sender, arg) -> result <- arg)
classWithEvent.TestEvent("Hello")
disp.Dispose()
classWithEvent.TestEvent("Bye")
equal "Hello" result
[<Fact>]
let ``test Classes can trigger CLI events on interfaces`` () =
let mutable result = 0
let mutable sender = null
let classWithEvent = new ClassWithInterfaceWithCLIEvent<_>()
(classWithEvent :> InterfaceWithCLIEvent<_>).Event.AddHandler(fun s x ->
sender <- s
result <- x
)
classWithEvent.TestEvent(5)
equal 5 result
equal (box classWithEvent) sender
[<Fact>]
let ``test Generic interface expression can have CLI events`` () = // See #3039
let mutable actualSender = ""
let mutable result = false
let event = Event<_,_>()
let ifaceWIthEvent =
{ new InterfaceWithCLIEvent<_> with
[<CLIEvent>]
member _.Event = event.Publish }
ifaceWIthEvent.Event.AddHandler(fun sender arg ->
actualSender <- string sender
result <- arg)
let expectedSender = "SENDER"
let expectedResult = true
event.Trigger(expectedSender, expectedResult)
equal expectedSender actualSender
equal expectedResult result
[<Fact>]
let ``test Events are unsubscribed correctly`` () = // See #609
let mutable counter = 0
let test = new Event<_>()
let firstSubscriber =
test.Publish
|> Observable.filter (fun x -> x < 25)
|> Observable.subscribe (fun x -> counter <- counter + x)
let secondSubscriber =
test.Publish
|> Observable.filter (fun x -> x > 25)
|> Observable.subscribe (fun x -> counter <- counter + x)
secondSubscriber.Dispose()
for i in [1..50] do
test.Trigger(i)
equal 300 counter