Ejemplo n.º 1
0
        public void Trivial()
        {
            SR.MethodInfo   mi  = typeof(String).GetMethod("ToString", Type.EmptyTypes);
            MethodReference mr1 = m_module.Import(mi);

            Assert.IsTrue(MethodMatcher.Match(mr1, mr1));
        }
Ejemplo n.º 2
0
        private bool Test(HttpMethod expected, HttpMethod actual)
        {
            var sut = new MethodMatcher(expected);

            return(sut.Matches(new HttpRequestMessage(actual,
                                                      "http://tempuri.org/home")));
        }
Ejemplo n.º 3
0
        public void Base()
        {
            SR.MethodInfo   mi   = typeof(String).GetMethod("ToString", Type.EmptyTypes);
            MethodReference mr1a = m_module.Import(mi);

            mi = typeof(String).GetMethod("ToString", new Type[] { typeof(IFormatProvider) });
            MethodReference mr1b = m_module.Import(mi);

            mi = typeof(Object).GetMethod("ToString");
            MethodReference mr2 = m_module.Import(mi);

            Assert.IsTrue(MethodMatcher.Match(mr1a, mr2));
            Assert.IsFalse(MethodMatcher.Match(mr1b, mr2));
        }
Ejemplo n.º 4
0
        private void Scan(MethodMatcher matcher)
        {
            if (s_cache.TryGetValue(GetType(), out d_methods))
            {
                return;
            }

            d_methods = new Dictionary <Type, List <MethodInfo> >();

            foreach (MethodInfo method in GetType().GetMethods(d_methodBinding))
            {
                if (!TypeIsA(method.ReturnType, d_returnType, (d_binding & BindingFlags.ExactReturnType) != 0))
                {
                    continue;
                }

                ParameterInfo[] parameters = method.GetParameters();

                if (parameters.Length != d_parameterTypes.Length)
                {
                    continue;
                }

                bool parametermatch = true;

                for (int i = 0; i < parameters.Length; ++i)
                {
                    if (!TypeIsA(parameters[i].ParameterType, d_parameterTypes[i], (i == 0 ? false : (d_binding & BindingFlags.ExactParameters) != 0)))
                    {
                        parametermatch = false;
                        break;
                    }
                }

                if (!parametermatch)
                {
                    continue;
                }

                if (matcher != null && !matcher(method))
                {
                    continue;
                }

                Add(method, parameters[0].ParameterType);
            }
        }
Ejemplo n.º 5
0
        public void Should_return_null_if_not_match_anything()
        {
            // Arrange
            var matcher = new MethodMatcher();
            var request = new RpcRequest
            {
                DeclaringType   = typeof(ISomeService).FullName,
                MethodName      = "MethodNotFound",
                MemberType      = MemberTypes.Method,
                MethodSignature = matcher.GetMethodSignature(new DynamicMethod("MethodNotFound", typeof(void), null, true))
            };

            // Action
            var method = matcher.Match <ISomeService>(request);

            // Assert
            Assert.IsNull(method);
        }
Ejemplo n.º 6
0
        public void Should_return_method_info_if_match()
        {
            // Arrange
            var matcher = new MethodMatcher();
            var request = new RpcRequest
            {
                DeclaringType = typeof(ISomeService).FullName,
                MethodName = "TryParse",
                MemberType = MemberTypes.Method,
                MethodSignature = matcher.GetMethodSignature(typeof(ISomeService).GetMethod("TryParse"))
            };

            // Action
            var method = matcher.Match<ISomeService>(request);

            // Assert
            Assert.IsNotNull(method);
        }
Ejemplo n.º 7
0
        public void Should_return_null_if_not_match_anything()
        {
            // Arrange
            var matcher = new MethodMatcher();
            var request = new RpcRequest
            {
                DeclaringType = typeof(ISomeService).FullName,
                MethodName = "MethodNotFound",
                MemberType = MemberTypes.Method,
                MethodSignature = matcher.GetMethodSignature(new DynamicMethod("MethodNotFound", typeof(void), null, true))
            };

            // Action
            var method = matcher.Match<ISomeService>(request);

            // Assert
            Assert.IsNull(method);
        }
Ejemplo n.º 8
0
        public void Should_return_method_info_if_match()
        {
            // Arrange
            var matcher = new MethodMatcher();
            var request = new RpcRequest
            {
                DeclaringType   = typeof(ISomeService).FullName,
                MethodName      = "TryParse",
                MemberType      = MemberTypes.Method,
                MethodSignature = matcher.GetMethodSignature(typeof(ISomeService).GetMethod("TryParse"))
            };

            // Action
            var method = matcher.Match <ISomeService>(request);

            // Assert
            Assert.IsNotNull(method);
        }
Ejemplo n.º 9
0
 public bool MatchesIgnoringIsActive(Invocation invocation)
 {
     if (Receiver != invocation.Receiver)
     {
         return(false);
     }
     if (!MethodMatcher.Matches(invocation.Method))
     {
         return(false);
     }
     if (!ArgumentsMatcher.Matches(invocation))
     {
         return(false);
     }
     if (!ExtraMatchersMatch(invocation))
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Create a new MetaAnnotationMatchingPointcut for the given annotation type.
        /// </summary>
        /// <param name="classAnnotationType">	the annotation type to look for at the class level
        ///                             (can be <code>null</code>) </param>
        /// <param name="methodAnnotationType"> the annotation type to look for at the method level
        ///                             (can be <code>null</code>) </param>
        public MetaAnnotationMatchingPointcut(Type classAnnotationType, Type methodAnnotationType)
        {
            Assert.isTrue((classAnnotationType != null || methodAnnotationType != null), "Either Class annotation type or Method annotation type needs to be specified (or both)");

            if (classAnnotationType != null)
            {
                this.classFilter = new AnnotationClassFilter(classAnnotationType);
            }
            else
            {
                this.classFilter = ClassFilter.TRUE;
            }

            if (methodAnnotationType != null)
            {
                this.methodMatcher = new MetaAnnotationMethodMatcher(methodAnnotationType);
            }
            else
            {
                this.methodMatcher = MethodMatcher.TRUE;
            }
        }
Ejemplo n.º 11
0
        // Generic bases work.
//		[Test]
        public void GenericBase()
        {
            SR.MethodInfo   mi  = typeof(Int16).GetMethod("CompareTo", new Type[] { typeof(Int16) });
            MethodReference mr1 = m_module.Import(mi);

            Type[] types = typeof(Int16).GetInterfaces();
            foreach (Type t in types)
            {
                if (t.IsGenericType)
                {
                    mi = t.GetMethod("CompareTo");
                    if (mi != null)
                    {
                        MethodReference mr2 = m_module.Import(mi);                              // this throws
                        Console.WriteLine(mr1);
                        Console.WriteLine(mr2);

                        Assert.IsTrue(MethodMatcher.Match(mr1, mr2));
                    }
                }
            }
        }
Ejemplo n.º 12
0
        public virtual void Init()
        {
            var type    = typeof(T);
            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);

            foreach (var m in methods)
            {
                foreach (var a in m.GetCustomAttributes())
                {
                    if (!(a is LocalRouteAttribute lra))
                    {
                        continue;
                    }

                    var name = lra.Name ?? m.Name;
                    if (string.IsNullOrEmpty(name))
                    {
                        throw new InvalidOperationException(
                                  $"local route name could not be resolved for method [{m.Name}]");
                    }
                    if (_matchersByName.ContainsKey(name))
                    {
                        throw new InvalidOperationException(
                                  $"duplicate local route name [{name}] for method [{m.Name}]");
                    }


                    var mm = new MethodMatcher
                    {
                        LocalRoute = lra,
                        Method     = m,
                        Matcher    = new TemplateMatcher(TemplateParser.Parse(lra.PathPattern), null),
                        Name       = name,
                    };

                    switch (lra)
                    {
                    case LocalListRouteAttribute _:
                        _listMatchers.Add(mm);
                        break;

                    case LocalReadRouteAttribute _:
                        _readMatchers.Add(mm);
                        break;

                    case LocalWriteRouteAttribute _:
                        _writeMatchers.Add(mm);
                        break;

                    case LocalDeleteRouteAttribute _:
                        _deleteMatchers.Add(mm);
                        break;

                    default:
                        throw new InvalidOperationException("unknown local route type");
                    }

                    _matchersByName.Add(name, mm);
                }
            }
        }
Ejemplo n.º 13
0
 public void MatchesFailingTest(MethodMetadata method, MethodMatcher matcher)
 {
     Assert.False(matcher.Matches(method));
 }
Ejemplo n.º 14
0
        public void DescribeTo(TextWriter writer)
        {
            if (MethodMatcher is MethodMatcher)
            {
                writer.Write(((MethodMatcher)MethodMatcher).ReturnType);
                writer.Write(" ");
            }

            writer.Write(Receiver.MockName);
            writer.Write(_methodSeparator);
            if (MethodMatcher != null)
            {
                MethodMatcher.DescribeTo(writer);
            }
            ArgumentsMatcher.DescribeTo(writer);

            if (_extraMatchers.Count > 0)
            {
                writer.Write(" Matching[");
                for (int i = 0; i < _extraMatchers.Count; i++)
                {
                    if (i != 0)
                    {
                        writer.Write(", ");
                    }

                    _extraMatchers[i].DescribeTo(writer);
                }
                writer.Write("]");
            }

            if (_actions.Count > 0)
            {
                writer.Write(" will ");
                (_actions[0]).DescribeTo(writer);
                for (int i = 1; i < _actions.Count; i++)
                {
                    if (i != 0)
                    {
                        writer.Write(", ");
                    }
                    _actions[i].DescribeTo(writer);
                }
            }

            if (IsValid)
            {
                writer.Write(" [EXPECTED: ");
                writer.Write(_expectationDescription);

                writer.Write(" CALLED: ");
                writer.Write(_callCount);
                writer.Write(" time");
                if (_callCount != 1)
                {
                    writer.Write("s");
                }
                writer.Write("]");
            }
            else
            {
                writer.Write(" [EXPECTATION NOT VALID because of runtime error or incomplete setup]");
            }

            if (!string.IsNullOrEmpty(_expectationComment))
            {
                writer.Write(" Comment: ");
                writer.Write(_expectationComment);
            }
            writer.WriteLine();
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Create a new MetaAnnotationMatchingPointcut for the given annotation type.
 /// </summary>
 /// <param name="classAnnotationType"> the annotation type to look for at the class level </param>
 /// <param name="checkInherited">			whether to explicitly check the superclasses and
 ///                            interfaces for the annotation type as well (even if the annotation type
 ///                            is not marked as inherited itself) </param>
 public MetaAnnotationMatchingPointcut(Type classAnnotationType, bool checkInherited)
 {
     this.classFilter   = new AnnotationClassFilter(classAnnotationType, checkInherited);
     this.methodMatcher = MethodMatcher.TRUE;
 }
Ejemplo n.º 16
0
		/// <summary>Returns the first base class (not including Object) method which declares the specified method. 
		/// Note that this may return an abstract method, or the original method, or null if a base
		/// type could not be found.</summary>
		public static MethodDefinition GetBasestMethod(this MethodDefinition method, AssemblyCache cache)
		{			
			DBC.Pre(method != null, "method is null");
			DBC.Pre(cache != null, "cache is null");
			
			MethodDefinition result = method;
			
			if (method.IsVirtual)
			{
				TypeDefinition type = cache.FindType(method.DeclaringType);
				if (type != null)
					type = cache.FindType(type.BaseType);
				
				while (type != null && type.FullName != "System.Object")
				{
					MethodMatcher matcher = new MethodMatcher(method);

					MethodDefinition[] candidates = type.Methods.GetMethod(method.Name);
					foreach (MethodDefinition candidate in candidates)
					{
						if (candidate.IsVirtual && !candidate.IsFinal && matcher == candidate)
							result = candidate;
					}

					type = cache.FindType(type.BaseType);
				}
				
				if (type == null)
					result = null;
			}
			
			return result;
		}
Ejemplo n.º 17
0
		private static TypeReference DoGetDeclaringInterface(TypeReference declared, MethodMatcher matcher, AssemblyCache cache)
		{			
			Log.DebugLine(true, "   finding {0}", declared.FullName);
			TypeDefinition type = cache.FindType(declared);
			if (type != null)
			{
				Log.DebugLine(true, "   found {0}, {1} interfaces", type.FullName, type.Interfaces.Count);
				foreach (TypeReference inRef in type.Interfaces)
				{
					Log.DebugLine(true, "   references {0}", inRef.FullName);
					TypeDefinition inType = cache.FindType(inRef);
					if (inType != null)
					{
						Log.DebugLine(true, "   checking {0}", inType);
						if (DoDeclares(inType, matcher))
						{
							return inType;
						}
					}
				}
			}
			
			return null;
		}
Ejemplo n.º 18
0
		private static bool DoDeclares(TypeDefinition type, MethodMatcher matcher)
		{			
			foreach (MethodDefinition method in type.Methods)
			{
				if (matcher == method)
				{
					return true;
				}
			}
			
			return false;
		}
Ejemplo n.º 19
0
        public DynamicVisitor(Type returnType, BindingFlags binding, System.Reflection.BindingFlags methodbinding, MethodMatcher matcher, params Type[] parameterTypes)
        {
            if (parameterTypes.Length == 0)
            {
                throw new Exception("Parameter types must have at least one element");
            }

            d_returnType     = returnType;
            d_parameterTypes = parameterTypes;
            d_binding        = binding;
            d_methodBinding  = methodbinding;

            Scan(matcher);
        }
Ejemplo n.º 20
0
 public void MatchesPassingTest(MethodMetadata method, MethodMatcher matcher)
 {
     Assert.True(matcher.Matches(method));
 }