1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
21
using NUnit.Framework;
23
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
26
public class QueryExpressionTests
29
public void SimpleExpression()
31
ParseUtilCSharp.AssertExpression(
32
"from c in customers where c.City == \"London\" select c",
37
Expression = new IdentifierExpression("customers")
39
new QueryWhereClause {
40
Condition = new BinaryOperatorExpression {
41
Left = new IdentifierExpression("c").Member("City"),
42
Operator = BinaryOperatorType.Equality,
43
Right = new PrimitiveExpression("London")
46
new QuerySelectClause {
47
Expression = new IdentifierExpression("c")
53
public void ExpressionWithType1()
55
ParseUtilCSharp.AssertExpression(
56
"from Customer c in customers select c",
60
Type = new SimpleType("Customer"),
62
Expression = new IdentifierExpression("customers")
64
new QuerySelectClause {
65
Expression = new IdentifierExpression("c")
71
public void ExpressionWithType2()
73
ParseUtilCSharp.AssertExpression(
74
"from int c in customers select c",
78
Type = new PrimitiveType("int"),
80
Expression = new IdentifierExpression("customers")
82
new QuerySelectClause {
83
Expression = new IdentifierExpression("c")
90
public void ExpressionWithType3()
92
ParseUtilCSharp.AssertExpression(
93
"from S<int[]>? c in customers select c",
97
Type = new ComposedType {
98
BaseType = new SimpleType {
101
new PrimitiveType("int").MakeArrayType()
104
HasNullableSpecifier = true
107
Expression = new IdentifierExpression("customers")
109
new QuerySelectClause {
110
Expression = new IdentifierExpression("c")
116
public void MultipleGenerators()
118
ParseUtilCSharp.AssertExpression(
121
where c.City == ""London""
123
where o.OrderDate.Year == 2005
124
select new { c.Name, o.OrderID, o.Total }",
125
new QueryExpression {
127
new QueryFromClause {
129
Expression = new IdentifierExpression("customers")
131
new QueryWhereClause {
132
Condition = new BinaryOperatorExpression {
133
Left = new IdentifierExpression("c").Member("City"),
134
Operator = BinaryOperatorType.Equality,
135
Right = new PrimitiveExpression("London")
138
new QueryFromClause {
140
Expression = new IdentifierExpression("c").Member("Orders")
142
new QueryWhereClause {
143
Condition = new BinaryOperatorExpression {
144
Left = new IdentifierExpression("o").Member("OrderDate").Member("Year"),
145
Operator = BinaryOperatorType.Equality,
146
Right = new PrimitiveExpression(2005)
149
new QuerySelectClause {
150
Expression = new AnonymousTypeCreateExpression {
152
new IdentifierExpression("c").Member("Name"),
153
new IdentifierExpression("o").Member("OrderID"),
154
new IdentifierExpression("o").Member("Total")
162
public void ExpressionWithOrderBy()
164
ParseUtilCSharp.AssertExpression(
165
"from c in customers orderby c.Name select c",
166
new QueryExpression {
168
new QueryFromClause {
170
Expression = new IdentifierExpression("customers")
172
new QueryOrderClause {
175
Expression = new IdentifierExpression("c").Member("Name")
179
new QuerySelectClause {
180
Expression = new IdentifierExpression("c")
186
public void ExpressionWithOrderByWithTwoOrderings()
188
ParseUtilCSharp.AssertExpression(
189
"from c in customers orderby c.Name, c.Address select c",
190
new QueryExpression {
192
new QueryFromClause {
194
Expression = new IdentifierExpression("customers")
196
new QueryOrderClause {
199
Expression = new IdentifierExpression("c").Member("Name")
202
Expression = new IdentifierExpression("c").Member("Address")
206
new QuerySelectClause {
207
Expression = new IdentifierExpression("c")
213
public void ExpressionWithOrderByWithTwoOrderBys()
215
ParseUtilCSharp.AssertExpression(
216
"from c in customers orderby c.Name orderby c.Address select c",
217
new QueryExpression {
219
new QueryFromClause {
221
Expression = new IdentifierExpression("customers")
223
new QueryOrderClause {
226
Expression = new IdentifierExpression("c").Member("Name")
230
new QueryOrderClause {
233
Expression = new IdentifierExpression("c").Member("Address")
237
new QuerySelectClause {
238
Expression = new IdentifierExpression("c")
244
public void ExpressionWithOrderByWithTwoOrderingsDescending()
246
ParseUtilCSharp.AssertExpression(
247
"from c in customers orderby c.Name descending, c.Address descending select c",
248
new QueryExpression {
250
new QueryFromClause {
252
Expression = new IdentifierExpression("customers")
254
new QueryOrderClause {
257
Expression = new IdentifierExpression("c").Member("Name"),
258
Direction = QueryOrderingDirection.Descending
261
Expression = new IdentifierExpression("c").Member("Address"),
262
Direction = QueryOrderingDirection.Descending
266
new QuerySelectClause {
267
Expression = new IdentifierExpression("c")
273
public void ExpressionWithOrderByWithTwoOrderByDecendings()
275
ParseUtilCSharp.AssertExpression(
276
"from c in customers orderby c.Name descending orderby c.Address descending select c",
277
new QueryExpression {
279
new QueryFromClause {
281
Expression = new IdentifierExpression("customers")
283
new QueryOrderClause {
286
Expression = new IdentifierExpression("c").Member("Name"),
287
Direction = QueryOrderingDirection.Descending
291
new QueryOrderClause {
294
Expression = new IdentifierExpression("c").Member("Address"),
295
Direction = QueryOrderingDirection.Descending
299
new QuerySelectClause {
300
Expression = new IdentifierExpression("c")
306
public void ExpressionWithOrderByAndLet()
308
ParseUtilCSharp.AssertExpression(
309
"from c in customers orderby c.Name descending let x = c select x",
310
new QueryExpression {
312
new QueryFromClause {
314
Expression = new IdentifierExpression("customers")
316
new QueryOrderClause {
319
Expression = new IdentifierExpression("c").Member("Name"),
320
Direction = QueryOrderingDirection.Descending
326
Expression = new IdentifierExpression("c")
328
new QuerySelectClause {
329
Expression = new IdentifierExpression("x")
335
public void QueryContinuation()
337
ParseUtilCSharp.AssertExpression(
338
"from a in b select c into d select e",
339
new QueryExpression {
341
new QueryContinuationClause {
342
PrecedingQuery = new QueryExpression {
344
new QueryFromClause {
346
Expression = new IdentifierExpression("b")
348
new QuerySelectClause { Expression = new IdentifierExpression("c") }
353
new QuerySelectClause { Expression = new IdentifierExpression("e") }
361
public void QueryContinuationWithMultipleFrom()
363
ParseUtilCSharp.AssertExpression(
364
"from a in b from c in d select e into f select g",
365
new QueryExpression {
367
new QueryContinuationClause {
368
PrecedingQuery = new QueryExpression {
370
new QueryFromClause {
372
Expression = new IdentifierExpression("b")
374
new QueryFromClause {
376
Expression = new IdentifierExpression("d")
378
new QuerySelectClause { Expression = new IdentifierExpression("e") }
383
new QuerySelectClause { Expression = new IdentifierExpression("g") }
390
public void MultipleQueryContinuation()
392
ParseUtilCSharp.AssertExpression(
393
"from a in b select c into d select e into f select g",
394
new QueryExpression {
396
new QueryContinuationClause {
397
PrecedingQuery = new QueryExpression {
399
new QueryContinuationClause {
400
PrecedingQuery = new QueryExpression {
402
new QueryFromClause {
404
Expression = new IdentifierExpression("b")
406
new QuerySelectClause { Expression = new IdentifierExpression("c") }
411
new QuerySelectClause { Expression = new IdentifierExpression("e") }
416
new QuerySelectClause { Expression = new IdentifierExpression("g") }
421
public void QueryWithGroupBy()
423
ParseUtilCSharp.AssertExpression(
424
"from a in b group c by d",
425
new QueryExpression {
427
new QueryFromClause {
429
Expression = new IdentifierExpression("b")
431
new QueryGroupClause {
432
Projection = new IdentifierExpression("c"),
433
Key = new IdentifierExpression("d")
439
public void QueryWithJoin()
441
ParseUtilCSharp.AssertExpression(
442
"from a in b join c in d on e equals f select g",
443
new QueryExpression {
445
new QueryFromClause {
447
Expression = new IdentifierExpression("b")
449
new QueryJoinClause {
450
JoinIdentifier = "c",
451
InExpression = new IdentifierExpression("d"),
452
OnExpression = new IdentifierExpression("e"),
453
EqualsExpression = new IdentifierExpression("f")
455
new QuerySelectClause {
456
Expression = new IdentifierExpression("g")
462
public void QueryWithGroupJoin()
464
ParseUtilCSharp.AssertExpression(
465
"from a in b join c in d on e equals f into g select h",
466
new QueryExpression {
468
new QueryFromClause {
470
Expression = new IdentifierExpression("b")
472
new QueryJoinClause {
473
JoinIdentifier = "c",
474
InExpression = new IdentifierExpression("d"),
475
OnExpression = new IdentifierExpression("e"),
476
EqualsExpression = new IdentifierExpression("f"),
479
new QuerySelectClause {
480
Expression = new IdentifierExpression("h")
486
public void fSelect()
488
ParseUtilCSharp.AssertExpression(
489
"from arg in args where float.Parse(arg) == 1.0fselect arg",
490
new QueryExpression {
492
new QueryFromClause {
494
Expression = new IdentifierExpression("args")
496
new QueryWhereClause {
497
Condition = new BinaryOperatorExpression(
498
new PrimitiveType("float").Invoke("Parse", new IdentifierExpression("arg")),
499
BinaryOperatorType.Equality,
500
new PrimitiveExpression(1.0f)
503
new QuerySelectClause {
504
Expression = new IdentifierExpression("arg")