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

Commit 7c82bc8

Browse files
committed
Revert "Revert AsyncValueCommandSupport"
This reverts commit 07a178c.
1 parent 07a178c commit 7c82bc8

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xamarin.CommunityToolkit.Exceptions;
4+
using Xamarin.CommunityToolkit.ObjectModel;
5+
using Xamarin.CommunityToolkit.UnitTests.ObjectModel.ICommandTests.AsyncValueCommandTests;
6+
using Xunit;
7+
8+
namespace Xamarin.CommunityToolkit.UnitTests.ObjectModel.ICommandTests.CommandFactoryTests
9+
{
10+
public class CommandFactoryAsyncValueCommandTests : BaseAsyncValueCommandTests
11+
{
12+
13+
[Fact]
14+
public void NullExecuteParameter()
15+
{
16+
// Arrange
17+
Func<ValueTask> execute = null;
18+
19+
// Act
20+
21+
// Assert
22+
Assert.Throws<ArgumentNullException>(() => CommandFactory.Create(execute));
23+
}
24+
25+
[Fact]
26+
public void NullCanExecuteParameter()
27+
{
28+
// Arrange
29+
var command = CommandFactory.Create(NoParameterTask, null);
30+
31+
// Act
32+
33+
// Assert
34+
Assert.True(command.CanExecute(null));
35+
Assert.True(command.CanExecute(string.Empty));
36+
Assert.True(command.CanExecute(0));
37+
}
38+
39+
[Fact]
40+
public void IntExecuteNullCanExecuteParameter()
41+
{
42+
// Arrange
43+
var command = CommandFactory.Create<int>(IntParameterTask, null);
44+
45+
// Act
46+
47+
// Assert
48+
Assert.True(command.CanExecute(null));
49+
Assert.True(command.CanExecute(string.Empty));
50+
Assert.True(command.CanExecute(0));
51+
52+
command.Execute(0);
53+
Assert.Throws<InvalidCommandParameterException>(() => command.Execute(null));
54+
Assert.Throws<InvalidCommandParameterException>(() => command.Execute(string.Empty));
55+
}
56+
57+
[Fact]
58+
public void IntExecuteTrueCanExecuteParameter()
59+
{
60+
// Arrange
61+
var command = CommandFactory.Create<int>(IntParameterTask, CanExecuteTrue);
62+
63+
// Act
64+
65+
// Assert
66+
Assert.True(command.CanExecute(null));
67+
Assert.True(command.CanExecute(string.Empty));
68+
Assert.True(command.CanExecute(0));
69+
}
70+
71+
[Fact]
72+
public void IntExecuteFalseCanExecuteParameter()
73+
{
74+
// Arrange
75+
var command = CommandFactory.Create<int>(IntParameterTask, CanExecuteFalse);
76+
77+
// Act
78+
79+
// Assert
80+
Assert.False(command.CanExecute(null));
81+
Assert.False(command.CanExecute(string.Empty));
82+
Assert.False(command.CanExecute(0));
83+
}
84+
85+
[Fact]
86+
public void IntExecuteNullTypedCanExecuteParameter()
87+
{
88+
// Arrange
89+
var command = CommandFactory.Create<int, bool>(IntParameterTask, null);
90+
91+
// Act
92+
93+
// Assert
94+
Assert.True(command.CanExecute(true));
95+
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(null));
96+
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(string.Empty));
97+
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(0));
98+
}
99+
100+
[Fact]
101+
public void IntExecuteBoolCanExecuteParameter()
102+
{
103+
// Arrange
104+
var command = CommandFactory.Create<int, bool>(IntParameterTask, CanExecuteTrue);
105+
106+
// Act
107+
108+
// Assert
109+
Assert.True(command.CanExecute(true));
110+
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(null));
111+
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(string.Empty));
112+
Assert.Throws<InvalidCommandParameterException>(() => command.CanExecute(0));
113+
}
114+
}
115+
}

src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/CommandFactory.shared.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ public static IAsyncCommand<TExecute, TCanExecute> Create<TExecute, TCanExecute>
4141
bool allowsMultipleExecutions = true) =>
4242
new AsyncCommand<TExecute, TCanExecute>(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions);
4343

44+
public static IAsyncValueCommand Create(
45+
Func<ValueTask> execute,
46+
Func<bool> canExecute = null,
47+
Action<Exception> onException = null,
48+
bool continueOnCapturedContext = false,
49+
bool allowsMultipleExecutions = true) =>
50+
new AsyncValueCommand(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions);
51+
52+
public static IAsyncValueCommand<TExecute> Create<TExecute>(
53+
Func<TExecute, ValueTask> execute,
54+
Func<bool> canExecute = null,
55+
Action<Exception> onException = null,
56+
bool continueOnCapturedContext = false,
57+
bool allowsMultipleExecutions = true) =>
58+
new AsyncValueCommand<TExecute>(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions);
59+
60+
public static IAsyncValueCommand<TExecute, TCanExecute> Create<TExecute, TCanExecute>(
61+
Func<TExecute, ValueTask> execute,
62+
Func<TCanExecute, bool> canExecute = null,
63+
Action<Exception> onException = null,
64+
bool continueOnCapturedContext = false,
65+
bool allowsMultipleExecutions = true) =>
66+
new AsyncValueCommand<TExecute, TCanExecute>(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions);
67+
4468
static Action<object> ConvertExecute<T>(Action<T> execute)
4569
{
4670
if (execute == null)

0 commit comments

Comments
 (0)