Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Revert "Revert AsyncValueCommandSupport"
Browse files Browse the repository at this point in the history
This reverts commit 07a178c.
  • Loading branch information
maxkoshevoi committed Jan 26, 2021
1 parent 07a178c commit 7c82bc8
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Threading.Tasks;
using Xamarin.CommunityToolkit.Exceptions;
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.CommunityToolkit.UnitTests.ObjectModel.ICommandTests.AsyncValueCommandTests;
using Xunit;

namespace Xamarin.CommunityToolkit.UnitTests.ObjectModel.ICommandTests.CommandFactoryTests
{
public class CommandFactoryAsyncValueCommandTests : BaseAsyncValueCommandTests
{

[Fact]
public void NullExecuteParameter()
{
// Arrange
Func<ValueTask> execute = null;

// Act

// Assert
Assert.Throws<ArgumentNullException>(() => CommandFactory.Create(execute));
}

[Fact]
public void NullCanExecuteParameter()
{
// Arrange
var command = CommandFactory.Create(NoParameterTask, null);

// Act

// Assert
Assert.True(command.CanExecute(null));
Assert.True(command.CanExecute(string.Empty));
Assert.True(command.CanExecute(0));
}

[Fact]
public void IntExecuteNullCanExecuteParameter()
{
// Arrange
var command = CommandFactory.Create<int>(IntParameterTask, null);

// Act

// Assert
Assert.True(command.CanExecute(null));
Assert.True(command.CanExecute(string.Empty));
Assert.True(command.CanExecute(0));

command.Execute(0);
Assert.Throws<InvalidCommandParameterException>(() => command.Execute(null));
Assert.Throws<InvalidCommandParameterException>(() => command.Execute(string.Empty));
}

[Fact]
public void IntExecuteTrueCanExecuteParameter()
{
// Arrange
var command = CommandFactory.Create<int>(IntParameterTask, CanExecuteTrue);

// Act

// Assert
Assert.True(command.CanExecute(null));
Assert.True(command.CanExecute(string.Empty));
Assert.True(command.CanExecute(0));
}

[Fact]
public void IntExecuteFalseCanExecuteParameter()
{
// Arrange
var command = CommandFactory.Create<int>(IntParameterTask, CanExecuteFalse);

// Act

// Assert
Assert.False(command.CanExecute(null));
Assert.False(command.CanExecute(string.Empty));
Assert.False(command.CanExecute(0));
}

[Fact]
public void IntExecuteNullTypedCanExecuteParameter()
{
// Arrange
var command = CommandFactory.Create<int, bool>(IntParameterTask, null);

// Act

// Assert
Assert.True(command.CanExecute(true));
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(null));
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(string.Empty));
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(0));
}

[Fact]
public void IntExecuteBoolCanExecuteParameter()
{
// Arrange
var command = CommandFactory.Create<int, bool>(IntParameterTask, CanExecuteTrue);

// Act

// Assert
Assert.True(command.CanExecute(true));
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(null));
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(string.Empty));
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(0));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public static IAsyncCommand<TExecute, TCanExecute> Create<TExecute, TCanExecute>
bool allowsMultipleExecutions = true) =>
new AsyncCommand<TExecute, TCanExecute>(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions);

public static IAsyncValueCommand Create(
Func<ValueTask> execute,
Func<bool> canExecute = null,
Action<Exception> onException = null,
bool continueOnCapturedContext = false,
bool allowsMultipleExecutions = true) =>
new AsyncValueCommand(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions);

public static IAsyncValueCommand<TExecute> Create<TExecute>(
Func<TExecute, ValueTask> execute,
Func<bool> canExecute = null,
Action<Exception> onException = null,
bool continueOnCapturedContext = false,
bool allowsMultipleExecutions = true) =>
new AsyncValueCommand<TExecute>(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions);

public static IAsyncValueCommand<TExecute, TCanExecute> Create<TExecute, TCanExecute>(
Func<TExecute, ValueTask> execute,
Func<TCanExecute, bool> canExecute = null,
Action<Exception> onException = null,
bool continueOnCapturedContext = false,
bool allowsMultipleExecutions = true) =>
new AsyncValueCommand<TExecute, TCanExecute>(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions);

static Action<object> ConvertExecute<T>(Action<T> execute)
{
if (execute == null)
Expand Down

0 comments on commit 7c82bc8

Please sign in to comment.