public static TestRefactoringContext Create (List<string> contents, int mainIndex, bool expectErrors = false, CSharpParser parser = null) { List<int> indexes = new List<int>(); List<int> selectionStarts = new List<int>(); List<int> selectionEnds = new List<int>(); List<IDocument> documents = new List<IDocument>(); List<CSharpUnresolvedFile> unresolvedFiles = new List<CSharpUnresolvedFile>(); List<SyntaxTree> units = new List<SyntaxTree>(); for (int i = 0; i < contents.Count; i++) { string content = contents[i]; int idx = content.IndexOf("$"); if (idx >= 0) content = content.Substring(0, idx) + content.Substring(idx + 1); int idx1 = content.IndexOf("<-"); int idx2 = content.IndexOf("->"); int selectionStart = 0; int selectionEnd = 0; if (0 <= idx1 && idx1 < idx2) { content = content.Substring(0, idx2) + content.Substring(idx2 + 2); content = content.Substring(0, idx1) + content.Substring(idx1 + 2); selectionStart = idx1; selectionEnd = idx2 - 2; idx = selectionEnd; } indexes.Add(idx); selectionStarts.Add(selectionStart); selectionEnds.Add(selectionEnd); var doc = new StringBuilderDocument(content); if (parser == null) parser = new CSharpParser(); var unit = parser.Parse(content, "program_" + i + ".cs"); if (!expectErrors) { if (parser.HasErrors) { Console.WriteLine(content); Console.WriteLine("----"); } foreach (var error in parser.ErrorsAndWarnings) { Console.WriteLine(error.Message); } Assert.IsFalse(parser.HasErrors, "The file " + i + " contains unexpected parsing errors."); } else { Assert.IsTrue(parser.HasErrors, "Expected parsing errors, but the file " + i + "doesn't contain any."); } unit.Freeze(); CSharpUnresolvedFile unresolvedFile = unit.ToTypeSystem(); units.Add(unit); documents.Add(doc); unresolvedFiles.Add(unresolvedFile); } IProjectContent pc = new CSharpProjectContent (); pc = pc.AddOrUpdateFiles (unresolvedFiles); pc = pc.AddAssemblyReferences (new[] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore }); var compilation = pc.CreateCompilation (); List<TestRefactoringContext> contexts = new List<TestRefactoringContext>(); for (int documentIndex = 0; documentIndex < documents.Count; ++documentIndex) { var doc = documents [documentIndex]; var resolver = new CSharpAstResolver (compilation, units[documentIndex], unresolvedFiles[documentIndex]); TextLocation location = TextLocation.Empty; if (indexes[documentIndex] >= 0) location = doc.GetLocation (indexes[documentIndex]); var context = new TestRefactoringContext(doc, location, resolver) { selectionStart = selectionStarts[documentIndex], selectionEnd = selectionEnds[documentIndex], projectContexts = contexts, version = parser.CompilerSettings.LanguageVersion, defaultNamespace = "Test" }; contexts.Add(context); } return contexts [mainIndex]; }
public static TestRefactoringContext Create(string content, bool expectErrors = false) { int idx = content.IndexOf ("$"); if (idx >= 0) content = content.Substring (0, idx) + content.Substring (idx + 1); int idx1 = content.IndexOf ("<-"); int idx2 = content.IndexOf ("->"); int selectionStart = 0; int selectionEnd = 0; if (0 <= idx1 && idx1 < idx2) { content = content.Substring (0, idx2) + content.Substring (idx2 + 2); content = content.Substring (0, idx1) + content.Substring (idx1 + 2); selectionStart = idx1; selectionEnd = idx2 - 2; idx = selectionEnd; } var doc = new StringBuilderDocument(content); var parser = new CSharpParser(); var unit = parser.Parse(content, "program.cs"); if (!expectErrors) { if (parser.HasErrors) { Console.WriteLine (content); Console.WriteLine ("----"); } foreach (var error in parser.Errors) { Console.WriteLine(error.Message); } Assert.IsFalse(parser.HasErrors, "The file contains unexpected parsing errors."); } else { Assert.IsTrue(parser.HasErrors, "Expected parsing errors, but the file doesn't contain any."); } unit.Freeze (); var unresolvedFile = unit.ToTypeSystem (); IProjectContent pc = new CSharpProjectContent (); pc = pc.AddOrUpdateFiles (unresolvedFile); pc = pc.AddAssemblyReferences (new[] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore }); var compilation = pc.CreateCompilation (); var resolver = new CSharpAstResolver (compilation, unit, unresolvedFile); TextLocation location = TextLocation.Empty; if (idx >= 0) location = doc.GetLocation (idx); return new TestRefactoringContext(doc, location, resolver) { selectionStart = selectionStart, selectionEnd = selectionEnd }; }
public static TestRefactoringContext Create (string content) { int idx = content.IndexOf ("$"); if (idx >= 0) content = content.Substring (0, idx) + content.Substring (idx + 1); int idx1 = content.IndexOf ("<-"); int idx2 = content.IndexOf ("->"); int selectionStart = 0; int selectionEnd = 0; if (0 <= idx1 && idx1 < idx2) { content = content.Substring (0, idx2) + content.Substring (idx2 + 2); content = content.Substring (0, idx1) + content.Substring (idx1 + 2); selectionStart = idx1; selectionEnd = idx2 - 2; idx = selectionEnd; } var doc = new StringBuilderDocument (content); var parser = new CSharpParser (); var unit = parser.Parse (content, "program.cs"); if (parser.HasErrors) parser.ErrorPrinter.Errors.ForEach (e => Console.WriteLine (e.Message)); Assert.IsFalse (parser.HasErrors, "File contains parsing errors."); unit.Freeze (); var parsedFile = unit.ToTypeSystem (); IProjectContent pc = new CSharpProjectContent (); pc = pc.UpdateProjectContent (null, parsedFile); pc = pc.AddAssemblyReferences (new[] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore }); var compilation = pc.CreateCompilation (); var resolver = new CSharpAstResolver (compilation, unit, parsedFile); TextLocation location = TextLocation.Empty; if (idx >= 0) location = doc.GetLocation (idx); return new TestRefactoringContext(doc, location, resolver) { selectionStart = selectionStart, selectionEnd = selectionEnd }; }