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

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using NUnit.Framework;

namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
{
	[TestFixture]
	public class TypeDeclarationTests
	{
		[Test]
		public void SimpleClassTypeDeclarationTest()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("class MyClass  : My.Base.Class  { }");
			
			Assert.AreEqual(ClassType.Class, td.ClassType);
			Assert.AreEqual("MyClass", td.Name);
			Assert.AreEqual("My.Base.Class", td.BaseTypes.First ().ToString ());
			Assert.AreEqual(Modifiers.None, td.Modifiers);
		}
		
		[Test]
		public void SimpleClassRegionTest()
		{
			const string program = "class MyClass\n{\n}\n";
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
			Assert.AreEqual(1, td.StartLocation.Line, "StartLocation.Y");
			Assert.AreEqual(1, td.StartLocation.Column, "StartLocation.X");
			TextLocation bodyStartLocation = td.NameToken.EndLocation;
			Assert.AreEqual(1, bodyStartLocation.Line, "BodyStartLocation.Y");
			Assert.AreEqual(14, bodyStartLocation.Column, "BodyStartLocation.X");
			Assert.AreEqual(3, td.EndLocation.Line, "EndLocation.Y");
			Assert.AreEqual(2, td.EndLocation.Column, "EndLocation.Y");
		}
		
		[Test]
		public void SimplePartialClassTypeDeclarationTest()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("partial class MyClass { }");
			Assert.IsFalse(td.IsNull);
			Assert.AreEqual(ClassType.Class, td.ClassType);
			Assert.AreEqual("MyClass", td.Name);
			Assert.AreEqual(Modifiers.Partial, td.Modifiers);
		}
		
		[Test]
		public void NestedClassesTest()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("class MyClass { partial class P1 {} public partial class P2 {} static class P3 {} internal static class P4 {} }");
			Assert.IsFalse(td.IsNull);
			Assert.AreEqual(ClassType.Class, td.ClassType);
			Assert.AreEqual("MyClass", td.Name);
			Assert.AreEqual(Modifiers.Partial, ((TypeDeclaration)td.Members.ElementAt(0)).Modifiers);
			Assert.AreEqual(Modifiers.Partial | Modifiers.Public, ((TypeDeclaration)td.Members.ElementAt(1)).Modifiers);
			Assert.AreEqual(Modifiers.Static, ((TypeDeclaration)td.Members.ElementAt(2)).Modifiers);
			Assert.AreEqual(Modifiers.Static | Modifiers.Internal, ((TypeDeclaration)td.Members.ElementAt(3)).Modifiers);
		}
		
		[Test]
		public void SimpleStaticClassTypeDeclarationTest()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("static class MyClass { }");
			Assert.IsFalse(td.IsNull);
			Assert.AreEqual(ClassType.Class, td.ClassType);
			Assert.AreEqual("MyClass", td.Name);
			Assert.AreEqual(Modifiers.Static, td.Modifiers);
		}
		
		[Test]
		public void GenericClassTypeDeclarationTest()
		{
			ParseUtilCSharp.AssertGlobal(
				"public class G<T> {}",
				new TypeDeclaration {
					ClassType = ClassType.Class,
					Modifiers = Modifiers.Public,
					Name = "G",
					TypeParameters = { new TypeParameterDeclaration { Name = "T" } }
				});
		}
		
		[Test]
		public void GenericClassWithWhere()
		{
			ParseUtilCSharp.AssertGlobal(
				@"public class Test<T> where T : IMyInterface { }",
				new TypeDeclaration {
					ClassType = ClassType.Class,
					Modifiers = Modifiers.Public,
					Name = "Test",
					TypeParameters = { new TypeParameterDeclaration { Name = "T" } },
					Constraints = {
						new Constraint {
							TypeParameter = new SimpleType ("T"),
							BaseTypes = { new SimpleType("IMyInterface") }
						}
					}});
		}
		
		[Test]
		public void ComplexGenericInterfaceTypeDeclarationTest()
		{
			ParseUtilCSharp.AssertGlobal(
				"public interface Generic<in T, out S> : System.IComparable where S : G<T[]>, new() where  T : MyNamespace.IMyInterface {}",
				new TypeDeclaration {
					ClassType = ClassType.Interface,
					Modifiers = Modifiers.Public,
					Name = "Generic",
					TypeParameters = {
						new TypeParameterDeclaration { Variance = VarianceModifier.Contravariant, Name = "T" },
						new TypeParameterDeclaration { Variance = VarianceModifier.Covariant, Name = "S" }
					},
					BaseTypes = {
						new MemberType {
							Target = new SimpleType("System"),
							MemberName = "IComparable"
						}
					},
					Constraints = {
						new Constraint {
							TypeParameter = new SimpleType ("S"),
							BaseTypes = {
								new SimpleType {
									Identifier = "G",
									TypeArguments = { new SimpleType("T").MakeArrayType() }
								},
								new PrimitiveType("new")
							}
						},
						new Constraint {
							TypeParameter = new SimpleType ("T"),
							BaseTypes = {
								new MemberType {
									Target = new SimpleType("MyNamespace"),
									MemberName = "IMyInterface"
								}
							}
						}
					}
				});
		}
		
		[Test]
		public void ComplexClassTypeDeclarationTest()
		{
			ParseUtilCSharp.AssertGlobal(
				@"
[MyAttr()]
public abstract class MyClass : MyBase, Interface1, My.Test.Interface2
{
}",
				new TypeDeclaration {
					ClassType = ClassType.Class,
					Attributes = {
						new AttributeSection {
							Attributes = {
								new Attribute { Type = new SimpleType("MyAttr") }
							}
						}
					},
					Modifiers = Modifiers.Public | Modifiers.Abstract,
					Name = "MyClass",
					BaseTypes = {
						new SimpleType("MyBase"),
						new SimpleType("Interface1"),
						new MemberType {
							Target = new MemberType {
								Target = new SimpleType("My"),
								MemberName = "Test"
							},
							MemberName = "Interface2"
						}
					}});
		}
		
		[Test]
		public void SimpleStructTypeDeclarationTest()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("struct MyStruct {}");
			
			Assert.AreEqual(ClassType.Struct, td.ClassType);
			Assert.AreEqual("MyStruct", td.Name);
		}
		
		[Test]
		public void SimpleInterfaceTypeDeclarationTest()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("interface MyInterface {}");
			
			Assert.AreEqual(ClassType.Interface, td.ClassType);
			Assert.AreEqual("MyInterface", td.Name);
		}
		
		[Test]
		public void SimpleEnumTypeDeclarationTest()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("enum MyEnum {}");
			
			Assert.AreEqual(ClassType.Enum, td.ClassType);
			Assert.AreEqual("MyEnum", td.Name);
		}
		
		[Test, Ignore("Mono parser bug?")]
		public void ContextSensitiveKeywordTest()
		{
			ParseUtilCSharp.AssertGlobal(
				"partial class partial<[partial: where] where> where where : partial<where> { }",
				new TypeDeclaration {
					ClassType = ClassType.Class,
					Modifiers = Modifiers.Partial,
					Name = "partial",
					TypeParameters = {
						new TypeParameterDeclaration {
							Attributes = {
								new AttributeSection {
									AttributeTarget = "partial",
									Attributes = { new Attribute { Type = new SimpleType("where") } }
								}
							},
							Name = "where"
						}
					},
					Constraints = {
						new Constraint {
							TypeParameter = new SimpleType ("where"),
							BaseTypes = {
								new SimpleType {
									Identifier = "partial",
									TypeArguments = { new SimpleType("where") }
								}
							}
						}
					}});
		}
		
		[Test]
		public void TypeInNamespaceTest()
		{
			NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal<NamespaceDeclaration>("namespace N { class MyClass { } }");
			
			Assert.AreEqual("N", ns.Name);
			Assert.AreEqual("MyClass", ((TypeDeclaration)ns.Members.Single()).Name);
		}
		
		[Test]
		public void StructInNamespaceTest()
		{
			NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal<NamespaceDeclaration>("namespace N { struct MyClass { } }");
			
			Assert.AreEqual("N", ns.Name);
			Assert.AreEqual("MyClass", ((TypeDeclaration)ns.Members.Single()).Name);
		}
		
		[Test]
		public void EnumInNamespaceTest()
		{
			NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal<NamespaceDeclaration>("namespace N { enum MyClass { } }");
			
			Assert.AreEqual("N", ns.Name);
			Assert.AreEqual("MyClass", ((TypeDeclaration)ns.Members.Single()).Name);
		}
		
		[Test]
		public void InterfaceInNamespaceTest()
		{
			NamespaceDeclaration ns = ParseUtilCSharp.ParseGlobal<NamespaceDeclaration>("namespace N { interface MyClass { } }");
			
			Assert.AreEqual("N", ns.Name);
			Assert.AreEqual("MyClass", ((TypeDeclaration)ns.Members.Single()).Name);
		}
		
		[Test]
		public void EnumWithInitializer()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("enum MyEnum { Val1 = 10 }");
			EnumMemberDeclaration member = (EnumMemberDeclaration)td.Members.Single();
			Assert.AreEqual("Val1", member.Name);
			Assert.AreEqual(10, ((PrimitiveExpression)member.Initializer).Value);
		}
		
		[Test]
		public void EnumWithBaseType()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("enum MyEnum : short { }");
			Assert.AreEqual("MyEnum", td.Name);
			Assert.AreEqual("short", ((PrimitiveType)td.BaseTypes.Single()).Keyword);
		}
		
		[Test, Ignore("Mono parser crash")]
		public void EnumWithIncorrectNewlineAfterIntegerLiteral ()
		{
			ParseUtilCSharp.AssertGlobal (
				"enum DisplayFlags { D = 4\r\r\n}",
				new TypeDeclaration {
					ClassType = ClassType.Enum,
					Name = "DisplayFlags",
					Members = {
						new EnumMemberDeclaration {
							Name = "D",
							Initializer = new PrimitiveExpression(4)
						}
					}});
		}
		
		[Test]
		public void EnumWithCommaAtEnd()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("enum MyEnum { A, }");
			Assert.AreEqual(
				new Role[] {
					Roles.EnumKeyword,
					Roles.Identifier,
					Roles.LBrace,
					Roles.TypeMemberRole,
					Roles.Comma,
					Roles.RBrace
				}, td.Children.Select(c => c.Role).ToArray());
		}
		
		[Test]
		public void EnumWithCommaAndSemicolonAtEnd()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("enum MyEnum { A, };");
			Assert.AreEqual(
				new Role[] {
					Roles.EnumKeyword,
					Roles.Identifier,
					Roles.LBrace,
					Roles.TypeMemberRole,
					Roles.Comma,
					Roles.RBrace,
					Roles.Semicolon
				}, td.Children.Select(c => c.Role).ToArray());
		}
		
		[Test, Ignore("Parser bug (incorrectly creates a comma at the end of the enum)")]
		public void EnumWithSemicolonAtEnd()
		{
			TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("enum MyEnum { A };");
			Assert.AreEqual(
				new Role[] {
					Roles.EnumKeyword,
					Roles.Identifier,
					Roles.LBrace,
					Roles.TypeMemberRole,
					Roles.RBrace,
					Roles.Semicolon
				}, td.Children.Select(c => c.Role).ToArray());
		}
	}
}