From: <fab...@us...> - 2010-07-30 17:03:06
|
Revision: 5082 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5082&view=rev Author: fabiomaulo Date: 2010-07-30 17:03:00 +0000 (Fri, 30 Jul 2010) Log Message: ----------- - ReflectionHelper tests - Renamed methods (more representative because return generic definition) - Added documentation for ReflectionHelper Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Linq/EnumerableHelper.cs trunk/nhibernate/src/NHibernate/Linq/Functions/QueryableGenerator.cs trunk/nhibernate/src/NHibernate/Linq/Functions/StringGenerator.cs trunk/nhibernate/src/NHibernate/Linq/LinqExtensionMethods.cs trunk/nhibernate/src/NHibernate/Linq/NhQueryProvider.cs trunk/nhibernate/src/NHibernate/Linq/NhRelinqQueryParser.cs trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessAggregate.cs trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessFirst.cs trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessSingle.cs trunk/nhibernate/src/NHibernate.Test/Linq/CustomExtensionsExample.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/UtilityTest/ReflectionHelperTest.cs Modified: trunk/nhibernate/src/NHibernate/Linq/EnumerableHelper.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/EnumerableHelper.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/EnumerableHelper.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -5,28 +5,58 @@ namespace NHibernate.Linq { - public static class ReflectionHelper - { - public delegate void Action(); + public static class ReflectionHelper + { + /// <summary> + /// Extract the <see cref="MethodInfo"/> from a given expression. + /// </summary> + /// <typeparam name="TSource">The declaring-type of the method.</typeparam> + /// <param name="method">The method.</param> + /// <returns>The <see cref="MethodInfo"/> of the no-generic method or the generic-definition for a generic-method.</returns> + /// <seealso cref="MethodInfo.GetGenericMethodDefinition"/> + public static MethodInfo GetMethodDefinition<TSource>(Expression<Action<TSource>> method) + { + if (method == null) + { + throw new ArgumentNullException("method"); + } + MethodInfo methodInfo = ((MethodCallExpression) method.Body).Method; + return methodInfo.IsGenericMethod ? methodInfo.GetGenericMethodDefinition() : methodInfo; + } - public static MethodInfo GetMethod<TSource>(Expression<Action<TSource>> method) - { - var methodInfo = ((MethodCallExpression) method.Body).Method; - return methodInfo.IsGenericMethod ? methodInfo.GetGenericMethodDefinition() : methodInfo; - } + /// <summary> + /// Extract the <see cref="MethodInfo"/> from a given expression. + /// </summary> + /// <param name="method">The method.</param> + /// <returns>The <see cref="MethodInfo"/> of the no-generic method or the generic-definition for a generic-method.</returns> + /// <seealso cref="MethodInfo.GetGenericMethodDefinition"/> + public static MethodInfo GetMethodDefinition(Expression<System.Action> method) + { + if (method == null) + { + throw new ArgumentNullException("method"); + } + var methodInfo = ((MethodCallExpression)method.Body).Method; + return methodInfo.IsGenericMethod ? methodInfo.GetGenericMethodDefinition() : methodInfo; + } - public static MethodInfo GetMethod(Expression<Action> method) - { - var methodInfo = ((MethodCallExpression)method.Body).Method; - return methodInfo.IsGenericMethod ? methodInfo.GetGenericMethodDefinition() : methodInfo; - } + /// <summary> + /// Gets the field or property to be accessed. + /// </summary> + /// <typeparam name="TSource">The declaring-type of the property.</typeparam> + /// <typeparam name="TResult">The type of the property.</typeparam> + /// <param name="property">The expression representing the property getter.</param> + /// <returns>The <see cref="MemberInfo"/> of the property.</returns> + public static MemberInfo GetProperty<TSource, TResult>(Expression<Func<TSource, TResult>> property) + { + if (property == null) + { + throw new ArgumentNullException("property"); + } + return ((MemberExpression)property.Body).Member; + } + } - public static MemberInfo GetProperty<TSource, TResult>(Expression<Func<TSource, TResult>> property) - { - return ((MemberExpression) property.Body).Member; - } - } - // TODO rename / remove - reflection helper above is better public static class EnumerableHelper { Modified: trunk/nhibernate/src/NHibernate/Linq/Functions/QueryableGenerator.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Functions/QueryableGenerator.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/Functions/QueryableGenerator.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -26,10 +26,10 @@ { SupportedMethods = new[] { - ReflectionHelper.GetMethod(() => Queryable.Any<object>(null)), - ReflectionHelper.GetMethod(() => Queryable.Any<object>(null, null)), - ReflectionHelper.GetMethod(() => Enumerable.Any<object>(null)), - ReflectionHelper.GetMethod(() => Enumerable.Any<object>(null, null)) + ReflectionHelper.GetMethodDefinition(() => Queryable.Any<object>(null)), + ReflectionHelper.GetMethodDefinition(() => Queryable.Any<object>(null, null)), + ReflectionHelper.GetMethodDefinition(() => Enumerable.Any<object>(null)), + ReflectionHelper.GetMethodDefinition(() => Enumerable.Any<object>(null, null)) }; } @@ -65,8 +65,8 @@ { SupportedMethods = new[] { - ReflectionHelper.GetMethod(() => Queryable.All<object>(null, null)), - ReflectionHelper.GetMethod(() => Enumerable.All<object>(null, null)) + ReflectionHelper.GetMethodDefinition(() => Queryable.All<object>(null, null)), + ReflectionHelper.GetMethodDefinition(() => Enumerable.All<object>(null, null)) }; } @@ -100,8 +100,8 @@ { SupportedMethods = new[] { - ReflectionHelper.GetMethod(() => Queryable.Min<object>(null)), - ReflectionHelper.GetMethod(() => Enumerable.Min<object>(null)) + ReflectionHelper.GetMethodDefinition(() => Queryable.Min<object>(null)), + ReflectionHelper.GetMethodDefinition(() => Enumerable.Min<object>(null)) }; } @@ -117,8 +117,8 @@ { SupportedMethods = new[] { - ReflectionHelper.GetMethod(() => Queryable.Max<object>(null)), - ReflectionHelper.GetMethod(() => Enumerable.Max<object>(null)) + ReflectionHelper.GetMethodDefinition(() => Queryable.Max<object>(null)), + ReflectionHelper.GetMethodDefinition(() => Enumerable.Max<object>(null)) }; } Modified: trunk/nhibernate/src/NHibernate/Linq/Functions/StringGenerator.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Functions/StringGenerator.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/Functions/StringGenerator.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -1,4 +1,4 @@ -using System.Collections.ObjectModel; +using System.Collections.ObjectModel; using System.Linq.Expressions; using System.Reflection; using NHibernate.Hql.Ast; @@ -40,7 +40,7 @@ { public StartsWithGenerator() { - SupportedMethods = new[] { ReflectionHelper.GetMethod<string>(x => x.StartsWith(null)) }; + SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<string>(x => x.StartsWith(null)) }; } public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) @@ -57,7 +57,7 @@ { public EndsWithGenerator() { - SupportedMethods = new[] { ReflectionHelper.GetMethod<string>(x => x.EndsWith(null)) }; + SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<string>(x => x.EndsWith(null)) }; } public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) @@ -74,7 +74,7 @@ { public ContainsGenerator() { - SupportedMethods = new[] { ReflectionHelper.GetMethod<string>(x => x.Contains(null)) }; + SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<string>(x => x.Contains(null)) }; } public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) @@ -92,7 +92,7 @@ { public EqualsGenerator() { - SupportedMethods = new[] { ReflectionHelper.GetMethod<string>(x => x.Equals((string)null)) }; + SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<string>(x => x.Equals((string)null)) }; } public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) @@ -109,10 +109,10 @@ { SupportedMethods = new[] { - ReflectionHelper.GetMethod<string>(x => x.ToUpper()), - ReflectionHelper.GetMethod<string>(x => x.ToUpperInvariant()), - ReflectionHelper.GetMethod<string>(x => x.ToLower()), - ReflectionHelper.GetMethod<string>(x => x.ToLowerInvariant()) + ReflectionHelper.GetMethodDefinition<string>(x => x.ToUpper()), + ReflectionHelper.GetMethodDefinition<string>(x => x.ToUpperInvariant()), + ReflectionHelper.GetMethodDefinition<string>(x => x.ToLower()), + ReflectionHelper.GetMethodDefinition<string>(x => x.ToLowerInvariant()) }; } @@ -139,8 +139,8 @@ { SupportedMethods = new[] { - ReflectionHelper.GetMethod<string>(s => s.Substring(0)), - ReflectionHelper.GetMethod<string>(s => s.Substring(0, 0)) + ReflectionHelper.GetMethodDefinition<string>(s => s.Substring(0)), + ReflectionHelper.GetMethodDefinition<string>(s => s.Substring(0, 0)) }; } public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) @@ -164,10 +164,10 @@ { SupportedMethods = new[] { - ReflectionHelper.GetMethod<string>(s => s.IndexOf(' ')), - ReflectionHelper.GetMethod<string>(s => s.IndexOf(" ")), - ReflectionHelper.GetMethod<string>(s => s.IndexOf(' ', 0)), - ReflectionHelper.GetMethod<string>(s => s.IndexOf(" ", 0)) + ReflectionHelper.GetMethodDefinition<string>(s => s.IndexOf(' ')), + ReflectionHelper.GetMethodDefinition<string>(s => s.IndexOf(" ")), + ReflectionHelper.GetMethodDefinition<string>(s => s.IndexOf(' ', 0)), + ReflectionHelper.GetMethodDefinition<string>(s => s.IndexOf(" ", 0)) }; } public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) @@ -192,8 +192,8 @@ { SupportedMethods = new[] { - ReflectionHelper.GetMethod<string>(s => s.Replace(' ', ' ')), - ReflectionHelper.GetMethod<string>(s => s.Replace("", "")) + ReflectionHelper.GetMethodDefinition<string>(s => s.Replace(' ', ' ')), + ReflectionHelper.GetMethodDefinition<string>(s => s.Replace("", "")) }; } Modified: trunk/nhibernate/src/NHibernate/Linq/LinqExtensionMethods.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/LinqExtensionMethods.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/LinqExtensionMethods.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using System.Linq.Expressions; namespace NHibernate.Linq @@ -12,7 +12,7 @@ public static IQueryable<T> Cacheable<T>(this IQueryable<T> query) { - var method = ReflectionHelper.GetMethod(() => Cacheable<object>(null)).MakeGenericMethod(typeof(T)); + var method = ReflectionHelper.GetMethodDefinition(() => Cacheable<object>(null)).MakeGenericMethod(typeof(T)); var callExpression = Expression.Call(method, query.Expression); @@ -21,7 +21,7 @@ public static IQueryable<T> CacheMode<T>(this IQueryable<T> query, CacheMode cacheMode) { - var method = ReflectionHelper.GetMethod(() => CacheMode<object>(null, NHibernate.CacheMode.Normal)).MakeGenericMethod(typeof(T)); + var method = ReflectionHelper.GetMethodDefinition(() => CacheMode<object>(null, NHibernate.CacheMode.Normal)).MakeGenericMethod(typeof(T)); var callExpression = Expression.Call(method, query.Expression, Expression.Constant(cacheMode)); @@ -30,7 +30,7 @@ public static IQueryable<T> CacheRegion<T>(this IQueryable<T> query, string region) { - var method = ReflectionHelper.GetMethod(() => CacheRegion<object>(null, null)).MakeGenericMethod(typeof(T)); + var method = ReflectionHelper.GetMethodDefinition(() => CacheRegion<object>(null, null)).MakeGenericMethod(typeof(T)); var callExpression = Expression.Call(method, query.Expression, Expression.Constant(region)); Modified: trunk/nhibernate/src/NHibernate/Linq/NhQueryProvider.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/NhQueryProvider.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/NhQueryProvider.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -1,4 +1,4 @@ -using System.Collections; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -57,7 +57,7 @@ public IQueryable CreateQuery(Expression expression) { - var m = ReflectionHelper.GetMethod((NhQueryProvider p) => p.CreateQuery<object>(null)).MakeGenericMethod(expression.Type.GetGenericArguments()[0]); + var m = ReflectionHelper.GetMethodDefinition((NhQueryProvider p) => p.CreateQuery<object>(null)).MakeGenericMethod(expression.Type.GetGenericArguments()[0]); return (IQueryable) m.Invoke(this, new[] {expression}); } Modified: trunk/nhibernate/src/NHibernate/Linq/NhRelinqQueryParser.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/NhRelinqQueryParser.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/NhRelinqQueryParser.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -23,9 +23,9 @@ new[] { MethodCallExpressionNodeTypeRegistry.GetRegisterableMethodDefinition( - ReflectionHelper.GetMethod(() => Queryable.Aggregate<object>(null, null))), + ReflectionHelper.GetMethodDefinition(() => Queryable.Aggregate<object>(null, null))), MethodCallExpressionNodeTypeRegistry.GetRegisterableMethodDefinition( - ReflectionHelper.GetMethod(() => Queryable.Aggregate<object, object>(null, null, null))) + ReflectionHelper.GetMethodDefinition(() => Queryable.Aggregate<object, object>(null, null, null))) }, typeof (AggregateExpressionNode)); @@ -33,7 +33,7 @@ new[] { MethodCallExpressionNodeTypeRegistry.GetRegisterableMethodDefinition( - ReflectionHelper.GetMethod((List<object> l) => l.Contains(null))), + ReflectionHelper.GetMethodDefinition((List<object> l) => l.Contains(null))), }, typeof (ContainsExpressionNode)); Modified: trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessAggregate.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessAggregate.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessAggregate.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -1,4 +1,4 @@ -using System.Collections; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -21,7 +21,7 @@ if (resultOperator.ParseInfo.ParsedExpression.Arguments.Count == 2) { - var aggregate = ReflectionHelper.GetMethod(() => Enumerable.Aggregate<object>(null, null)); + var aggregate = ReflectionHelper.GetMethodDefinition(() => Enumerable.Aggregate<object>(null, null)); aggregate = aggregate.GetGenericMethodDefinition().MakeGenericMethod(inputType); call = Expression.Call( @@ -33,7 +33,7 @@ } else if (resultOperator.ParseInfo.ParsedExpression.Arguments.Count == 3) { - var aggregate = ReflectionHelper.GetMethod(() => Enumerable.Aggregate<object, object>(null, null, null)); + var aggregate = ReflectionHelper.GetMethodDefinition(() => Enumerable.Aggregate<object, object>(null, null, null)); aggregate = aggregate.GetGenericMethodDefinition().MakeGenericMethod(inputType, accumulatorType); call = Expression.Call( @@ -46,7 +46,7 @@ else { var selectorType = resultOperator.OptionalSelector.Type.GetGenericArguments()[2]; - var aggregate = ReflectionHelper.GetMethod(() => Enumerable.Aggregate<object, object, object>(null, null, null, null)); + var aggregate = ReflectionHelper.GetMethodDefinition(() => Enumerable.Aggregate<object, object, object>(null, null, null, null)); aggregate = aggregate.GetGenericMethodDefinition().MakeGenericMethod(inputType, accumulatorType, selectorType); call = Expression.Call( Modified: trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessFirst.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessFirst.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessFirst.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using Remotion.Data.Linq.Clauses.ResultOperators; namespace NHibernate.Linq.Visitors.ResultOperatorProcessors @@ -8,8 +8,8 @@ public void Process(FirstResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree) { var firstMethod = resultOperator.ReturnDefaultWhenEmpty - ? ReflectionHelper.GetMethod(() => Queryable.FirstOrDefault<object>(null)) - : ReflectionHelper.GetMethod(() => Queryable.First<object>(null)); + ? ReflectionHelper.GetMethodDefinition(() => Queryable.FirstOrDefault<object>(null)) + : ReflectionHelper.GetMethodDefinition(() => Queryable.First<object>(null)); AddClientSideEval(firstMethod, queryModelVisitor, tree); Modified: trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessSingle.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessSingle.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessSingle.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using Remotion.Data.Linq.Clauses.ResultOperators; namespace NHibernate.Linq.Visitors.ResultOperatorProcessors @@ -8,8 +8,8 @@ public void Process(SingleResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree) { var firstMethod = resultOperator.ReturnDefaultWhenEmpty - ? ReflectionHelper.GetMethod(() => Queryable.SingleOrDefault<object>(null)) - : ReflectionHelper.GetMethod(() => Queryable.Single<object>(null)); + ? ReflectionHelper.GetMethodDefinition(() => Queryable.SingleOrDefault<object>(null)) + : ReflectionHelper.GetMethodDefinition(() => Queryable.Single<object>(null)); AddClientSideEval(firstMethod, queryModelVisitor, tree); } Modified: trunk/nhibernate/src/NHibernate.Test/Linq/CustomExtensionsExample.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Linq/CustomExtensionsExample.cs 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate.Test/Linq/CustomExtensionsExample.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -29,7 +29,7 @@ { public MyLinqToHqlGeneratorsRegistry():base() { - RegisterGenerator(ReflectionHelper.GetMethod(() => MyLinqExtensions.IsLike(null, null)), + RegisterGenerator(ReflectionHelper.GetMethodDefinition(() => MyLinqExtensions.IsLike(null, null)), new IsLikeGenerator()); } } @@ -38,7 +38,7 @@ { public IsLikeGenerator() { - SupportedMethods = new[] {ReflectionHelper.GetMethod(() => MyLinqExtensions.IsLike(null, null))}; + SupportedMethods = new[] {ReflectionHelper.GetMethodDefinition(() => MyLinqExtensions.IsLike(null, null))}; } public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-07-29 13:10:59 UTC (rev 5081) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-07-30 17:03:00 UTC (rev 5082) @@ -440,6 +440,7 @@ <Compile Include="Linq\QueryCacheableTests.cs" /> <Compile Include="Linq\QueryReuseTests.cs" /> <Compile Include="Linq\ReadonlyTestCase.cs" /> + <Compile Include="UtilityTest\ReflectionHelperTest.cs" /> <Compile Include="Linq\RegresstionTests.cs" /> <Compile Include="Linq\SelectionTests.cs" /> <Compile Include="Linq\WhereSubqueryTests.cs" /> Added: trunk/nhibernate/src/NHibernate.Test/UtilityTest/ReflectionHelperTest.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/UtilityTest/ReflectionHelperTest.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/UtilityTest/ReflectionHelperTest.cs 2010-07-30 17:03:00 UTC (rev 5082) @@ -0,0 +1,74 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using NHibernate.Linq; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Test.UtilityTest +{ + public class ReflectionHelperTest + { + private class MyClass + { + public void NoGenericMethod() {} + public void GenericMethod<T>() { } + public string BaseProperty { get; set; } + public bool BaseBool { get; set; } + } + + [Test] + public void WhenGetMethodForNullThenThrows() + { + Executing.This(() => ReflectionHelper.GetMethodDefinition((Expression<System.Action>) null)).Should().Throw<ArgumentNullException>(); + } + + [Test] + public void WhenGenericGetMethodForNullThenThrows() + { + Executing.This(() => ReflectionHelper.GetMethodDefinition<object>((Expression<System.Action<object>>)null)).Should().Throw<ArgumentNullException>(); + } + + [Test] + public void WhenGetPropertyForNullThenThrows() + { + Executing.This(() => ReflectionHelper.GetProperty<object, object>(null)).Should().Throw<ArgumentNullException>(); + } + + [Test] + public void WhenGenericMethodOfClassThenReturnGenericDefinition() + { + ReflectionHelper.GetMethodDefinition<MyClass>(mc => mc.GenericMethod<int>()).Should().Be(typeof (MyClass).GetMethod("GenericMethod").GetGenericMethodDefinition()); + } + + [Test] + public void WhenNoGenericMethodOfClassThenReturnDefinition() + { + ReflectionHelper.GetMethodDefinition<MyClass>(mc => mc.NoGenericMethod()).Should().Be(typeof(MyClass).GetMethod("NoGenericMethod")); + } + + [Test] + public void WhenStaticGenericMethodThenReturnGenericDefinition() + { + ReflectionHelper.GetMethodDefinition(() => Enumerable.All<int>(null, null)).Should().Be(typeof(Enumerable).GetMethod("All").GetGenericMethodDefinition()); + } + + [Test] + public void WhenStaticNoGenericMethodThenReturnDefinition() + { + ReflectionHelper.GetMethodDefinition(() => string.Join(null, null)).Should().Be(typeof(string).GetMethod("Join", new []{typeof(string), typeof(string[])})); + } + + [Test] + public void WhenGetPropertyThenReturnPropertyInfo() + { + ReflectionHelper.GetProperty<MyClass, string>(mc => mc.BaseProperty).Should().Be(typeof(MyClass).GetProperty("BaseProperty")); + } + + [Test] + public void WhenGetPropertyForBoolThenReturnPropertyInfo() + { + ReflectionHelper.GetProperty<MyClass, bool>(mc => mc.BaseBool).Should().Be(typeof(MyClass).GetProperty("BaseBool")); + } + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |