-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.cs
More file actions
176 lines (150 loc) · 5.33 KB
/
Tests.cs
File metadata and controls
176 lines (150 loc) · 5.33 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
public class Tests
{
#region Example
[Fact]
public Task VerifySalaryCalculation()
{
var expression = ComplexExpressionTrees.SalaryCalculation();
return Verify(expression);
}
#endregion
[Fact]
public Task VerifyNullSafeAccess()
{
var expression = ComplexExpressionTrees.NullSafeAccess();
return Verify(expression);
}
[Fact]
public Task VerifyStringManipulation()
{
var expression = ComplexExpressionTrees.StringManipulation();
return Verify(expression);
}
[Fact]
public Task VerifySalaryCalculationNested()
{
var expression = ComplexExpressionTrees.SalaryCalculation();
return Verify(new
{
expression
});
}
[Fact]
public Task VerifyNullSafeAccessNested()
{
var expression = ComplexExpressionTrees.NullSafeAccess();
return Verify(new
{
expression
});
}
[Fact]
public Task VerifyStringManipulationNested()
{
var expression = ComplexExpressionTrees.StringManipulation();
return Verify(new
{
expression
});
}
static readonly Person[] testPeople =
[
new("John", "Smith", 25, "New York", 55000m, true),
new("Jane", "Doe", 45, "New Jersey", 75000m, true),
new("Bob", "Wilson", 70, "Boston", 40000m, false),
new("Al", "Brown", 22, null, 30000m, true),
new("Sarah", "Johnson", 35, "Newark", 60000m, false)
];
[Fact]
public void SalaryCalculation_ShouldCompile()
{
// Arrange & Act
var expression = ComplexExpressionTrees.SalaryCalculation();
var compiled = expression.Compile();
// Assert
Assert.NotNull(compiled);
Assert.Equal(typeof(decimal), expression.ReturnType);
}
[Theory]
[InlineData("John", "Smith", 63250)] // Age 25 < 30: $55,000 * 1.15
[InlineData("Jane", "Doe", 81000)] // Age 45 (30-50): $75,000 * 1.08
[InlineData("Bob", "Wilson", 41200)] // Age 70 > 50: $40,000 * 1.03
[InlineData("Al", "Brown", 34500)] // Age 22 < 30: $30,000 * 1.15
[InlineData("Sarah", "Johnson", 64800)] // Age 35 (30-50): $60,000 * 1.08
public void SalaryCalculation_ShouldCalculateCorrectBonus(string firstName, string lastName, decimal expectedSalary)
{
// Arrange
var expression = ComplexExpressionTrees.SalaryCalculation();
var compiled = expression.Compile();
var person = testPeople.First(p => p.FirstName == firstName && p.LastName == lastName);
// Act
var result = compiled(person);
// Assert
Assert.Equal(expectedSalary, result);
}
[Fact]
public void StringManipulation_ShouldCompile()
{
// Arrange & Act
var expression = ComplexExpressionTrees.StringManipulation();
var compiled = expression.Compile();
// Assert
Assert.NotNull(compiled);
Assert.Equal(typeof(string), expression.ReturnType);
}
[Theory]
[InlineData("John", "Smith", "NEW YORK - Smi")]
[InlineData("Jane", "Doe", "NEW JERSEY - Doe")]
[InlineData("Bob", "Wilson", "BOSTON - Wil")]
[InlineData("Al", "Brown", "UNKNOWN - Bro")]
[InlineData("Sarah", "Johnson", "NEWARK - Joh")]
public void StringManipulation_ShouldFormatCorrectly(string firstName, string lastName, string expected)
{
// Arrange
var expression = ComplexExpressionTrees.StringManipulation();
var compiled = expression.Compile();
var person = testPeople.First(p => p.FirstName == firstName && p.LastName == lastName);
// Act
var result = compiled(person);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void NullSafeAccess_ShouldCompile()
{
// Arrange & Act
var expression = ComplexExpressionTrees.NullSafeAccess();
var compiled = expression.Compile();
// Assert
Assert.NotNull(compiled);
Assert.Equal(typeof(string), expression.ReturnType);
}
[Theory]
[InlineData("John", "Smith", "n")] // New York -> 'n'
[InlineData("Jane", "Doe", "n")] // New Jersey -> 'n'
[InlineData("Bob", "Wilson", "b")] // Boston -> 'b'
[InlineData("Al", "Brown", "unknown")] // null -> "unknown"
[InlineData("Sarah", "Johnson", "n")] // Newark -> 'n'
public void NullSafeAccess_ShouldHandleNullAndExtractFirstChar(string firstName, string lastName, string expected)
{
// Arrange
var expression = ComplexExpressionTrees.NullSafeAccess();
var compiled = expression.Compile();
var person = testPeople.First(p => p.FirstName == firstName && p.LastName == lastName);
// Act
var result = compiled(person);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void SalaryCalculation_ShouldContainConditionalExpressions()
{
// Arrange
var expression = ComplexExpressionTrees.SalaryCalculation();
// Act & Assert - verify nested conditionals
Assert.Equal(ExpressionType.Conditional, expression.Body.NodeType);
Assert.IsAssignableFrom<ConditionalExpression>(expression.Body);
var conditional = (ConditionalExpression) expression.Body;
Assert.Equal(ExpressionType.Conditional, conditional.IfFalse.NodeType); // Nested conditional
}
}