~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/QueryExpressionTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
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:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
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.
 
18
 
 
19
using System;
 
20
using System.Linq;
 
21
using NUnit.Framework;
 
22
 
 
23
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
 
24
{
 
25
        [TestFixture]
 
26
        public class QueryExpressionTests
 
27
        {
 
28
                [Test]
 
29
                public void SimpleExpression()
 
30
                {
 
31
                        ParseUtilCSharp.AssertExpression(
 
32
                                "from c in customers where c.City == \"London\" select c",
 
33
                                new QueryExpression {
 
34
                                        Clauses = {
 
35
                                                new QueryFromClause {
 
36
                                                        Identifier = "c",
 
37
                                                        Expression = new IdentifierExpression("customers")
 
38
                                                },
 
39
                                                new QueryWhereClause {
 
40
                                                        Condition = new BinaryOperatorExpression {
 
41
                                                                Left = new IdentifierExpression("c").Member("City"),
 
42
                                                                Operator = BinaryOperatorType.Equality,
 
43
                                                                Right = new PrimitiveExpression("London")
 
44
                                                        }
 
45
                                                },
 
46
                                                new QuerySelectClause {
 
47
                                                        Expression = new IdentifierExpression("c")
 
48
                                                }
 
49
                                        }});
 
50
                }
 
51
                
 
52
                [Test]
 
53
                public void ExpressionWithType1()
 
54
                {
 
55
                        ParseUtilCSharp.AssertExpression(
 
56
                                "from Customer c in customers select c",
 
57
                                new QueryExpression {
 
58
                                        Clauses = {
 
59
                                                new QueryFromClause {
 
60
                                                        Type = new SimpleType("Customer"),
 
61
                                                        Identifier = "c",
 
62
                                                        Expression = new IdentifierExpression("customers")
 
63
                                                },
 
64
                                                new QuerySelectClause {
 
65
                                                        Expression = new IdentifierExpression("c")
 
66
                                                }
 
67
                                        }});
 
68
                }
 
69
                
 
70
                [Test]
 
71
                public void ExpressionWithType2()
 
72
                {
 
73
                        ParseUtilCSharp.AssertExpression(
 
74
                                "from int c in customers select c",
 
75
                                new QueryExpression {
 
76
                                        Clauses = {
 
77
                                                new QueryFromClause {
 
78
                                                        Type = new PrimitiveType("int"),
 
79
                                                        Identifier = "c",
 
80
                                                        Expression = new IdentifierExpression("customers")
 
81
                                                },
 
82
                                                new QuerySelectClause {
 
83
                                                        Expression = new IdentifierExpression("c")
 
84
                                                }
 
85
                                        }});
 
86
                }
 
87
                
 
88
                
 
89
                [Test]
 
90
                public void ExpressionWithType3()
 
91
                {
 
92
                        ParseUtilCSharp.AssertExpression(
 
93
                                "from S<int[]>? c in customers select c",
 
94
                                new QueryExpression {
 
95
                                        Clauses = {
 
96
                                                new QueryFromClause {
 
97
                                                        Type = new ComposedType {
 
98
                                                                BaseType = new SimpleType {
 
99
                                                                        Identifier = "S",
 
100
                                                                        TypeArguments = {
 
101
                                                                                new PrimitiveType("int").MakeArrayType()
 
102
                                                                        }
 
103
                                                                },
 
104
                                                                HasNullableSpecifier = true
 
105
                                                        },
 
106
                                                        Identifier = "c",
 
107
                                                        Expression = new IdentifierExpression("customers")
 
108
                                                },
 
109
                                                new QuerySelectClause {
 
110
                                                        Expression = new IdentifierExpression("c")
 
111
                                                }
 
112
                                        }});
 
113
                }
 
114
                
 
115
                [Test]
 
116
                public void MultipleGenerators()
 
117
                {
 
118
                        ParseUtilCSharp.AssertExpression(
 
119
                                @"
 
120
from c in customers
 
121
where c.City == ""London""
 
122
from o in c.Orders
 
123
where o.OrderDate.Year == 2005
 
124
select new { c.Name, o.OrderID, o.Total }",
 
125
                                new QueryExpression {
 
126
                                        Clauses = {
 
127
                                                new QueryFromClause {
 
128
                                                        Identifier = "c",
 
129
                                                        Expression = new IdentifierExpression("customers")
 
130
                                                },
 
131
                                                new QueryWhereClause {
 
132
                                                        Condition = new BinaryOperatorExpression {
 
133
                                                                Left = new IdentifierExpression("c").Member("City"),
 
134
                                                                Operator = BinaryOperatorType.Equality,
 
135
                                                                Right = new PrimitiveExpression("London")
 
136
                                                        }
 
137
                                                },
 
138
                                                new QueryFromClause {
 
139
                                                        Identifier = "o",
 
140
                                                        Expression = new IdentifierExpression("c").Member("Orders")
 
141
                                                },
 
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)
 
147
                                                        }
 
148
                                                },
 
149
                                                new QuerySelectClause {
 
150
                                                        Expression = new AnonymousTypeCreateExpression {
 
151
                                                                Initializers = {
 
152
                                                                        new IdentifierExpression("c").Member("Name"),
 
153
                                                                        new IdentifierExpression("o").Member("OrderID"),
 
154
                                                                        new IdentifierExpression("o").Member("Total")
 
155
                                                                }
 
156
                                                        }
 
157
                                                }
 
158
                                        }});
 
159
                }
 
160
                
 
161
                [Test]
 
162
                public void ExpressionWithOrderBy()
 
163
                {
 
164
                        ParseUtilCSharp.AssertExpression(
 
165
                                "from c in customers orderby c.Name select c",
 
166
                                new QueryExpression {
 
167
                                        Clauses = {
 
168
                                                new QueryFromClause {
 
169
                                                        Identifier = "c",
 
170
                                                        Expression = new IdentifierExpression("customers")
 
171
                                                },
 
172
                                                new QueryOrderClause {
 
173
                                                        Orderings = {
 
174
                                                                new QueryOrdering {
 
175
                                                                        Expression = new IdentifierExpression("c").Member("Name")
 
176
                                                                }
 
177
                                                        }
 
178
                                                },
 
179
                                                new QuerySelectClause {
 
180
                                                        Expression = new IdentifierExpression("c")
 
181
                                                }
 
182
                                        }});
 
183
                }
 
184
 
 
185
                [Test]
 
186
                public void ExpressionWithOrderByWithTwoOrderings()
 
187
                {
 
188
                        ParseUtilCSharp.AssertExpression(
 
189
                                "from c in customers orderby c.Name, c.Address select c",
 
190
                                new QueryExpression {
 
191
                                        Clauses = {
 
192
                                                new QueryFromClause {
 
193
                                                        Identifier = "c",
 
194
                                                        Expression = new IdentifierExpression("customers")
 
195
                                                },
 
196
                                                new QueryOrderClause {
 
197
                                                        Orderings = {
 
198
                                                                new QueryOrdering {
 
199
                                                                        Expression = new IdentifierExpression("c").Member("Name")
 
200
                                                                },
 
201
                                                                new QueryOrdering {
 
202
                                                                        Expression = new IdentifierExpression("c").Member("Address")
 
203
                                                                }
 
204
                                                        }
 
205
                                                },
 
206
                                                new QuerySelectClause {
 
207
                                                        Expression = new IdentifierExpression("c")
 
208
                                                }
 
209
                                        }});
 
210
                }
 
211
 
 
212
                [Test]
 
213
                public void ExpressionWithOrderByWithTwoOrderBys()
 
214
                {
 
215
                        ParseUtilCSharp.AssertExpression(
 
216
                                "from c in customers orderby c.Name orderby c.Address select c",
 
217
                                new QueryExpression {
 
218
                                        Clauses = {
 
219
                                                new QueryFromClause {
 
220
                                                        Identifier = "c",
 
221
                                                        Expression = new IdentifierExpression("customers")
 
222
                                                },
 
223
                                                new QueryOrderClause {
 
224
                                                        Orderings = {
 
225
                                                                new QueryOrdering {
 
226
                                                                        Expression = new IdentifierExpression("c").Member("Name")
 
227
                                                                }
 
228
                                                        }
 
229
                                                },
 
230
                                                new QueryOrderClause {
 
231
                                                        Orderings = {
 
232
                                                                new QueryOrdering {
 
233
                                                                        Expression = new IdentifierExpression("c").Member("Address")
 
234
                                                                }
 
235
                                                        }
 
236
                                                },
 
237
                                                new QuerySelectClause {
 
238
                                                        Expression = new IdentifierExpression("c")
 
239
                                                }
 
240
                                        }});
 
241
                }
 
242
                
 
243
                [Test]
 
244
                public void ExpressionWithOrderByWithTwoOrderingsDescending()
 
245
                {
 
246
                        ParseUtilCSharp.AssertExpression(
 
247
                                "from c in customers orderby c.Name descending, c.Address descending select c",
 
248
                                new QueryExpression {
 
249
                                        Clauses = {
 
250
                                                new QueryFromClause {
 
251
                                                        Identifier = "c",
 
252
                                                        Expression = new IdentifierExpression("customers")
 
253
                                                },
 
254
                                                new QueryOrderClause {
 
255
                                                        Orderings = {
 
256
                                                                new QueryOrdering {
 
257
                                                                        Expression = new IdentifierExpression("c").Member("Name"),
 
258
                                                                        Direction = QueryOrderingDirection.Descending
 
259
                                                                },
 
260
                                                                new QueryOrdering {
 
261
                                                                        Expression = new IdentifierExpression("c").Member("Address"),
 
262
                                                                        Direction = QueryOrderingDirection.Descending
 
263
                                                                }
 
264
                                                        }
 
265
                                                },
 
266
                                                new QuerySelectClause {
 
267
                                                        Expression = new IdentifierExpression("c")
 
268
                                                }
 
269
                                        }});
 
270
                }
 
271
 
 
272
                [Test]
 
273
                public void ExpressionWithOrderByWithTwoOrderByDecendings()
 
274
                {
 
275
                        ParseUtilCSharp.AssertExpression(
 
276
                                "from c in customers orderby c.Name descending orderby c.Address descending select c",
 
277
                                new QueryExpression {
 
278
                                        Clauses = {
 
279
                                                new QueryFromClause {
 
280
                                                        Identifier = "c",
 
281
                                                        Expression = new IdentifierExpression("customers")
 
282
                                                },
 
283
                                                new QueryOrderClause {
 
284
                                                        Orderings = {
 
285
                                                                new QueryOrdering {
 
286
                                                                        Expression = new IdentifierExpression("c").Member("Name"),
 
287
                                                                        Direction = QueryOrderingDirection.Descending
 
288
                                                                }
 
289
                                                        }
 
290
                                                },
 
291
                                                new QueryOrderClause {
 
292
                                                        Orderings = {
 
293
                                                                new QueryOrdering {
 
294
                                                                        Expression = new IdentifierExpression("c").Member("Address"),
 
295
                                                                        Direction = QueryOrderingDirection.Descending
 
296
                                                                }
 
297
                                                        }
 
298
                                                },
 
299
                                                new QuerySelectClause {
 
300
                                                        Expression = new IdentifierExpression("c")
 
301
                                                }
 
302
                                        }});
 
303
                }
 
304
 
 
305
                [Test]
 
306
                public void ExpressionWithOrderByAndLet()
 
307
                {
 
308
                        ParseUtilCSharp.AssertExpression(
 
309
                                "from c in customers orderby c.Name descending let x = c select x",
 
310
                                new QueryExpression {
 
311
                                        Clauses = {
 
312
                                                new QueryFromClause {
 
313
                                                        Identifier = "c",
 
314
                                                        Expression = new IdentifierExpression("customers")
 
315
                                                },
 
316
                                                new QueryOrderClause {
 
317
                                                        Orderings = {
 
318
                                                                new QueryOrdering {
 
319
                                                                        Expression = new IdentifierExpression("c").Member("Name"),
 
320
                                                                        Direction = QueryOrderingDirection.Descending
 
321
                                                                }
 
322
                                                        }
 
323
                                                },
 
324
                                                new QueryLetClause {
 
325
                                                        Identifier = "x",
 
326
                                                        Expression = new IdentifierExpression("c")
 
327
                                                },
 
328
                                                new QuerySelectClause {
 
329
                                                        Expression = new IdentifierExpression("x")
 
330
                                                }
 
331
                                        }});
 
332
                }
 
333
                
 
334
                [Test]
 
335
                public void QueryContinuation()
 
336
                {
 
337
                        ParseUtilCSharp.AssertExpression(
 
338
                                "from a in b select c into d select e",
 
339
                                new QueryExpression {
 
340
                                        Clauses = {
 
341
                                                new QueryContinuationClause {
 
342
                                                        PrecedingQuery = new QueryExpression {
 
343
                                                                Clauses = {
 
344
                                                                        new QueryFromClause {
 
345
                                                                                Identifier = "a",
 
346
                                                                                Expression = new IdentifierExpression("b")
 
347
                                                                        },
 
348
                                                                        new QuerySelectClause { Expression = new IdentifierExpression("c") }
 
349
                                                                }
 
350
                                                        },
 
351
                                                        Identifier = "d"
 
352
                                                },
 
353
                                                new QuerySelectClause { Expression = new IdentifierExpression("e") }
 
354
                                        }
 
355
                                }
 
356
                        );
 
357
                }
 
358
                
 
359
                
 
360
                [Test]
 
361
                public void QueryContinuationWithMultipleFrom()
 
362
                {
 
363
                        ParseUtilCSharp.AssertExpression(
 
364
                                "from a in b from c in d select e into f select g",
 
365
                                new QueryExpression {
 
366
                                        Clauses = {
 
367
                                                new QueryContinuationClause {
 
368
                                                        PrecedingQuery = new QueryExpression {
 
369
                                                                Clauses = {
 
370
                                                                        new QueryFromClause {
 
371
                                                                                Identifier = "a",
 
372
                                                                                Expression = new IdentifierExpression("b")
 
373
                                                                        },
 
374
                                                                        new QueryFromClause {
 
375
                                                                                Identifier = "c",
 
376
                                                                                Expression = new IdentifierExpression("d")
 
377
                                                                        },
 
378
                                                                        new QuerySelectClause { Expression = new IdentifierExpression("e") }
 
379
                                                                }
 
380
                                                        },
 
381
                                                        Identifier = "f"
 
382
                                                },
 
383
                                                new QuerySelectClause { Expression = new IdentifierExpression("g") }
 
384
                                        }
 
385
                                }
 
386
                        );
 
387
                }
 
388
                
 
389
                [Test]
 
390
                public void MultipleQueryContinuation()
 
391
                {
 
392
                        ParseUtilCSharp.AssertExpression(
 
393
                                "from a in b select c into d select e into f select g",
 
394
                                new QueryExpression {
 
395
                                        Clauses = {
 
396
                                                new QueryContinuationClause {
 
397
                                                        PrecedingQuery = new QueryExpression {
 
398
                                                                Clauses = {
 
399
                                                                        new QueryContinuationClause {
 
400
                                                                                PrecedingQuery = new QueryExpression {
 
401
                                                                                        Clauses = {
 
402
                                                                                                new QueryFromClause {
 
403
                                                                                                        Identifier = "a",
 
404
                                                                                                        Expression = new IdentifierExpression("b")
 
405
                                                                                                },
 
406
                                                                                                new QuerySelectClause { Expression = new IdentifierExpression("c") }
 
407
                                                                                        }
 
408
                                                                                },
 
409
                                                                                Identifier = "d"
 
410
                                                                        },
 
411
                                                                        new QuerySelectClause { Expression = new IdentifierExpression("e") }
 
412
                                                                }
 
413
                                                        },
 
414
                                                        Identifier = "f"
 
415
                                                },
 
416
                                                new QuerySelectClause { Expression = new IdentifierExpression("g") }
 
417
                                        }});
 
418
                }
 
419
                
 
420
                [Test]
 
421
                public void QueryWithGroupBy()
 
422
                {
 
423
                        ParseUtilCSharp.AssertExpression(
 
424
                                "from a in b group c by d",
 
425
                                new QueryExpression {
 
426
                                        Clauses = {
 
427
                                                new QueryFromClause {
 
428
                                                        Identifier = "a",
 
429
                                                        Expression = new IdentifierExpression("b")
 
430
                                                },
 
431
                                                new QueryGroupClause {
 
432
                                                        Projection = new IdentifierExpression("c"),
 
433
                                                        Key = new IdentifierExpression("d")
 
434
                                                }
 
435
                                        }});
 
436
                }
 
437
                
 
438
                [Test]
 
439
                public void QueryWithJoin()
 
440
                {
 
441
                        ParseUtilCSharp.AssertExpression(
 
442
                                "from a in b join c in d on e equals f select g",
 
443
                                new QueryExpression {
 
444
                                        Clauses = {
 
445
                                                new QueryFromClause {
 
446
                                                        Identifier = "a",
 
447
                                                        Expression = new IdentifierExpression("b")
 
448
                                                },
 
449
                                                new QueryJoinClause {
 
450
                                                        JoinIdentifier = "c",
 
451
                                                        InExpression = new IdentifierExpression("d"),
 
452
                                                        OnExpression = new IdentifierExpression("e"),
 
453
                                                        EqualsExpression = new IdentifierExpression("f")
 
454
                                                },
 
455
                                                new QuerySelectClause {
 
456
                                                        Expression = new IdentifierExpression("g")
 
457
                                                }
 
458
                                        }});
 
459
                }
 
460
                
 
461
                [Test]
 
462
                public void QueryWithGroupJoin()
 
463
                {
 
464
                        ParseUtilCSharp.AssertExpression(
 
465
                                "from a in b join c in d on e equals f into g select h",
 
466
                                new QueryExpression {
 
467
                                        Clauses = {
 
468
                                                new QueryFromClause {
 
469
                                                        Identifier = "a",
 
470
                                                        Expression = new IdentifierExpression("b")
 
471
                                                },
 
472
                                                new QueryJoinClause {
 
473
                                                        JoinIdentifier = "c",
 
474
                                                        InExpression = new IdentifierExpression("d"),
 
475
                                                        OnExpression = new IdentifierExpression("e"),
 
476
                                                        EqualsExpression = new IdentifierExpression("f"),
 
477
                                                        IntoIdentifier = "g"
 
478
                                                },
 
479
                                                new QuerySelectClause {
 
480
                                                        Expression = new IdentifierExpression("h")
 
481
                                                }
 
482
                                        }});
 
483
                }
 
484
                
 
485
                [Test]
 
486
                public void fSelect()
 
487
                {
 
488
                        ParseUtilCSharp.AssertExpression(
 
489
                                "from arg in args where float.Parse(arg) == 1.0fselect arg",
 
490
                                new QueryExpression {
 
491
                                        Clauses = {
 
492
                                                new QueryFromClause {
 
493
                                                        Identifier = "arg",
 
494
                                                        Expression = new IdentifierExpression("args")
 
495
                                                },
 
496
                                                new QueryWhereClause {
 
497
                                                        Condition = new BinaryOperatorExpression(
 
498
                                                                new PrimitiveType("float").Invoke("Parse", new IdentifierExpression("arg")),
 
499
                                                                BinaryOperatorType.Equality,
 
500
                                                                new PrimitiveExpression(1.0f)
 
501
                                                        )
 
502
                                                },
 
503
                                                new QuerySelectClause {
 
504
                                                        Expression = new IdentifierExpression("arg")
 
505
                                                }
 
506
                                        }});
 
507
                }
 
508
        }
 
509
}