Skip to content
Browse files

More fixes

  • Loading branch information...
1 parent 6da17be commit 8c9dacb06927990ed7e79ebf80402a488e07a3aa Matthew Baxter-Reynolds (@mbrit) committed
View
19 Moq.MetroStyle/Castle/AbstractInvocation.cs
@@ -116,6 +116,25 @@ public MethodInfo GetConcreteMethodInvocationTarget()
public object ReturnValue { get; set; }
+#if NETFX_CORE
+ // @mbrit - 2012-05-31 - something is broken with casting, or boxing, or similiar
+ // on the WinRT side. this approach provides for performing stronger type mangling
+ // in this explicit method, rather than relying on emitting the correct IL...
+ public T GetReturnValueForWinRt<T>()
+ {
+ // if we don't have a value, just punt it back...
+ if (this.ReturnValue == null)
+ return default(T);
+
+ // if we can cast, just do that...
+ var type = this.ReturnValue.GetType();
+ if (typeof(T).IsAssignableFrom(type))
+ return (T)this.ReturnValue;
+ else
+ return (T)Convert.ChangeType(this.ReturnValue, typeof(T));
+ }
+#endif
+
public object[] Arguments
{
get { return arguments; }
View
33 Moq.MetroStyle/Castle/Generators/Emitters/SimpleAST/ExplicitConvertExpression.cs
@@ -0,0 +1,33 @@
+//#if NETFX_CORE
+
+//using System;
+//using System.Collections.Generic;
+//using System.Linq;
+//using System.Reflection.Emit;
+//using System.Text;
+//using System.Threading.Tasks;
+//using Castle.DynamicProxy.Generators.Emitters;
+//using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
+
+//namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
+//{
+// public class ExplicitConvertExpression : ConvertExpression
+// {
+// public ExplicitConvertExpression(Type targetType, Expression right)
+// : base(targetType, right)
+// {
+// }
+
+// public override void Emit(IMemberEmitter member, ILGenerator gen)
+// {
+// if (fromType == typeof(object) && target == typeof(int))
+// {
+// throw new NotImplementedException("This operation has not been implemented.");
+// }
+// else
+// base.Emit(member, gen);
+// }
+// }
+//}
+
+//#endif
View
2 Moq.MetroStyle/Castle/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs
@@ -22,7 +22,7 @@ public class MethodInvocationExpression : Expression
protected readonly Expression[] args;
protected readonly MethodInfo method;
protected readonly Reference owner;
-
+
public MethodInvocationExpression(MethodInfo method, params Expression[] args) :
this(SelfReference.Self, method, args)
{
View
25 Moq.MetroStyle/Castle/Generators/MethodWithInvocationGenerator.cs
@@ -129,8 +129,29 @@ protected FieldReference BuildMethodInterceptorsField(ClassEmitter @class, Metho
if (MethodToOverride.ReturnType != typeof(void))
{
// Emit code to return with cast from ReturnValue
- var getRetVal = new MethodInvocationExpression(invocationLocal, InvocationMethods.GetReturnValue);
- emitter.CodeBuilder.AddStatement(new ReturnStatement(new ConvertExpression(emitter.ReturnType, getRetVal)));
+
+ // @mbrit - 2012-05-31 - see the note associated with the GetReturnValueForWinRt declaration
+ // for more information on this...
+
+ var useWinRtGenericHandler = false;
+#if NETFX_CORE
+ if (emitter.ReturnType == typeof(int))
+ useWinRtGenericHandler = true;
+#endif
+ if(!(useWinRtGenericHandler))
+ {
+ var getRetVal = new MethodInvocationExpression(invocationLocal, InvocationMethods.GetReturnValue);
+ emitter.CodeBuilder.AddStatement(new ReturnStatement(new ConvertExpression(emitter.ReturnType, getRetVal)));
+ }
+ else
+ {
+#if NETFX_CORE
+ var grvArgs = new Type[] { emitter.ReturnType };
+ var grvCall = InvocationMethods.GetReturnValueForWinRt.MakeGenericMethod(grvArgs);
+ var getRetVal = new MethodInvocationExpression(invocationLocal, grvCall);
+ emitter.CodeBuilder.AddStatement(new ReturnStatement(getRetVal));
+#endif
+ }
}
else
{
View
5 Moq.MetroStyle/Castle/Tokens/InvocationMethods.cs
@@ -62,6 +62,11 @@ public static class InvocationMethods
public static readonly MethodInfo GetReturnValue =
typeof(AbstractInvocation).GetMethod("get_ReturnValue");
+#if NETFX_CORE
+ public static readonly MethodInfo GetReturnValueForWinRt =
+ typeof(AbstractInvocation).GetMethod("GetReturnValueForWinRt");
+#endif
+
public static readonly ConstructorInfo InheritanceInvocationConstructorNoSelector =
typeof(InheritanceInvocation).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,
new[]
View
1 Moq.MetroStyle/Moq.MetroStyle.csproj
@@ -468,6 +468,7 @@
<Compile Include="Castle\Generators\Emitters\SimpleAST\ConvertExpression.cs" />
<Compile Include="Castle\Generators\Emitters\SimpleAST\DefaultValueExpression.cs" />
<Compile Include="Castle\Generators\Emitters\SimpleAST\EndExceptionBlockStatement.cs" />
+ <Compile Include="Castle\Generators\Emitters\SimpleAST\ExplicitConvertExpression.cs" />
<Compile Include="Castle\Generators\Emitters\SimpleAST\Expression.cs" />
<Compile Include="Castle\Generators\Emitters\SimpleAST\ExpressionStatement.cs" />
<Compile Include="Castle\Generators\Emitters\SimpleAST\FieldReference.cs" />
View
174 Moq.MetroStyle/Shim/TypeExtender.cs
@@ -44,23 +44,7 @@ internal static bool IsSubclassOf(this Type type, Type toCheck)
public static MethodInfo GetMethod(this Type type, string name, BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
{
- // walk up the hierarchy...
- var info = type.GetTypeInfo();
- while(info != null)
- {
- foreach (var method in info.DeclaredMethods.Where(v => v.Name == name))
- {
- if (method.CheckBindings(flags))
- return method;
- }
-
- // up...
- info = info.BaseType.GetTypeInfo();
- }
-
-
- // not found
- throw new MissingMethodException(string.Format("A method called '{0}' on type '{1}' was not found.", name, type));
+ return GetMember<MethodInfo>(type, name, flags);
}
public static MethodInfo GetMethod(this Type type, string name, Type[] parameters)
@@ -204,39 +188,49 @@ internal static object[] GetCustomAttributes(this Type type, Type attributeType,
internal static ConstructorInfo[] GetConstructors(this Type type, BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
{
- // flags are ignored - only public instance members are available in WinRT...
- return type.GetTypeInfo().DeclaredConstructors.ToArray();
+ return GetMembers<ConstructorInfo>(type, flags).ToArray();
}
internal static PropertyInfo[] GetProperties(this Type type, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public)
{
- return type.GetTypeInfo().DeclaredProperties.ToArray();
+ return GetMembers<PropertyInfo>(type, flags).ToArray();
}
internal static MethodInfo[] GetMethods(this Type type, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public)
{
- return type.GetTypeInfo().DeclaredMethods.ToArray();
+ return GetMembers<MethodInfo>(type, flags).ToArray();
}
internal static FieldInfo[] GetFields(this Type type, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public)
{
- return type.GetTypeInfo().DeclaredFields.ToArray();
+ return GetMembers<FieldInfo>(type, flags).ToArray();
}
internal static EventInfo[] GetEvents(this Type type, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public)
{
- return type.GetTypeInfo().DeclaredEvents.ToArray();
+ return GetMembers<EventInfo>(type, flags).ToArray();
}
- public static FieldInfo GetField(this Type type, string name, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public)
+ private static List<T> GetMembers<T>(Type type, BindingFlags flags)
+ where T : MemberInfo
{
- foreach (var field in type.GetTypeInfo().DeclaredFields.Where(v => v.Name == name))
+ var results = new List<T>();
+
+ var info = type.GetTypeInfo();
+ while (true)
{
- if (field.CheckBindings(flags))
- return field;
+ foreach (T member in info.DeclaredMembers.Where(v => typeof(T).IsAssignableFrom(v.GetType())))
+ {
+ if (member.CheckBindings(flags))
+ results.Add(member);
+ }
+
+ if (info.BaseType == null)
+ break;
+ info = info.BaseType.GetTypeInfo();
}
- throw new MissingMemberException(string.Format("Failed to find field '{0}' on type '{1}'.", name, type));
+ return results;
}
public static ConstructorInfo GetConstructor(this Type type, params Type[] types)
@@ -281,31 +275,64 @@ private static bool CheckParameters(MethodBase method, Type[] parameters)
public static PropertyInfo GetProperty(this Type type, string name, BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
{
+ return GetMember<PropertyInfo>(type, name, flags);
+ }
+
+ public static EventInfo GetEvent(this Type type, string name, BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
+ {
+ return GetMember<EventInfo>(type, name, flags);
+ }
+
+ public static FieldInfo GetField(this Type type, string name, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public)
+ {
+ return GetMember<FieldInfo>(type, name, flags);
+ }
+
+ private static T GetMember<T>(Type type, string name, BindingFlags flags)
+ where T : MemberInfo
+ {
// walk up the hierarchy...
var info = type.GetTypeInfo();
- while (info != null)
+ while (true)
{
- foreach (var prop in info.DeclaredProperties.Where(v => v.Name == name))
+ // walk...
+ foreach (var member in info.DeclaredMembers.Where(v => typeof(T).IsAssignableFrom(v.GetType()) && v.Name == name))
{
- if (prop.CheckBindings(flags))
- return prop;
+ if (member.CheckBindings(flags))
+ return (T)member;
}
// up...
+ if (info.BaseType == null)
+ break;
info = info.BaseType.GetTypeInfo();
}
- throw new MissingMemberException(string.Format("A property with name '{0}' was not found on '{1}'.", name, type));
- }
-
- public static EventInfo GetEvent(this Type type, string name, BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
- {
- throw new NotImplementedException("This operation has not been implemented.");
+ // msdn docs say missing methods return null...
+ return null;
}
public static Type GetInterface(this Type type, string name, bool ignoreCase = false)
{
- throw new NotImplementedException("This operation has not been implemented.");
+ // walk up the hierarchy...
+ var info = type.GetTypeInfo();
+ while (true)
+ {
+ foreach (var iface in type.GetTypeInfo().ImplementedInterfaces)
+ {
+ if (ignoreCase && string.Compare(iface.Name, name, StringComparison.CurrentCultureIgnoreCase) == 0)
+ return iface;
+ else if (!(ignoreCase) && iface.Name == name)
+ return iface;
+ }
+
+ // up...
+ if (info.BaseType == null)
+ break;
+ info = info.BaseType.GetTypeInfo();
+ }
+
+ throw new MissingMemberException(string.Format("An interface with name '{0}' was not found on '{1}'.", name, type));
}
internal static Type[] GetGenericArguments(this Type type)
@@ -318,22 +345,10 @@ internal static Type[] GetGenericParameterConstraints(this Type type)
return type.GetTypeInfo().GetGenericParameterConstraints();
}
- //internal static Type[] FindInterfaces(this Type type, Func<Type, object, bool> filter, object criteria)
- //{
- // List<Type> results = new List<Type>();
- // foreach (var iface in type.GetTypeInfo().ImplementedInterfaces)
- // {
- // if (filter(iface, criteria))
- // results.Add(iface);
- // }
-
- // return results.ToArray();
- //}
-
internal static object GetCustomAttribute<T>(this Type type, bool inherit = false)
where T : Attribute
{
- throw new NotImplementedException("This operation has not been implemented.");
+ return type.GetTypeInfo().GetCustomAttribute<T>(inherit);
}
internal static InterfaceMapping GetInterfaceMap(this Type type, Type interfaceType)
@@ -415,8 +430,59 @@ internal static bool HasAttribute(this MemberInfo member, Type type, bool inheri
internal static bool CheckBindings(this MemberInfo member, BindingFlags flags)
{
- // TBD...
- return true;
+ if ((member.IsStatic() & (flags & BindingFlags.Static) == BindingFlags.Static) ||
+ (!(member.IsStatic()) & (flags & BindingFlags.Instance) == BindingFlags.Instance))
+ {
+ if ((member.IsPublic() & (flags & BindingFlags.Public) == BindingFlags.Public) ||
+ (!(member.IsPublic()) & (flags & BindingFlags.NonPublic) == BindingFlags.NonPublic))
+ {
+ return true;
+ }
+ else
+ return false;
+ }
+ else
+ return false;
+ }
+
+ internal static bool IsStatic(this MemberInfo member)
+ {
+ if (member is MethodBase)
+ return ((MethodBase)member).IsStatic;
+ else if (member is PropertyInfo)
+ {
+ PropertyInfo prop = (PropertyInfo)member;
+ return (prop.GetMethod != null && prop.GetMethod.IsStatic) || (prop.SetMethod != null && prop.SetMethod.IsStatic);
+ }
+ else if (member is FieldInfo)
+ return ((FieldInfo)member).IsStatic;
+ else if (member is EventInfo)
+ {
+ EventInfo evt = (EventInfo)member;
+ return (evt.AddMethod != null && evt.AddMethod.IsStatic) || (evt.RemoveMethod != null && evt.RemoveMethod.IsStatic);
+ }
+ else
+ throw new NotSupportedException(string.Format("Cannot handle '{0}'.", member.GetType()));
+ }
+
+ internal static bool IsPublic(this MemberInfo member)
+ {
+ if (member is MethodBase)
+ return ((MethodBase)member).IsPublic;
+ else if (member is PropertyInfo)
+ {
+ PropertyInfo prop = (PropertyInfo)member;
+ return (prop.GetMethod != null && prop.GetMethod.IsPublic) || (prop.SetMethod != null && prop.SetMethod.IsPublic);
+ }
+ else if (member is FieldInfo)
+ return ((FieldInfo)member).IsPublic;
+ else if (member is EventInfo)
+ {
+ EventInfo evt = (EventInfo)member;
+ return (evt.AddMethod != null && evt.AddMethod.IsPublic) || (evt.RemoveMethod != null && evt.RemoveMethod.IsPublic);
+ }
+ else
+ throw new NotSupportedException(string.Format("Cannot handle '{0}'.", member.GetType()));
}
}
View
2 Moq.Tests.MetroStyle/Moq.Tests.MetroStyle.csproj
@@ -224,6 +224,8 @@
<Link>VerifyFixture.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="ShimTests\TypeExtenderFoo.cs" />
+ <Compile Include="ShimTests\TypeExtenderTests.cs" />
<Compile Include="Shim\AssertExtender.cs" />
<Compile Include="Shim\IComponent.cs" />
<Compile Include="Shim\IContainer.cs" />
View
5 Moq.Tests.MetroStyle/Shim/AssertExtender.cs
@@ -80,9 +80,10 @@ internal static void Same(object a, object b)
Assert.AreSame(a, b);
}
- internal static void IsAssignableFrom<T1>(object value)
+ internal static void IsAssignableFrom<T>(object value)
{
- throw new NotImplementedException();
+ if (!(typeof(T).GetType().IsAssignableFrom(value.GetType())))
+ throw new InvalidOperationException(string.Format("Assignability mismatch: '{0}' and '{1}'.", value.GetType(), typeof(T)));
}
internal static void DoesNotThrow(Action callback)
View
99 Moq.Tests.MetroStyle/ShimTests/TypeExtenderFoo.cs
@@ -0,0 +1,99 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Moq.Tests
+{
+ public class TypeExtenderFoo
+ {
+ public object PublicInstanceProperty { get; set; }
+ protected object ProtectedInstanceProperty { get; set; }
+ private object PrivateInstanceProperty { get; set; }
+
+ public static object PublicStaticProperty { get; set; }
+ protected static object ProtectedStaticProperty { get; set; }
+ private static object PrivateStaticProperty { get; set; }
+
+ public object PublicInstanceField;
+ protected object ProtectedInstanceField;
+ private object PrivateInstanceField;
+
+ public static object PublicStaticField;
+ protected static object ProtectedStaticField;
+ private static object PrivateStaticField;
+
+ public event EventHandler PublicInstanceEvent;
+ protected event EventHandler ProtectedInstanceEvent;
+ private event EventHandler PrivateInstanceEvent;
+
+ public static event EventHandler PublicStaticEvent;
+ protected static event EventHandler ProtectedStaticEvent;
+ private static event EventHandler PrivateStaticEvent;
+
+ public TypeExtenderFoo()
+ {
+ }
+
+ public void PublicInstanceMethod()
+ {
+ }
+
+ protected void ProtectedInstanceMethod()
+ {
+ }
+
+ private void PrivateInstanceMethod()
+ {
+ }
+
+ public static void PublicStaticMethod()
+ {
+ }
+
+ protected static void ProtectedStaticMethod()
+ {
+ }
+
+ private static void PrivateStaticMethod()
+ {
+ }
+
+ // @mbrit - 2012-05-31 - this method is to get rid of warnings...
+ public void FixWarnings()
+ {
+ this.PrivateInstanceField = null;
+ Debug.WriteLine(this.PrivateInstanceField);
+
+ this.PublicInstanceEvent += Sink;
+ this.ProtectedInstanceEvent += Sink;
+ this.PrivateInstanceEvent += Sink;
+
+ this.PublicInstanceEvent(null, null);
+ this.ProtectedInstanceEvent(null, null);
+ this.PrivateInstanceEvent(null, null);
+ }
+
+ // @mbrit - 2012-05-31 - this method is to get rid of warnings...
+ public static void FixWarningsStatic()
+ {
+ PrivateStaticField = null;
+ Debug.WriteLine(PrivateStaticField);
+
+ PublicStaticEvent += Sink;
+ ProtectedStaticEvent += Sink;
+ PrivateStaticEvent += Sink;
+
+ PublicStaticEvent(null, null);
+ ProtectedStaticEvent(null, null);
+ PrivateStaticEvent(null, null);
+ }
+
+ private static void Sink(object sender, EventArgs e)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
View
526 Moq.Tests.MetroStyle/ShimTests/TypeExtenderTests.cs
@@ -0,0 +1,526 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Reflection;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Moq.Tests
+{
+ [TestClass]
+ public class TypeExtenderTests
+ {
+ [TestMethod]
+ public void TestGetPublicInstanceMethod()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetMethod("PublicInstanceMethod", BindingFlags.Public | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedInstanceMethod()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetMethod("ProtectedInstanceMethod", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateInstanceMethod()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetMethod("PrivateInstanceMethod", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPublicInstanceMethodDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetMethod("PublicInstanceMethod", BindingFlags.Public | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedInstanceMethodDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetMethod("ProtectedInstanceMethod", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateInstanceMethodDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetMethod("PrivateInstanceMethod", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPublicStaticMethod()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetMethod("PublicStaticMethod", BindingFlags.Public | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedStaticMethod()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetMethod("ProtectedStaticMethod", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateStaticMethod()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetMethod("PrivateStaticMethod", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPublicStaticMethodDoesntMatchStatice()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetMethod("PublicStaticMethod", BindingFlags.Public | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedStaticMethodDoesntMatchStatice()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetMethod("ProtectedStaticMethod", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateStaticMethodDoesntMatchStatice()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetMethod("PrivateStaticMethod", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPublicInstanceProperty()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetProperty("PublicInstanceProperty", BindingFlags.Public | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedInstanceProperty()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetProperty("ProtectedInstanceProperty", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateInstanceProperty()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetProperty("PrivateInstanceProperty", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPublicInstancePropertyDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetProperty("PublicInstanceProperty", BindingFlags.Public | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedInstancePropertyDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetProperty("ProtectedInstanceProperty", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateInstancePropertyDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetProperty("PrivateInstanceProperty", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPublicStaticProperty()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetProperty("PublicStaticProperty", BindingFlags.Public | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedStaticProperty()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetProperty("ProtectedStaticProperty", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateStaticProperty()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetProperty("PrivateStaticProperty", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPublicStaticPropertyDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetProperty("PublicStaticProperty", BindingFlags.Public | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedStaticPropertyDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetProperty("ProtectedStaticProperty", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateStaticPropertyDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetProperty("PrivateStaticProperty", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPublicInstanceField()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetField("PublicInstanceField", BindingFlags.Public | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedInstanceField()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetField("ProtectedInstanceField", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateInstanceField()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetField("PrivateInstanceField", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPublicInstanceFieldDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetField("PublicInstanceField", BindingFlags.Public | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedInstanceFieldDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetField("ProtectedInstanceField", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateInstanceFieldDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetField("PrivateInstanceField", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPublicStaticField()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetField("PublicStaticField", BindingFlags.Public | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedStaticField()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetField("ProtectedStaticField", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateStaticField()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetField("PrivateStaticField", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPublicStaticFieldDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetField("PublicStaticField", BindingFlags.Public | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedStaticFieldDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetField("ProtectedStaticField", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateStaticFieldDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetField("PrivateStaticField", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPublicInstanceEvent()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetEvent("PublicInstanceEvent", BindingFlags.Public | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedInstanceEvent()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetEvent("ProtectedInstanceEvent", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateInstanceEvent()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetEvent("PrivateInstanceEvent", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPublicInstanceEventDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetEvent("PublicInstanceEvent", BindingFlags.Public | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedInstanceEventDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetEvent("ProtectedInstanceEvent", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateInstanceEventDoesntMatchStatic()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetEvent("PrivateInstanceEvent", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPublicStaticEvent()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetEvent("PublicStaticEvent", BindingFlags.Public | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedStaticEvent()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetEvent("ProtectedStaticEvent", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateStaticEvent()
+ {
+ Assert.IsNotNull(typeof(TypeExtenderFoo).GetEvent("PrivateStaticEvent", BindingFlags.NonPublic | BindingFlags.Static));
+ }
+
+ [TestMethod]
+ public void TestGetPublicStaticEventDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetEvent("PublicStaticEvent", BindingFlags.Public | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetProtectedStaticEventDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetEvent("ProtectedStaticEvent", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod]
+ public void TestGetPrivateStaticEventDoesntMatchInstance()
+ {
+ Assert.IsNull(typeof(TypeExtenderFoo).GetEvent("PrivateStaticEvent", BindingFlags.NonPublic | BindingFlags.Instance));
+ }
+
+ [TestMethod()]
+ public void TestGetMethodsPublicInstance()
+ {
+ var methods = typeof(TypeExtenderFoo).GetMethods(BindingFlags.Public | BindingFlags.Instance);
+
+ Assert.IsNotNull(methods.Where(v => v.Name == "PublicInstanceMethod").FirstOrDefault());
+
+ Assert.IsNull(methods.Where(v => v.Name == "ProtectedInstanceMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PrivateInstanceMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PublicStaticMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "ProtectedStaticMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PrivateStaticMethod").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetMethodsNonPublicInstance()
+ {
+ var methods = typeof(TypeExtenderFoo).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
+
+ Assert.IsNotNull(methods.Where(v => v.Name == "ProtectedInstanceMethod").FirstOrDefault());
+ Assert.IsNotNull(methods.Where(v => v.Name == "PrivateInstanceMethod").FirstOrDefault());
+
+ Assert.IsNull(methods.Where(v => v.Name == "PublicInstanceMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PublicStaticMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "ProtectedStaticMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PrivateStaticMethod").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetPropertiesPublicInstance()
+ {
+ var properties = typeof(TypeExtenderFoo).GetProperties(BindingFlags.Public | BindingFlags.Instance);
+
+ Assert.IsNotNull(properties.Where(v => v.Name == "PublicInstanceProperty").FirstOrDefault());
+
+ Assert.IsNull(properties.Where(v => v.Name == "ProtectedInstanceProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PrivateInstanceProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PublicStaticProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "ProtectedStaticProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PrivateStaticProperty").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetPropertiessNonPublicInstance()
+ {
+ var properties = typeof(TypeExtenderFoo).GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);
+
+ Assert.IsNotNull(properties.Where(v => v.Name == "ProtectedInstanceProperty").FirstOrDefault());
+ Assert.IsNotNull(properties.Where(v => v.Name == "PrivateInstanceProperty").FirstOrDefault());
+
+ Assert.IsNull(properties.Where(v => v.Name == "PublicInstanceProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PublicStaticProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "ProtectedStaticProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PrivateStaticProperty").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetFieldsPublicInstance()
+ {
+ var fields = typeof(TypeExtenderFoo).GetFields(BindingFlags.Public | BindingFlags.Instance);
+
+ Assert.IsNotNull(fields.Where(v => v.Name == "PublicInstanceField").FirstOrDefault());
+
+ Assert.IsNull(fields.Where(v => v.Name == "ProtectedInstanceField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PrivateInstanceField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PublicStaticField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "ProtectedStaticField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PrivateStaticField").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetFieldsPublicNonPublicInstance()
+ {
+ var fields = typeof(TypeExtenderFoo).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
+
+ Assert.IsNotNull(fields.Where(v => v.Name == "ProtectedInstanceField").FirstOrDefault());
+ Assert.IsNotNull(fields.Where(v => v.Name == "PrivateInstanceField").FirstOrDefault());
+
+ Assert.IsNull(fields.Where(v => v.Name == "PublicInstanceField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PublicStaticField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "ProtectedStaticField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PrivateStaticField").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetEventsPublicInstance()
+ {
+ var evts = typeof(TypeExtenderFoo).GetEvents(BindingFlags.Public | BindingFlags.Instance);
+
+ Assert.IsNotNull(evts.Where(v => v.Name == "PublicInstanceEvent").FirstOrDefault());
+
+ Assert.IsNull(evts.Where(v => v.Name == "ProtectedInstanceEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PrivateInstanceEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PublicStaticEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "ProtectedStaticEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PrivateStaticEvent").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetEventsPublicNonPublicInstance()
+ {
+ var evts = typeof(TypeExtenderFoo).GetEvents(BindingFlags.NonPublic | BindingFlags.Instance);
+
+ Assert.IsNotNull(evts.Where(v => v.Name == "ProtectedInstanceEvent").FirstOrDefault());
+ Assert.IsNotNull(evts.Where(v => v.Name == "PrivateInstanceEvent").FirstOrDefault());
+
+ Assert.IsNull(evts.Where(v => v.Name == "PublicInstanceEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PublicStaticEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "ProtectedStaticEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PrivateStaticEvent").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetMethodsPublicStatic()
+ {
+ var methods = typeof(TypeExtenderFoo).GetMethods(BindingFlags.Public | BindingFlags.Static);
+
+ Assert.IsNotNull(methods.Where(v => v.Name == "PublicStaticMethod").FirstOrDefault());
+
+ Assert.IsNull(methods.Where(v => v.Name == "ProtectedStaticMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PrivateStaticMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PublicInstanceMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "ProtectedInstanceMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PrivateInstanceMethod").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetMethodsNonPublicStatic()
+ {
+ var methods = typeof(TypeExtenderFoo).GetMethods(BindingFlags.NonPublic | BindingFlags.Static);
+
+ Assert.IsNotNull(methods.Where(v => v.Name == "ProtectedStaticMethod").FirstOrDefault());
+ Assert.IsNotNull(methods.Where(v => v.Name == "PrivateStaticMethod").FirstOrDefault());
+
+ Assert.IsNull(methods.Where(v => v.Name == "PublicStaticMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PublicInstanceMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "ProtectedInstanceMethod").FirstOrDefault());
+ Assert.IsNull(methods.Where(v => v.Name == "PrivateInstanceMethod").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetPropertiesPublicStatic()
+ {
+ var properties = typeof(TypeExtenderFoo).GetProperties(BindingFlags.Public | BindingFlags.Static);
+
+ Assert.IsNotNull(properties.Where(v => v.Name == "PublicStaticProperty").FirstOrDefault());
+
+ Assert.IsNull(properties.Where(v => v.Name == "ProtectedStaticProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PrivateStaticProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PublicInstanceProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "ProtectedInstanceProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PrivateInstanceProperty").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetPropertiessNonPublicStatic()
+ {
+ var properties = typeof(TypeExtenderFoo).GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
+
+ Assert.IsNotNull(properties.Where(v => v.Name == "ProtectedStaticProperty").FirstOrDefault());
+ Assert.IsNotNull(properties.Where(v => v.Name == "PrivateStaticProperty").FirstOrDefault());
+
+ Assert.IsNull(properties.Where(v => v.Name == "PublicStaticProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PublicInstanceProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "ProtectedInstanceProperty").FirstOrDefault());
+ Assert.IsNull(properties.Where(v => v.Name == "PrivateInstanceProperty").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetFieldsPublicStatic()
+ {
+ var fields = typeof(TypeExtenderFoo).GetFields(BindingFlags.Public | BindingFlags.Static);
+
+ Assert.IsNotNull(fields.Where(v => v.Name == "PublicStaticField").FirstOrDefault());
+
+ Assert.IsNull(fields.Where(v => v.Name == "ProtectedStaticField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PrivateStaticField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PublicInstanceField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "ProtectedInstanceField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PrivateInstanceField").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetFieldsPublicNonPublicStatic()
+ {
+ var fields = typeof(TypeExtenderFoo).GetFields(BindingFlags.NonPublic | BindingFlags.Static);
+
+ Assert.IsNotNull(fields.Where(v => v.Name == "ProtectedStaticField").FirstOrDefault());
+ Assert.IsNotNull(fields.Where(v => v.Name == "PrivateStaticField").FirstOrDefault());
+
+ Assert.IsNull(fields.Where(v => v.Name == "PublicStaticField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PublicInstanceField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "ProtectedInstanceField").FirstOrDefault());
+ Assert.IsNull(fields.Where(v => v.Name == "PrivateInstanceField").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetEventsPublicStatic()
+ {
+ var evts = typeof(TypeExtenderFoo).GetEvents(BindingFlags.Public | BindingFlags.Static);
+
+ Assert.IsNotNull(evts.Where(v => v.Name == "PublicStaticEvent").FirstOrDefault());
+
+ Assert.IsNull(evts.Where(v => v.Name == "ProtectedStaticEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PrivateStaticEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PublicInstanceEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "ProtectedInstanceEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PrivateInstanceEvent").FirstOrDefault());
+ }
+
+ [TestMethod()]
+ public void TestGetEventsPublicNonPublicStatic()
+ {
+ var evts = typeof(TypeExtenderFoo).GetEvents(BindingFlags.NonPublic | BindingFlags.Static);
+
+ Assert.IsNotNull(evts.Where(v => v.Name == "ProtectedStaticEvent").FirstOrDefault());
+ Assert.IsNotNull(evts.Where(v => v.Name == "PrivateStaticEvent").FirstOrDefault());
+
+ Assert.IsNull(evts.Where(v => v.Name == "PublicStaticEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PublicInstanceEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "ProtectedInstanceEvent").FirstOrDefault());
+ Assert.IsNull(evts.Where(v => v.Name == "PrivateInstanceEvent").FirstOrDefault());
+ }
+ }
+}
View
2 UnitTests/Linq/QueryableMocksFixture.cs
@@ -68,7 +68,7 @@ public void ShouldSupportEnum()
}
[Fact]
- public void ShoulSupportMethod()
+ public void ShouldSupportMethod()
{
var expected = new Mock<IBar>().Object;
var target = Mocks.Of<IFoo>().First(x => x.Find(It.IsAny<string>()) == expected);
View
25 UnitTests/MockFixture.cs
@@ -66,12 +66,15 @@ public void CallParameterCanBeVariable()
[Fact]
public void CallParameterCanBeMethodCall()
{
+ // @mbrit - 2012-05-31 - this doesn't work - looks like a problem with boxing (the values
+ // returned seem to be an internal reference to the object...)
int value = 5;
var mock = new Mock<IFoo>();
mock.Setup(x => x.Echo(GetValue(value))).Returns(() => value * 2);
- Assert.Equal(value * 2, mock.Object.Echo(value * 2));
+ var result = mock.Object.Echo(value * 2);
+ Assert.Equal(value * 2, result);
}
private int GetValue(int value)
@@ -80,6 +83,23 @@ private int GetValue(int value)
}
[Fact]
+ public void CallParameterCanBeMethodCall2()
+ {
+ // @mbrit - 2012-05-31 - test to try out the theory that it's boxing that's breaking this...
+ string value = "5";
+ var mock = new Mock<IFoo>();
+
+ mock.Setup(x => x.EchoString(GetValue(value))).Returns(() => value + value);
+
+ Assert.Equal(value + value, mock.Object.EchoString(value + value));
+ }
+
+ private string GetValue(string value)
+ {
+ return value + value;
+ }
+
+ [Fact]
public void ExpectsVoidCall()
{
var mock = new Mock<IFoo>();
@@ -879,6 +899,9 @@ public interface IFoo
void Submit();
string Execute(string command);
int this[int index] { get; set; }
+
+ // @mbrit - 2012-05-30 - added this to text boxing...
+ string EchoString(string value);
}
public interface IParams

0 comments on commit 8c9dacb

Please sign in to comment.
Something went wrong with that request. Please try again.