• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NetFabric / NetFabric.CodeAnalysis / 6277926364

22 Sep 2023 06:38PM UTC coverage: 73.953% (-9.2%) from 83.158%
6277926364

push

github

web-flow
Add IsIndexable (#28)

323 of 452 branches covered (0.0%)

Branch coverage included in aggregate %.

349 of 349 new or added lines in 18 files covered. (100.0%)

648 of 861 relevant lines covered (75.26%)

20.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.32
/NetFabric.Reflection/Expressions/ExpressionEx.For.cs
1
using NetFabric.Reflection;
2
using System;
3
using System.Linq.Expressions;
4
using static System.Linq.Expressions.Expression;
5

6
namespace NetFabric.Expressions;
7

8
public static partial class ExpressionEx
9
{
10
    /// <summary>
11
    /// Creates a 'for' loop expression in C# with the specified initialization, condition,
12
    /// iterator, and loop body expressions.
13
    /// </summary>
14
    /// <param name="initialization">
15
    /// An <see cref="Expression"/> representing the loop initialization.
16
    /// This expression is executed once before the loop begins.
17
    /// </param>
18
    /// <param name="condition">
19
    /// An <see cref="Expression"/> representing the loop condition.
20
    /// The loop continues executing as long as this condition evaluates to true.
21
    /// </param>
22
    /// <param name="iterator">
23
    /// An <see cref="Expression"/> representing the loop iterator.
24
    /// This expression is executed after each iteration of the loop.
25
    /// </param>
26
    /// <param name="body">
27
    /// An <see cref="Expression"/> representing the body of the loop.
28
    /// This expression contains the statements to be executed in each iteration.
29
    /// </param>
30
    /// <returns>
31
    /// An <see cref="Expression"/> representing the 'for' loop with the specified components.
32
    /// </returns>
33
    public static Expression For(Expression initialization, Expression condition, Expression iterator, Expression body) 
34
        => Block(
14✔
35
            initialization,
14✔
36
            While(
14✔
37
                condition, 
14✔
38
                Block(
14✔
39
                    body,
14✔
40
                    iterator
14✔
41
                )
14✔
42
            )
14✔
43
        );
14✔
44

45
    /// <summary>
46
    /// Creates a 'for' loop expression in C# for iterating over elements of an array.
47
    /// </summary>
48
    /// <param name="array">
49
    /// An <see cref="Expression"/> representing the array to iterate over.
50
    /// </param>
51
    /// <param name="indexableInfo">
52
    /// An <see cref="IndexableInfo"/> object providing information about the indexable element,
53
    /// including the index variable and any additional indexing information. This object is obtained by
54
    /// calling the <see cref="IsIndexable(Type, out IndexableInfo?)"/> method on the array type.
55
    /// </param>
56
    /// <param name="body">
57
    /// A delegate that defines the loop body, taking an <see cref="Expression"/> representing the current
58
    /// element and returning an <see cref="Expression"/> representing the body of the loop for that element.
59
    /// </param>
60
    /// <returns>
61
    /// An <see cref="Expression"/> representing the 'for' loop with the specified components.
62
    /// </returns>
63
    /// <remarks>
64
    /// This method is specifically designed for creating 'for' loops to iterate over elements of arrays.
65
    /// The <paramref name="array"/> should represent an array, and the <paramref name="indexableInfo"/>
66
    /// should provide the necessary information to access and iterate over its elements. The <paramref name="body"/>
67
    /// delegate defines the operations to be performed on each element during iteration.
68
    /// </remarks>
69
    public static Expression For(Expression array, IndexableInfo indexableInfo, Func<Expression, Expression> body)
70
    {
71
        var arrayType = array.Type;
6✔
72
        if (!arrayType.IsArray)
6!
73
            throw new ArgumentException($"'{arrayType}' is not an array", nameof(array));
×
74

75
        var indexVariable = Variable(indexableInfo.CountOrLength.GetGetMethod()!.ReturnType, "__index__");
6✔
76
        var arrayVariable = Variable(arrayType, "__array__");
6✔
77
        return Block(
6!
78
            new[] { indexVariable, arrayVariable },
6✔
79
            Assign(arrayVariable, array), // local array for JIT compiler to remove bounds check
6✔
80
            For(
6✔
81
                Assign(indexVariable, Constant(0)),
6✔
82
                LessThan(indexVariable, Property(arrayVariable, indexableInfo.CountOrLength)),
6✔
83
                PostIncrementAssign(indexVariable),
6✔
84
                body(indexableInfo.Indexer is null
6✔
85
                    ? ArrayIndex(arrayVariable, indexVariable)
6✔
86
                    : Property(arrayVariable, indexableInfo.Indexer, indexVariable))
6✔
87
            )
6✔
88
        );
6✔
89
    }
90

91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc