-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAsyncCommandGenerationTests.cs
More file actions
82 lines (67 loc) · 3.45 KB
/
AsyncCommandGenerationTests.cs
File metadata and controls
82 lines (67 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using NUnit.Framework;
using System;
using System.Reflection;
using System.Threading.Tasks;
using DevExpress.Mvvm.CodeGenerators.MvvmToolkit;
using Microsoft.Toolkit.Mvvm.Input;
namespace MvvmToolkit.Mvvm.Tests {
[GenerateViewModel]
partial class GenerateAsyncCommands {
readonly Task<int> task = new(() => 1);
[GenerateCommand]
public Task WithNoArg() => Task.CompletedTask;
[GenerateCommand]
public Task WithArg(int arg) => Task.CompletedTask;
[GenerateCommand]
public Task WithNullableArg(int? arg) => Task.CompletedTask;
public Task SomeMethod() => Task.CompletedTask;
[GenerateCommand(Name = "MyAsyncCommand", CanExecuteMethod = "CanDoIt")]
public Task Method(int arg) => Task.CompletedTask;
public bool CanDoIt(int arg) => arg > 0;
[GenerateCommand]
public Task<int> GenericTask() => task;
}
[TestFixture]
public class AsyncCommandGenerationTests {
[Test]
public void CallRequiredMethodForAsyncCommand() {
var generated = new GenerateAsyncCommands();
var method = GetFieldValue<Action<int>, RelayCommand<int>>(generated.MyAsyncCommand, "execute");
StringAssert.Contains("MyAsyncCommand", method.Method.Name);
var canMethod = GetFieldValue<Predicate<int>, RelayCommand<int>>(generated.MyAsyncCommand, "canExecute");
var expectedCanMethod = generated.GetType().GetMethod("CanDoIt").Name;
Assert.AreEqual(expectedCanMethod, canMethod.Method.Name);
var canExecuteMethod = GetFieldValue<Predicate<int>, RelayCommand>(generated.GenericTaskCommand, "canExecute");
Assert.IsNull(canExecuteMethod);
}
static TResult GetFieldValue<TResult, T>(T source, string fieldName) {
var fieldInfo = source.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
Assert.IsNotNull(fieldInfo);
return (TResult)fieldInfo.GetValue(source);
}
[Test]
public void AsyncCommandImplementation() {
var generated = new GenerateAsyncCommands();
Assert.IsNotNull(generated.GetType().GetProperty("WithNoArgCommand"));
Assert.IsNotNull(generated.GetType().GetProperty("WithArgCommand"));
Assert.IsNotNull(generated.GetType().GetProperty("WithNullableArgCommand"));
Assert.IsNull(generated.GetType().GetProperty("With2ArgsCommand"));
Assert.IsNull(generated.GetType().GetProperty("ReturnNoTaskCommand"));
Assert.IsNull(generated.GetType().GetProperty("SomeMethodCommand"));
}
[Test]
public void ArgumentTypeForAsyncCommand() {
var generated = new GenerateAsyncCommands();
var noArgumentType = generated.WithNoArgCommand.GetType();
Assert.IsEmpty(noArgumentType.GetGenericArguments());
var expectedType = typeof(RelayCommand);
Assert.AreEqual(expectedType, noArgumentType);
var intArgumentType = generated.WithArgCommand.GetType().GetGenericArguments()[0];
var intExpectedType = typeof(int);
Assert.AreEqual(intExpectedType, intArgumentType);
var nullableIntArgumentType = generated.WithNullableArgCommand.GetType().GetGenericArguments()[0];
var nullableIntExpectedType = typeof(int?);
Assert.AreEqual(nullableIntExpectedType, nullableIntArgumentType);
}
}
}