﻿using System;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace OnionSeed.Extensions
{
	public class AsyncExtensionsTests
	{
		[Fact]
		public void RunSynchronously_ShouldExecuteAsyncMethod()
		{
			// Arrange
			var called = false;
			Func<Task> method = () =>
			{
				called = true;
				return Task.Delay(30);
			};

			// Act
			method.RunSynchronously();

			// Assert
			called.Should().BeTrue();
		}

		[Fact]
		public void RunSynchronously_ShouldExecuteAsyncMethod_AndReturnResult()
		{
			// Arrange
			const int value = 21;
			Func<Task<int>> method = () =>
			{
				return Task.Delay(30)
					.ContinueWith(t => value, TaskScheduler.Current);
			};

			// Act
			var result = method.RunSynchronously();

			// Assert
			result.Should().Be(value);
		}
	}
}
