forked from robotlegs/robotlegs-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventCommandMapTest.as
593 lines (515 loc) · 20.8 KB
/
EventCommandMapTest.as
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//------------------------------------------------------------------------------
// Copyright (c) 2009-2013 the original author or authors. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//------------------------------------------------------------------------------
package robotlegs.bender.extensions.eventCommandMap.impl
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import org.hamcrest.assertThat;
import org.hamcrest.collection.array;
import org.hamcrest.core.not;
import org.hamcrest.object.equalTo;
import org.hamcrest.object.instanceOf;
import org.swiftsuspenders.Injector;
import robotlegs.bender.extensions.commandCenter.dsl.ICommandMapper;
import robotlegs.bender.extensions.commandCenter.dsl.ICommandUnmapper;
import robotlegs.bender.extensions.commandCenter.support.CallbackCommand;
import robotlegs.bender.extensions.commandCenter.support.CallbackCommand2;
import robotlegs.bender.extensions.commandCenter.support.ClassReportingCallbackCommand;
import robotlegs.bender.extensions.commandCenter.support.ClassReportingCallbackCommand2;
import robotlegs.bender.extensions.commandCenter.support.ClassReportingCallbackGuard;
import robotlegs.bender.extensions.commandCenter.support.ClassReportingCallbackGuard2;
import robotlegs.bender.extensions.commandCenter.support.ClassReportingCallbackHook;
import robotlegs.bender.extensions.commandCenter.support.NullCommand;
import robotlegs.bender.extensions.eventCommandMap.api.IEventCommandMap;
import robotlegs.bender.extensions.eventCommandMap.support.CascadingCommand;
import robotlegs.bender.extensions.eventCommandMap.support.EventInjectedCallbackCommand;
import robotlegs.bender.extensions.eventCommandMap.support.EventInjectedCallbackGuard;
import robotlegs.bender.extensions.eventCommandMap.support.SupportEvent;
import robotlegs.bender.framework.api.IContext;
import robotlegs.bender.framework.impl.Context;
import robotlegs.bender.framework.impl.guardSupport.GrumpyGuard;
import robotlegs.bender.framework.impl.guardSupport.HappyGuard;
public class EventCommandMapTest
{
/*============================================================================*/
/* Private Properties */
/*============================================================================*/
private var subject:IEventCommandMap;
private var mapper:ICommandMapper;
private var reportedExecutions:Array;
private var injector:Injector;
private var dispatcher:IEventDispatcher;
/*============================================================================*/
/* Test Setup and Teardown */
/*============================================================================*/
[Before]
public function before():void
{
reportedExecutions = [];
const context:IContext = new Context();
injector = context.injector;
injector.map(Function, "reportingFunction").toValue(reportingFunction);
dispatcher = new EventDispatcher();
subject = new EventCommandMap(context, dispatcher);
}
/*============================================================================*/
/* Tests */
/*============================================================================*/
[Test]
public function map_creates_mapper():void
{
assertThat(subject.map(SupportEvent.TYPE1, SupportEvent), instanceOf(ICommandMapper));
}
[Test]
public function map_to_identical_Type_but_different_Event_returns_different_mapper():void
{
mapper = subject.map(SupportEvent.TYPE1, SupportEvent);
assertThat(subject.map(SupportEvent.TYPE1, Event), not(equalTo(mapper)));
}
[Test]
public function map_to_different_Type_but_identical_Event_returns_different_mapper():void
{
mapper = subject.map(SupportEvent.TYPE1, SupportEvent);
assertThat(subject.map(SupportEvent.TYPE2, SupportEvent), not(equalTo(mapper)));
}
[Test]
public function unmap_returns_mapper():void
{
mapper = subject.map(SupportEvent.TYPE1, SupportEvent);
assertThat(subject.unmap(SupportEvent.TYPE1, SupportEvent), instanceOf(ICommandUnmapper));
}
[Test]
public function robust_to_unmapping_non_existent_mappings():void
{
subject.unmap(SupportEvent.TYPE1).fromCommand(NullCommand);
// note: no assertion, just testing for the lack of an NPE
}
[Test]
public function command_executes_successfully():void
{
assertThat(commandExecutionCount(1), equalTo(1));
}
[Test]
public function command_executes_repeatedly():void
{
assertThat(commandExecutionCount(5), equalTo(5));
}
[Test]
public function fireOnce_command_executes_once():void
{
assertThat(oneshotCommandExecutionCount(5), equalTo(1));
}
[Test]
public function event_is_injected_into_command():void
{
var injectedEvent:Event = null;
injector.map(Function, 'executeCallback').toValue(function(command:EventInjectedCallbackCommand):void
{
injectedEvent = command.event;
});
subject.map(SupportEvent.TYPE1, Event)
.toCommand(EventInjectedCallbackCommand);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}
[Test]
public function event_is_passed_to_execute_method():void
{
var actualEvent:SupportEvent;
injector.map(Function, 'executeCallback').toValue(function(event:SupportEvent):void
{
actualEvent = event;
});
subject.map(SupportEvent.TYPE1, SupportEvent)
.toCommand(EventParametersCommand);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
assertThat(actualEvent, equalTo(event));
}
[Test]
public function concretely_specified_typed_event_is_injected_into_command_as_typed_event():void
{
var injectedEvent:SupportEvent = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
{
injectedEvent = command.typedEvent;
});
subject.map(SupportEvent.TYPE1, SupportEvent)
.toCommand(SupportEventTriggeredSelfReportingCallbackCommand);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}
[Test]
public function abstractly_specified_typed_event_is_injected_into_command_as_untyped_event():void
{
var injectedEvent:Event = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
{
injectedEvent = command.untypedEvent;
});
subject.map(SupportEvent.TYPE1, Event)
.toCommand(SupportEventTriggeredSelfReportingCallbackCommand);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}
[Test]
public function unspecified_typed_event_is_injected_into_command_as_typed_event():void
{
var injectedEvent:SupportEvent = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
{
injectedEvent = command.typedEvent;
});
subject.map(SupportEvent.TYPE1)
.toCommand(SupportEventTriggeredSelfReportingCallbackCommand);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}
[Test]
public function unspecified_untyped_event_is_injected_into_command_as_untyped_event():void
{
const eventType:String = 'eventType';
var injectedEvent:Event = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
{
injectedEvent = command.untypedEvent;
});
subject.map(eventType)
.toCommand(SupportEventTriggeredSelfReportingCallbackCommand);
const event:Event = new Event(eventType);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}
[Test]
public function specified_untyped_event_is_injected_into_command_as_untyped_event():void
{
const eventType:String = 'eventType';
var injectedEvent:Event = null;
injector.map(Function, 'executeCallback').toValue(function(command:SupportEventTriggeredSelfReportingCallbackCommand):void
{
injectedEvent = command.untypedEvent;
});
subject.map(eventType, Event)
.toCommand(SupportEventTriggeredSelfReportingCallbackCommand);
const event:Event = new Event(eventType);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}
[Test]
public function command_does_not_execute_when_incorrect_eventType_dispatched():void
{
var executeCount:uint = 0;
injector.map(Function, 'executeCallback').toValue(function():void
{
executeCount++;
});
subject.map(SupportEvent.TYPE1).toCommand(CallbackCommand);
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE2));
assertThat(executeCount, equalTo(0));
}
[Test]
public function command_does_not_execute_when_incorrect_eventClass_dispatched():void
{
var executeCount:uint = 0;
injector.map(Function, 'executeCallback').toValue(function():void
{
executeCount++;
});
subject.map(SupportEvent.TYPE1, SupportEvent).toCommand(CallbackCommand);
dispatcher.dispatchEvent(new Event(SupportEvent.TYPE1));
assertThat(executeCount, equalTo(0));
}
[Test]
public function command_does_not_execute_after_event_unmapped():void
{
var executeCount:uint = 0;
injector.map(Function, 'executeCallback').toValue(function():void
{
executeCount++;
});
subject.map(SupportEvent.TYPE1, SupportEvent).toCommand(CallbackCommand);
subject.unmap(SupportEvent.TYPE1, SupportEvent).fromCommand(CallbackCommand);
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
assertThat(executeCount, equalTo(0));
}
[Test]
public function oneshot_mappings_should_not_bork_stacked_mappings():void
{
var executeCount:uint = 0;
injector.map(Function, 'executeCallback').toValue(function():void
{
executeCount++;
});
subject.map(SupportEvent.TYPE1, SupportEvent).toCommand(CallbackCommand).once();
subject.map(SupportEvent.TYPE1, SupportEvent).toCommand(CallbackCommand2).once();
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
assertThat(executeCount, equalTo(2));
}
[Test]
public function one_shot_command_should_not_cause_infinite_loop_when_dispatching_to_self():void
{
injector.map(Function, 'executeCallback').toValue(function():void
{
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
});
subject.map(SupportEvent.TYPE1, null).toCommand(CallbackCommand).once();
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
// note: no assertion. we just want to know if an error is thrown
}
[Test]
public function commands_should_not_stomp_over_event_mappings():void
{
injector.map(Function, 'executeCallback').toValue(function():void
{
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE2));
});
subject.map(SupportEvent.TYPE1).toCommand(CallbackCommand);
subject.map(SupportEvent.TYPE2, null).toCommand(CallbackCommand).once();
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
// note: no assertion. we just want to know if an error is thrown
}
[Test]
public function commands_are_executed_in_order():void
{
subject.map(SupportEvent.TYPE1).toCommand(ClassReportingCallbackCommand);
subject.map(SupportEvent.TYPE1).toCommand(ClassReportingCallbackCommand2);
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
assertThat(reportedExecutions, array(ClassReportingCallbackCommand, ClassReportingCallbackCommand2));
}
[Test]
public function hooks_are_called():void
{
assertThat(hookCallCount(ClassReportingCallbackHook, ClassReportingCallbackHook), equalTo(2));
}
[Test]
public function command_executes_when_the_guard_allows():void
{
assertThat(commandExecutionCountWithGuards(HappyGuard), equalTo(1));
}
[Test]
public function command_executes_when_all_guards_allow():void
{
assertThat(commandExecutionCountWithGuards(HappyGuard, HappyGuard), equalTo(1));
}
[Test]
public function command_does_not_execute_when_the_guard_denies():void
{
assertThat(commandExecutionCountWithGuards(GrumpyGuard), equalTo(0));
}
[Test]
public function command_does_not_execute_when_any_guards_denies():void
{
assertThat(commandExecutionCountWithGuards(HappyGuard, GrumpyGuard), equalTo(0));
}
[Test]
public function command_does_not_execute_when_all_guards_deny():void
{
assertThat(commandExecutionCountWithGuards(GrumpyGuard, GrumpyGuard), equalTo(0));
}
[Test]
public function event_is_injected_into_guard():void
{
var injectedEvent:Event = null;
injector.map(Function, 'approveCallback').toValue(function(guard:EventInjectedCallbackGuard):void
{
injectedEvent = guard.event;
});
subject
.map(SupportEvent.TYPE1, Event)
.toCommand(NullCommand)
.withGuards(EventInjectedCallbackGuard);
const event:SupportEvent = new SupportEvent(SupportEvent.TYPE1);
dispatcher.dispatchEvent(event);
assertThat(injectedEvent, equalTo(event));
}
[Test]
public function cascading_events_do_not_throw_unmap_errors():void
{
injector.map(IEventDispatcher).toValue(dispatcher);
injector.map(IEventCommandMap).toValue(subject);
subject
.map(CascadingCommand.EVENT_TYPE)
.toCommand(CascadingCommand).once();
dispatcher.dispatchEvent(new Event(CascadingCommand.EVENT_TYPE));
}
[Test]
public function execution_sequence_is_guard_command_guard_command_for_multiple_mappings_to_same_event():void
{
subject.map(SupportEvent.TYPE1).toCommand(ClassReportingCallbackCommand).withGuards(ClassReportingCallbackGuard);
subject.map(SupportEvent.TYPE1).toCommand(ClassReportingCallbackCommand2).withGuards(ClassReportingCallbackGuard2);
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
const expectedOrder:Array = [ClassReportingCallbackGuard, ClassReportingCallbackCommand, ClassReportingCallbackGuard2, ClassReportingCallbackCommand2];
assertThat(reportedExecutions, array(expectedOrder));
}
[Test]
public function previously_constructed_command_does_not_slip_through_the_loop():void
{
subject.map(SupportEvent.TYPE1).toCommand(ClassReportingCallbackCommand).withGuards(HappyGuard);
subject.map(SupportEvent.TYPE1).toCommand(ClassReportingCallbackCommand2).withGuards(GrumpyGuard);
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
const expectedOrder:Array = [ClassReportingCallbackCommand];
assertThat(reportedExecutions, array(expectedOrder));
}
[Test]
public function commands_mapped_during_execution_are_not_executed():void
{
injector.map(IEventCommandMap).toValue(subject);
injector.map(Class, 'nestedCommand').toValue(ClassReportingCallbackCommand);
subject.map(SupportEvent.TYPE1, Event)
.toCommand(CommandMappingCommand).once();
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
assertThat(reportedExecutions, array());
}
[Test]
public function commands_unmapped_during_execution_are_still_executed():void
{
injector.map(IEventCommandMap).toValue(subject);
injector.map(Class, 'nestedCommand').toValue(ClassReportingCallbackCommand);
subject.map(SupportEvent.TYPE1, Event)
.toCommand(CommandUnmappingCommand).once();
subject.map(SupportEvent.TYPE1, Event)
.toCommand(ClassReportingCallbackCommand).once();
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
assertThat(reportedExecutions, array(ClassReportingCallbackCommand));
}
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
private function commandExecutionCount(totalEvents:int = 1, oneshot:Boolean = false):uint
{
var executeCount:uint = 0;
injector.map(Function, 'executeCallback').toValue(function():void
{
executeCount++;
});
subject.map(SupportEvent.TYPE1, SupportEvent).toCommand(CallbackCommand).once(oneshot);
while (totalEvents--)
{
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
}
return executeCount;
}
private function oneshotCommandExecutionCount(totalEvents:int = 1):uint
{
return commandExecutionCount(totalEvents, true);
}
private function hookCallCount(... hooks):uint
{
var hookCallCount:uint = 0;
injector.unmap(Function, 'reportingFunction');
injector.map(Function, 'reportingFunction').toValue(function(hookClas:Class):void {
hookCallCount++;
});
subject
.map(SupportEvent.TYPE1)
.toCommand(NullCommand)
.withHooks(hooks);
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
return hookCallCount;
}
private function commandExecutionCountWithGuards(... guards):uint
{
var executionCount:uint = 0;
injector.map(Function, 'executeCallback').toValue(function():void
{
executionCount++;
});
subject
.map(SupportEvent.TYPE1)
.toCommand(CallbackCommand)
.withGuards(guards);
dispatcher.dispatchEvent(new SupportEvent(SupportEvent.TYPE1));
return executionCount;
}
private function reportingFunction(item:Object):void
{
reportedExecutions.push(item);
}
}
}
import flash.events.Event;
import robotlegs.bender.extensions.commandCenter.support.ClassReportingCallbackHook;
import robotlegs.bender.extensions.commandCenter.support.NullCommand;
import robotlegs.bender.extensions.eventCommandMap.api.IEventCommandMap;
import robotlegs.bender.extensions.eventCommandMap.support.SupportEvent;
class SupportEventTriggeredSelfReportingCallbackCommand
{
/*============================================================================*/
/* Public Properties */
/*============================================================================*/
[Inject(optional=true)]
public var untypedEvent:Event;
[Inject(optional=true)]
public var typedEvent:SupportEvent;
[Inject(name="executeCallback")]
public var callback:Function;
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
public function execute():void
{
callback(this);
}
}
class EventParametersCommand
{
/*============================================================================*/
/* Public Properties */
/*============================================================================*/
[Inject(name="executeCallback")]
public var callback:Function;
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
public function execute(event:SupportEvent):void
{
callback(event);
}
}
class CommandMappingCommand
{
/*============================================================================*/
/* Public Properties */
/*============================================================================*/
[Inject]
public var event:Event;
[Inject(name="nestedCommand")]
public var commandClass:Class;
[Inject]
public var eventCommandMap:IEventCommandMap;
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
public function execute():void
{
eventCommandMap.map(event.type, Event).toCommand(commandClass);
}
}
class CommandUnmappingCommand
{
/*============================================================================*/
/* Public Properties */
/*============================================================================*/
[Inject]
public var event:Event;
[Inject(name="nestedCommand")]
public var commandClass:Class;
[Inject]
public var eventCommandMap:IEventCommandMap;
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
public function execute():void
{
eventCommandMap.unmap(event.type, Event).fromCommand(commandClass);
}
}