-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 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
20 changes: 20 additions & 0 deletions
20
DynamicExpressions.UnitTests/DynamicExpressions.UnitTests.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,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> |
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,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; } | ||
} | ||
} |
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