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
117 lines (95 loc) · 3.56 KB
/
ExpressionHelperTests.cs
File metadata and controls
117 lines (95 loc) · 3.56 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
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 IExpressionHelper _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_GenerateAndAlsoNotNullExpression()
{
// Assign
Expression<Func<Item, int>> expression = (x) => x.Relation1.Relation2.Id;
// Act
Expression result = _expressionHelper.GenerateAndAlsoNotNullExpression(expression);
// Assert
Check.That(result.ToString()).IsEqualTo("(((x != null) AndAlso (x.Relation1 != null)) AndAlso (x.Relation1.Relation2 != null))");
}
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; }
}
}
}