<commit_before>//------------------------------------------------------------------------------
//  Copyright (c) 2011 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.mediatorMap.impl
{
	import flash.display.Sprite;
	import mx.core.UIComponent;
	import org.flexunit.assertThat;
	import org.flexunit.async.Async;
	import org.fluint.uiImpersonation.UIImpersonator;
	import org.hamcrest.collection.array;
	import org.hamcrest.object.equalTo;
	import org.hamcrest.object.instanceOf;
	import org.hamcrest.object.isFalse;
	import org.hamcrest.object.isTrue;
	import org.hamcrest.object.nullValue;
	import org.swiftsuspenders.Injector;
	import robotlegs.bender.extensions.mediatorMap.api.IMediatorMapping;
	import robotlegs.bender.extensions.mediatorMap.support.CallbackMediator;
	import utils.checkFlex;
	import utils.traceAndSkipTest;
	import robotlegs.bender.extensions.matching.ITypeFilter;
	import robotlegs.bender.extensions.matching.TypeMatcher;

	public class DefaultMediatorManagerTest
	{

		/*============================================================================*/
		/* Private Properties                                                         */
		/*============================================================================*/

		private var injector:Injector;

		private var factory:MediatorFactory;

		private var manager:DefaultMediatorManager;

		private var container:UIComponent;

		/*============================================================================*/
		/* Test Setup and Teardown                                                    */
		/*============================================================================*/

		[Before(async, ui)]
		public function before():void
		{
			injector = new Injector();
			factory = new MediatorFactory(injector);
			manager = new DefaultMediatorManager(factory);
			container = new UIComponent();
			UIImpersonator.addChild(container);
		}

		/*============================================================================*/
		/* Tests                                                                      */
		/*============================================================================*/

		[Test(async, ui)]
		public function mediator_is_removed_when_view_leaves_stage():void
		{
			const view:Sprite = new Sprite();
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Sprite]), CallbackMediator);
			container.addChild(view);
			factory.createMediators(view, Sprite, [mapping]);
			container.removeChild(view);
			assertThat(factory.getMediator(view, mapping), nullValue());
		}

		[Test]
		public function mediator_is_initialized():void
		{
			const expected:Array = ['initialize'];
			const actual:Array = [];
			injector.map(Function, 'initializeCallback').toValue(function(phase:String):void {
				actual.push(phase);
			});
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Sprite]), SomeMediator);
			factory.createMediators(new Sprite(), Sprite, [mapping]);
			assertThat(actual, array(expected));
		}

		[Test]
		public function mediator_is_given_view():void
		{
			const expected:Sprite = new Sprite();
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Sprite]), SomeMediator);
			const mediator:SomeMediator = factory.createMediators(expected, Sprite, [mapping])[0];
			assertThat(mediator.view, equalTo(expected));
		}

		[Test]
		public function mediator_is_destroyed():void
		{
			const expected:Array = ['destroy'];
			const actual:Array = [];
			const view:Sprite = new Sprite();
			injector.map(Function, 'destroyCallback').toValue(function(phase:String):void {
				actual.push(phase);
			});
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Sprite]), SomeMediator);
			factory.createMediators(view, Sprite, [mapping])
			factory.removeMediators(view);
			assertThat(actual, array(expected));
		}

		[Test(async, ui)]
		public function mediator_for_UIComponent_is_only_initialized_after_creationComplete():void
		{
			// early exit for as3 only test environments
			if(!checkFlex()) 
				return traceAndSkipTest('mediator_for_UIComponent_is_only_initialized_after_creationComplete - flex not available');
			
			const view:UIComponent = new UIComponent();
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([UIComponent]), SomeMediator);
			const mediator:SomeMediator = factory.createMediators(view, Sprite, [mapping])[0];
			assertThat(mediator.initialized, isFalse());
			container.addChild(view);
			delayAssertion(function():void {
				assertThat(mediator.initialized, isTrue());
			}, 50)
		}

		/*============================================================================*/
		/* Private Functions                                                          */
		/*============================================================================*/

		private function delayAssertion(closure:Function, delay:Number = 50):void
		{
			Async.delayCall(this, closure, delay);
		}
		<commit_msg>why u no test drive your bugfix, lazy villain<commit_after>//------------------------------------------------------------------------------
//  Copyright (c) 2011 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.mediatorMap.impl
{
	import flash.display.Sprite;
	import mx.core.UIComponent;
	import org.flexunit.assertThat;
	import org.flexunit.async.Async;
	import org.fluint.uiImpersonation.UIImpersonator;
	import org.hamcrest.collection.array;
	import org.hamcrest.object.equalTo;
	import org.hamcrest.object.instanceOf;
	import org.hamcrest.object.isFalse;
	import org.hamcrest.object.isTrue;
	import org.hamcrest.object.nullValue;
	import org.swiftsuspenders.Injector;
	import robotlegs.bender.extensions.mediatorMap.api.IMediatorMapping;
	import robotlegs.bender.extensions.mediatorMap.support.CallbackMediator;
	import utils.checkFlex;
	import utils.traceAndSkipTest;
	import robotlegs.bender.extensions.matching.ITypeFilter;
	import robotlegs.bender.extensions.matching.TypeMatcher;

	public class DefaultMediatorManagerTest
	{

		/*============================================================================*/
		/* Private Properties                                                         */
		/*============================================================================*/

		private var injector:Injector;

		private var factory:MediatorFactory;

		private var manager:DefaultMediatorManager;

		private var container:UIComponent;

		/*============================================================================*/
		/* Test Setup and Teardown                                                    */
		/*============================================================================*/

		[Before(async, ui)]
		public function before():void
		{
			injector = new Injector();
			factory = new MediatorFactory(injector);
			manager = new DefaultMediatorManager(factory);
			container = new UIComponent();
			UIImpersonator.addChild(container);
		}

		/*============================================================================*/
		/* Tests                                                                      */
		/*============================================================================*/

		[Test(async, ui)]
		public function mediator_is_removed_when_view_leaves_stage():void
		{
			const view:Sprite = new Sprite();
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Sprite]), CallbackMediator);
			container.addChild(view);
			factory.createMediators(view, Sprite, [mapping]);
			container.removeChild(view);
			assertThat(factory.getMediator(view, mapping), nullValue());
		}

		[Test]
		public function mediator_is_initialized():void
		{
			const expected:Array = ['initialize'];
			const actual:Array = [];
			injector.map(Function, 'initializeCallback').toValue(function(phase:String):void {
				actual.push(phase);
			});
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Sprite]), SomeMediator);
			factory.createMediators(new Sprite(), Sprite, [mapping]);
			assertThat(actual, array(expected));
		}

		[Test]
		public function mediator_is_given_view():void
		{
			const expected:Sprite = new Sprite();
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Sprite]), SomeMediator);
			const mediator:SomeMediator = factory.createMediators(expected, Sprite, [mapping])[0];
			assertThat(mediator.view, equalTo(expected));
		}

		[Test]
		public function mediator_is_destroyed():void
		{
			const expected:Array = ['destroy'];
			const actual:Array = [];
			const view:Sprite = new Sprite();
			injector.map(Function, 'destroyCallback').toValue(function(phase:String):void {
				actual.push(phase);
			});
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Sprite]), SomeMediator);
			factory.createMediators(view, Sprite, [mapping]);
			factory.removeMediators(view);
			assertThat(actual, array(expected));
		}

		[Test(async, ui)]
		public function mediator_for_UIComponent_is_only_initialized_after_creationComplete():void
		{
			// early exit for as3 only test environments
			if(!checkFlex()) 
				return traceAndSkipTest('mediator_for_UIComponent_is_only_initialized_after_creationComplete - flex not available');
			
			const view:UIComponent = new UIComponent();
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([UIComponent]), SomeMediator);
			const mediator:SomeMediator = factory.createMediators(view, Sprite, [mapping])[0];
			assertThat(mediator.initialized, isFalse());
			container.addChild(view);
			delayAssertion(function():void {
				assertThat(mediator.initialized, isTrue());
			}, 50)
		}

		[Test]
		public function mediator_is_given_NonDisplayObject_view():void
		{
			const expected:Number = 1.5;
			const mapping:IMediatorMapping = new MediatorMapping(createTypeFilter([Number]), SomeMediator);
			const mediator:SomeMediator = factory.createMediators(expected, Number, [mapping])[0];
			assertThat(mediator.view, equalTo(expected));
		}

		/*============================================================================*/
		/* Private Functions                                                          */
		/*============================================================================*/

		private function delayAssertion(closure:Function, delay:Number = 50):void
		{
			Async.delayCall(this, closure, delay);
		}
		
{'fraction_non_alphanumeric': 0.2358214416392243, 'fraction_numerical': 0.0022868642517380167, 'mean_word_length': 3.826931567328918, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5328645', 'n_tokens_mistral': 3181, 'n_tokens_neox': 2772, 'n_words': 533}