Skip to content

Commit

Permalink
Add PropertyGetter unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zHaytam committed May 16, 2020
1 parent 7b1ed8f commit a2f7f09
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DynamicExpressions\DynamicExpressions.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions DynamicExpressions.UnitTests/DynamicExpressions.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DynamicExpressions\DynamicExpressions.csproj" />
</ItemGroup>

</Project>
63 changes: 63 additions & 0 deletions DynamicExpressions.UnitTests/PropertyGetterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace DynamicExpressions.UnitTests
{
public class PropertyGetterTests
{
[Fact]
public void GetPropertyGetter_ShouldThrow_WhenPropertyIsNull()
{
var ex = Assert.Throws<ArgumentNullException>(() =>
{
DynamicExpressions.GetPropertyGetter<PropertyGetterTests>(null);
});

Assert.Equal("Value cannot be null. (Parameter 'property')", ex.Message);
}

[Fact]
public void GetPropertyGetter_ShouldReturnCorrectGetter()
{
var entry = new Entry { Id = 2 };

var getter = DynamicExpressions.GetPropertyGetter<Entry>("Id").Compile();
var id = getter(entry);

Assert.Equal(entry.Id, id);
}

[Fact]
public void GetPropertyGetter_ShouldBeUsableInQueryableOrderBy()
{
var entries = new List<Entry>
{
new Entry { Id = 1 },
new Entry { Id = 2 },
};

var getter = DynamicExpressions.GetPropertyGetter<Entry>("Id");
var sortedEntries = entries.AsQueryable().OrderByDescending(getter).ToList();

Assert.Equal(entries[0], sortedEntries[1]);
Assert.Equal(entries[1], sortedEntries[0]);
}

[Fact]
public void GetPropertyGetter_ShouldThrow_WhenPropertyDoesntExist()
{
var entry = new Entry { Id = 2 };

var ex = Assert.Throws<ArgumentException>(() => DynamicExpressions.GetPropertyGetter<Entry>("Test"));

Assert.Equal("Instance property 'Test' is not defined for type 'DynamicExpressions.UnitTests.Entry' (Parameter 'propertyName')", ex.Message);
}
}

internal class Entry
{
public int Id { get; set; }
}
}
6 changes: 6 additions & 0 deletions DynamicExpressions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DynamicExpressions", "Dynam
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DynamicExpressions.Benchmarks", "DynamicExpressions.Benchmarks\DynamicExpressions.Benchmarks.csproj", "{B0EC7669-9F45-4CE1-B53C-917BC5480D5C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicExpressions.UnitTests", "DynamicExpressions.UnitTests\DynamicExpressions.UnitTests.csproj", "{48453801-E6A9-4FF9-994F-BC172E8056D9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{B0EC7669-9F45-4CE1-B53C-917BC5480D5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0EC7669-9F45-4CE1-B53C-917BC5480D5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0EC7669-9F45-4CE1-B53C-917BC5480D5C}.Release|Any CPU.Build.0 = Release|Any CPU
{48453801-E6A9-4FF9-994F-BC172E8056D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48453801-E6A9-4FF9-994F-BC172E8056D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48453801-E6A9-4FF9-994F-BC172E8056D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48453801-E6A9-4FF9-994F-BC172E8056D9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit a2f7f09

Please sign in to comment.