diff --git a/src/RunCoverage.bat b/src/RunCoverage.bat index fd8216ef3..3610fcf58 100644 --- a/src/RunCoverage.bat +++ b/src/RunCoverage.bat @@ -6,20 +6,16 @@ CD %~dp0 FOR /D /R %%X IN (%USERNAME%*) DO RD /S /Q "%%X" REM Run the tests against the targeted output -call :RunOpenCoverUnitTestMetrics - +call :GenerateCoverage + REM Generate the report output based on the test results if %errorlevel% equ 0 ( call :RunReportGeneratorOutput ) - -REM Launch the report -if %errorlevel% equ 0 ( - call :RunLaunchReport -) + exit /b %errorlevel% -:RunOpenCoverUnitTestMetrics +:GenerateCoverage "%UserProfile%\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe" ^ -register:user ^ -target:"C:\Program Files\dotnet\dotnet.exe" ^ @@ -28,10 +24,19 @@ exit /b %errorlevel% -skipautoprops ^ -output:"%~dp0\GeneratedReports\Infrastructure.xml" ^ -oldStyle + +"%UserProfile%\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe" ^ +-register:user ^ +-target:"C:\Program Files\dotnet\dotnet.exe" ^ +-targetargs:"test %~dp0\pinkparrot_write\PinkParrot.Write.Tests" ^ +-filter:"+[PinkParrot*]*" ^ +-skipautoprops ^ +-output:"%~dp0\GeneratedReports\Write.xml" ^ +-oldStyle exit /b %errorlevel% :RunReportGeneratorOutput "%UserProfile%\.nuget\packages\ReportGenerator\2.4.5\tools\ReportGenerator.exe" ^ --reports:"%~dp0\GeneratedReports\Infrastructure.xml" ^ +-reports:"%~dp0\GeneratedReports\*.xml" ^ -targetdir:"%~dp0\GeneratedReports\Output" exit /b %errorlevel% \ No newline at end of file diff --git a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/PropertiesBagTests.cs b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/PropertiesBagTests.cs index 23d7511fc..221791b01 100644 --- a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/PropertiesBagTests.cs +++ b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/PropertiesBagTests.cs @@ -97,8 +97,17 @@ namespace PinkParrot.Infrastructure bag.Set("Key2", 1); Assert.Equal(2, bag.Count); - Assert.Equal(new[] {"Key1", "Key2"}, bag.PropertyNames.ToArray()); - Assert.Equal(new[] {"Key1", "Key2"}, bag.Properties.Keys.ToArray()); + } + + [Fact] + public void Should_calculate_keys_correctly() + { + bag.Set("Key1", 1); + bag.Set("Key2", 1); + + Assert.Equal(new[] { "Key1", "Key2" }, bag.PropertyNames.ToArray()); + Assert.Equal(new[] { "Key1", "Key2" }, bag.Properties.Keys.ToArray()); + Assert.Equal(new[] { "Key1", "Key2" }, bag.GetDynamicMemberNames().ToArray()); } [Fact] diff --git a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/PropertiesTypeAccessorTest.cs b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/PropertiesTypeAccessorTest.cs new file mode 100644 index 000000000..5b3118a1f --- /dev/null +++ b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/PropertiesTypeAccessorTest.cs @@ -0,0 +1,107 @@ +// ========================================================================== +// PropertiesTypeAccessorTest.cs +// PinkParrot Headless CMS +// ========================================================================== +// Copyright (c) PinkParrot Group +// All rights reserved. +// ========================================================================== + +// ReSharper disable ValueParameterNotUsed + +using System; +using System.Linq; +using Xunit; + +namespace PinkParrot.Infrastructure.Reflection +{ + public class PropertiesTypeAccessorTest + { + public class TestClass + { + private int target; + + public int ReadWrite + { + get { return target; } + set { target = value; } + } + + public int Read + { + get { return target; } + } + + public int Write + { + set { target = value; } + } + } + + private readonly TestClass target = new TestClass(); + private readonly PropertiesTypeAccessor accessor = PropertiesTypeAccessor.Create(typeof(TestClass)); + + [Fact] + public void Should_provide_properties() + { + var properties = accessor.Properties.Select(x => x.Name).ToArray(); + + Assert.Equal(new[] { "ReadWrite", "Read", "Write" }, properties); + } + + [Fact] + public void Should_set_read_write_property() + { + accessor.SetValue(target, "ReadWrite", 123); + + Assert.Equal(123, target.Read); + } + + [Fact] + public void Should_set_write_property() + { + accessor.SetValue(target, "Write", 123); + + Assert.Equal(123, target.Read); + } + + [Fact] + public void Should_throw_if_setting_unknown_property() + { + Assert.Throws(() => accessor.SetValue(target, "Unknown", 123)); + } + + [Fact] + public void Should_throw_if_setting_readonly() + { + Assert.Throws(() => accessor.SetValue(target, "Read", 123)); + } + + [Fact] + public void Should_get_read_write_property() + { + target.Write = 123; + + Assert.Equal(123, accessor.GetValue(target, "ReadWrite")); + } + + [Fact] + public void Should_get_read_property() + { + target.Write = 123; + + Assert.Equal(123, accessor.GetValue(target, "Read")); + } + + [Fact] + public void Should_throw_if_getting_unknown_property() + { + Assert.Throws(() => accessor.GetValue(target, "Unknown")); + } + + [Fact] + public void Should_throw_if_getting_readonly() + { + Assert.Throws(() => accessor.GetValue(target, "Write")); + } + } +} diff --git a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/SimpleMapperTests.cs b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/SimpleMapperTests.cs index c6ad001e9..5e233dee2 100644 --- a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/SimpleMapperTests.cs +++ b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/SimpleMapperTests.cs @@ -22,6 +22,10 @@ namespace PinkParrot.Infrastructure.Reflection public long MappedNumber { get; set; } public Guid MappedGuid { get; set; } + + public long WrongType1 { get; set; } + + public long WrongType2 { get; set; } } public class Class1 : Class1Base @@ -46,6 +50,10 @@ namespace PinkParrot.Infrastructure.Reflection { get { return "Value"; } } + + public DateTime WrongType1 { get; set; } + + public TimeSpan WrongType2 { get; set; } } [Fact] @@ -77,6 +85,8 @@ namespace PinkParrot.Infrastructure.Reflection Assert.Equal(class1.MappedString, class2.MappedString); Assert.Equal(class1.MappedNumber, class2.MappedNumber); Assert.Equal(class1.MappedGuid.ToString(), class2.MappedGuid); + Assert.Equal(class1.WrongType1, 0L); + Assert.Equal(class1.WrongType2, 0L); Assert.NotEqual(class1.UnmappedString, class2.UnmappedString); } } diff --git a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/TypeNameRegistryTests.cs b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/TypeNameRegistryTests.cs new file mode 100644 index 000000000..0bb361784 --- /dev/null +++ b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/TypeNameRegistryTests.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Xunit; + +namespace PinkParrot.Infrastructure +{ + public class TypeNameRegistryTests + { + [TypeName("my")] + public sealed class MyType + { + } + + [Fact] + public void Should_register_and_retrieve_types() + { + TypeNameRegistry.Map(typeof(int), "number"); + + Assert.Equal("number", TypeNameRegistry.GetName()); + Assert.Equal("number", TypeNameRegistry.GetName(typeof(int))); + + Assert.Equal(typeof(int), TypeNameRegistry.GetType("number")); + Assert.Equal(typeof(int), TypeNameRegistry.GetType("Number")); + } + + [Fact] + public void Should_register_from_assembly() + { + TypeNameRegistry.Map(typeof(TypeNameRegistryTests).GetTypeInfo().Assembly); + + Assert.Equal("my", TypeNameRegistry.GetName()); + Assert.Equal("my", TypeNameRegistry.GetName(typeof(MyType))); + + Assert.Equal(typeof(MyType), TypeNameRegistry.GetType("my")); + Assert.Equal(typeof(MyType), TypeNameRegistry.GetType("My")); + } + + [Fact] + public void Should_throw_if_type_is_already_registered() + { + TypeNameRegistry.Map(typeof(long), "long"); + + Assert.Throws(() => TypeNameRegistry.Map(typeof(long), "longer")); + } + + [Fact] + public void Should_throw_if_name_is_already_registered() + { + TypeNameRegistry.Map(typeof(short), "short"); + + Assert.Throws(() => TypeNameRegistry.Map(typeof(byte), "short")); + } + + [Fact] + public void Should_throw_if_name_is_not_supported() + { + Assert.Throws(() => TypeNameRegistry.GetType("unsupported")); + } + + [Fact] + public void Should_throw_if_type_is_not_supported() + { + Assert.Throws(() => TypeNameRegistry.GetName()); + } + } +} diff --git a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure/HideAttribute.cs b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure/HideAttribute.cs deleted file mode 100644 index f24e6838b..000000000 --- a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure/HideAttribute.cs +++ /dev/null @@ -1,17 +0,0 @@ -// ========================================================================== -// HideAttribute.cs -// PinkParrot Headless CMS -// ========================================================================== -// Copyright (c) PinkParrot Group -// All rights reserved. -// ========================================================================== - -using System; - -namespace PinkParrot.Infrastructure -{ - [AttributeUsage(AttributeTargets.Property)] - public class HideAttribute : Attribute - { - } -} \ No newline at end of file diff --git a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure/Reflection/PropertiesTypeAccessor.cs b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure/Reflection/PropertiesTypeAccessor.cs index 5e8ccb10d..3e8794ddb 100644 --- a/src/pinkparrot_infrastructure/PinkParrot.Infrastructure/Reflection/PropertiesTypeAccessor.cs +++ b/src/pinkparrot_infrastructure/PinkParrot.Infrastructure/Reflection/PropertiesTypeAccessor.cs @@ -32,7 +32,7 @@ namespace PinkParrot.Infrastructure.Reflection { var allProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); - foreach (var property in allProperties.Where(property => property.CanRead && property.CanWrite)) + foreach (var property in allProperties) { accessors[property.Name] = new PropertyAccessor(type, property);