Browse Source

More tests

pull/1/head
Sebastian Stehle 9 years ago
parent
commit
db68d5fc8b
  1. 19
      src/RunCoverage.bat
  2. 9
      src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/PropertiesBagTests.cs
  3. 107
      src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/PropertiesTypeAccessorTest.cs
  4. 10
      src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/SimpleMapperTests.cs
  5. 69
      src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/TypeNameRegistryTests.cs
  6. 17
      src/pinkparrot_infrastructure/PinkParrot.Infrastructure/HideAttribute.cs
  7. 2
      src/pinkparrot_infrastructure/PinkParrot.Infrastructure/Reflection/PropertiesTypeAccessor.cs

19
src/RunCoverage.bat

@ -6,20 +6,16 @@ CD %~dp0
FOR /D /R %%X IN (%USERNAME%*) DO RD /S /Q "%%X" FOR /D /R %%X IN (%USERNAME%*) DO RD /S /Q "%%X"
REM Run the tests against the targeted output REM Run the tests against the targeted output
call :RunOpenCoverUnitTestMetrics call :GenerateCoverage
REM Generate the report output based on the test results REM Generate the report output based on the test results
if %errorlevel% equ 0 ( if %errorlevel% equ 0 (
call :RunReportGeneratorOutput call :RunReportGeneratorOutput
) )
REM Launch the report
if %errorlevel% equ 0 (
call :RunLaunchReport
)
exit /b %errorlevel% exit /b %errorlevel%
:RunOpenCoverUnitTestMetrics :GenerateCoverage
"%UserProfile%\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe" ^ "%UserProfile%\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe" ^
-register:user ^ -register:user ^
-target:"C:\Program Files\dotnet\dotnet.exe" ^ -target:"C:\Program Files\dotnet\dotnet.exe" ^
@ -28,10 +24,19 @@ exit /b %errorlevel%
-skipautoprops ^ -skipautoprops ^
-output:"%~dp0\GeneratedReports\Infrastructure.xml" ^ -output:"%~dp0\GeneratedReports\Infrastructure.xml" ^
-oldStyle -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% exit /b %errorlevel%
:RunReportGeneratorOutput :RunReportGeneratorOutput
"%UserProfile%\.nuget\packages\ReportGenerator\2.4.5\tools\ReportGenerator.exe" ^ "%UserProfile%\.nuget\packages\ReportGenerator\2.4.5\tools\ReportGenerator.exe" ^
-reports:"%~dp0\GeneratedReports\Infrastructure.xml" ^ -reports:"%~dp0\GeneratedReports\*.xml" ^
-targetdir:"%~dp0\GeneratedReports\Output" -targetdir:"%~dp0\GeneratedReports\Output"
exit /b %errorlevel% exit /b %errorlevel%

9
src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/PropertiesBagTests.cs

@ -97,8 +97,17 @@ namespace PinkParrot.Infrastructure
bag.Set("Key2", 1); bag.Set("Key2", 1);
Assert.Equal(2, bag.Count); Assert.Equal(2, bag.Count);
}
[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.PropertyNames.ToArray());
Assert.Equal(new[] { "Key1", "Key2" }, bag.Properties.Keys.ToArray()); Assert.Equal(new[] { "Key1", "Key2" }, bag.Properties.Keys.ToArray());
Assert.Equal(new[] { "Key1", "Key2" }, bag.GetDynamicMemberNames().ToArray());
} }
[Fact] [Fact]

107
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<ArgumentException>(() => accessor.SetValue(target, "Unknown", 123));
}
[Fact]
public void Should_throw_if_setting_readonly()
{
Assert.Throws<NotSupportedException>(() => 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<ArgumentException>(() => accessor.GetValue(target, "Unknown"));
}
[Fact]
public void Should_throw_if_getting_readonly()
{
Assert.Throws<NotSupportedException>(() => accessor.GetValue(target, "Write"));
}
}
}

10
src/pinkparrot_infrastructure/PinkParrot.Infrastructure.Tests/Reflection/SimpleMapperTests.cs

@ -22,6 +22,10 @@ namespace PinkParrot.Infrastructure.Reflection
public long MappedNumber { get; set; } public long MappedNumber { get; set; }
public Guid MappedGuid { get; set; } public Guid MappedGuid { get; set; }
public long WrongType1 { get; set; }
public long WrongType2 { get; set; }
} }
public class Class1 : Class1Base public class Class1 : Class1Base
@ -46,6 +50,10 @@ namespace PinkParrot.Infrastructure.Reflection
{ {
get { return "Value"; } get { return "Value"; }
} }
public DateTime WrongType1 { get; set; }
public TimeSpan WrongType2 { get; set; }
} }
[Fact] [Fact]
@ -77,6 +85,8 @@ namespace PinkParrot.Infrastructure.Reflection
Assert.Equal(class1.MappedString, class2.MappedString); Assert.Equal(class1.MappedString, class2.MappedString);
Assert.Equal(class1.MappedNumber, class2.MappedNumber); Assert.Equal(class1.MappedNumber, class2.MappedNumber);
Assert.Equal(class1.MappedGuid.ToString(), class2.MappedGuid); Assert.Equal(class1.MappedGuid.ToString(), class2.MappedGuid);
Assert.Equal(class1.WrongType1, 0L);
Assert.Equal(class1.WrongType2, 0L);
Assert.NotEqual(class1.UnmappedString, class2.UnmappedString); Assert.NotEqual(class1.UnmappedString, class2.UnmappedString);
} }
} }

69
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<int>());
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<MyType>());
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<ArgumentException>(() => TypeNameRegistry.Map(typeof(long), "longer"));
}
[Fact]
public void Should_throw_if_name_is_already_registered()
{
TypeNameRegistry.Map(typeof(short), "short");
Assert.Throws<ArgumentException>(() => TypeNameRegistry.Map(typeof(byte), "short"));
}
[Fact]
public void Should_throw_if_name_is_not_supported()
{
Assert.Throws<ArgumentException>(() => TypeNameRegistry.GetType("unsupported"));
}
[Fact]
public void Should_throw_if_type_is_not_supported()
{
Assert.Throws<ArgumentException>(() => TypeNameRegistry.GetName<Guid>());
}
}
}

17
src/pinkparrot_infrastructure/PinkParrot.Infrastructure/HideAttribute.cs

@ -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
{
}
}

2
src/pinkparrot_infrastructure/PinkParrot.Infrastructure/Reflection/PropertiesTypeAccessor.cs

@ -32,7 +32,7 @@ namespace PinkParrot.Infrastructure.Reflection
{ {
var allProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); 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); accessors[property.Name] = new PropertyAccessor(type, property);

Loading…
Cancel
Save