1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-06-02 14:34:11 +02:00
.NET-Community-Toolkit/tests/CommunityToolkit.Mvvm.UnitTests/Test_ICommandAttribute.cs
Sergio Pedri 51c7a67c20 More code style improvements and tweaks
- Remove unused namespaces
- Remove unused attributes
- Remove unnecessary #nullable enable directives
- Sort using directives
- Simplify names
2021-11-01 22:29:46 +01:00

114 lines
2.9 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommunityToolkit.Mvvm.UnitTests;
[TestClass]
public partial class Test_ICommandAttribute
{
[TestCategory("Mvvm")]
[TestMethod]
public async Task Test_ICommandAttribute_RelayCommand()
{
MyViewModel? model = new();
Assert.AreEqual(model.Counter, 0);
model.IncrementCounterCommand.Execute(null);
Assert.AreEqual(model.Counter, 1);
model.IncrementCounterWithValueCommand.Execute(5);
Assert.AreEqual(model.Counter, 6);
await model.DelayAndIncrementCounterCommand.ExecuteAsync(null);
Assert.AreEqual(model.Counter, 7);
await model.DelayAndIncrementCounterWithTokenCommand.ExecuteAsync(null);
Assert.AreEqual(model.Counter, 8);
await model.DelayAndIncrementCounterWithValueCommand.ExecuteAsync(5);
Assert.AreEqual(model.Counter, 13);
await model.DelayAndIncrementCounterWithValueAndTokenCommand.ExecuteAsync(5);
Assert.AreEqual(model.Counter, 18);
}
public sealed partial class MyViewModel
{
public int Counter { get; private set; }
/// <summary>This is a single line summary.</summary>
[ICommand]
private void IncrementCounter()
{
Counter++;
}
/// <summary>
/// This is a multiline summary
/// </summary>
[ICommand]
private void IncrementCounterWithValue(int count)
{
Counter += count;
}
/// <summary>This is single line with also other stuff below</summary>
/// <returns>Foo bar baz</returns>
/// <returns>A task</returns>
[ICommand]
private async Task DelayAndIncrementCounterAsync()
{
await Task.Delay(50);
Counter += 1;
}
#region Test region
/// <summary>
/// This is multi line with also other stuff below
/// </summary>
/// <returns>Foo bar baz</returns>
/// <returns>A task</returns>
[ICommand]
private async Task DelayAndIncrementCounterWithTokenAsync(CancellationToken token)
{
await Task.Delay(50);
Counter += 1;
}
// This should not be ported over
[ICommand]
private async Task DelayAndIncrementCounterWithValueAsync(int count)
{
await Task.Delay(50);
Counter += count;
}
#endregion
[ICommand]
private async Task DelayAndIncrementCounterWithValueAndTokenAsync(int count, CancellationToken token)
{
await Task.Delay(50);
Counter += count;
}
}
}