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
            });
        }
Exemplo n.º 2
0
        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
            });
        }