-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#142 - Rewrite PresentationModels for reflection (2).
- Loading branch information
Showing
8 changed files
with
154 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
test/TestConsole/PresentationModels/TestFieldMetadataValidatorKey.cs
This file was deleted.
Oops, something went wrong.
87 changes: 0 additions & 87 deletions
87
test/TestConsole/PresentationModels/TestPresentationModels.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
test/UnitTest.PresentationModels/Models/RegisterUserModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Neptuo.PresentationModels.Models | ||
{ | ||
class RegisterUserModel | ||
{ | ||
[Required] | ||
public string Username { get; set; } | ||
|
||
[Required] | ||
public string Password { get; set; } | ||
|
||
[Required] | ||
[Compare("Password")] | ||
public string PasswordAgain { get; set; } | ||
|
||
[Required] | ||
public int? Age { get; set; } | ||
|
||
public ICollection<int> RoleIDs { get; } | ||
|
||
public RegisterUserModel() => RoleIDs = new List<int>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neptuo.PresentationModels.Models; | ||
using Neptuo.PresentationModels.TypeModels; | ||
using Neptuo.PresentationModels.TypeModels.DataAnnotations; | ||
using Neptuo.PresentationModels.TypeModels.DataAnnotations.Validators; | ||
using Neptuo.PresentationModels.TypeModels.ValueUpdates; | ||
using Neptuo.PresentationModels.Validation; | ||
using Neptuo.PresentationModels.Validators; | ||
using Neptuo.PresentationModels.Validators.Handlers; | ||
using Neptuo.Validators; | ||
using Neptuo.Validators.Handlers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Neptuo.PresentationModels | ||
{ | ||
[TestClass] | ||
public class TestReflection | ||
{ | ||
private AttributeMetadataReaderCollection metadataReaders; | ||
private FieldMetadataValidatorCollection fieldMetadataValidators; | ||
private ReflectionValueUpdaterCollection valueUpdaters; | ||
private TypeModelDefinitionCollection modelDefinitions; | ||
|
||
public TestReflection() | ||
{ | ||
metadataReaders = new AttributeMetadataReaderCollection() | ||
.Add(new CompareMetadataReader()) | ||
.Add(new DataTypeMetadataReader()) | ||
.Add(new DefaultValueMetadataReader()) | ||
.Add(new DescriptionMetadataReader()) | ||
.Add(new DisplayMetadataReader()) | ||
.Add(new RequiredMetadataReader()) | ||
.Add(new StringLengthMetadataReader()); | ||
|
||
fieldMetadataValidators = new FieldMetadataValidatorCollection() | ||
.Add(null, null, "Required", new RequiredMetadataValidator()) | ||
.Add(null, null, "MatchProperty", new MatchPropertyMetadataValidator()); | ||
|
||
valueUpdaters = new ReflectionValueUpdaterCollection() | ||
.Add<ICollection<int>>(new CollectionItemReflectionValueUpdater<int>()); | ||
|
||
modelDefinitions = new TypeModelDefinitionCollection() | ||
.AddReflectionSearchHandler(metadataReaders); | ||
} | ||
|
||
[TestMethod] | ||
public void BuildModelDefinition() | ||
{ | ||
IModelDefinition modelDefinition = modelDefinitions.Get<RegisterUserModel>(); | ||
Assert.AreEqual(5, modelDefinition.Fields.Count()); | ||
} | ||
|
||
[TestMethod] | ||
public void GetValues() | ||
{ | ||
IModelDefinition modelDefinition = modelDefinitions.Get<RegisterUserModel>(); | ||
|
||
RegisterUserModel model = new RegisterUserModel(); | ||
model.Username = "pepa"; | ||
model.Password = "x"; | ||
model.PasswordAgain = "y"; | ||
IModelValueProvider reflection = new ReflectionModelValueProvider(model, valueUpdaters); | ||
DictionaryModelValueProvider dictionary = new DictionaryModelValueProvider(); | ||
|
||
CopyModelValueProvider copyProvider = new CopyModelValueProvider(modelDefinition, true); | ||
copyProvider.Update(dictionary, reflection); | ||
|
||
object value; | ||
Assert.IsTrue(dictionary.TryGetValue(nameof(RegisterUserModel.Username), out value)); | ||
Assert.AreEqual(model.Username, value); | ||
|
||
Assert.IsTrue(dictionary.TryGetValue(nameof(RegisterUserModel.Password), out value)); | ||
Assert.AreEqual(model.Password, value); | ||
|
||
Assert.IsTrue(dictionary.TryGetValue(nameof(RegisterUserModel.PasswordAgain), out value)); | ||
Assert.AreEqual(model.PasswordAgain, value); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test/UnitTest.PresentationModels/UnitTest.PresentationModels.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<RootNamespace>Neptuo.PresentationModels</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.Serialization.Xml\Neptuo.PresentationModels.Serialization.Xml.csproj" /> | ||
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.TypeModels.DataAnnotations\Neptuo.PresentationModels.TypeModels.DataAnnotations.csproj" /> | ||
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.TypeModels\Neptuo.PresentationModels.TypeModels.csproj" /> | ||
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.UI\Neptuo.PresentationModels.UI.csproj" /> | ||
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.Validators\Neptuo.PresentationModels.Validators.csproj" /> | ||
<ProjectReference Include="..\..\src\Neptuo.PresentationModels\Neptuo.PresentationModels.csproj" /> | ||
<ProjectReference Include="..\..\src\Neptuo.Validators\Neptuo.Validators.csproj" /> | ||
<ProjectReference Include="..\..\src\Neptuo\Neptuo.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |