mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-08-16 05:31:46 +02:00
.github
CommunityToolkit.Common
CommunityToolkit.Diagnostics
CommunityToolkit.HighPerformance
CommunityToolkit.Mvvm
CommunityToolkit.Mvvm.SourceGenerators
build
tests
CommunityToolkit.Common.UnitTests
CommunityToolkit.Diagnostics.UnitTests
CommunityToolkit.HighPerformance.UnitTests
CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests
CommunityToolkit.Mvvm.ExternalAssembly
CommunityToolkit.Mvvm.Internals.UnitTests
CommunityToolkit.Mvvm.SourceGenerators.UnitTests
CommunityToolkit.Mvvm.UnitTests
Collections
Helpers
CommunityToolkit.Mvvm.UnitTests.csproj
Test_ArgumentNullException.ComponentModel.cs
Test_ArgumentNullException.DependencyInjection.cs
Test_ArgumentNullException.Input.cs
Test_ArgumentNullException.Messaging.cs
Test_AsyncRelayCommand.cs
Test_AsyncRelayCommand{T}.cs
Test_INotifyPropertyChangedAttribute.cs
Test_IRecipientGenerator.cs
Test_Messenger.Request.cs
Test_Messenger.cs
Test_ObservableObject.cs
Test_ObservableObjectAttribute.cs
Test_ObservablePropertyAttribute.RootNamespace.cs
Test_ObservablePropertyAttribute.cs
Test_ObservableRecipient.cs
Test_ObservableRecipientAttribute.cs
Test_ObservableValidator.cs
Test_RelayCommand.cs
Test_RelayCommandAttribute.cs
Test_RelayCommand{T}.cs
Test_SourceGenerators.cs
.editorconfig
.git-blame-ignore-revs
.gitattributes
.gitignore
.runsettings
CODE_OF_CONDUCT.md
Contributing.md
Directory.Build.props
Directory.Build.targets
License.md
README.md
ThirdPartyNotices.txt
azure-pipelines.yml
dotnet Community Toolkit.sln
toolkit.snk
version.json
81 lines
2.0 KiB
C#
81 lines
2.0 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;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace CommunityToolkit.Mvvm.UnitTests;
|
|
|
|
[TestClass]
|
|
public class Test_RelayCommand
|
|
{
|
|
[TestMethod]
|
|
public void Test_RelayCommand_AlwaysEnabled()
|
|
{
|
|
int ticks = 0;
|
|
|
|
RelayCommand? command = new(() => ticks++);
|
|
|
|
Assert.IsTrue(command.CanExecute(null));
|
|
Assert.IsTrue(command.CanExecute(new object()));
|
|
|
|
(object?, EventArgs?) args = default;
|
|
|
|
command.CanExecuteChanged += (s, e) => args = (s, e);
|
|
|
|
command.NotifyCanExecuteChanged();
|
|
|
|
Assert.AreSame(args.Item1, command);
|
|
Assert.AreSame(args.Item2, EventArgs.Empty);
|
|
|
|
command.Execute(null);
|
|
|
|
Assert.AreEqual(ticks, 1);
|
|
|
|
command.Execute(new object());
|
|
|
|
Assert.AreEqual(ticks, 2);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_RelayCommand_WithCanExecuteFunctionTrue()
|
|
{
|
|
int ticks = 0;
|
|
|
|
RelayCommand? command = new(() => ticks++, () => true);
|
|
|
|
Assert.IsTrue(command.CanExecute(null));
|
|
Assert.IsTrue(command.CanExecute(new object()));
|
|
|
|
command.Execute(null);
|
|
|
|
Assert.AreEqual(ticks, 1);
|
|
|
|
command.Execute(new object());
|
|
|
|
Assert.AreEqual(ticks, 2);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_RelayCommand_WithCanExecuteFunctionFalse()
|
|
{
|
|
int ticks = 0;
|
|
|
|
RelayCommand? command = new(() => ticks++, () => false);
|
|
|
|
Assert.IsFalse(command.CanExecute(null));
|
|
Assert.IsFalse(command.CanExecute(new object()));
|
|
|
|
command.Execute(null);
|
|
|
|
// Logic is unconditionally invoked, the caller should check CanExecute first
|
|
Assert.AreEqual(ticks, 1);
|
|
|
|
command.Execute(new object());
|
|
|
|
Assert.AreEqual(ticks, 2);
|
|
}
|
|
}
|