Skip to content

Commit bfa9e1c

Browse files
committed
Add unit tests for command ArgumentException-s
1 parent 56b5fe4 commit bfa9e1c

File tree

3 files changed

+119
-12
lines changed

3 files changed

+119
-12
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace CommunityToolkit.Mvvm.UnitTests.Helpers;
9+
10+
/// <summary>
11+
/// A helper class to validate scenarios related to <see cref="Exception"/>-s.
12+
/// </summary>
13+
internal static class ExceptionHelper
14+
{
15+
/// <summary>
16+
/// Asserts that a given action throws an <see cref="ArgumentException"/> with a specific parameter name.
17+
/// </summary>
18+
/// <param name="action">The input <see cref="Action"/> to invoke.</param>
19+
/// <param name="parameterName">The expected parameter name.</param>
20+
public static void ThrowsArgumentExceptionWithParameterName(Action action, string parameterName)
21+
{
22+
bool success = false;
23+
24+
try
25+
{
26+
action();
27+
}
28+
catch (Exception e)
29+
{
30+
Assert.IsTrue(e.GetType() == typeof(ArgumentException));
31+
Assert.AreEqual(parameterName, ((ArgumentException)e).ParamName);
32+
33+
success = true;
34+
}
35+
36+
Assert.IsTrue(success);
37+
}
38+
}

tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand{T}.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public async Task Test_AsyncRelayCommandOfT_AlwaysEnabled()
6262

6363
Assert.AreEqual(ticks, 2);
6464

65-
_ = Assert.ThrowsException<InvalidCastException>(() => command.Execute(new object()));
65+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(new object()), "parameter");
66+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(42), "parameter");
67+
68+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(new object()), "parameter");
69+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(42), "parameter");
6670
}
6771

6872
[TestMethod]
@@ -87,6 +91,12 @@ public void Test_AsyncRelayCommandOfT_WithCanExecuteFunctionTrue()
8791
command.Execute("2");
8892

8993
Assert.AreEqual(ticks, 2);
94+
95+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(new object()), "parameter");
96+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(42), "parameter");
97+
98+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(new object()), "parameter");
99+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(42), "parameter");
90100
}
91101

92102
[TestMethod]
@@ -112,10 +122,16 @@ public void Test_AsyncRelayCommandOfT_WithCanExecuteFunctionFalse()
112122
command.Execute("42");
113123

114124
Assert.AreEqual(ticks, 42);
125+
126+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(new object()), "parameter");
127+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(42), "parameter");
128+
129+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(new object()), "parameter");
130+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(42), "parameter");
115131
}
116132

117133
[TestMethod]
118-
public void Test_AsyncRelayCommandOfT_NullWithValueType()
134+
public void Test_AsyncRelayCommandOfT_InvalidArgumentWithValueType()
119135
{
120136
int n = 0;
121137

@@ -125,18 +141,38 @@ public void Test_AsyncRelayCommandOfT_NullWithValueType()
125141
return Task.CompletedTask;
126142
});
127143

144+
// Special case
128145
Assert.IsFalse(command.CanExecute(null));
129-
_ = Assert.ThrowsException<NullReferenceException>(() => command.Execute(null));
130146

131-
command = new AsyncRelayCommand<int>(
147+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute("Hello"), "parameter");
148+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(3.14f), "parameter");
149+
150+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(null), "parameter");
151+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute("Hello"), "parameter");
152+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(3.14f), "parameter");
153+
}
154+
155+
[TestMethod]
156+
public void Test_AsyncRelayCommandOfT_InvalidArgumentWithValueType_WithCanExecute()
157+
{
158+
int n = 0;
159+
160+
AsyncRelayCommand<int>? command = new(
132161
i =>
133162
{
134163
n = i;
135164
return Task.CompletedTask;
136165
}, i => i > 0);
137166

167+
// Special case
138168
Assert.IsFalse(command.CanExecute(null));
139-
_ = Assert.ThrowsException<NullReferenceException>(() => command.Execute(null));
169+
170+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute("Hello"), "parameter");
171+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(3.14f), "parameter");
172+
173+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(null), "parameter");
174+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute("Hello"), "parameter");
175+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(3.14f), "parameter");
140176
}
141177

142178
[TestMethod]
@@ -239,7 +275,11 @@ private static async Task Test_AsyncRelayCommandOfT_AllowConcurrentExecutions_Te
239275

240276
Assert.IsFalse(command.IsRunning);
241277

242-
_ = Assert.ThrowsException<InvalidCastException>(() => command.Execute(new object()));
278+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(new object()), "parameter");
279+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(3.14f), "parameter");
280+
281+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(new object()), "parameter");
282+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(3.14f), "parameter");
243283
}
244284

245285
[TestMethod]

tests/CommunityToolkit.Mvvm.UnitTests/Test_RelayCommand{T}.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using CommunityToolkit.Mvvm.Input;
7+
using CommunityToolkit.Mvvm.UnitTests.Helpers;
78
using Microsoft.VisualStudio.TestTools.UnitTesting;
89

910
namespace CommunityToolkit.Mvvm.UnitTests;
@@ -21,7 +22,11 @@ public void Test_RelayCommandOfT_AlwaysEnabled()
2122
Assert.IsTrue(command.CanExecute("Text"));
2223
Assert.IsTrue(command.CanExecute(null));
2324

24-
_ = Assert.ThrowsException<InvalidCastException>(() => command.CanExecute(new object()));
25+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(new object()), "parameter");
26+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(42), "parameter");
27+
28+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(new object()), "parameter");
29+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(42), "parameter");
2530

2631
(object?, EventArgs?) args = default;
2732

@@ -51,7 +56,11 @@ public void Test_RelayCommand_WithCanExecuteFunction()
5156
Assert.IsTrue(command.CanExecute("Text"));
5257
Assert.IsFalse(command.CanExecute(null));
5358

54-
_ = Assert.ThrowsException<InvalidCastException>(() => command.CanExecute(new object()));
59+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(new object()), "parameter");
60+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(42), "parameter");
61+
62+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(new object()), "parameter");
63+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(42), "parameter");
5564

5665
command.Execute((object)"Hello");
5766

@@ -64,18 +73,38 @@ public void Test_RelayCommand_WithCanExecuteFunction()
6473
}
6574

6675
[TestMethod]
67-
public void Test_RelayCommand_NullWithValueType()
76+
public void Test_RelayCommand_InvalidArgumentWithValueType()
6877
{
6978
int n = 0;
7079

7180
RelayCommand<int>? command = new(i => n = i);
7281

82+
// Special case
7383
Assert.IsFalse(command.CanExecute(null));
74-
_ = Assert.ThrowsException<NullReferenceException>(() => command.Execute(null));
7584

76-
command = new RelayCommand<int>(i => n = i, i => i > 0);
85+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute("Hello"), "parameter");
86+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(3.14f), "parameter");
7787

88+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(null), "parameter");
89+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute("Hello"), "parameter");
90+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(3.14f), "parameter");
91+
}
92+
93+
[TestMethod]
94+
public void Test_RelayCommand_InvalidArgumentWithValueType_WithCanExecute()
95+
{
96+
int n = 0;
97+
98+
RelayCommand<int>? command = new(i => n = i, i => i > 0);
99+
100+
// Special case
78101
Assert.IsFalse(command.CanExecute(null));
79-
_ = Assert.ThrowsException<NullReferenceException>(() => command.Execute(null));
102+
103+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute("Hello"), "parameter");
104+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.CanExecute(3.14f), "parameter");
105+
106+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(null), "parameter");
107+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute("Hello"), "parameter");
108+
ExceptionHelper.ThrowsArgumentExceptionWithParameterName(() => command.Execute(3.14f), "parameter");
80109
}
81110
}

0 commit comments

Comments
 (0)