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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/MethodDeclarationTests.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 ICSharpCode.NRefactory.TypeSystem;
 
22
using NUnit.Framework;
 
23
 
 
24
namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
 
25
{
 
26
        [TestFixture]
 
27
        public class MethodDeclarationTests
 
28
        {
 
29
                [Test]
 
30
                public void SimpleMethodDeclarationTest()
 
31
                {
 
32
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>("void MyMethod() {} ");
 
33
                        Assert.AreEqual("void", ((PrimitiveType)md.ReturnType).Keyword);
 
34
                        Assert.AreEqual(0, md.Parameters.Count());
 
35
                        Assert.IsFalse(md.IsExtensionMethod);
 
36
                }
 
37
                
 
38
                [Test]
 
39
                public void AbstractMethodDeclarationTest()
 
40
                {
 
41
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>("abstract void MyMethod();");
 
42
                        Assert.AreEqual("void", ((PrimitiveType)md.ReturnType).Keyword);
 
43
                        Assert.AreEqual(0, md.Parameters.Count());
 
44
                        Assert.IsFalse(md.IsExtensionMethod);
 
45
                        Assert.IsTrue(md.Body.IsNull);
 
46
                        Assert.AreEqual(Modifiers.Abstract, md.Modifiers);
 
47
                }
 
48
                
 
49
                [Test]
 
50
                public void DefiningPartialMethodDeclarationTest()
 
51
                {
 
52
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>("partial void MyMethod();");
 
53
                        Assert.AreEqual("void", ((PrimitiveType)md.ReturnType).Keyword);
 
54
                        Assert.AreEqual(0, md.Parameters.Count());
 
55
                        Assert.IsFalse(md.IsExtensionMethod);
 
56
                        Assert.IsTrue(md.Body.IsNull);
 
57
                        Assert.AreEqual(Modifiers.Partial, md.Modifiers);
 
58
                }
 
59
                
 
60
                [Test]
 
61
                public void ImplementingPartialMethodDeclarationTest()
 
62
                {
 
63
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>("partial void MyMethod() { }");
 
64
                        Assert.AreEqual("void", ((PrimitiveType)md.ReturnType).Keyword);
 
65
                        Assert.AreEqual(0, md.Parameters.Count());
 
66
                        Assert.IsFalse(md.IsExtensionMethod);
 
67
                        Assert.IsFalse(md.Body.IsNull);
 
68
                        Assert.AreEqual(Modifiers.Partial, md.Modifiers);
 
69
                }
 
70
                
 
71
                [Test]
 
72
                public void SimpleMethodRegionTest()
 
73
                {
 
74
                        const string program = @"
 
75
                void MyMethod()
 
76
                {
 
77
                        OtherMethod();
 
78
                }
 
79
";
 
80
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(program);
 
81
                        Assert.AreEqual(2, md.StartLocation.Line, "StartLocation.Y");
 
82
                        Assert.AreEqual(5, md.EndLocation.Line, "EndLocation.Y");
 
83
                        Assert.AreEqual(3, md.StartLocation.Column, "StartLocation.X");
 
84
                        Assert.AreEqual(4, md.EndLocation.Column, "EndLocation.X");
 
85
                }
 
86
                
 
87
                [Test]
 
88
                public void MethodWithModifiersRegionTest()
 
89
                {
 
90
                        const string program = @"
 
91
                public static void MyMethod()
 
92
                {
 
93
                        OtherMethod();
 
94
                }
 
95
";
 
96
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(program);
 
97
                        Assert.AreEqual(2, md.StartLocation.Line, "StartLocation.Y");
 
98
                        Assert.AreEqual(5, md.EndLocation.Line, "EndLocation.Y");
 
99
                        Assert.AreEqual(3, md.StartLocation.Column, "StartLocation.X");
 
100
                        Assert.AreEqual(4, md.EndLocation.Column, "EndLocation.X");
 
101
                }
 
102
                
 
103
                [Test]
 
104
                public void MethodWithUnnamedParameterDeclarationTest()
 
105
                {
 
106
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>("void MyMethod(int) {} ", true);
 
107
                        Assert.AreEqual("void", md.ReturnType.ToString ());
 
108
                        Assert.AreEqual(1, md.Parameters.Count());
 
109
                        Assert.AreEqual("int", ((PrimitiveType)md.Parameters.Single().Type).Keyword);
 
110
                }
 
111
                
 
112
                [Test]
 
113
                public void GenericVoidMethodDeclarationTest()
 
114
                {
 
115
                        ParseUtilCSharp.AssertTypeMember(
 
116
                                "void MyMethod<T>(T a) {} ",
 
117
                                new MethodDeclaration {
 
118
                                        ReturnType = new PrimitiveType("void"),
 
119
                                        Name = "MyMethod",
 
120
                                        TypeParameters = { new TypeParameterDeclaration { Name = "T" } },
 
121
                                        Parameters = { new ParameterDeclaration(new SimpleType("T"), "a") },
 
122
                                        Body = new BlockStatement()
 
123
                                });
 
124
                }
 
125
                
 
126
                [Test]
 
127
                public void GenericMethodDeclarationTest()
 
128
                {
 
129
                        ParseUtilCSharp.AssertTypeMember(
 
130
                                "T MyMethod<T>(T a) {} ",
 
131
                                new MethodDeclaration {
 
132
                                        ReturnType = new SimpleType("T"),
 
133
                                        Name = "MyMethod",
 
134
                                        TypeParameters = { new TypeParameterDeclaration { Name = "T" } },
 
135
                                        Parameters = { new ParameterDeclaration(new SimpleType("T"), "a") },
 
136
                                        Body = new BlockStatement()
 
137
                                });
 
138
                }
 
139
                
 
140
                [Test]
 
141
                public void GenericMethodDeclarationWithConstraintTest()
 
142
                {
 
143
                        ParseUtilCSharp.AssertTypeMember(
 
144
                                "T MyMethod<T>(T a) where T : ISomeInterface {} ",
 
145
                                new MethodDeclaration {
 
146
                                        ReturnType = new SimpleType("T"),
 
147
                                        Name = "MyMethod",
 
148
                                        TypeParameters = { new TypeParameterDeclaration { Name = "T" } },
 
149
                                        Parameters = { new ParameterDeclaration(new SimpleType("T"), "a") },
 
150
                                        Constraints = {
 
151
                                                new Constraint {
 
152
                                                        TypeParameter = new SimpleType ("T"),
 
153
                                                        BaseTypes = { new SimpleType("ISomeInterface") }
 
154
                                                }
 
155
                                        },
 
156
                                        Body = new BlockStatement()
 
157
                                });
 
158
                }
 
159
                
 
160
                
 
161
                [Test]
 
162
                public void GenericMethodWithTwoConstraints()
 
163
                {
 
164
                        ParseUtilCSharp.AssertTypeMember(
 
165
                                "void MyMethod<A, B>() where A : IA where B : IB {} ",
 
166
                                new MethodDeclaration {
 
167
                                        ReturnType = new PrimitiveType("void"),
 
168
                                        Name = "MyMethod",
 
169
                                        TypeParameters = {
 
170
                                                new TypeParameterDeclaration { Name = "A" },
 
171
                                                new TypeParameterDeclaration { Name = "B" }
 
172
                                        },
 
173
                                        Constraints = {
 
174
                                                new Constraint {
 
175
                                                        TypeParameter = new SimpleType("A"),
 
176
                                                        BaseTypes = { new SimpleType("IA") }
 
177
                                                },
 
178
                                                new Constraint {
 
179
                                                        TypeParameter = new SimpleType("B"),
 
180
                                                        BaseTypes = { new SimpleType("IB") }
 
181
                                                }
 
182
                                        },
 
183
                                        Body = new BlockStatement()
 
184
                                });
 
185
                }
 
186
                
 
187
                [Test]
 
188
                public void GenericMethodDeclarationWithThreeConstraintsTest()
 
189
                {
 
190
                        ParseUtilCSharp.AssertTypeMember(
 
191
                                "A MyMethod<A, B, C>() where B : BB where A : BA, IA where C : class {} ",
 
192
                                new MethodDeclaration {
 
193
                                        ReturnType = new SimpleType("A"),
 
194
                                        Name = "MyMethod",
 
195
                                        TypeParameters = {
 
196
                                                new TypeParameterDeclaration { Name = "A" },
 
197
                                                new TypeParameterDeclaration { Name = "B" },
 
198
                                                new TypeParameterDeclaration { Name = "C" }
 
199
                                        },
 
200
                                        Constraints = {
 
201
                                                new Constraint {
 
202
                                                        TypeParameter = new SimpleType ("B"),
 
203
                                                        BaseTypes = { new SimpleType("BB") }
 
204
                                                },
 
205
                                                new Constraint {
 
206
                                                        TypeParameter = new SimpleType ("A"),
 
207
                                                        BaseTypes = { new SimpleType("BA"), new SimpleType("IA") }
 
208
                                                },
 
209
                                                new Constraint {
 
210
                                                        TypeParameter = new SimpleType ("C"),
 
211
                                                        BaseTypes = { new PrimitiveType("class") }
 
212
                                                }
 
213
                                        },
 
214
                                        Body = new BlockStatement()
 
215
                                });
 
216
                }
 
217
                
 
218
                [Test]
 
219
                public void GenericMethodInInterface()
 
220
                {
 
221
                        ParseUtilCSharp.AssertGlobal(
 
222
                                @"interface MyInterface {
 
223
        T MyMethod<T>(T a) where T : ISomeInterface;
 
224
}
 
225
",
 
226
                                new TypeDeclaration {
 
227
                                        ClassType = ClassType.Interface,
 
228
                                        Name = "MyInterface",
 
229
                                        Members = {
 
230
                                                new MethodDeclaration {
 
231
                                                        ReturnType = new SimpleType("T"),
 
232
                                                        Name = "MyMethod",
 
233
                                                        TypeParameters = { new TypeParameterDeclaration { Name = "T" } },
 
234
                                                        Parameters = { new ParameterDeclaration(new SimpleType("T"), "a") },
 
235
                                                        Constraints = {
 
236
                                                                new Constraint {
 
237
                                                                        TypeParameter = new SimpleType ("T"),
 
238
                                                                        BaseTypes = { new SimpleType("ISomeInterface") }
 
239
                                                                }
 
240
                                                        }
 
241
                                                }}});
 
242
                }
 
243
                
 
244
                [Test]
 
245
                public void GenericVoidMethodInInterface()
 
246
                {
 
247
                        ParseUtilCSharp.AssertGlobal(
 
248
                                @"interface MyInterface {
 
249
        void MyMethod<T>(T a) where T : ISomeInterface;
 
250
}
 
251
",
 
252
                                new TypeDeclaration {
 
253
                                        ClassType = ClassType.Interface,
 
254
                                        Name = "MyInterface",
 
255
                                        Members = {
 
256
                                                new MethodDeclaration {
 
257
                                                        ReturnType = new PrimitiveType("void"),
 
258
                                                        Name = "MyMethod",
 
259
                                                        TypeParameters = { new TypeParameterDeclaration { Name = "T" } },
 
260
                                                        Parameters = { new ParameterDeclaration(new SimpleType("T"), "a") },
 
261
                                                        Constraints = {
 
262
                                                                new Constraint {
 
263
                                                                        TypeParameter = new SimpleType ("T"),
 
264
                                                                        BaseTypes = { new SimpleType("ISomeInterface") }
 
265
                                                                }
 
266
                                                        }
 
267
                                                }}});
 
268
                }
 
269
                
 
270
                [Test]
 
271
                public void ShadowingMethodInInterface ()
 
272
                {
 
273
                        ParseUtilCSharp.AssertGlobal (
 
274
                                @"interface MyInterface : IDisposable {
 
275
        new void Dispose();
 
276
}
 
277
",
 
278
                                new TypeDeclaration {
 
279
                                        ClassType = ClassType.Interface,
 
280
                                        Name = "MyInterface",
 
281
                                        BaseTypes = { new SimpleType("IDisposable") },
 
282
                                        Members = {
 
283
                                                new MethodDeclaration {
 
284
                                                        Modifiers = Modifiers.New,
 
285
                                                        ReturnType = new PrimitiveType("void"),
 
286
                                                        Name = "Dispose"
 
287
                                                }}});
 
288
                }
 
289
                
 
290
                [Test]
 
291
                public void MethodImplementingInterfaceTest()
 
292
                {
 
293
                        ParseUtilCSharp.AssertTypeMember(
 
294
                                "int MyInterface.MyMethod() {} ",
 
295
                                new MethodDeclaration {
 
296
                                        ReturnType = new PrimitiveType("int"),
 
297
                                        PrivateImplementationType = new SimpleType("MyInterface"),
 
298
                                        Name = "MyMethod",
 
299
                                        Body = new BlockStatement()
 
300
                                });
 
301
                }
 
302
                
 
303
                [Test]
 
304
                public void MethodImplementingGenericInterfaceTest()
 
305
                {
 
306
                        ParseUtilCSharp.AssertTypeMember(
 
307
                                "int MyInterface<string>.MyMethod() {} ",
 
308
                                new MethodDeclaration {
 
309
                                        ReturnType = new PrimitiveType("int"),
 
310
                                        PrivateImplementationType = new SimpleType("MyInterface") { TypeArguments = { new PrimitiveType("string") } },
 
311
                                        Name = "MyMethod",
 
312
                                        Body = new BlockStatement()
 
313
                                });
 
314
                }
 
315
                
 
316
                [Test]
 
317
                public void VoidMethodImplementingInterfaceTest()
 
318
                {
 
319
                        ParseUtilCSharp.AssertTypeMember (
 
320
                                "void MyInterface.MyMethod() {} ",
 
321
                                new MethodDeclaration {
 
322
                                        ReturnType = new PrimitiveType("void"),
 
323
                                        PrivateImplementationType = new SimpleType("MyInterface"),
 
324
                                        Name = "MyMethod",
 
325
                                        Body = new BlockStatement()
 
326
                                });
 
327
                }
 
328
                
 
329
                [Test]
 
330
                public void VoidMethodImplementingGenericInterfaceTest()
 
331
                {
 
332
                        ParseUtilCSharp.AssertTypeMember (
 
333
                                "void MyInterface<string>.MyMethod() {} ",
 
334
                                new MethodDeclaration {
 
335
                                        ReturnType = new PrimitiveType("void"),
 
336
                                        PrivateImplementationType = new SimpleType("MyInterface") { TypeArguments = { new PrimitiveType("string") } },
 
337
                                        Name = "MyMethod",
 
338
                                        Body = new BlockStatement()
 
339
                                });
 
340
                }
 
341
                
 
342
                [Test]
 
343
                public void IncompleteConstraintsTest()
 
344
                {
 
345
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(
 
346
                                "void a<T>() where T { }", true // expect errors
 
347
                        );
 
348
                        Assert.AreEqual("a", md.Name);
 
349
                        Assert.AreEqual(1, md.TypeParameters.Count);
 
350
                        Assert.AreEqual("T", md.TypeParameters.Single().Name);
 
351
                        Assert.AreEqual(1, md.Constraints.Count());
 
352
                        Assert.AreEqual(0, md.Constraints.First ().BaseTypes.Count());
 
353
                        
 
354
                }
 
355
                
 
356
                [Test]
 
357
                public void ExtensionMethodTest()
 
358
                {
 
359
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(
 
360
                                "public static int ToInt32(this string s) { return int.Parse(s); }"
 
361
                        );
 
362
                        Assert.AreEqual("ToInt32", md.Name);
 
363
                        Assert.AreEqual("s", md.Parameters.First().Name);
 
364
                        Assert.AreEqual(ParameterModifier.This, md.Parameters.First().ParameterModifier);
 
365
                        Assert.AreEqual("string", ((PrimitiveType)md.Parameters.First().Type).Keyword);
 
366
                        Assert.IsTrue(md.IsExtensionMethod);
 
367
                }
 
368
                
 
369
                [Test]
 
370
                public void ExtensionMethodWithAttributeTest()
 
371
                {
 
372
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(
 
373
                                "public static int ToInt32([Attr] this string s) { return int.Parse(s); }"
 
374
                        );
 
375
                        Assert.AreEqual("ToInt32", md.Name);
 
376
                        Assert.IsTrue(md.IsExtensionMethod);
 
377
                        Assert.AreEqual("s", md.Parameters.Single().Name);
 
378
                        Assert.AreEqual(KnownTypeCode.String, ((PrimitiveType)md.Parameters.Single().Type).KnownTypeCode);
 
379
                        Assert.AreEqual(1, md.Parameters.Single().Attributes.Count);
 
380
                }
 
381
                
 
382
                [Test]
 
383
                public void VoidExtensionMethodTest()
 
384
                {
 
385
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(
 
386
                                "public static void Print(this string s) { Console.WriteLine(s); }"
 
387
                        );
 
388
                        Assert.AreEqual("Print", md.Name);
 
389
                        Assert.AreEqual("s", md.Parameters.First().Name);
 
390
                        Assert.AreEqual(ParameterModifier.This, md.Parameters.First().ParameterModifier);
 
391
                        Assert.AreEqual("string", ((PrimitiveType)md.Parameters.First().Type).Keyword);
 
392
                        Assert.IsTrue(md.IsExtensionMethod);
 
393
                }
 
394
                
 
395
                [Test]
 
396
                public void MethodWithEmptyAssignmentErrorInBody()
 
397
                {
 
398
                        MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(
 
399
                                "void A ()\n" +
 
400
                                "{\n" +
 
401
                                "int a = 3;\n" +
 
402
                                " = 4;\n" +
 
403
                                "}", true // expect errors
 
404
                        );
 
405
                        Assert.AreEqual("A", md.Name);
 
406
                        Assert.AreEqual(new TextLocation(2, 1), md.Body.StartLocation);
 
407
                        Assert.AreEqual(new TextLocation(5, 2), md.Body.EndLocation);
 
408
                }
 
409
                
 
410
                [Test]
 
411
                public void OptionalParameterTest()
 
412
                {
 
413
                        ParseUtilCSharp.AssertTypeMember(
 
414
                                "public void Foo(string bar = null, int baz = 0) { }",
 
415
                                new MethodDeclaration {
 
416
                                        Modifiers = Modifiers.Public,
 
417
                                        ReturnType = new PrimitiveType("void"),
 
418
                                        Name = "Foo",
 
419
                                        Body = new BlockStatement(),
 
420
                                        Parameters = {
 
421
                                                new ParameterDeclaration {
 
422
                                                        Type = new PrimitiveType("string"),
 
423
                                                        Name = "bar",
 
424
                                                        DefaultExpression = new NullReferenceExpression()
 
425
                                                },
 
426
                                                new ParameterDeclaration {
 
427
                                                        Type = new PrimitiveType("int"),
 
428
                                                        Name = "baz",
 
429
                                                        DefaultExpression = new PrimitiveExpression(0)
 
430
                                                }
 
431
                                        }});
 
432
                }
 
433
                
 
434
                [Test]
 
435
                public void AsyncMethod()
 
436
                {
 
437
                        ParseUtilCSharp.AssertTypeMember(
 
438
                                "async void MyMethod() {}",
 
439
                                new MethodDeclaration {
 
440
                                        Modifiers = Modifiers.Async,
 
441
                                        ReturnType = new PrimitiveType("void"),
 
442
                                        Name = "MyMethod",
 
443
                                        Body = new BlockStatement()
 
444
                                });
 
445
                }
 
446
                
 
447
                [Test, Ignore("parser bug, reported upstream.")]
 
448
                public void AsyncAsyncAsync()
 
449
                {
 
450
                        ParseUtilCSharp.AssertTypeMember(
 
451
                                "async async async(async async) {}",
 
452
                                new MethodDeclaration {
 
453
                                        Modifiers = Modifiers.Async,
 
454
                                        ReturnType = new SimpleType("async"),
 
455
                                        Name = "async",
 
456
                                        Body = new BlockStatement(),
 
457
                                        Parameters = {
 
458
                                                new ParameterDeclaration(new SimpleType("async"), "async")
 
459
                                        }});
 
460
                }
 
461
        }
 
462
}