Example #1
0
        void TestCreateInterface(string interfacecode, string outputString)
        {
            var dom = new SimpleProjectDom();

            var parser = new McsParser();
            var unit   = parser.Parse(dom, "Interface.cs", interfacecode);

            DomType stubType = new DomType("Stub");

            stubType.SourceProjectDom = dom;
            stubType.CompilationUnit  = new CompilationUnit("Stub.cs");
            var iface = unit.CompilationUnit.Types[0];
            var gen   = new CSharpCodeGenerator();

            gen.EolMarker = "\n";
            string generated = gen.CreateInterfaceImplementation(stubType, iface, false);

            // crop #region
            generated = generated.Substring(generated.IndexOf("implementation") + "implementation".Length);
            generated = generated.Substring(0, generated.LastIndexOf("#"));
            generated = generated.Trim();
            System.Console.WriteLine(generated);
            Assert.AreEqual(outputString, generated);
        }
Example #2
0
        void RunTest(string test, LocalVariable localVariable)
        {
            StringBuilder      testText = new StringBuilder();
            List <DomLocation> expectedReferences = new List <DomLocation> ();
            DomLocation        memberLocation = DomLocation.Empty;
            int line = 1, col = 1;

            foreach (char ch in test)
            {
                switch (ch)
                {
                case '$':
                    memberLocation = new DomLocation(line, col);
                    break;

                case '@':
                    expectedReferences.Add(new DomLocation(line, col));
                    break;

                default:
                    col++;
                    if (ch == '\n')
                    {
                        col = 1;
                        line++;
                    }
                    testText.Append(ch);
                    break;
                }
            }
            DotNetProject project = new DotNetAssemblyProject("C#");

            project.FileName = "/tmp/a.csproj";

            SimpleProjectDom dom  = new SimpleProjectDom();

            dom.Project = project;
            ProjectDomService.RegisterDom(dom, "Project:" + project.FileName);

            ParsedDocument parsedDocument = parser.Parse(null, "a.cs", testText.ToString());

            dom.Add(parsedDocument.CompilationUnit);

            TestViewContent testViewContent = new TestViewContent();

            testViewContent.Name = "a.cs";
            testViewContent.Text = testText.ToString();
            //	RefactorerContext ctx = new RefactorerContext (dom, new DumbTextFileProvider(testViewContent), null);
            NRefactoryResolver resolver = new NRefactoryResolver(dom,
                                                                 parsedDocument.CompilationUnit,
                                                                 testViewContent.Data,
                                                                 "a.cs");
            SearchMemberVisitor smv = new SearchMemberVisitor(memberLocation.Line);

            if (localVariable != null)
            {
                ((LocalVariable)localVariable).DeclaringMember = parsedDocument.CompilationUnit.GetMemberAt(expectedReferences[0]);
                smv.FoundMember = localVariable;
            }
            else
            {
                smv.Visit(parsedDocument.CompilationUnit, null);
                if (smv.FoundMember == null)
                {
                    ResolveResult resolveResult = resolver.ResolveIdentifier("a", memberLocation);
                    if (resolveResult is LocalVariableResolveResult)
                    {
                        smv.FoundMember = ((LocalVariableResolveResult)resolveResult).LocalVariable;
                    }
                }
            }

            Assert.IsNotNull(smv.FoundMember, "Member to search not found.");
            if (smv.FoundMember is IType)
            {
                smv.FoundMember = dom.GetType(((IType)smv.FoundMember).FullName,
                                              ((IType)smv.FoundMember).TypeParameters.Count,
                                              true);
            }
            FindMemberAstVisitor astVisitor = new FindMemberAstVisitor(testViewContent.GetTextEditorData().Document, resolver, smv.FoundMember);

            astVisitor.RunVisitor();

            int           i = 0, j = 0;
            StringBuilder errorText = new StringBuilder();
            Document      doc       = new Document();

            doc.Text = testViewContent.Text;
            while (i < expectedReferences.Count && j < astVisitor.FoundReferences.Count)
            {
                if (expectedReferences[i].Line != astVisitor.FoundReferences[j].Line || expectedReferences[i].Column != astVisitor.FoundReferences[j].Column)
                {
                    if (expectedReferences[i].Line < astVisitor.FoundReferences[j].Line)
                    {
                        errorText.Append("Reference at  line " + expectedReferences[i].Line + " not found.");
                        errorText.AppendLine();
                        errorText.Append(doc.GetTextAt(doc.GetLine(expectedReferences[i].Line)).Replace('\t', ' '));
                        errorText.AppendLine();
                        errorText.Append(new string (' ', expectedReferences[i].Column)); errorText.Append('^');
                        errorText.AppendLine();
                        i++;
                        continue;
                    }
                    if (expectedReferences[i].Line > astVisitor.FoundReferences[j].Line)
                    {
                        errorText.Append("Found unexpected Reference at line " + astVisitor.FoundReferences[j].Line);
                        errorText.AppendLine();
                        errorText.Append(doc.GetTextAt(doc.GetLine(astVisitor.FoundReferences[j].Line)).Replace('\t', ' '));
                        errorText.AppendLine();
                        errorText.Append(new string (' ', astVisitor.FoundReferences[j].Column)); errorText.Append('^');
                        errorText.AppendLine();
                        j++;
                        continue;
                    }

                    errorText.Append("Column mismatch at line " + astVisitor.FoundReferences[j].Line + " was: " + astVisitor.FoundReferences[j].Column + " should be:" + expectedReferences[i].Column);
                    errorText.AppendLine();
                    errorText.Append(doc.GetTextAt(doc.GetLine(astVisitor.FoundReferences[j].Line)).Replace('\t', ' '));
                    errorText.Append(new string (' ', expectedReferences[i].Column)); errorText.Append('^');
                    errorText.AppendLine();
                    errorText.Append(new string (' ', astVisitor.FoundReferences[j].Column)); errorText.Append('^');
                    errorText.AppendLine();
                }
                i++; j++;
            }
            while (i < expectedReferences.Count)
            {
                errorText.Append("Reference at  line " + expectedReferences[i].Line + " not found.");
                errorText.AppendLine();
                errorText.Append(doc.GetTextAt(doc.GetLine(expectedReferences[i].Line)).Replace('\t', ' '));
                errorText.AppendLine();
                errorText.Append(new string (' ', expectedReferences[j].Column)); errorText.Append('^');
                errorText.AppendLine();
                i++;
            }
            while (j < astVisitor.FoundReferences.Count)
            {
                errorText.Append("Found unexpected Reference at line " + astVisitor.FoundReferences[j].Line);
                errorText.AppendLine();
                errorText.Append(doc.GetTextAt(doc.GetLine(astVisitor.FoundReferences[j].Line)).Replace('\t', ' '));
                errorText.AppendLine();
                errorText.Append(new string (' ', astVisitor.FoundReferences[i].Column)); errorText.Append('^');
                errorText.AppendLine();
                j++;
            }
            if (errorText.Length > 0)
            {
                Assert.Fail("Member to find:" + smv.FoundMember + Environment.NewLine + errorText.ToString() + Environment.NewLine + "found : " + astVisitor.FoundReferences.Count + " expected:" + expectedReferences.Count);
            }
        }