File: SemanticHighlightingTests.cs

package info (click to toggle)
monodevelop 4.0.12%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 219,596 kB
  • ctags: 253,200
  • sloc: cs: 1,486,058; xml: 952,347; java: 60,981; makefile: 4,213; sh: 1,727; ansic: 867; objc: 302; sql: 111
file content (384 lines) | stat: -rw-r--r-- 9,611 bytes parent folder | download | duplicates (4)
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
//
// SemanticHighlightingTests.cs
//
// Author:
//       Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
//
// 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 ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.TypeSystem;
using NUnit.Framework;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Editor;

namespace ICSharpCode.NRefactory.CSharp.Analysis
{
	[TestFixture]
	public class SemanticHighlightingTests : SemanticHighlightingVisitor<FieldInfo>
	{
		static void SetupColors(object o)
		{
			var fields = typeof(SemanticHighlightingVisitor<FieldInfo>).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
			foreach (var field in fields) {
				if (field.FieldType == typeof(FieldInfo))
					field.SetValue(o, field);
			}
		}
		protected override void Colorize(TextLocation start, TextLocation end, FieldInfo color)
		{
			throw new NotImplementedException ();
		}

		[SetUp]
		public void Setup ()
		{
			SetupColors (this);
		}

		class TestSemanticHighlightingVisitor : SemanticHighlightingVisitor<FieldInfo>
		{

			public TestSemanticHighlightingVisitor(CSharpAstResolver resolver)
			{
				base.resolver = resolver;
				this.regionStart = new TextLocation (1, 1);
				this.regionEnd = new TextLocation (int.MaxValue, int.MaxValue);
				SetupColors(this);
			}

			List<Tuple<DomRegion, string>> colors = new List<Tuple<DomRegion, string>> ();

			protected override void Colorize(TextLocation start, TextLocation end, FieldInfo color)
			{
				colors.Add (Tuple.Create (new DomRegion (start, end), color != null ? color.Name : null));
			}

			public string GetColor(TextLocation loc)
			{
				foreach (var color in colors) {
					if (color.Item1.IsInside (loc))
						return color.Item2;
				}
				return null;
			}
		}

		static TestSemanticHighlightingVisitor CreateHighighting (string text)
		{
			var syntaxTree = SyntaxTree.Parse (text, "a.cs");
			if (syntaxTree.Errors.Count > 0) {
				Console.WriteLine (text);
				Console.WriteLine("---");
				syntaxTree.Errors.ForEach (err => Console.WriteLine (err.Message));
				Assert.Fail ("parse error.");
			}
			var project = new CSharpProjectContent().AddAssemblyReferences(new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore });
			var file = syntaxTree.ToTypeSystem();
			project = project.AddOrUpdateFiles(file);

			var resolver = new CSharpAstResolver(project.CreateCompilation(), syntaxTree, file);
			var result = new TestSemanticHighlightingVisitor (resolver);
			syntaxTree.AcceptVisitor (result);
			return result;
		}

		void TestColor(string text, FieldInfo keywordColor)
		{
			var sb = new StringBuilder ();
			var offsets = new List<int> ();
			foreach (var ch in text) {
				if (ch == '$') {
					offsets.Add (sb.Length);
					continue;
				}
				sb.Append (ch);
			}
			var visitor = CreateHighighting (sb.ToString ());
			var doc = new ReadOnlyDocument (sb.ToString ());

			foreach (var offset in offsets) {
				var loc = doc.GetLocation (offset);
				var color = visitor.GetColor (loc);
				Assert.AreEqual (keywordColor.Name, color, "Color at " + loc + " is wrong:" + color);
			}
		}

		[Test]
		public void TestClassDeclaration()
		{
			TestColor (@"class $Class { }", referenceTypeColor);
		}

		[Test]
		public void TestStructDeclaration()
		{
			TestColor (@"struct $Class { }", valueTypeColor);
		}
		
		[Test]
		public void TestInterfaceDeclaration()
		{
			TestColor (@"interface $Class { }", interfaceTypeColor);
		}
		
		[Test]
		public void TestEnumDeclaration()
		{
			TestColor (@"enum $Class { }", enumerationTypeColor);
		}

		[Test]
		public void TestDelegateDeclaration()
		{
			TestColor (@"delegate void $Class();", delegateTypeColor);
		}

		[Test]
		public void TestTypeParameter()
		{
			TestColor (@"class Class<$T> where $T : class { void Foo<$Tx> () where $Tx : $T {} } ", typeParameterTypeColor);
		}

		[Test]
		public void TestMethodDeclaration()
		{
			TestColor (@"class Class { void $Foo () {} }", methodDeclarationColor);
		}
		
		[Test]
		public void TestMethodCall()
		{
			TestColor (@"class Class { void Foo () { $Foo (); } }", methodCallColor);
		}

		[Test]
		public void TestFieldDeclaration()
		{
			TestColor (@"class Class { int $foo; }", fieldDeclarationColor);
		}

		[Test]
		public void TestFieldAccess()
		{
			TestColor (@"using System; class Class {  int Bar; void Foo () { Console.WriteLine ($Bar); } }", fieldAccessColor);
		}

		[Test]
		public void TestPropertyDeclaration()
		{
			TestColor (@"class Class { int $Foo { get; set; } }", propertyDeclarationColor);
		}

		[Test]
		public void TestPropertyAccess()
		{
			TestColor (@"using System; class Class { int Bar {get; set; } void Foo () { Console.WriteLine ($Bar); } }", propertyAccessColor);
		}

		[Test]
		public void TestEventDeclaration()
		{
			TestColor (@"class Class { event System.EventHandler $Foo, $Bar; }", eventDeclarationColor);
		}

		[Test]
		public void TestCustomEventDeclaration()
		{
			TestColor (@"class Class { event System.EventHandler $Foo { add {} remove {} } }", eventDeclarationColor);
		}

		
		[Test]
		public void TestEventAccess()
		{
			TestColor (@"class Class {  event System.EventHandler Bar; void Foo () { $Bar += (o, s) => {}; } }", eventAccessColor);
		}

		[Test]
		public void TestMethodParameterDeclaration()
		{
			TestColor (@"class Class { void Foo (int $a, string $b) {} }", parameterDeclarationColor);
		}
		[Test]
		public void TestMethodParameterAccess()
		{
			TestColor (@"class Class { void Foo (int a, string b) { int c = $a + $b;} }", parameterAccessColor);
		}

		[Test]
		public void TestIndexerParameterDeclaration()
		{
			TestColor (@"class Class { int this[int $a, string $b] { get { } } }", parameterDeclarationColor);
		}

		[Test]
		public void TestIndexerParameterAccess()
		{
			TestColor (@"class Class { int this[int a, string b] { get { return $a + $b; } } }", parameterAccessColor);
		}

		[Test]
		public void TestVariableDeclaration()
		{
			TestColor (@"class Class { void Test () { int $bar, $foo; } }", variableDeclarationColor);
		}

		[Test]
		public void TestVariableAccess()
		{
			TestColor (@"class Class { void Test () { int bar, foo; int c = $foo + $bar; } }", variableAccessColor);
		}

		[Test]
		public void TestValueKeywordInPropertySetter()
		{
			TestColor (@"class Class { int Property { get {} set { test = $value; } } }", valueKeywordColor);
		}

		[Test]
		public void TestValueKeywordInPropertyGetter()
		{
			TestColor (@"class Class { int value; int Property { get { return $value; } set { } } }", fieldAccessColor);
		}

		[Test]
		public void TestValueKeywordInCustomEvent()
		{
			TestColor (@"using System;
class Class {
	public event EventHandler Property { 
		add { Console.WriteLine ($value); } 
		remove { Console.WriteLine ($value); }
	}
}", valueKeywordColor);
		}
	
		[Test]
		public void TestExternAliasKeyword()
		{
			TestColor (@"extern $alias FooBar;", externAliasKeywordColor);
		}

		/// <summary>
		/// Bug 9539 - Semantic highlighting does not detect invalid type
		/// </summary>
		[Test]
		public void TestBug9539()
		{
			string code =@"class C<T>
		{
			public class N<U>
			{
			}
		}
		
		class A
		{
			public static int Main ()
			{
				var a = typeof (C<>.$N<,>); // The type is not red even if this linedoes not compile
				return 0;
			}
		}
";
			TestColor (code, syntaxErrorColor);
		}

		[Test]
		public void TestNullTypeError()
		{
			string code =@"
		class A
		{
			public static void Main ()
			{
				$var a = null;
			}
		}
";
			TestColor (code, syntaxErrorColor);
		}


		[Test]
		public void TestVarColorUserTypeCase()
		{
			string code =@"class var {
			$var aVar;
			public static void Main ()
			{
				$var a = new $var ();
			}
		}
";
			TestColor (code, referenceTypeColor);
		}

		[Test]
		public void TestVarColor()
		{
			TestColor (@"class MyClass {
			public static void Main ()
			{
				$var a = 344;
				foreach ($var b in new [] { 1,2, 3}) {}
			}
		}
", varKeywordTypeColor);
		}


		[Test]
		public void TestStringFormatItemColor()
		{
			TestColor (@"using System;
class MyClass {
			public static void Main ()
			{
				string str = string.Format ("" ${0} ${1} ${2} "", 1, 2, 3);
			}
		}
", stringFormatItemColor);
		}

		[Test]
		public void TestStringFormatItemInVerbatimStringColor()
		{
			TestColor (@"using System;
class MyClass {
			public static void Main ()
			{
				Console.WriteLine (@"" ${0}

 ${1} 


${2} "", 1, 2, 3);
			}
		}
", stringFormatItemColor);
		}
	}
}