Example #1
0
        private static bool NodeMatchesFilter(CallPattern callPattern, IMatcherTreeNode node)
        {
            var filter = callPattern.Filter;

            if (filter == null)
            {
                return(true);
            }

            var args     = new List <object>();
            var nodeIter = node;

            while (nodeIter != null)
            {
                var valueMatcher = nodeIter.Matcher as IValueMatcher;
                if (valueMatcher != null)
                {
                    args.Add(valueMatcher.Value);
                }
                nodeIter = nodeIter.Parent;
            }

            if (!callPattern.Method.IsStatic && filter.Method.GetParameters().Length + 1 == args.Count)
            {
                args.RemoveAt(args.Count - 1);
            }

            args.Reverse();
            var argsArray = args.ToArray();

            object state;

            MockingUtil.BindToMethod(MockingUtil.Default, new[] { filter.Method }, ref argsArray, null, null, null, out state);

            var filterFunc = MockingUtil.MakeFuncCaller(filter);
            var isMatch    = (bool)ProfilerInterceptor.GuardExternal(() => filterFunc(argsArray, filter));

            DebugView.TraceEvent(IndentLevel.Matcher, () => String.Format("Matcher predicate {0} call to {2} with arguments ({1})",
                                                                          isMatch ? "passed" : "rejected", String.Join(", ", args.Select(x => x.ToString()).ToArray()),
                                                                          callPattern.Method));

            return(isMatch);
        }
        public IMatcherTreeNode DetachMethodMock()
        {
            IMatcherTreeNode current = this;

            while (current.Parent != null && current.Parent.Children.Count == 1)
            {
                current.Parent.Children.Clear();
                current = current.Parent;
            }

            if (current.Parent != null)
            {
                current.Parent.Children.Remove(current);
            }

            while (current.Parent != null)
            {
                current = current.Parent;
            }

            return(current);
        }
        public void ReattachMethodMock()
        {
            IMatcherTreeNode current = this;

            while (current.Parent != null && current.Parent.Children.Count == 1)
            {
                current.Parent.Children.Clear();
                current = current.Parent;
            }

            if (current.Parent != null)
            {
                current.Parent.Children.Remove(current);
            }

            while (current.Parent != null)
            {
                current = current.Parent;
            }

            ((MethodInfoMatcherTreeNode)current).AddChild(MethodMock.CallPattern, this);
        }
 public MatcherNodeAndParent(IMatcherTreeNode node, IMatcherTreeNode parent)
 {
     this.Node   = node;
     this.Parent = parent;
 }
		public MatcherNodeAndParent(IMatcherTreeNode node, IMatcherTreeNode parent)
		{
			this.Node = node;
			this.Parent = parent;
		}
		private static bool NodeMatchesFilter(CallPattern callPattern, IMatcherTreeNode node)
		{
			var filter = callPattern.Filter;
			if (filter == null)
				return true;

			var args = new List<object>();
			var nodeIter = node;
			while (nodeIter != null)
			{
				var valueMatcher = nodeIter.Matcher as IValueMatcher;
				if (valueMatcher != null)
				{
					args.Add(valueMatcher.Value);
				}
				nodeIter = nodeIter.Parent;
			}

			if (!callPattern.Method.IsStatic && filter.Method.GetParameters().Length + 1 == args.Count)
			{
				args.RemoveAt(args.Count - 1);
			}

			args.Reverse();
			var argsArray = args.ToArray();

			object state;
			MockingUtil.BindToMethod(MockingUtil.Default, new[] { filter.Method }, ref argsArray, null, null, null, out state);

			var filterFunc = MockingUtil.MakeFuncCaller(filter);
			var isMatch = (bool) ProfilerInterceptor.GuardExternal(() => filterFunc(argsArray, filter));

			DebugView.TraceEvent(IndentLevel.Matcher, () => String.Format("Matcher predicate {0} call to {2} with arguments ({1})",
				isMatch ? "passed" : "rejected", String.Join(", ", args.Select(x => x.ToString()).ToArray()),
				callPattern.Method));

			return isMatch;
		}