forked from zzzprojects/System.Linq.Dynamic.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionHelperTests.cs
More file actions
162 lines (130 loc) · 5.49 KB
/
ExpressionHelperTests.cs
File metadata and controls
162 lines (130 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using NFluent;
using System.Linq.Dynamic.Core.Parser;
using System.Linq.Expressions;
using Xunit;
namespace System.Linq.Dynamic.Core.Tests.Parser
{
public class ExpressionHelperTests
{
private readonly ExpressionHelper _expressionHelper;
public ExpressionHelperTests()
{
_expressionHelper = new ExpressionHelper(ParsingConfig.Default);
}
[Fact]
public void ExpressionHelper_WrapConstantExpression_false()
{
// Assign
var config = new ParsingConfig
{
UseParameterizedNamesInDynamicQuery = false
};
var expressionHelper = new ExpressionHelper(config);
string value = "test";
Expression expression = Expression.Constant(value);
// Act
expressionHelper.WrapConstantExpression(ref expression);
// Assert
Check.That(expression).IsInstanceOf<ConstantExpression>();
Check.That(expression.ToString()).Equals("\"test\"");
}
[Fact]
public void ExpressionHelper_WrapConstantExpression_true()
{
// Assign
var config = new ParsingConfig
{
UseParameterizedNamesInDynamicQuery = true
};
var expressionHelper = new ExpressionHelper(config);
string value = "test";
Expression expression = Expression.Constant(value);
// Act
expressionHelper.WrapConstantExpression(ref expression);
expressionHelper.WrapConstantExpression(ref expression);
// Assert
Check.That(expression.GetType().FullName).Equals("System.Linq.Expressions.PropertyExpression");
Check.That(expression.ToString()).Equals("value(System.Linq.Dynamic.Core.Parser.WrappedValue`1[System.String]).Value");
}
[Fact]
public void ExpressionHelper_OptimizeStringForEqualityIfPossible_Guid()
{
// Assign
string guidAsString = Guid.NewGuid().ToString();
// Act
Expression result = _expressionHelper.OptimizeStringForEqualityIfPossible(guidAsString, typeof(Guid));
// Assert
Check.That(result).IsInstanceOf<ConstantExpression>();
Check.That(result.ToString()).Equals(guidAsString);
}
[Fact]
public void ExpressionHelper_OptimizeStringForEqualityIfPossible_Guid_Invalid()
{
// Assign
string guidAsString = "x";
// Act
Expression result = _expressionHelper.OptimizeStringForEqualityIfPossible(guidAsString, typeof(Guid));
// Assert
Check.That(result).IsNull();
}
[Fact]
public void ExpressionHelper_TryGenerateAndAlsoNotNullExpression_NestedNonNullable()
{
// Assign
Expression<Func<Item, int>> expression = x => x.Relation1.Relation2.Id;
// Act
bool result = _expressionHelper.TryGenerateAndAlsoNotNullExpression(expression, true, out Expression generatedExpression);
// Assert
Check.That(result).IsTrue();
Check.That(generatedExpression.ToString()).IsEqualTo("(((x != null) AndAlso (x.Relation1 != null)) AndAlso (x.Relation1.Relation2 != null))");
}
[Fact]
public void ExpressionHelper_TryGenerateAndAlsoNotNullExpression_NestedNullable_AddSelfFalse()
{
// Assign
Expression<Func<Item, int?>> expression = x => x.Relation1.Relation2.IdNullable;
// Act
bool result = _expressionHelper.TryGenerateAndAlsoNotNullExpression(expression, false, out Expression generatedExpression);
// Assert
Check.That(result).IsTrue();
Check.That(generatedExpression.ToString()).IsEqualTo("(((x != null) AndAlso (x.Relation1 != null)) AndAlso (x.Relation1.Relation2 != null))");
}
[Fact]
public void ExpressionHelper_TryGenerateAndAlsoNotNullExpression_NestedNullable_AddSelfTrue()
{
// Assign
Expression<Func<Item, int?>> expression = x => x.Relation1.Relation2.IdNullable;
// Act
bool result = _expressionHelper.TryGenerateAndAlsoNotNullExpression(expression, true, out Expression generatedExpression);
// Assert
Check.That(result).IsTrue();
Check.That(generatedExpression.ToString()).IsEqualTo("((((x != null) AndAlso (x.Relation1 != null)) AndAlso (x.Relation1.Relation2 != null)) AndAlso (x => x.Relation1.Relation2.IdNullable != null))");
}
[Fact]
public void ExpressionHelper_TryGenerateAndAlsoNotNullExpression_NonNullable()
{
// Assign
Expression<Func<Item, int>> expression = x => x.Id;
// Act
bool result = _expressionHelper.TryGenerateAndAlsoNotNullExpression(expression, true, out Expression generatedExpression);
// Assert
Check.That(result).IsFalse();
Check.That(generatedExpression.ToString()).IsEqualTo("x => x.Id");
}
class Item
{
public int Id { get; set; }
public Relation1 Relation1 { get; set; }
}
class Relation1
{
public int Id { get; set; }
public Relation2 Relation2 { get; set; }
}
class Relation2
{
public int Id { get; set; }
public int? IdNullable { get; set; }
}
}
}