protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var instanceType = node.Object == null ? null : node.Object.Type;

            var map = new[] { new { Param = instanceType, Arg = node.Object } }.ToList();
            map.AddRange(node.Method.GetParameters()
                .Zip(node.Arguments, (p, a) => new { Param = p.ParameterType, Arg = a }));

            // for any local collection parameters in the method, make a
            // replacement argument which will print its elements
            var replacements = (map.Where(x => x.Param != null && x.Param.IsGenericType)
                .Select(x => new {x, g = x.Param.GetGenericTypeDefinition()})
                .Where(@t => @t.g == typeof (IEnumerable<>) || @t.g == typeof (List<>))
                .Where(@t => @t.x.Arg.NodeType == ExpressionType.Constant)
                .Select(@t => new {@t, elementType = @t.x.Param.GetGenericArguments().Single()})
                .Select(
                    @t =>
                        new
                        {
                            @[email protected],
                            Replacement =
                                Expression.Constant("{" + string.Join("|", (IEnumerable) ((ConstantExpression) @[email protected]).Value) + "}")
                        })).ToList();

            if (replacements.Any())
            {
                var args = map.Select(x => (from r in replacements
                                            where r.Arg == x.Arg
                                            select r.Replacement).SingleOrDefault() ?? x.Arg).ToList();

                node = node.Update(args.First(), args.Skip(1));
            }

            return base.VisitMethodCall(node);
        }
Exemplo n.º 2
0
        public void SingleElementIndexedSelector()
        {
            var source = new[]
            {
                new  { name = "Prakash", custID = 98088 }
            };
            string[] expected = { "Prakash" };

            Assert.Equal(expected, source.Select((e, index) => e.name));
        }
Exemplo n.º 3
0
 public void SelectProperty()
 {
     var source = new[]{
         new { name="Prakash", custID=98088 },
         new { name="Bob", custID=29099 },
         new { name="Chris", custID=39033 },
         new { name=(string)null, custID=30349 },
         new { name="Prakash", custID=39030 }
     };
     string[] expected = { "Prakash", "Bob", "Chris", null, "Prakash" };
     Assert.Equal(expected, source.Select(e => e.name));
 }
        public void TryGetInstance_WithConcreteBclLists_ProducesLists()
        {
            var resolver = new ListTypeResolver();
            var types = new[]
                            {
                                typeof (ArrayList),
                                typeof (List<>),
                                typeof (Collection<>)
                            };

            foreach (var name in types.Select(t => t.FullName)) {
                IList<object> list;
                Assert.IsTrue(resolver.TryGetListInstance(name, out list));
                Assert.NotNull(list);
            }
        }
Exemplo n.º 5
0
        public void SelectWhere_Array_ReturnsExpectedValues()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, bool> evenPredicate = (value) => value % 2 == 0;
            Func<int, int> addSelector = (value) => value + 1;

            IEnumerable<int> result = source.Select(addSelector).Where(evenPredicate);

            Assert.Equal(3, result.Count());
            Assert.Equal(2, result.ElementAt(0));
            Assert.Equal(4, result.ElementAt(1));
            Assert.Equal(6, result.ElementAt(2));
        }
Exemplo n.º 6
0
 public void SelectPropertyUsingIndex()
 {
     var source = new[]{
         new { name="Prakash", custID=98088 },
         new { name="Bob", custID=29099 },
         new { name="Chris", custID=39033 }
     };
     string[] expected = { "Prakash", null, null };
     Assert.Equal(expected, source.Select((e, i) => i == 0 ? e.name : null));
 }
Exemplo n.º 7
0
 public void Select_SourceIsArray_Count()
 {
     var source = new[] { 1, 2, 3, 4 };
     Assert.Equal(source.Length, source.Select(i => i * 2).Count());
 }
Exemplo n.º 8
0
        public void Select_ResetCalledOnEnumerator_ExceptionThrown()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, int> selector = i => i + 1;

            var result = source.Select(selector);
            var enumerator = result.GetEnumerator();

            Assert.Throws<NotSupportedException>(() => enumerator.Reset());
        }
Exemplo n.º 9
0
        public void Select_GetEnumeratorCalledTwice_DifferentInstancesReturned()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            var query = source.Select(i => i + 1);

            var enumerator1 = query.GetEnumerator();
            var enumerator2 = query.GetEnumerator();

            Assert.Same(query, enumerator1);
            Assert.NotSame(enumerator1, enumerator2);

            enumerator1.Dispose();
            enumerator2.Dispose();
        }
Exemplo n.º 10
0
        public void Select_ExceptionThrownFromSelector_IteratorCanBeUsedAfterExceptionIsCaught()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, int> selector = i =>
            {
                if (i == 1)
                    throw new InvalidOperationException();
                return i + 1;
            };

            var result = source.Select(selector);
            var enumerator = result.GetEnumerator();

            Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
            enumerator.MoveNext();
            Assert.Equal(3 /* 2 + 1 */, enumerator.Current);
        }
Exemplo n.º 11
0
        public void Select_ExceptionThrownFromSelector_ExceptionPropagatedToTheCaller()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, int> selector = i => { throw new InvalidOperationException(); };

            var result = source.Select(selector);
            var enumerator = result.GetEnumerator();

            Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
        }
Exemplo n.º 12
0
        public void SelectSelect_SourceIsAnArray_ReturnsExpectedValues()
        {
            Func<int, int> selector = i => i + 1;
            int[] source = new[] { 1, 2, 3, 4, 5 };

            IEnumerable<int> query = source.Select(selector).Select(selector);

            int index = 0;
            foreach (var item in query)
            {
                var expected = selector(selector(source[index]));
                Assert.Equal(expected, item);
                index++;
            }

            Assert.Equal(source.Length, index);
        }
Exemplo n.º 13
0
        public void Select_SourceIsAnArray_CurrentIsDefaultOfTAfterEnumeration()
        {
            int[] source = new[] { 1 };
            Func<int, int> selector = i => i + 1;

            IEnumerable<int> query = source.Select(selector);

            var enumerator = query.GetEnumerator();
            while (enumerator.MoveNext()) ;

            Assert.Equal(default(int), enumerator.Current);
        }
Exemplo n.º 14
0
 public void SelectPropertyPassingIndexOnLast()
 {
     var source = new[]{
         new { name="Prakash", custID=98088},
         new { name="Bob", custID=29099 },
         new { name="Chris", custID=39033 },
         new { name="Robert", custID=39033 },
         new { name="Allen", custID=39033 },
         new { name="Chuck", custID=39033 }
     };
     string[] expected = { null, null, null, null, null, "Chuck" };
     Assert.Equal(expected, source.Select((e, i) => i == 5 ? e.name : null));
 }