~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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// 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.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.CSharp.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem;
using Microsoft.CSharp;
using NUnit.Framework;

namespace ICSharpCode.NRefactory.CSharp
{
	[TestFixture]
	public class CodeDomConvertVisitorTests : ResolverTestBase
	{
		CodeDomConvertVisitor convertVisitor;
		CSharpUnresolvedFile unresolvedFile;
		
		public override void SetUp()
		{
			base.SetUp();
			unresolvedFile = new CSharpUnresolvedFile("test.cs");
			unresolvedFile.RootUsingScope.Usings.Add(MakeReference("System"));
			unresolvedFile.RootUsingScope.Usings.Add(MakeReference("System.Collections.Generic"));
			unresolvedFile.RootUsingScope.Usings.Add(MakeReference("System.Linq"));
			
			convertVisitor = new CodeDomConvertVisitor();
			convertVisitor.AllowSnippetNodes = false;
			convertVisitor.UseFullyQualifiedTypeNames = true;
		}
		
		#region Helper methods
		CodeObject ConvertInContext(string program)
		{
			var p = PrepareResolver(program);
			return convertVisitor.Convert(p.Item2, p.Item1);
		}
		
		string ConvertHelper(AstNode node, Action<CSharpCodeProvider, CodeObject, TextWriter, CodeGeneratorOptions> action)
		{
			CSharpResolver resolver = new CSharpResolver(compilation);
			resolver = resolver.WithCurrentUsingScope(unresolvedFile.RootUsingScope.Resolve(compilation));
			resolver = resolver.WithCurrentTypeDefinition(compilation.FindType(KnownTypeCode.Object).GetDefinition());
			var codeObj = convertVisitor.Convert(node, new CSharpAstResolver(resolver, node));
			
			StringWriter writer = new StringWriter();
			writer.NewLine = " ";
			action(new CSharpCodeProvider(), codeObj, writer, new CodeGeneratorOptions { IndentString = " " });
			return Regex.Replace(writer.ToString(), @"\s+", " ").Trim();
		}
		
		string ConvertExpression(Expression expr)
		{
			return ConvertHelper(expr, (p,obj,w,opt) => p.GenerateCodeFromExpression((CodeExpression)obj, w, opt));
		}
		
		string ConvertExpression(string code)
		{
			CSharpParser parser = new CSharpParser();
			var expr = parser.ParseExpression(code);
			Assert.IsFalse(parser.HasErrors);
			return ConvertExpression(expr);
		}
		
		string ConvertStatement(Statement statement)
		{
			return ConvertHelper(statement, (p,obj,w,opt) => p.GenerateCodeFromStatement((CodeStatement)obj, w, opt));
		}
		
		string ConvertStatement(string code)
		{
			CSharpParser parser = new CSharpParser();
			var expr = parser.ParseStatements(code).Single();
			Assert.IsFalse(parser.HasErrors);
			return ConvertStatement(expr);
		}
		
		string ConvertMember(EntityDeclaration entity)
		{
			return ConvertHelper(entity, (p,obj,w,opt) => p.GenerateCodeFromMember((CodeTypeMember)obj, w, opt));
		}
		
		string ConvertMember(string code)
		{
			CSharpParser parser = new CSharpParser();
			var expr = parser.ParseTypeMembers(code).Single();
			Assert.IsFalse(parser.HasErrors);
			return ConvertMember(expr);
		}
		
		string ConvertTypeDeclaration(EntityDeclaration decl)
		{
			return ConvertHelper(decl, (p,obj,w,opt) => p.GenerateCodeFromType((CodeTypeDeclaration)obj, w, opt));
		}
		
		string ConvertTypeDeclaration(string code)
		{
			CSharpParser parser = new CSharpParser();
			var syntaxTree = parser.Parse(code, "program.cs");
			Assert.IsFalse(parser.HasErrors);
			return ConvertTypeDeclaration((EntityDeclaration)syntaxTree.Children.Single());
		}
		
		string ConvertSyntaxTree(SyntaxTree syntaxTree)
		{
			return ConvertHelper(syntaxTree, (p,obj,w,opt) => p.GenerateCodeFromCompileUnit((CodeCompileUnit)obj, w, opt));
		}
		
		string ConvertSyntaxTree(string code)
		{
			CSharpParser parser = new CSharpParser();
			var syntaxTree = parser.Parse(code, "program.cs");
			Assert.IsFalse(parser.HasErrors);
			var result = ConvertSyntaxTree(syntaxTree);
			var idx = result.IndexOf("namespace", StringComparison.Ordinal);
			if (idx > 0)
				result = result.Substring (idx);
			return result;
		}
		#endregion
		
		#region Type References
		[Test]
		public void MultidimensionalArrayTypeReference()
		{
			Assert.AreEqual("default(int[,][])", ConvertExpression("default(int[,][])"));
		}
		
		[Test]
		public void NestedTypeInGenericType()
		{
			Assert.AreEqual("default(System.Collections.Generic.List<string>.Enumerator)",
			                ConvertExpression("default(List<string>.Enumerator)"));
			convertVisitor.UseFullyQualifiedTypeNames = false;
			Assert.AreEqual("default(List<string>.Enumerator)",
			                ConvertExpression("default(List<string>.Enumerator)"));
		}
		#endregion
		
		#region Arrays
		[Test]
		public void CreateArray()
		{
			Assert.AreEqual("new int[10]", ConvertExpression("new int[10]"));
		}
		
		[Test, ExpectedException(typeof(NotSupportedException))]
		public void CreateJaggedArray()
		{
			ConvertExpression("new int[10][]");
		}
		
		[Test, ExpectedException(typeof(NotSupportedException))]
		public void Create2DArray()
		{
			ConvertExpression("new int[10, 20]");
		}
		
		[Test]
		public void CreateImplicitlyTypedArray()
		{
			// implicitly-typed array not supported in CodeDom, so the conversion should infer the type
			Assert.AreEqual("new int[] { 1, 2, 3}", ConvertExpression("new [] { 1, 2, 3 }"));
			Assert.AreEqual("new System.Collections.Generic.List<string>[] { new System.Collections.Generic.List<string>()}",
			                ConvertExpression("new [] { new List<string>() }"));
		}
		
		[Test, ExpectedException(typeof(NotSupportedException))]
		public void Create2DImplicitlyTypedArray()
		{
			ConvertExpression("new [,] { { 1, 2 }, { 3, 4 }}");
		}
		#endregion
		
		#region Operators
		[Test]
		public void ArithmeticOperators()
		{
			Assert.AreEqual("(0 + 1)", ConvertExpression("0 + 1"));
			Assert.AreEqual("(0 - 1)", ConvertExpression("0 - 1"));
			Assert.AreEqual("(0 * 1)", ConvertExpression("0 * 1"));
			Assert.AreEqual("(0 / 1)", ConvertExpression("0 / 1"));
			Assert.AreEqual("(0 % 1)", ConvertExpression("0 % 1"));
			Assert.AreEqual("(0 & 1)", ConvertExpression("0 & 1"));
			Assert.AreEqual("(0 | 1)", ConvertExpression("0 | 1"));
			Assert.AreEqual("(0 < 1)", ConvertExpression("0 < 1"));
			Assert.AreEqual("(0 > 1)", ConvertExpression("0 > 1"));
			Assert.AreEqual("(0 <= 1)", ConvertExpression("0 <= 1"));
			Assert.AreEqual("(0 >= 1)", ConvertExpression("0 >= 1"));
			Assert.AreEqual("(true && false)", ConvertExpression("true && false"));
			Assert.AreEqual("(true || false)", ConvertExpression("true || false"));
		}
		
		[Test]
		public void EqualityOperator()
		{
			Assert.AreEqual("(0 == 1)", ConvertExpression("0 == 1"));
			Assert.AreEqual("(default(object) == null)", ConvertExpression("default(object) == null"));
		}
		
		[Test]
		public void InEqualityOperator()
		{
			Assert.AreEqual("((0 == 1) == false)", ConvertExpression("0 != 1"));
			Assert.AreEqual("(default(object) != null)", ConvertExpression("default(object) != null"));
		}
		
		[Test]
		public void UnaryOperators()
		{
			Assert.AreEqual("(a == false)", ConvertExpression("!a"));
			Assert.AreEqual("(0 - a)", ConvertExpression("-a"));
			Assert.AreEqual("a", ConvertExpression("+a"));
		}
		
		[Test]
		public void Cast()
		{
			Assert.AreEqual("((double)(0))", ConvertExpression("(double)0"));
		}
		#endregion
		
		#region Member Access
		[Test]
		public void StaticProperty()
		{
			Assert.AreEqual("System.Environment.TickCount", ConvertExpression("Environment.TickCount"));
		}
		
		[Test]
		public void FullyQualifiedEnumFieldAccess()
		{
			string program = "class A { object x = $System.StringComparison.Ordinal$; }";
			var cfre = (CodeFieldReferenceExpression)ConvertInContext(program);
			Assert.AreEqual("Ordinal", cfre.FieldName);
			var ctre = ((CodeTypeReferenceExpression)cfre.TargetObject);
			Assert.AreEqual("System.StringComparison", ctre.Type.BaseType);
		}
		
		[Test]
		public void EnumFieldAccess()
		{
			string program = "using System; class A { object x = $StringComparison.Ordinal$; }";
			var cfre = (CodeFieldReferenceExpression)ConvertInContext(program);
			Assert.AreEqual("Ordinal", cfre.FieldName);
			var ctre = ((CodeTypeReferenceExpression)cfre.TargetObject);
			Assert.AreEqual("System.StringComparison", ctre.Type.BaseType);
		}
		
		[Test]
		public void InstanceMethodInvocation()
		{
			Assert.AreEqual("this.Equals(null)", ConvertExpression("Equals(null)"));
		}
		
		[Test]
		public void StaticMethodInvocation()
		{
			Assert.AreEqual("object.Equals(null, null)", ConvertExpression("Equals(null, null)"));
		}
		
		[Test]
		public void BaseMemberAccess()
		{
			Assert.AreEqual("base.X", ConvertExpression("base.X"));
			Assert.AreEqual("base[i]", ConvertExpression("base[i]"));
		}
		
		[Test]
		public void GenericMethodReference()
		{
			Assert.AreEqual("this.Stuff<string>", ConvertExpression("this.Stuff<string>"));
			Assert.AreEqual("this.Stuff<string>", ConvertExpression("Stuff<string>"));
		}
		
		[Test]
		public void ByReferenceCall()
		{
			Assert.AreEqual("a.Call(ref x, out y, z)", ConvertExpression("a.Call(ref x, out y, z)"));
		}
		
		[Test]
		public void MemberAccessOnType()
		{
			Assert.AreEqual("string.Empty", ConvertExpression("string.Empty"));
		}
		#endregion
		
		#region Statements
		[Test]
		public void MethodInvocationStatement()
		{
			Assert.AreEqual("a.SomeMethod();", ConvertStatement("a.SomeMethod();"));
		}
		
		[Test]
		public void Assignment()
		{
			Assert.AreEqual("a = 1;", ConvertStatement("a = 1;"));
		}
		
		[Test, ExpectedException(typeof(NotSupportedException))]
		public void AssignmentNotSupportedInExpression()
		{
			ConvertStatement("a = b = 1;");
		}
		
		[Test]
		public void BlockStatement()
		{
			Assert.AreEqual("if (true) { a = 1; b = 2; }",
			                ConvertStatement("{ a = 1; b = 2; }"));
		}
		
		[Test]
		public void CompoundAssign()
		{
			Assert.AreEqual("a = (a + 1);", ConvertStatement("a += 1;"));
			Assert.AreEqual("a = (a - 1);", ConvertStatement("a -= 1;"));
			Assert.AreEqual("a = (a * 1);", ConvertStatement("a *= 1;"));
			Assert.AreEqual("a = (a / 1);", ConvertStatement("a /= 1;"));
			Assert.AreEqual("a = (a % 1);", ConvertStatement("a %= 1;"));
			Assert.AreEqual("a = (a & 1);", ConvertStatement("a &= 1;"));
			Assert.AreEqual("a = (a | 1);", ConvertStatement("a |= 1;"));
		}
		
		[Test]
		public void Increment()
		{
			Assert.AreEqual("a = (a + 1);", ConvertStatement("a++;"));
			Assert.AreEqual("a = (a + 1);", ConvertStatement("++a;"));
			Assert.AreEqual("a = (a - 1);", ConvertStatement("a--;"));
			Assert.AreEqual("a = (a - 1);", ConvertStatement("--a;"));
		}
		
		[Test]
		public void ForLoop()
		{
			Assert.AreEqual("for (int i = 0; (i < 10); i = (i + 1)) { }",
			                ConvertStatement("for (int i = 0; i < 10; i++) {}"));
		}
		
		[Test]
		public void WhileLoop()
		{
			Assert.AreEqual("for (new object(); (i < 10); new object()) { }",
			                ConvertStatement("while (i < 10);"));
		}
		
		[Test]
		public void VariableDeclarationWithArrayInitializer()
		{
			Assert.AreEqual("int[] nums = new int[] { 1, 2};",
			                ConvertStatement("int[] nums = { 1, 2 };"));
		}
		
		[Test]
		public void TryCatch()
		{
			Assert.AreEqual("try { a = 1; } catch (System.Exception ex) { ex.ToString(); }",
			                ConvertStatement("try { a = 1; } catch (Exception ex) { ex.ToString(); }"));
		}
		
		[Test]
		public void TryEmptyCatch()
		{
			Assert.AreEqual("try { a = 1; } catch (System.Exception ) { }",
			                ConvertStatement("try { a = 1; } catch (Exception) { }"));
		}
		
		[Test]
		public void TryFinally()
		{
			Assert.AreEqual("try { a = 1; } finally { a = 0; }",
			                ConvertStatement("try { a = 1; } finally { a = 0; }"));
		}
		#endregion
		
		#region Type Members
		[Test]
		public void MethodParameterNamedValue()
		{
			Assert.AreEqual("void M(string value) { System.Console.WriteLine(value); }",
			                ConvertMember("void M(string value) { Console.WriteLine(value); }"));
		}
		
		[Test]
		public void ValueInProperty()
		{
			Assert.AreEqual("string P { set { System.Console.WriteLine(value); } }",
			                ConvertMember("string P { set { Console.WriteLine(value); } }"));
		}
		
		[Test]
		public void MethodWithAttribute()
		{
			Assert.AreEqual("[Test()] void MethodWithAttribute() { }",
			                ConvertMember("[Test] void MethodWithAttribute() { }"));
		}
		
		[Test]
		public void PublicNonVirtualMethod()
		{
			Assert.AreEqual("public void Method() { }",
			                ConvertMember("public void Method() { }"));
		}
		
		[Test]
		public void PublicVirtualMethod()
		{
			Assert.AreEqual("public virtual void Method() { }",
			                ConvertMember("public virtual void Method() { }"));
		}
		
		[Test]
		public void NestedClass()
		{
			Assert.AreEqual("public class Outer { public class Inner { } }",
			                ConvertTypeDeclaration("class Outer { class Inner { } }"));
		}
		
		[Test]
		public void Constructor()
		{
			string code = "public class Test : Base { public Test(string x) : base(x) { } }";
			Assert.AreEqual(code, ConvertTypeDeclaration(code));
		}
		
		[Test]
		public void Enum()
		{
			string code = "public enum E { [Description(\"Text\")] None, B = 2, }";
			Assert.AreEqual(code, ConvertTypeDeclaration(code));
		}
		
		[Test]
		public void Field()
		{
			Assert.AreEqual("public class X {" +
			                " int A;" +
			                " int B; }",
			                ConvertMember("public class X { int A, B; }"));
		}
		
		[Test]
		public void Event()
		{
			Assert.AreEqual("public class X {" +
			                " protected event System.EventHandler A;" +
			                " protected event System.EventHandler B; }",
			                ConvertMember("public class X { protected event EventHandler A, B; }"));
		}
		#endregion
		
		#region SyntaxTrees
		[Test]
		public void TestGlobalNamespaceFix ()
		{
			Assert.AreEqual("namespace A { using System; public class AClass { } } namespace B { using System.IO; using System; public class AClass { } }",
			                ConvertSyntaxTree("using System; namespace A { public class AClass {} } namespace B { using System.IO; public class AClass {} }"));
		}
		#endregion
	}
}