Skip to content

Microsoft Mvvm Toolkit support #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 21 commits into
base: 22.1.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>DEBUG;TRACE;FRAMEWORK</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;FRAMEWORK</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand All @@ -48,6 +50,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="DevExpressMvvm" Version="20.2.3" />
<PackageReference Include="Microsoft.Toolkit.Mvvm">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="MvvmLightLibsStd10" Version="5.4.1.1" />
<PackageReference Include="Prism.Wpf" Version="8.1.97" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<AssemblyName>DevExpress.Mvvm.CodeGenerators.NetCoreTests</AssemblyName>
<LangVersion>9</LangVersion>
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
<WarningsAsErrors>8618,8604</WarningsAsErrors>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<UseWPF>true</UseWPF>
</PropertyGroup>

Expand All @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.1.2" />
<PackageReference Include="DevExpressMvvm" Version="20.2.3" />
<PackageReference Include="MvvmLightLibsStd10" Version="5.4.1.1" />
<PackageReference Include="Prism.Wpf" Version="8.1.97" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ partial class AttributeTransfer {
[TestFixture]
public class AttributeTransferTests {
[Test]
public void AttributeTransfer() {
var noAttributeProperty = typeof(AttributeTransfer).GetProperty("NoAttribute");
public void AttributeTransferTest() {
var noAttributeProperty = typeof(AttributeTransfer).GetProperty(nameof(AttributeTransfer.NoAttribute));
var attributes = Attribute.GetCustomAttributes(noAttributeProperty);
var expectedAttributes = new Attribute[] { };
Assert.AreEqual(expectedAttributes, attributes);

var withMultipleAttributesProperty = typeof(AttributeTransfer).GetProperty("WithMultipleAttributes");
var withMultipleAttributesProperty = typeof(AttributeTransfer).GetProperty(nameof(AttributeTransfer.WithMultipleAttributes));
attributes = Attribute.GetCustomAttributes(withMultipleAttributesProperty);
expectedAttributes = new Attribute[] {
new System.ComponentModel.DataAnnotations.RangeAttribute(0, 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void NonNullableParameter1(string str) { }
int? nullableInt2;
[GenerateProperty]
string string2;
#pragma warning disable 8632
[GenerateProperty]
string? nullableString2;

Expand All @@ -48,6 +49,7 @@ void NonNullableParameter2(string str) { }
[GenerateCommand]
Task NonNullableParameterAsync2(string str) => Task.CompletedTask;

void OnString1Changed(string str) { }
void OnString1Changed(string str) { }
#pragma warning restore 8632
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ partial class WithTwoMvvmAttribute {
[Prism.GenerateProperty]
int prismProperty;
[Prism.GenerateCommand]
void PrismMethod() { }
void PrismMethod() { prismProperty++; }
}

[TestFixture]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ partial class ChildWithParentsGeneratedRaiseMethod : ParentWithGeneraetedRaiseMe
class ParentWithoutRaiseMethod : INotifyPropertyChanged, INotifyPropertyChanging {
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
public ParentWithoutRaiseMethod() {
//avoid warnings
PropertyChanged?.Invoke(null, null);
PropertyChanging?.Invoke(null, null);
}
}
[GenerateViewModel(ImplementINotifyPropertyChanging = true)]
partial class ChildWithoutRaiseMethod : ParentWithoutRaiseMethod {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ partial class AttributeTransfer {
[TestFixture]
public class AttributeTransferTests {
[Test]
public void AttributeTransfer() {
var noAttributeProperty = typeof(AttributeTransfer).GetProperty("NoAttribute");
public void AttributeTransferTest() {
var noAttributeProperty = typeof(AttributeTransfer).GetProperty(nameof(AttributeTransfer.NoAttribute));
var attributes = Attribute.GetCustomAttributes(noAttributeProperty);
var expectedAttributes = new Attribute[] { };
Assert.AreEqual(expectedAttributes, attributes);

var withMultipleAttributesProperty = typeof(AttributeTransfer).GetProperty("WithMultipleAttributes");
var withMultipleAttributesProperty = typeof(AttributeTransfer).GetProperty(nameof(AttributeTransfer.WithMultipleAttributes));
attributes = Attribute.GetCustomAttributes(withMultipleAttributesProperty);
expectedAttributes = new Attribute[] {
new System.ComponentModel.DataAnnotations.RangeAttribute(0, 1),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ partial class NotImplementICU { }
[GenerateViewModel(ImplementICleanup = true)]
partial class ImplementICUParent { }
[GenerateViewModel(ImplementICleanup = true)]
partial class ImplementICUChild { }
partial class ImplementICUChild : ImplementICUParent { }

[GenerateViewModel(ImplementINotifyPropertyChanging = true,
ImplementICleanup = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void NonNullableParameter1(string str) { }
int? nullableInt2;
[GenerateProperty]
string string2;
#pragma warning disable 8632
[GenerateProperty]
string? nullableString2;

Expand All @@ -47,5 +48,6 @@ void NonNullableParameter2(string str) { }
Task NonNullableParameterAsync2(string str) => Task.CompletedTask;

void OnString1Changed(string str) { }
#pragma warning restore 8632
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ partial class WithTwoMvvmAttribute {
[DevExpress.Mvvm.CodeGenerators.GenerateProperty]
int dxProperty;
[DevExpress.Mvvm.CodeGenerators.GenerateCommand]
void DxMethod() { }
void DxMethod() { dxProperty++; }
}

[TestFixture]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using DevExpress.Mvvm.CodeGenerators.MvvmLight;
using GalaSoft.MvvmLight;
using DevExpress.Mvvm.CodeGenerators.Tests;

namespace MvvmLight.Mvvm.Tests {
[GenerateViewModel(ImplementINotifyPropertyChanging = true)]
Expand All @@ -22,6 +23,11 @@ partial class ChildWithParentsGeneratedRaiseMethod : ParentWithGeneraetedRaiseMe
class ParentWithoutRaiseMethod : INotifyPropertyChanged, INotifyPropertyChanging {
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
public ParentWithoutRaiseMethod() {
//avoid warnings
PropertyChanged?.Invoke(null, null);
PropertyChanging?.Invoke(null, null);
}
}
[GenerateViewModel(ImplementINotifyPropertyChanging = true)]
partial class ChildWithoutRaiseMethod : ParentWithoutRaiseMethod {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using DevExpress.Mvvm.CodeGenerators.Tests.Included;
using NUnit.Framework;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using DevExpress.Mvvm.CodeGenerators.MvvmToolkit;

namespace MvvmToolkit.Mvvm.Tests {
class MyCustomAttribute : Attribute {
public MyCustomAttribute(int value, string str, bool condition, TestEnum testEnum) { }
}
namespace Included {
enum TestEnum {
Num = 1,
String = 2
}
}
[GenerateViewModel]
partial class AttributeTransfer {
const int number = 1;

[GenerateProperty]
int noAttribute;

[GenerateProperty, System.ComponentModel.DataAnnotations.Range(0, 1)]
[Required, MyCustom(number,
"Some string",
true, TestEnum.Num)]
int withMultipleAttributes;
}

[TestFixture]
public class AttributeTransferTests {
[Test]
public void AttributeTransferTest() {
var noAttributeProperty = typeof(AttributeTransfer).GetProperty(nameof(AttributeTransfer.NoAttribute));
var attributes = Attribute.GetCustomAttributes(noAttributeProperty);
var expectedAttributes = new Attribute[] { };
Assert.AreEqual(expectedAttributes, attributes);

var withMultipleAttributesProperty = typeof(AttributeTransfer).GetProperty(nameof(AttributeTransfer.WithMultipleAttributes));
attributes = Attribute.GetCustomAttributes(withMultipleAttributesProperty);
expectedAttributes = new Attribute[] {
new System.ComponentModel.DataAnnotations.RangeAttribute(0, 1),
new RequiredAttribute(),
new MyCustomAttribute(1, "Some string", true, TestEnum.Num)
};
Assert.AreEqual(expectedAttributes, attributes);
}
}
}
Loading