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); }
public string CreateImpedimentData(bool? isPrivate, int? ownerId, int? responsibleId) { var parts = new[] { isPrivate.GetValueOrDefault() ? string.Empty : "public", ownerId.HasValue ? EncodeStringId(ownerId.Value, "Owner") : string.Empty, responsibleId.HasValue ? EncodeStringId(responsibleId.Value, "Responsible") : string.Empty }; return string.Join(" ", parts.Where(x => !string.IsNullOrEmpty(x))); }
public void Where_GetEnumeratorReturnsUniqueInstances() { int[] source = new[] { 1, 2, 3, 4, 5 }; var result = source.Where(value => true); using (var enumerator1 = result.GetEnumerator()) using (var enumerator2 = result.GetEnumerator()) { Assert.Same(result, enumerator1); Assert.NotSame(enumerator1, enumerator2); } }
public void Select_SourceThrowsOnReset() { int[] source = new[] { 1, 2, 3, 4, 5 }; var enumerator = source.Where(value => true).GetEnumerator(); Assert.Throws<NotSupportedException>(() => enumerator.Reset()); }
public void Where_PredicateThrowsException() { int[] source = new[] { 1, 2, 3, 4, 5 }; Func<int, bool> predicate = value => { if (value == 1) { throw new InvalidOperationException(); } return true; }; var enumerator = source.Where(predicate).GetEnumerator(); // Ensure the first MoveNext call throws an exception Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext()); // Ensure Current is set to the default value of type T int currentValue = enumerator.Current; Assert.Equal(default(int), currentValue); // Ensure subsequent MoveNext calls succeed Assert.True(enumerator.MoveNext()); Assert.Equal(2, enumerator.Current); }
public void WhereSelectSelect_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.Where(evenPredicate).Select(i => i).Select(addSelector); Assert.Equal(2, result.Count()); Assert.Equal(3, result.ElementAt(0)); Assert.Equal(5, result.ElementAt(1)); }
public void WhereWhere_Array_ReturnsExpectedValues() { int[] source = new[] { 1, 2, 3, 4, 5 }; Func<int, bool> evenPredicate = (value) => value % 2 == 0; IEnumerable<int> result = source.Where(evenPredicate).Where(evenPredicate); Assert.Equal(2, result.Count()); Assert.Equal(2, result.ElementAt(0)); Assert.Equal(4, result.ElementAt(1)); }
public void Where_Array_CurrentIsDefaultOfTAfterEnumeration() { int[] source = new[] { 1 }; Func<int, bool> truePredicate = (value) => true; var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); }
public void Where_Array_ReturnsExpectedValues_Complex() { int[] source = new[] { 2, 1, 3, 5, 4 }; Func<int, int, bool> complexPredicate = (value, index) => { return (value == index); }; IEnumerable<int> result = source.Where(complexPredicate); Assert.Equal(2, result.Count()); Assert.Equal(1, result.ElementAt(0)); Assert.Equal(4, result.ElementAt(1)); }
public void Where_Array_ReturnsExpectedValues_False() { int[] source = new[] { 1, 2, 3, 4, 5 }; Func<int, bool> falsePredicate = (value) => false; IEnumerable<int> result = source.Where(falsePredicate); Assert.Equal(0, result.Count()); }
public void Where_Array_ReturnsExpectedValues_True() { int[] source = new[] { 1, 2, 3, 4, 5 }; Func<int, bool> truePredicate = (value) => true; IEnumerable<int> result = source.Where(truePredicate); Assert.Equal(source.Length, result.Count()); for (int i = 0; i < source.Length; i++) { Assert.Equal(source.ElementAt(i), result.ElementAt(i)); } }