Skip to content

Commit

Permalink
Merge pull request #447 from slang25/tooling-tidy-up
Browse files Browse the repository at this point in the history
Consolidate conditional code
  • Loading branch information
josephwoodward committed Jul 6, 2017
2 parents fa13863 + dc4f535 commit 0d877ee
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Shouldly;

// ReSharper disable CheckNamespace
namespace ExpressionToString
Expand Down Expand Up @@ -229,20 +230,11 @@ void VisitArguments(Expression[] arguments)

static bool CheckIfAnonymousType(Type type)
{
#if NewReflection
// hack: the only way to detect anonymous types right now
var typeInfo = type.GetTypeInfo();
var isDefined = typeInfo.IsDefined(typeof(CompilerGeneratedAttribute), false);
return isDefined
&& (typeInfo.IsGenericType && type.Name.Contains("AnonymousType") || type.Name.Contains("DisplayClass"))
&& (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"));
#else
// hack: the only way to detect anonymous types right now
var isDefined = type.IsDefined(typeof(CompilerGeneratedAttribute), false);
return isDefined
&& (type.IsGenericType && type.Name.Contains("AnonymousType") || type.Name.Contains("DisplayClass"))
&& (type.IsGenericType() && type.Name.Contains("AnonymousType") || type.Name.Contains("DisplayClass"))
&& (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"));
#endif
}

static string ToString(ExpressionType type)
Expand Down
7 changes: 1 addition & 6 deletions src/Shouldly/Internals/EqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ public bool Equals(T x, T y)
return true;

// Null?
#if NewReflection
var typeInfo = type.GetTypeInfo();
if (!typeInfo.IsValueType || (typeInfo.IsGenericType && type.GetGenericTypeDefinition().GetTypeInfo().IsAssignableFrom(NullableType.GetTypeInfo())))
#else
if (!type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition().IsAssignableFrom(NullableType)))
#endif
if (!type.IsValueType() || (type.IsGenericType() && type.GetGenericTypeDefinition().IsAssignableFrom(NullableType)))
{
if (object.Equals(x, default(T)))
return object.Equals(y, default(T));
Expand Down
17 changes: 2 additions & 15 deletions src/Shouldly/Internals/Is.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

#if NewReflection
using System.Reflection;
#endif

namespace Shouldly
{
Expand Down Expand Up @@ -177,13 +174,7 @@ public static bool Equal(TimeSpan actual, TimeSpan expected, TimeSpan tolerance)

public static bool InstanceOf(object o, Type expected)
{
if (o == null)
return false;
#if NewReflection
return expected.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo());
#else
return expected.IsInstanceOfType(o);
#endif
return o != null ? expected.IsInstanceOfType(o) : false;
}

public static bool StringMatchingRegex(string actual, string regexPattern)
Expand Down Expand Up @@ -315,11 +306,7 @@ static decimal Compare<T>(T actual, T expected, IComparer<T> comparer)
}
static decimal Compare<T>(IComparable<T> comparable, T expected)
{
#if NewReflection
if (!typeof(T).GetTypeInfo().IsValueType)
#else
if (!typeof(T).IsValueType)
#endif
if (!typeof(T).IsValueType())
{
// ReSharper disable CompareNonConstrainedGenericWithNull
if (comparable == null)
Expand Down
7 changes: 1 addition & 6 deletions src/Shouldly/Internals/ShouldlyCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ internal static bool IsShouldlyMethod(this MethodBase method)
{
if (method.DeclaringType == null)
return false;
#if NewReflection
var declaringType = method.DeclaringType.GetTypeInfo();
return declaringType.GetCustomAttributes(typeof(ShouldlyMethodsAttribute), true).Any()
|| (method.DeclaringType.DeclaringType != null && declaringType.DeclaringType.GetTypeInfo().GetCustomAttributes(typeof(ShouldlyMethodsAttribute), true).Any());
#else

return method.DeclaringType.GetCustomAttributes(typeof(ShouldlyMethodsAttribute), true).Any()
|| (method.DeclaringType.DeclaringType != null && method.DeclaringType.DeclaringType.GetCustomAttributes(typeof(ShouldlyMethodsAttribute), true).Any());
#endif
}
#endif

Expand Down
13 changes: 1 addition & 12 deletions src/Shouldly/Internals/StringHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,11 @@ internal static string ToStringAwesomely(this object value)
}
#endif

#if NewReflection
var typeInfo = type.GetTypeInfo();
if (typeInfo.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
var key = type.GetRuntimeProperty("Key").GetValue(value, null);
var v = type.GetRuntimeProperty("Value").GetValue(value, null);
return $"[{key.ToStringAwesomely()} => {v.ToStringAwesomely()}]";
}
#else
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>)){
if (type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>)){
var key = type.GetProperty("Key").GetValue(value, null);
var v = type.GetProperty("Value").GetValue(value, null);
return $"[{key.ToStringAwesomely()} => {v.ToStringAwesomely()}]";
}
#endif


var toString = value.ToString();
if (toString == type.FullName)
Expand Down
26 changes: 26 additions & 0 deletions src/Shouldly/Internals/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Reflection;

namespace Shouldly
{
internal static class TypeExtensions
{
public static bool IsValueType(this Type type) =>
#if NewReflection
type.GetTypeInfo().IsValueType;
#else
type.IsValueType;
#endif
public static bool IsGenericType(this Type type) =>
#if NewReflection
type.GetTypeInfo().IsGenericType;
#else
type.IsGenericType;
#endif

#if NewReflection
public static bool IsDefined(this Type type, Type attributeType, bool inherit) =>
type.GetTypeInfo().IsDefined(attributeType, inherit);
#endif
}
}
5 changes: 1 addition & 4 deletions src/Shouldly/ShouldStaticClasses/DynamicShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using JetBrains.Annotations;

#if NewReflection
using System.Reflection;
#endif
using JetBrains.Annotations;

namespace Shouldly
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if Async
using System.Threading;
using System;
using System.Reflection;
using System.Threading.Tasks;
using JetBrains.Annotations;

Expand Down Expand Up @@ -136,12 +137,10 @@ private static void CompleteIn(Task actual, TimeSpan timeout, [InstantHandle] Fu
private static void PreserveStackTrace(Exception exception)
{
// TODO Need to sort this out for core
#if !NewReflection
System.Reflection.MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace",
BindingFlags.Instance | BindingFlags.NonPublic);

preserveStackTrace?.Invoke(exception, null);
#endif
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using JetBrains.Annotations;

#if NewReflection
using System.Reflection;
#endif
using JetBrains.Annotations;

namespace Shouldly.ShouldlyExtensionMethods
{
Expand Down Expand Up @@ -42,11 +39,7 @@ public static void ShouldNotHaveFlag(this Enum actual, Enum expectedFlag, string

static void CheckEnumHasFlagAttribute(Enum actual)
{
#if NewReflection
if (!actual.GetType().GetTypeInfo().IsDefined(typeof(FlagsAttribute), false))
#else
if (!actual.GetType().IsDefined(typeof(FlagsAttribute), false))
#endif
{
throw new ArgumentException("Enum doesn't have Flags attribute", nameof(actual));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ public static void ShouldBeAssignableTo(this object actual, Type expected, [Inst
{
actual.AssertAwesomely(v =>
{
#if NewReflection
if (actual == null && !expected.GetTypeInfo().IsValueType)
if (actual == null && !expected.IsValueType())
return true;
#else
if (actual == null && !expected.IsValueType)
return true;
#endif
return Is.InstanceOf(v, expected);
}, actual, expected, customMessage);
Expand Down

0 comments on commit 0d877ee

Please sign in to comment.