/test/org/robotlegs/v2/extensions/hooks/HookMapTest.as

https://github.com/ZackPierce/robotlegs-framework · ActionScript · 173 lines · 125 code · 25 blank · 23 comment · 1 complexity · 2647a7654c8fb6c75519e249820cf36d MD5 · raw file

  1. //------------------------------------------------------------------------------
  2. // Copyright (c) 2011 the original author or authors. All Rights Reserved.
  3. //
  4. // NOTICE: You are permitted to use, modify, and distribute this file
  5. // in accordance with the terms of the license agreement accompanying it.
  6. //------------------------------------------------------------------------------
  7. package org.robotlegs.v2.extensions.hooks
  8. {
  9. import flash.display.DisplayObject;
  10. import flash.display.MovieClip;
  11. import flash.display.Sprite;
  12. import flash.utils.describeType;
  13. import org.flexunit.asserts.*;
  14. import org.flexunit.asserts.assertEqualsVectorsIgnoringOrder;
  15. import org.robotlegs.v2.core.impl.TypeMatcher;
  16. import org.robotlegs.v2.extensions.guards.GuardsProcessor;
  17. import org.robotlegs.v2.extensions.guards.support.GrumpyGuard;
  18. import org.robotlegs.v2.extensions.guards.support.HappyGuard;
  19. import org.robotlegs.v2.extensions.hooks.support.*
  20. import org.swiftsuspenders.DescribeTypeJSONReflector;
  21. import org.swiftsuspenders.Injector;
  22. import org.swiftsuspenders.Reflector;
  23. public class HookMapTest
  24. {
  25. /*============================================================================*/
  26. /* Private Properties */
  27. /*============================================================================*/
  28. private var hookTracker:HookTracker;
  29. private var injector:Injector;
  30. private var instance:HookMap;
  31. /*============================================================================*/
  32. /* Test Setup and Teardown */
  33. /*============================================================================*/
  34. [Before]
  35. public function setUp():void
  36. {
  37. instance = new HookMap();
  38. injector = new Injector();
  39. hookTracker = new HookTracker();
  40. instance.injector = injector;
  41. instance.reflector = new DescribeTypeJSONReflector();
  42. instance.hooksProcessor = new HooksProcessor();
  43. instance.guardsProcessor = new GuardsProcessor();
  44. injector.map(HookTracker).toValue(hookTracker);
  45. }
  46. [After]
  47. public function tearDown():void
  48. {
  49. instance = null;
  50. injector = null;
  51. }
  52. /*============================================================================*/
  53. /* Tests */
  54. /*============================================================================*/
  55. [Test]
  56. public function a_grumpy_guard_prevents_the_hook_from_running():void
  57. {
  58. instance.mapMatcher(new TypeMatcher().allOf(DisplayObject)).withHooks(TrackableHook1, TrackableHook2).withGuards(HappyGuard, GrumpyGuard);
  59. instance.process(new Sprite());
  60. var expectedHooksConfirmed:Vector.<String> = new <String>[];
  61. assertEqualsVectorsIgnoringOrder('no hooks run when guards prevent it', expectedHooksConfirmed, hookTracker.hooksConfirmed);
  62. }
  63. [Test]
  64. public function a_hook_is_run_after_mapping_with_injections():void
  65. {
  66. instance.map(ExampleTarget).withHooks(TrackableHook1);
  67. instance.process(new ExampleTarget());
  68. var expectedHooksConfirmed:Vector.<String> = new <String>['TrackableHook1'];
  69. assertEqualsVectorsIgnoringOrder('hook ran in response to trigger class', expectedHooksConfirmed, hookTracker.hooksConfirmed);
  70. }
  71. [Test]
  72. public function a_hook_not_run_after_mapping_if_the_item_is_a_subclass():void
  73. {
  74. instance.map(DisplayObject).withHooks(TrackableHook1);
  75. instance.process(new Sprite());
  76. var expectedHooksConfirmed:Vector.<String> = new <String>[];
  77. assertEqualsVectorsIgnoringOrder(expectedHooksConfirmed, hookTracker.hooksConfirmed);
  78. }
  79. [Test]
  80. public function all_happy_guards_allow_the_hook_to_run():void
  81. {
  82. instance.mapMatcher(new TypeMatcher().allOf(DisplayObject)).withHooks(TrackableHook1, TrackableHook2).withGuards(HappyGuard);
  83. instance.process(new Sprite());
  84. var expectedHooksConfirmed:Vector.<String> = new <String>['TrackableHook1', 'TrackableHook2'];
  85. assertEqualsVectorsIgnoringOrder('both hooks have run when the guards approved of it', expectedHooksConfirmed, hookTracker.hooksConfirmed);
  86. }
  87. [Test]
  88. public function can_be_instantiated():void
  89. {
  90. assertTrue("instance is HookMap", instance is HookMap);
  91. }
  92. [Test]
  93. public function multiple_hooks_run_after_mapping():void
  94. {
  95. instance.map(ExampleTarget).withHooks(TrackableHook1, TrackableHook2);
  96. instance.process(new ExampleTarget());
  97. var expectedHooksConfirmed:Vector.<String> = new <String>['TrackableHook1', 'TrackableHook2'];
  98. assertEqualsVectorsIgnoringOrder('both hooks have run in response to trigger class', expectedHooksConfirmed, hookTracker.hooksConfirmed);
  99. }
  100. [Test]
  101. public function no_hooks_run_for_unmatched_object():void
  102. {
  103. instance.mapMatcher(new TypeMatcher().allOf(MovieClip)).withHooks(TrackableHook1, TrackableHook2);
  104. instance.process(new Sprite());
  105. var expectedHooksConfirmed:Vector.<String> = new <String>[];
  106. assertEqualsVectorsIgnoringOrder('no hooks run for unmatched object', expectedHooksConfirmed, hookTracker.hooksConfirmed);
  107. }
  108. [Test]
  109. public function returns_false_if_not_interested_even_if_no_guards():void
  110. {
  111. instance.mapMatcher(new TypeMatcher().allOf(MovieClip)).withHooks(TrackableHook1, TrackableHook2);
  112. assertFalse(instance.process(new Sprite()));
  113. }
  114. [Test]
  115. public function returns_true_if_interested_even_if_guards_block_running():void
  116. {
  117. instance.mapMatcher(new TypeMatcher().allOf(DisplayObject)).withHooks(TrackableHook1, TrackableHook2).withGuards(HappyGuard, GrumpyGuard);
  118. assertTrue(instance.process(new Sprite()));
  119. }
  120. [Test]
  121. public function runs_hooks_against_matched_matcher():void
  122. {
  123. instance.mapMatcher(new TypeMatcher().allOf(DisplayObject)).withHooks(TrackableHook1, TrackableHook2);
  124. instance.process(new Sprite());
  125. var expectedHooksConfirmed:Vector.<String> = new <String>['TrackableHook1', 'TrackableHook2'];
  126. assertEqualsVectorsIgnoringOrder('both hooks have run in response to class matching the matcher', expectedHooksConfirmed, hookTracker.hooksConfirmed);
  127. }
  128. [Test]
  129. public function test_failure_seen():void
  130. {
  131. assertTrue("Failing test", true);
  132. }
  133. // unmapping_the_hook_prevents_it_from_running
  134. // overmapping behaviour
  135. /*============================================================================*/
  136. /* Protected Functions */
  137. /*============================================================================*/
  138. }
  139. }
  140. class ExampleTarget
  141. {
  142. /*============================================================================*/
  143. /* Constructor */
  144. /*============================================================================*/
  145. public function ExampleTarget()
  146. {
  147. }
  148. }