Skip to content

Commit 91bfbca

Browse files
committed
Propertyselector extension tests added
1 parent c1b33f1 commit 91bfbca

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Linq.Expressions;
4+
using NUnit.Framework;
5+
using RediSearchSharp.Utils;
6+
7+
namespace RediSearchSharp.Tests
8+
{
9+
[TestFixture]
10+
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
11+
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
12+
public class PropertySelectorExtensionsTests
13+
{
14+
public class GetMemberName
15+
{
16+
private class ChildType
17+
{
18+
public string ChildTypeProperty { get; set; }
19+
}
20+
21+
private class TestType
22+
{
23+
public string TestTypeProperty { get; set; }
24+
public ChildType ChildType { get; set; }
25+
}
26+
27+
[Test]
28+
public void Should_throw_when_the_property_selector_is_not_a_member_expression()
29+
{
30+
Expression<Func<TestType, string>> testExpression = t => t.ToString();
31+
Assert.Throws<ArgumentException>(() =>
32+
{
33+
testExpression.GetMemberName();
34+
});
35+
}
36+
37+
[Test]
38+
public void Should_throw_when_the_expression_is_not_referring_to_a_property_of_the_type()
39+
{
40+
Expression<Func<TestType, string>> testExpression = t => t.ChildType.ChildTypeProperty;
41+
Assert.Throws<ArgumentException>(() =>
42+
{
43+
testExpression.GetMemberName();
44+
});
45+
}
46+
47+
[Test]
48+
public void Should_return_the_property_name()
49+
{
50+
Expression<Func<TestType, string>> testExpression = t => t.TestTypeProperty;
51+
52+
string memberName = testExpression.GetMemberName();
53+
54+
Assert.That(memberName, Is.EqualTo("TestTypeProperty"));
55+
}
56+
}
57+
}
58+
}

RediSearchSharp/Utils/PropertySelectorExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace RediSearchSharp.Utils
55
{
66
public static class PropertySelectorExtensions
77
{
8-
public static string GetMemberName<TEntity, TProperty>(this Expression<Func<TEntity, TProperty>> propertySelector)
8+
public static string GetMemberName<TEntity, TProperty>(
9+
this Expression<Func<TEntity, TProperty>> propertySelector)
910
{
1011
if (!(propertySelector.Body is MemberExpression memberExpression))
1112
{
@@ -14,13 +15,12 @@ public static string GetMemberName<TEntity, TProperty>(this Expression<Func<TEnt
1415

1516
var memberInfo = memberExpression.Member;
1617

17-
#if NETSTANDARD2_0 || NET45 || NET46
18-
if (memberInfo.ReflectedType != typeof(TEntity) &&
19-
!typeof(TEntity).IsAssignableFrom(memberInfo.ReflectedType))
20-
{
21-
throw new ArgumentException("Property selector does not refer to a property of the entity.");
22-
}
23-
#endif
18+
if (memberInfo.ReflectedType != typeof(TEntity) &&
19+
!typeof(TEntity).IsAssignableFrom(memberInfo.ReflectedType))
20+
{
21+
throw new ArgumentException("Property selector does not refer to a property of the entity.");
22+
}
23+
2424
return memberInfo.Name;
2525
}
2626
}

0 commit comments

Comments
 (0)