public static Create ( string content ) : TestRefactoringContext | ||
content | string | |
return | TestRefactoringContext |
protected List <CodeAction> GetActions <T> (string input) where T : CodeActionProvider, new () { var ctx = TestRefactoringContext.Create(input); ctx.FormattingOptions = formattingOptions; return(new T().GetActions(ctx).ToList()); }
protected static void TestWrongContext(ICodeActionProvider action, string input) { var context = TestRefactoringContext.Create(input); bool isValid = action.GetActions(context).Any(); if (!isValid) { Console.WriteLine("invalid node is:" + context.GetNode()); } Assert.IsTrue(!isValid, action.GetType() + " shouldn't be valid there."); }
protected void TestActionDescriptions(CodeActionProvider provider, string input, params string[] expected) { var ctx = TestRefactoringContext.Create(input); ctx.FormattingOptions = formattingOptions; var actions = provider.GetActions(ctx).ToList(); Assert.AreEqual( expected, actions.Select(a => a.Description).ToArray()); }
public void TestBug10671() { var input = @" namespace TestConsole { public class Test : $BaseMissing { } } "; // action allowed to create a nested class var context = TestRefactoringContext.Create(input, false); var actions = new CreateClassDeclarationAction().GetActions(context); Assert.AreEqual(1, actions.Count()); }
protected static string RunContextAction(ICodeActionProvider action, string input, int actionIndex = 0) { var context = TestRefactoringContext.Create(input); bool isValid = action.GetActions(context).Any(); if (!isValid) { Console.WriteLine("invalid node is:" + context.GetNode()); } Assert.IsTrue(isValid, action.GetType() + " is invalid."); using (var script = context.StartScript()) { action.GetActions(context).Skip(actionIndex).First().Run(script); } return(context.doc.Text); }
public void TestAddBaseTypeGenerics() { var context = TestRefactoringContext.Create(@" public class $Foo<T> where T : new() { }"); using (var script = context.StartScript()) { var type = context.GetNode <TypeDeclaration>(); script.ChangeBaseTypes(type, new AstType[] { AstType.Create("System.Test") }); } Assert.AreEqual(@" public class Foo<T> : System.Test where T : new() { }", context.Text); }
public void TestReplaceBaseType() { var context = TestRefactoringContext.Create(@" public class $Foo : System.IDisposable { }"); using (var script = context.StartScript()) { var type = context.GetNode <TypeDeclaration>(); script.ChangeBaseTypes(type, new AstType[] { AstType.Create("System.Test") }); } Assert.AreEqual(@" public class Foo : System.Test { }", context.Text); }
public void TestRemoveBaseType() { var context = TestRefactoringContext.Create(@" public class $Foo : System.IDisposable { }"); using (var script = context.StartScript()) { var type = context.GetNode <TypeDeclaration>(); script.ChangeBaseTypes(type, Enumerable.Empty <AstType>()); } Assert.AreEqual(@" public class Foo { }", context.Text); }
public void TestGlobalOperation() { List <string> contents = new List <string>() { @"class Test1 { public int $x; }", @"class Test2 { public void Foo(Test1 test1) { test1.x = 1; test1.x = 2; } }" }; var context = TestRefactoringContext.Create(contents, 0); using (var script = context.StartScript()) { var variable = context.GetNode <VariableInitializer>(); script.DoGlobalOperationOn(new List <IEntity>() { ((MemberResolveResult)context.Resolve(variable)).Member }, (rCtx, rScript, nodes) => { foreach (var node in nodes) { rScript.Replace(node, new IdentifierExpression("replacement")); } }); } Assert.AreEqual(@"class Test1 { public int replacement; }", context.GetSideDocumentText(0)); Assert.AreEqual(@"class Test2 { public void Foo(Test1 test1) { replacement = 1; replacement = 2; } }", context.GetSideDocumentText(1)); }
public void TestRenameInterfaceMethod() { List <string> contents = new List <string>() { @"interface ITest1 { int $method (); } class Test2 : ITest1 { int method () { } }", @"class Test3 : ITest1 { int method () { } }" }; var context = TestRefactoringContext.Create(contents, 0); using (var script = context.StartScript()) { var method = context.GetNode <MethodDeclaration>(); script.Rename(((MemberResolveResult)context.Resolve(method)).Member, "newName"); } Assert.AreEqual(@"interface ITest1 { int newName (); } class Test2 : ITest1 { int newName () { } }", context.GetSideDocumentText(0)); Assert.AreEqual(@"class Test3 : ITest1 { int newName () { } }", context.GetSideDocumentText(1)); }
// TODO: Remove this when the formatter handles object and collection initializers // This tests the expected code vs the actual code based on their ASTs instead of the text they produce. public new void Test <T>(string input, string output, int action = 0, bool expectErrors = false) where T : CodeActionProvider, new () { string result = RunContextAction(new T(), HomogenizeEol(input), action, expectErrors); var expectedContext = TestRefactoringContext.Create(output, expectErrors); var actualContext = TestRefactoringContext.Create(result, expectErrors); bool passed = expectedContext.RootNode.IsMatch(actualContext.RootNode); if (!passed) { Console.WriteLine("-----------Expected:"); Console.WriteLine(output); Console.WriteLine("-----------Got:"); Console.WriteLine(result); } Assert.IsTrue(passed, "The generated code and the expected code was not syntactically identical. See output for details."); }
public void TestRename() { List <string> contents = new List <string>() { @"class Test1 { public int $x; }", @"class Test2 { public void Foo(Test1 test1) { test1.x = 1; test1.x = 2; } }" }; var context = TestRefactoringContext.Create(contents, 0); using (var script = context.StartScript()) { var variable = context.GetNode <VariableInitializer>(); script.Rename(((MemberResolveResult)context.Resolve(variable)).Member, "newName"); } Assert.AreEqual(@"class Test1 { public int newName; }", context.GetSideDocumentText(0)); Assert.AreEqual(@"class Test2 { public void Foo(Test1 test1) { test1.newName = 1; test1.newName = 2; } }", context.GetSideDocumentText(1)); }