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
|
//
// ResolveNamespaceTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 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 NUnit.Framework;
using MonoDevelop.CSharpBinding.Tests;
using System.Collections.Generic;
using MonoDevelop.CSharpBinding;
using MonoDevelop.Ide.Gui;
using Mono.TextEditor;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.CSharp;
using MonoDevelop.CSharp;
using MonoDevelop.CSharp.Completion;
using MonoDevelop.Refactoring;
using System.Linq;
namespace MonoDevelop.CSharpBinding.Refactoring
{
[Ignore("Ignored till the tests run inside md.")]
[TestFixture]
public class ResolveNamespaceTests : UnitTests.TestBase
{
static Document Setup (string input)
{
var tww = new TestWorkbenchWindow ();
var content = new TestViewContent ();
tww.ViewContent = content;
content.ContentName = "a.cs";
content.GetTextEditorData ().Document.MimeType = "text/x-csharp";
var doc = new Document (tww);
var text = input;
int endPos = text.IndexOf ('$');
if (endPos >= 0)
text = text.Substring (0, endPos) + text.Substring (endPos + 1);
content.Text = text;
content.CursorPosition = Math.Max (0, endPos);
var compExt = new CSharpCompletionTextEditorExtension ();
compExt.Initialize (doc);
content.Contents.Add (compExt);
doc.UpdateParseDocument ();
return doc;
}
HashSet<MonoDevelop.Refactoring.ResolveCommandHandler.PossibleNamespace> GetResult (string input)
{
var doc = Setup (input);
var location = doc.Editor.Caret.Location;
ResolveResult resolveResult;
AstNode node;
doc.TryResolveAt (location, out resolveResult, out node);
return ResolveCommandHandler.GetPossibleNamespaces (doc, node, ref resolveResult);
}
[Test]
public void TestObjectCreationType ()
{
var result = GetResult (@"class Test {
void MyMethod ()
{
var list = new $List<string> ();
}
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Collections.Generic"));
}
[Test]
public void TestLocalVariableType ()
{
var result = GetResult (@"class Test {
void MyMethod ()
{
$List<string> list;
}
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Collections.Generic"));
}
[Test]
public void TestParameterType ()
{
var result = GetResult (@"class Test {
void MyMethod ($List<string> list)
{
}
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Collections.Generic"));
}
[Test]
public void TestFieldType ()
{
var result = GetResult (@"class Test {
$List<string> list;
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Collections.Generic"));
}
[Test]
public void TestBaseType ()
{
var result = GetResult (@"class Test : $List<string> {}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Collections.Generic"));
}
[Test]
public void TestLocalVariableValid ()
{
var result = GetResult (@"using System.Collections.Generic;
class Test {
void MyMethod ()
{
$List<string> list;
}
}");
Assert.AreEqual (0, result.Count);
}
[Test]
public void TestAttributeCase1 ()
{
var result = GetResult (@"
[$Obsolete]
class Test {
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System"));
}
[Test]
public void TestAttributeCase2 ()
{
var result = GetResult (@"
[$SerializableAttribute]
class Test {
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System"));
}
[Test]
public void TestAmbigiousResolveResult ()
{
var result = GetResult (@"namespace Foo { class Bar {} }
namespace Foo2 { class Bar {} }
namespace My
{
using Foo;
using Foo2;
class Program
{
public static void Main ()
{
$Bar bar;
}
}
}");
foreach (var a in result)
Console.WriteLine (a);
Assert.IsTrue (result.Any (t => t.Namespace == "Foo"));
Assert.IsTrue (result.Any (t => t.Namespace == "Foo2"));
}
[Test]
public void TestExtensionMethod ()
{
var result = GetResult (@"class Program
{
public static void Main (string[] args)
{
args.$First ();
}
}");
foreach (var a in result)
Console.WriteLine (a);
Assert.IsTrue (result.Any (t => t.Namespace == "System.Linq"));
}
#region Bug 3453 - [New Resolver] "Resolve" doesn't show up from time
[Test]
public void TestBug3453Case1 ()
{
var result = GetResult (@"class Test {
string GetName ()
{
var encoding = $Encoding
return encoding.EncodingName;
}
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Text"));
}
[Test]
public void TestBug3453Case2 ()
{
var result = GetResult (@"class Test {
string GetName ()
{
$Encoding
return encoding.EncodingName;
}
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Text"));
}
[Test]
public void TestBug3453Case3 ()
{
var result = GetResult (@"class Test {
string GetName ()
{
$Encoding.
}
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Text"));
}
[Test]
public void TestBug3453Case3WithGeneris ()
{
var result = GetResult (@"class Test {
string GetName ()
{
$List<string>.
}
}");
Assert.IsTrue (result.Any (t => t.Namespace == "System.Collections.Generic"));
}
#endregion
/// <summary>
/// Bug 4361 - Cannot 'resolve' an unknown type
/// </summary>
[Test]
public void TestBug4361 ()
{
var result = GetResult (@"using System;
namespace sadfhgjhkfj
{
class MainClass
{
public static void Main (string[] args)
{
while (true) {
var t = new Thre$ad
}
}
}
}");
foreach (var a in result)
Console.WriteLine (a);
Assert.IsTrue (result.Any (t => t.Namespace == "System.Threading"));
}
[Test]
public void TestBug4361Case2 ()
{
var result = GetResult (@"using System;
namespace sadfhgjhkfj
{
class MainClass
{
public static void Main (string[] args)
{
while (true) {
var t = new Thre$ad[0];
}
}
}
}");
foreach (var a in result)
Console.WriteLine (a);
Assert.IsTrue (result.Any (t => t.Namespace == "System.Threading"));
}
/// <summary>
/// Bug 4493 - 'Resolve' context action offers incorrect options
/// </summary>
[Test]
public void TestBug4493 ()
{
var result = GetResult (@"using System;
namespace sadfhgjhkfj
{
class MainClass
{
public static void Main (string[] args)
{
S$tack<int> a;
}
}
}"
);
Assert.IsFalse (result.Any (t => t.Namespace == "System.Collections"));
Assert.IsTrue (result.Any (t => t.Namespace == "System.Collections.Generic"));
}
/// <summary>
/// Bug 5206 - Resolve -> Add Using statement does not work after "|"
/// </summary>
[Test]
public void TestBug5206 ()
{
var result = GetResult (@"using System;
namespace TestConsole
{
class MainClass
{
public static void Main (string[] args)
{
var f = typeof (int).GetFields (System.Reflection.BindingFlags.Static | Binding$Flags
}
}
}"
);
Assert.IsTrue (result.Any (t => t.Namespace == "System.Reflection"));
}
/// <summary>
/// Bug 4749 - Resolve is incorrect for inner classes
/// </summary>
[Test]
public void TestBug4749 ()
{
var result = GetResult (@"namespace Test { public class Foo { public class Bar {} } }
class Program
{
public static void Main ()
{
$Bar bar;
}
}
");
foreach (var a in result)
Console.WriteLine (a);
Assert.IsTrue (result.Any (t => t.Namespace == "Test.Foo" && !t.IsAccessibleWithGlobalUsing));
}
/// <summary>
/// Bug 10059 - Resolve type fails for nested static types
/// </summary>
[Test]
public void TestBug10059 ()
{
var result = GetResult (@"namespace ConsoleTest
{
class MainClass
{
$Environment.SpecialFolder F { get; }
}
}
"
);
Assert.IsTrue (result.Any (t => t.Namespace == "System"));
}
}
}
|