public void Equals_GenericClass_MethodWithGenericClassArgumentsFromBaseType()
        {
            var methodFromBaseType = ScriptingHelper.GetInstanceMethod(typeof(ProxiedChildGeneric <int, string>), "ProxiedChildGenericToString", new[] { typeof(int), typeof(string) });
            var method             = ScriptingHelper.GetInstanceMethod(typeof(ProxiedChildChildGeneric <int, string>), "ProxiedChildGenericToString", new[] { typeof(int), typeof(string) });

            Assert.That(MethodInfoEqualityComparer.Get.Equals(methodFromBaseType, method), Is.True);
        }
        public void Equals_GenericMethodFromBaseType_NonGenericOverloadInType()
        {
            var methodFromBaseType = ScriptingHelper.GetAnyGenericInstanceMethod(typeof(Proxied), "OverloadedGenericToString", 2);
            var method             = ScriptingHelper.GetInstanceMethod(typeof(ProxiedChildChild), "OverloadedGenericToString", new[] { typeof(int), typeof(int) });

            Assert.That(MethodInfoEqualityComparer.Get.Equals(methodFromBaseType, method), Is.False);
        }
        public void IsMethodEqualToBaseTypeMethod()
        {
            var moduleScopeStub = MockRepository.GenerateStub <ModuleScope> ();

            var baseType    = typeof(ProxiedChildGeneric <int, string>);
            var proxiedType = typeof(ProxiedChildChildGeneric <int, string>);

            var typeFilter = new TypeLevelTypeFilter(new[] { baseType });
            var stableBindingProxyBuilder = new StableBindingProxyBuilder(proxiedType, typeFilter, moduleScopeStub);

            var methodFromBaseType = ScriptingHelper.GetInstanceMethod(baseType, "ProxiedChildGenericToString", new[] { typeof(int), typeof(string) });
            var method             = ScriptingHelper.GetInstanceMethod(proxiedType, "ProxiedChildGenericToString", new[] { typeof(int), typeof(string) });

            Assert.That(methodFromBaseType, Is.Not.EqualTo(method));

            // Shows methodInfo.GetBaseDefinition()-bug: Results should be equal.
            Assert.That(methodFromBaseType.GetBaseDefinition(), Is.Not.EqualTo(method.GetBaseDefinition()));

            Assert.That(methodFromBaseType.ReflectedType, Is.EqualTo(baseType));

            Assert.That(stableBindingProxyBuilder.IsMethodEqualToBaseTypeMethod(method, method), Is.True);
            Assert.That(stableBindingProxyBuilder.IsMethodEqualToBaseTypeMethod(method, methodFromBaseType), Is.True);

            var sumMethod = proxiedType.GetMethod("Sum", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            Assert.That(stableBindingProxyBuilder.IsMethodEqualToBaseTypeMethod(sumMethod, methodFromBaseType), Is.False);
        }
        public void IsMethodKnownInClass_MethodNotHiddenThroughNew()
        {
            //var moduleScopeStub = MockRepository.GenerateStub<ModuleScope> ();
            var baseType = typeof(Proxied);
            //var typeFilter = new TypeLevelTypeFilter (new[] { baseType });
            var proxiedType = typeof(ProxiedChildChild);
            //var stableBindingProxyBuilder = new StableBindingProxyBuilder (proxiedType, typeFilter, moduleScopeStub);

            var methodFromBaseType = GetAnyInstanceMethod(baseType, "Sum");
            var method             = GetAnyInstanceMethod(proxiedType, "Sum");

            Assert.That(methodFromBaseType, Is.Not.EqualTo(method));

            // Shows methodInfo.GetBaseDefinition()-bug: Results should be equal.
            Assert.That(methodFromBaseType.GetBaseDefinition(), Is.Not.EqualTo(method.GetBaseDefinition()));

            // "Sum" is defined in Proxied
            AssertIsMethodKnownInBaseType(baseType, GetAnyInstanceMethod(baseType, "Sum"), Is.True);
            AssertIsMethodKnownInBaseType(baseType, GetAnyInstanceMethod(typeof(ProxiedChild), "Sum"), Is.True);
            AssertIsMethodKnownInBaseType(baseType, method, Is.True);

            // "OverrideMe" is overridden in ProxiedChild
            AssertIsMethodKnownInBaseType(baseType, GetAnyInstanceMethod(baseType, "OverrideMe"), Is.True);
            AssertIsMethodKnownInBaseType(baseType, GetAnyInstanceMethod(typeof(ProxiedChild), "OverrideMe"), Is.True);
            AssertIsMethodKnownInBaseType(baseType, GetAnyInstanceMethod(proxiedType, "OverrideMe"), Is.True);

            // "PrependName" is redefined with new in ProxiedChildChild
            AssertIsMethodKnownInBaseType(baseType, GetAnyInstanceMethod(baseType, "PrependName"), Is.True);
            //AssertIsMethodKnownInClass (baseType, GetAnyInstanceMethod (typeof (ProxiedChild), "PrependName"), Is.True);
            //AssertIsMethodKnownInClass (baseType, GetAnyInstanceMethod (proxiedType, "PrependName"), Is.False);
            AssertIsMethodKnownInBaseType(baseType,
                                          ScriptingHelper.GetInstanceMethod(typeof(ProxiedChild), "PrependName", new[] { typeof(String) }), Is.True);
            AssertIsMethodKnownInBaseType(baseType,
                                          ScriptingHelper.GetInstanceMethod(proxiedType, "PrependName", new[] { typeof(String) }), Is.False);
        }
        public void Equals_DifferingArgumentsMethodFromBaseType()
        {
            var methodFromBaseType = ScriptingHelper.GetInstanceMethod(typeof(ProxiedChild), "BraKet", new[] { typeof(string), typeof(int) });
            var method             = ScriptingHelper.GetInstanceMethod(typeof(ProxiedChildChild), "BraKet", new Type[0]);

            Assert.That(methodFromBaseType, Is.Not.EqualTo(method));
            Assert.That(MethodInfoEqualityComparer.Get.Equals(methodFromBaseType, method), Is.False);
        }
        public void Equals_MethodFromBaseType()
        {
            var methodFromBaseType = ScriptingHelper.GetInstanceMethod(typeof(Proxied), "Sum");
            var method             = ScriptingHelper.GetInstanceMethod(typeof(ProxiedChildChild), "Sum");

            Assert.That(methodFromBaseType, Is.Not.EqualTo(method));
            Assert.That(methodFromBaseType.GetBaseDefinition(), Is.Not.EqualTo(method.GetBaseDefinition()));
            Assert.That(MethodInfoEqualityComparer.Get.Equals(methodFromBaseType, method), Is.True);
        }
        public void Equals_MethodFromBaseTypeHiddenByInterfaceMethod()
        {
            var methodFromBaseType = ScriptingHelper.GetInstanceMethod(typeof(Proxied), "PrependName", new[] { typeof(string) });
            var method             = ScriptingHelper.GetInstanceMethod(typeof(ProxiedChildChild), "PrependName", new[] { typeof(string) });

            Assert.That(methodFromBaseType, Is.Not.EqualTo(method));
            Assert.That(methodFromBaseType.GetBaseDefinition(), Is.Not.EqualTo(method.GetBaseDefinition()));

            // Default comparison taking all method attributes into account fails, since IPrependName adds the Final, Virtual and
            // VtableLayoutMask attributes.
            Assert.That(MethodInfoEqualityComparer.Get.Equals(methodFromBaseType, method), Is.False);

            // Comparing ignoring method attributes coming from IPrependName works.
            var comparerNoVirtualNoFinal = new MethodInfoEqualityComparer(~(MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.VtableLayoutMask));

            Assert.That(comparerNoVirtualNoFinal.Equals(methodFromBaseType, method), Is.True);
        }
        public void Ctor_Mask()
        {
            var method      = ScriptingHelper.GetInstanceMethod(typeof(CtorTest), "Foo", new[] { typeof(string) });
            var childMethod = ScriptingHelper.GetInstanceMethod(typeof(CtorTestChild), "Foo", new[] { typeof(string) });

            Assert.That(method.Attributes, Is.EqualTo(MethodAttributes.PrivateScope | MethodAttributes.Public | MethodAttributes.HideBySig));
            const MethodAttributes childMethodAdditionalAttributes = MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.VtableLayoutMask;

            Assert.That(childMethod.Attributes, Is.EqualTo(method.Attributes | childMethodAdditionalAttributes));

            var comparerDefault = new MethodInfoEqualityComparer();

            Assert.That(comparerDefault.Equals(method, childMethod), Is.False);

            var comparerNoVirtualNoFinal = new MethodInfoEqualityComparer(~childMethodAdditionalAttributes);

            Assert.That(comparerNoVirtualNoFinal.Equals(method, childMethod), Is.True);

            var comparerMethodFromBaseTypeAttributes = new MethodInfoEqualityComparer(method.Attributes);

            Assert.That(comparerMethodFromBaseTypeAttributes.Equals(method, childMethod), Is.True);
        }