Skip to content

Commit

Permalink
* Major refactoring of the scanning process
Browse files Browse the repository at this point in the history
* Fixed unit tests
* Extracted specific target type scanners
  • Loading branch information
valdisiljuconoks authored and valdisiljuconoks committed Nov 12, 2016
1 parent f7199e9 commit 2928cfd
Show file tree
Hide file tree
Showing 32 changed files with 679 additions and 334 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,17 @@
<Content Include="Views\web.config" />
<Content Include="packages.config" />
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Shared\EditorTemplates\Username.cshtml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
</None>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="Views\Shared\DisplayTemplates\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\DbLocalizationProvider.AdminUI\DbLocalizationProvider.AdminUI.csproj">
<Project>{8f0618d8-8200-45e9-9d1e-b268e98e0a84}</Project>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ public class BaseViewModel
[StringLength(100, MinimumLength = 5)]
public string Message { get; set; }

[Display(Name = "Base username:", Description = "")]
[StringLength(100, MinimumLength = 5)]
[UIHint("Username")]
public string BaseUsername { get; set; }

public string CustomMessage { get; } = "Resource like property on base view model";
}

[LocalizedModel(Inherited = false)]
public class HomeViewModel : BaseViewModel
{
[Display(Name = "The user name:")]
[Display(Name = "The user name:", Description = "")]
[Required]
[UIHint("Username")]
public string Username { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
@Html.ValidationMessageFor(m => m.Message)
</div>
<div>
@Html.LabelFor(m => m.Username)
@Html.EditorFor(m => m.Username)
@Html.ValidationMessageFor(m => m.Username)
</div>
<div>
@Html.EditorFor(m => m.BaseUsername)
</div>
<input type="submit" />
</form>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@model string

@Html.LabelFor(m => m)
@Html.TextBoxFor(m => m)
@Html.ValidationMessageFor(m => m)

[@ViewData.ModelMetadata.Description]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace DbLocalizationProvider.Tests.DataAnnotations
{
[LocalizedModel]
public class BaseViewModel
{
[Display(Name = "Base property", Description = "")]
[Required]
public string BaseProperty { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace DbLocalizationProvider.Tests.DataAnnotations
{
[LocalizedModel(Inherited = false)]
public class SampleViewModelWithBase : BaseViewModel
{
[Display(Name = "Child property", Description = "")]
[Required]
[StringLength(100)]
public string ChildProperty { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Linq;
using DbLocalizationProvider.Sync;
using Xunit;

namespace DbLocalizationProvider.Tests.DataAnnotations
{
public class ViewModelWithInheritanceTests
{
[Fact]
public void NotInheritedModel_ContainsOnlyDeclaredProperties()
{
var sut = new TypeDiscoveryHelper();
var properties = sut.ScanResources(typeof(SampleViewModelWithBase));
var keys = properties.Select(p => p.Key).ToList();

Assert.Contains("DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.ChildProperty-Description", keys);
Assert.DoesNotContain("DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.BaseProperty", keys);
Assert.DoesNotContain("DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.BaseProperty-Required", keys);
Assert.DoesNotContain("DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.ChildProperty-Description-Required", keys);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="DataAnnotations\BaseViewModel.cs" />
<Compile Include="DataAnnotations\SampleViewModelWithBase.cs" />
<Compile Include="DataAnnotations\ViewModelWithInheritanceTests.cs" />
<Compile Include="DiscoveryTests\SampleViewModelWithIncludedOnly.cs" />
<Compile Include="DiscoveryTests\ViewModelWithIncludedOnlyTests.cs" />
<Compile Include="GenericModels\ClosedGenericModel.cs" />
Expand Down Expand Up @@ -107,6 +110,7 @@
<Compile Include="ResourceKeys.cs" />
<Compile Include="SampleStatus.cs" />
<Compile Include="SampleViewModel.cs" />
<Compile Include="ScannerTests\TypeScannerTests.cs" />
<Compile Include="SerializationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringJoinTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ namespace DbLocalizationProvider.Tests.DiscoveryTests
{
public class ViewModelWithIncludedOnlyTests
{
public ViewModelWithIncludedOnlyTests()
{
_sut = new TypeDiscoveryHelper();
}

private readonly TypeDiscoveryHelper _sut;

[Fact]
public void ModelWithBase_IncludedPorperty_ShouldDiscoverOnlyExplicitProperties()
{
var properties = TypeDiscoveryHelper.GetAllProperties(typeof(SampleViewModelWithIncludedOnlyWithBase), contextAwareScanning: false)
.Select(p => p.Key)
.ToList();
var properties = _sut.ScanResources(typeof(SampleViewModelWithIncludedOnlyWithBase))
.Select(p => p.Key)
.ToList();

Assert.Contains("DbLocalizationProvider.Tests.DiscoveryTests.SampleViewModelWithIncludedOnlyWithBase.IncludedProperty", properties);
Assert.DoesNotContain("DbLocalizationProvider.Tests.DiscoveryTests.SampleViewModelWithIncludedOnlyWithBase.ExcludedProperty", properties);
Expand All @@ -23,9 +30,9 @@ public void ModelWithBase_IncludedPorperty_ShouldDiscoverOnlyExplicitProperties(
[Fact]
public void ModelWithIncludedPorperty_ShouldDiscoverOnlyExplicitProperties()
{
var properties = TypeDiscoveryHelper.GetAllProperties(typeof(SampleViewModelWithIncludedOnly), contextAwareScanning: false)
.Select(p => p.Key)
.ToList();
var properties = _sut.ScanResources(typeof(SampleViewModelWithIncludedOnly))
.Select(p => p.Key)
.ToList();

Assert.Contains("DbLocalizationProvider.Tests.DiscoveryTests.SampleViewModelWithIncludedOnly.IncludedProperty", properties);
Assert.DoesNotContain("DbLocalizationProvider.Tests.DiscoveryTests.SampleViewModelWithIncludedOnly.ExcludedProperty", properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ namespace DbLocalizationProvider.Tests.GenericModels
{
public class GenericModelTests
{
public GenericModelTests()
{
_sut = new TypeDiscoveryHelper();
}

private readonly TypeDiscoveryHelper _sut;

[Fact]
public void TestGenericProperty()
{
var properties = TypeDiscoveryHelper.GetAllProperties(typeof(OpenGenericModel<>), contextAwareScanning: false);
var properties = _sut.ScanResources(typeof(OpenGenericModel<>));

Assert.NotEmpty(properties);
}

[Fact]
public void TestGenericProperty_FromChildClass()
{
var properties = TypeDiscoveryHelper.GetAllProperties(typeof(ClosedGenericModel), contextAwareScanning: false);
var properties = _sut.ScanResources(typeof(ClosedGenericModel));

Assert.NotEmpty(properties);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ public class InheritedViewModelExpressionTests
[Fact]
public void Test()
{
var properties =
new[] { typeof(SampleViewModelWithBaseNotInherit), typeof(BaseLocalizedViewModel) }
.Select(t => TypeDiscoveryHelper.GetAllProperties(t, contextAwareScanning: false))
.ToList();
var sut = new TypeDiscoveryHelper();

var properties = new[] { typeof(SampleViewModelWithBaseNotInherit), typeof(BaseLocalizedViewModel) }
.Select(t => sut.ScanResources(t))
.ToList();

var childModel = new SampleViewModelWithBaseNotInherit();
var basePropertyKey = ExpressionHelper.GetFullMemberName(() => childModel.BaseProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ namespace DbLocalizationProvider.Tests.InheritedModels
{
public class ViewModelWithBaseTests
{
public ViewModelWithBaseTests()
{
_sut = new TypeDiscoveryHelper();
}

private readonly TypeDiscoveryHelper _sut;

[Fact]
public void BaseProperty_HasChildClassResourceKey()
{
var properties = TypeDiscoveryHelper.GetAllProperties(typeof(SampleViewModelWithBase), contextAwareScanning: false)
.Select(p => p.Key)
.ToList();
var properties = _sut.ScanResources(typeof(SampleViewModelWithBase))
.Select(p => p.Key)
.ToList();

Assert.Contains("DbLocalizationProvider.Tests.InheritedModels.SampleViewModelWithBase.BaseProperty", properties);
Assert.Contains("DbLocalizationProvider.Tests.InheritedModels.SampleViewModelWithBase.BaseProperty-Required", properties);
Expand All @@ -23,9 +30,9 @@ public void BaseProperty_HasChildClassResourceKey()
[Fact]
public void BaseProperty_HasChildClassResourceKey_DoesNotIncludeInheritedProperties()
{
var properties = TypeDiscoveryHelper.GetAllProperties(typeof(SampleViewModelWithBaseNotInherit), contextAwareScanning: false)
.Select(p => p.Key)
.ToList();
var properties = _sut.ScanResources(typeof(SampleViewModelWithBaseNotInherit))
.Select(p => p.Key)
.ToList();

Assert.Contains("DbLocalizationProvider.Tests.InheritedModels.SampleViewModelWithBaseNotInherit.ChildProperty", properties);
Assert.DoesNotContain("DbLocalizationProvider.Tests.InheritedModels.SampleViewModelWithBaseNotInherit.BaseProperty", properties);
Expand All @@ -36,7 +43,7 @@ public void BuildResourceKey_ForBaseClassProperty_ExcludedFromChild_ShouldReturn
{
var properties =
new[] { typeof(SampleViewModelWithBaseNotInherit), typeof(BaseLocalizedViewModel) }
.Select(t => TypeDiscoveryHelper.GetAllProperties(t, contextAwareScanning: false))
.Select(t => _sut.ScanResources(t))
.ToList();

var childPropertyKey = ResourceKeyBuilder.BuildResourceKey(typeof(SampleViewModelWithBaseNotInherit), "ChildProperty");
Expand All @@ -53,7 +60,7 @@ public void BuildResourceKey_ForSecondBaseClassProperty_ExcludedFromChild_Should
{
var properties =
new[] { typeof(SampleViewModelWithBaseNotInherit), typeof(BaseLocalizedViewModel), typeof(VeryBaseLocalizedViewModel) }
.Select(t => TypeDiscoveryHelper.GetAllProperties(t, contextAwareScanning: false))
.Select(t => _sut.ScanResources(t))
.ToList();

var veryBasePropertyKey = ResourceKeyBuilder.BuildResourceKey(typeof(SampleViewModelWithBaseNotInherit), "VeryBaseProperty");
Expand Down
3 changes: 2 additions & 1 deletion Tests/DbLocalizationProvider.Tests/LocalizedEnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ public class LocalizedEnumTests
public LocalizedEnumTests()
{
var types = new[] { typeof(DocumentEntity) };
var sut = new TypeDiscoveryHelper();

Assert.NotEmpty(types);

_properties = types.SelectMany(t => TypeDiscoveryHelper.GetAllProperties(t, contextAwareScanning: false));
_properties = types.SelectMany(t => sut.ScanResources(t));
}

private readonly IEnumerable<DiscoveredResource> _properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public LocalizedEnumsDiscoveryTests()
[Fact]
public void DiscoverEnumValue_NameAsTranslation()
{
var sut = new TypeDiscoveryHelper();
var type = _types.First(t => t.FullName == "DbLocalizationProvider.Tests.SampleStatus");
var properties = TypeDiscoveryHelper.GetAllProperties(type);
var properties = sut.ScanResources(type);

var openStatus = properties.First(p => p.Key == "DbLocalizationProvider.Tests.SampleStatus.Open");

Expand Down
30 changes: 15 additions & 15 deletions Tests/DbLocalizationProvider.Tests/LocalizedModelsDiscoveryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@ namespace DbLocalizationProvider.Tests
{
public class LocalizedModelsDiscoveryTests
{
private readonly IEnumerable<DiscoveredResource> _properties;

public LocalizedModelsDiscoveryTests()
{
var types = new[] { typeof(SampleViewModel), typeof(SubViewModel) };//TypeDiscoveryHelper.GetTypesWithAttribute<LocalizedModelAttribute>().ToList();
var types = new[] { typeof(SampleViewModel), typeof(SubViewModel) };
var sut = new TypeDiscoveryHelper();

Assert.NotEmpty(types);

_properties = types.SelectMany(t => TypeDiscoveryHelper.GetAllProperties(t, contextAwareScanning: false));
_properties = types.SelectMany(t => sut.ScanResources(t));
}

private readonly IEnumerable<DiscoveredResource> _properties;

[Fact]
public void PropertyWithAttributes_DisplayDescription_Discovered()
{
var resource = _properties.FirstOrDefault(p => p.Key == "DbLocalizationProvider.Tests.SampleViewModel.PropertyWithDescription");
Assert.NotNull(resource);

var propertyWithDescriptionResource = _properties.FirstOrDefault(p => p.Key == "DbLocalizationProvider.Tests.SampleViewModel.PropertyWithDescription-Description");
Assert.NotNull(propertyWithDescriptionResource);
}

[Fact]
Expand Down Expand Up @@ -52,16 +63,5 @@ public void SingleLevel_ScalarProperties_NoAttribute()
var nullable = _properties.FirstOrDefault(p => p.Key == "DbLocalizationProvider.Tests.SampleViewModel.NullableInt");
Assert.NotNull(nullable);
}


[Fact]
public void PropertyWithAttributes_DisplayDescription_Discovered()
{
var resource = _properties.FirstOrDefault(p => p.Key == "DbLocalizationProvider.Tests.SampleViewModel.PropertyWithDescription");
Assert.NotNull(resource);

var propertyWithDescriptionResource = _properties.FirstOrDefault(p => p.Key == "DbLocalizationProvider.Tests.SampleViewModel.PropertyWithDescription-Description");
Assert.NotNull(propertyWithDescriptionResource);
}
}
}
Loading

0 comments on commit 2928cfd

Please sign in to comment.