|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
| 4 | +using System.Collections.Generic; |
4 | 5 | using System.Linq.Expressions;
|
5 | 6 | using Microsoft.TestCommon;
|
6 | 7 |
|
@@ -78,9 +79,83 @@ public void LambdaBasedExpressionTextTests()
|
78 | 79 | "The expression compiler was unable to evaluate the indexer expression '(s.Length - 4)' because it references the model parameter 's' which is unavailable.");
|
79 | 80 | }
|
80 | 81 |
|
| 82 | + public static TheoryDataSet<LambdaExpression, string> ComplicatedLambdaExpressions |
| 83 | + { |
| 84 | + get |
| 85 | + { |
| 86 | + var collection = new List<DummyModelContainer>(); |
| 87 | + var index = 20; |
| 88 | + var data = new TheoryDataSet<LambdaExpression, string> |
| 89 | + { |
| 90 | + |
| 91 | + { |
| 92 | + Lambda((List<DummyModelContainer> m) => collection[10].Model.FirstName), |
| 93 | + "collection[10].Model.FirstName" |
| 94 | + }, |
| 95 | + { |
| 96 | + Lambda((List<DummyModelContainer> m) => m[10].Model.FirstName), |
| 97 | + "[10].Model.FirstName" |
| 98 | + }, |
| 99 | + { |
| 100 | + Lambda((List<DummyModelContainer> m) => collection[index].Model.FirstName), |
| 101 | + "collection[20].Model.FirstName" |
| 102 | + }, |
| 103 | + { |
| 104 | + Lambda((List<DummyModelContainer> m) => m[index].Model.FirstName), |
| 105 | + "[20].Model.FirstName" |
| 106 | + }, |
| 107 | + }; |
| 108 | + |
| 109 | + return data; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + [Theory] |
| 114 | + [PropertyData("ComplicatedLambdaExpressions")] |
| 115 | + public void GetExpressionText_WithComplicatedLambdaExpressions_ReturnsExpectedText( |
| 116 | + LambdaExpression expression, |
| 117 | + string expectedText) |
| 118 | + { |
| 119 | + // Arrange & Act |
| 120 | + var result = ExpressionHelper.GetExpressionText(expression); |
| 121 | + |
| 122 | + // Assert |
| 123 | + Assert.Equal(expectedText, result); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void GetExpressionText_WithinALoop_ReturnsExpectedText() |
| 128 | + { |
| 129 | + // Arrange 0 |
| 130 | + var collection = new List<DummyModelContainer>(); |
| 131 | + |
| 132 | + for (var i = 0; i < 2; i++) |
| 133 | + { |
| 134 | + // Arrange 1 |
| 135 | + var expectedText = string.Format("collection[{0}].Model.FirstName", i); |
| 136 | + |
| 137 | + // Act 1 |
| 138 | + var result = ExpressionHelper.GetExpressionText(Lambda( |
| 139 | + (List<DummyModelContainer> m) => collection[i].Model.FirstName)); |
| 140 | + |
| 141 | + // Assert 1 |
| 142 | + Assert.Equal(expectedText, result); |
| 143 | + |
| 144 | + // Arrange 2 |
| 145 | + expectedText = string.Format("[{0}].Model.FirstName", i); |
| 146 | + |
| 147 | + // Act 2 |
| 148 | + result = ExpressionHelper.GetExpressionText(Lambda( |
| 149 | + (List<DummyModelContainer> m) => m[i].Model.FirstName)); |
| 150 | + |
| 151 | + // Assert 2 |
| 152 | + Assert.Equal(expectedText, result); |
| 153 | + } |
| 154 | + } |
| 155 | + |
81 | 156 | // Helpers
|
82 | 157 |
|
83 |
| - private LambdaExpression Lambda<T1, T2>(Expression<Func<T1, T2>> expression) |
| 158 | + private static LambdaExpression Lambda<T1, T2>(Expression<Func<T1, T2>> expression) |
84 | 159 | {
|
85 | 160 | return expression;
|
86 | 161 | }
|
|
0 commit comments