<commit_before>package flixel.tweens;

import flixel.tweens.FlxTween.TweenCallback;
import flixel.util.FlxTimer;
import massive.munit.Assert;

class FlxTweenTest extends FlxTest
{
	var value:Int;
	
	@Before
	function before()
	{
		value = 0;
	}
	
	@Test
	function testIssue1104()
	{
		FlxTween.tween(this, { value:  1000 }, 1);
		FlxTween.tween(this, { value: -1000 }, 1, { startDelay: 1 });
		
		// check that there is actually some tweening going on
		step(10);
		var sampleValue = value;
		
		step(10);
		Assert.areNotEqual(value, sampleValue);
	}
	
	@Test
	function testPauseLoopingTweenInOnComplete()
	{
		var tweenActive:Bool = false;
		
		var tween = FlxTween.tween(this, { value: 50 }, 0.05, {
			type: FlxTween.LOOPING,
			onComplete: function (tween:FlxTween)
			{
				tween.active = false;
				new FlxTimer().start(0.05, function(_)
				{
					tweenActive = tween.active;
				});
			}
		});
		
		step(10);
		Assert.isFalse(tweenActive);
		
		var resumeSuccesful:Bool = false;
		
		tween.active = true;
		tween.onComplete = function (_)
		{
			resumeSuccesful = true;
		};
		
		step(10);
		Assert.isTrue(resumeSuccesful);
	}
	
	@Test
	function testScaleIsValidWithStartDelay()
	{
		FlxTween.num(0, 1, 0.2, { startDelay: 0.1 }, function (f:Float)
		{
			Assert.isNotNaN(f);
			Assert.isNotNull(f);
		});
		step();
	}
	
	@Test
	function testCancelNoCallback()
	{
		var tween = FlxTween.tween(this, { value: 100 }, 0.01, {
			onComplete: function (_)
				Assert.fail("Callback called")
		});
		tween.cancel();
		step();
	}

	@Test
	function testCancelChainFirstTweenUnfinished()
	{
		var chain = createChain(4);

		while (!chain.updated[0])
			step();
		
		Assert.isFalse(chain.completed[0]);

		chain.tweens[0].cancelChain();

		for (i in 0...chain.count)
			finishTween(chain.tweens[i]);

		for (i in 1...chain.count)
			Assert.isFalse(chain.updated[i] || chain.completed[i]);
	}

	@Test
	function testCancelChainFirstTweenFinished()
	{
		var chain = createChain(4);
		
		while (!chain.updated[1])
			step();
		
		Assert.isTrue(chain.updated[0] && chain.completed[0]);
		Assert.isFalse(chain.completed[1]);
		
		chain.tweens[0].cancelChain();
		
		for (i in 1...chain.count)
			finishTween(chain.tweens[i]);
		
		for (i in 2...chain.count)
			Assert.isFalse(chain.updated[i] || chain.completed[i]);
	}

	function createChain(count:Int)
	{
		var updated = [for (i in 0...count) false];
		var completed = [for (i in 0...count) false];
		var tweens = [for (i in 0...count)
			makeTween(0.1,
				function (_) completed[i] = true,
				function (_) updated[i] = true
		)];

		for (i in 1...count)
			tweens[0].wait(0.1).then(tweens[i]);

		return {
			count: count,
			updated: updated,
			completed: completed,
			tweens: tweens
		}
	}

	@Test
	function testLinearChain()
	{
		testChain(4, function(tweens)
			tweens[0]
				.then(tweens[1])
				.then(tweens[2])
				.then(tweens[3])
		);
	}
<commit_msg>FlxTweenTest: add a test for cancel() + chains<commit_after>package flixel.tweens;

import flixel.tweens.FlxTween.TweenCallback;
import flixel.util.FlxTimer;
import massive.munit.Assert;

class FlxTweenTest extends FlxTest
{
	var value:Int;
	
	@Before
	function before()
	{
		value = 0;
	}
	
	@Test
	function testIssue1104()
	{
		FlxTween.tween(this, { value:  1000 }, 1);
		FlxTween.tween(this, { value: -1000 }, 1, { startDelay: 1 });
		
		// check that there is actually some tweening going on
		step(10);
		var sampleValue = value;
		
		step(10);
		Assert.areNotEqual(value, sampleValue);
	}
	
	@Test
	function testPauseLoopingTweenInOnComplete()
	{
		var tweenActive:Bool = false;
		
		var tween = FlxTween.tween(this, { value: 50 }, 0.05, {
			type: FlxTween.LOOPING,
			onComplete: function (tween:FlxTween)
			{
				tween.active = false;
				new FlxTimer().start(0.05, function(_)
				{
					tweenActive = tween.active;
				});
			}
		});
		
		step(10);
		Assert.isFalse(tweenActive);
		
		var resumeSuccesful:Bool = false;
		
		tween.active = true;
		tween.onComplete = function (_)
		{
			resumeSuccesful = true;
		};
		
		step(10);
		Assert.isTrue(resumeSuccesful);
	}
	
	@Test
	function testScaleIsValidWithStartDelay()
	{
		FlxTween.num(0, 1, 0.2, { startDelay: 0.1 }, function (f:Float)
		{
			Assert.isNotNaN(f);
			Assert.isNotNull(f);
		});
		step();
	}
	
	@Test
	function testCancelNoCallback()
	{
		var tween = FlxTween.tween(this, { value: 100 }, 0.01, {
			onComplete: function (_)
				Assert.fail("Callback called")
		});
		tween.cancel();
		step();
	}

	@Test
	function testCancelChainFirstTweenUnfinished()
	{
		var chain = createChain(4);

		while (!chain.updated[0])
			step();
		
		Assert.isFalse(chain.completed[0]);

		chain.tweens[0].cancelChain();

		for (i in 0...chain.count)
			finishTween(chain.tweens[i]);

		for (i in 1...chain.count)
			Assert.isFalse(chain.updated[i] || chain.completed[i]);
	}

	@Test
	function testCancelChainFirstTweenFinished()
	{
		var chain = createChain(4);
		
		while (!chain.updated[1])
			step();
		
		Assert.isTrue(chain.updated[0] && chain.completed[0]);
		Assert.isFalse(chain.completed[1]);
		
		chain.tweens[0].cancelChain();
		
		for (i in 1...chain.count)
			finishTween(chain.tweens[i]);
		
		for (i in 2...chain.count)
			Assert.isFalse(chain.updated[i] || chain.completed[i]);
	}

	function createChain(count:Int)
	{
		var updated = [for (i in 0...count) false];
		var completed = [for (i in 0...count) false];
		var tweens = [for (i in 0...count)
			makeTween(0.1,
				function (_) completed[i] = true,
				function (_) updated[i] = true
		)];

		for (i in 1...count)
			tweens[0].wait(0.1).then(tweens[i]);

		return {
			count: count,
			updated: updated,
			completed: completed,
			tweens: tweens
		}
	}

	@Test
	function testCancelYieldToChain()
	{
		var completed1 = false;
		var tween1 = makeTween(0.1, function(_) completed1 = true);

		var completed2 = false;
		var tween2 = makeTween(0.1, function(_) completed2 = true);

		tween1.then(tween2);
		tween1.cancel();
		finishTween(tween2);

		Assert.isFalse(completed1);
		Assert.isTrue(completed2);
	}

	@Test
	function testLinearChain()
	{
		testChain(4, function(tweens)
			tweens[0]
				.then(tweens[1])
				.then(tweens[2])
				.then(tweens[3])
		);
	}

{'fraction_non_alphanumeric': 0.15357315696080012, 'fraction_numerical': 0.025810614615260526, 'mean_word_length': 3.323570432357043, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2264162', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2365, 'n_words': 454}