Beispiel #1
0
        public static bool MatchesIgnoreVarNames(this CloneDesc p, string expectedCode)
        {
            RenameLocalVariableExpansion rename = new RenameLocalVariableExpansion();

            var expected = AstMatchHelper.ParseToMethodDeclaration(expectedCode);

            foreach (var prd in rename.FindCandidatesViaRefactoringPermutations(new TargetInfo(expected, p.PermutatedMethod), p.PermutatedMethod))
            {
                if (expected.Matches(prd))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #2
0
        public void ExerciseOnExtractedCandidateEvent()
        {
            const string codeText = @"    
                                public class CloneInDoWhileBlock
                                {
                                    void Foo()
                                    {
                                        do
                                        {
                                            int i = 7 + 8;
                                            int j = 9 +10;
                                            Console.WriteLine(i + j);

                                            /* BEGIN */
                                            Console.WriteLine(7);
                                            /* END */
                                        }
                                        while (DateTime.Now < DateTime.Today);
                                    }

                                    private void Bar()
                                    {
                                        Console.WriteLine(7);
                                        Console.WriteLine(7);
                                        Console.WriteLine(7);
                                    }
                                }";

            MethodsOnASingleClassCloneFinder finder = new MethodsOnASingleClassCloneFinder(new OscillatingExtractMethodExpansionFactory());

            CloneDesc largest = null;

            finder.OnExtractedCandidate += ((sender, args) =>
            {
                TestLog.EmbedPlainText("Each: ", args.Candidate.PermutatedMethod.Print());

                if (largest == null || largest.PermutatedMethod.CountNodes() < args.Candidate.PermutatedMethod.CountNodes())
                {
                    largest = args.Candidate;
                }
            });

            finder.GetCloneReplacements(codeText);

            TestLog.EmbedPlainText("The largest:", largest.PermutatedMethod.Print());
        }
        public void DoCloneDetection(string codefile, MethodsOnASingleClassCloneFinder cloneFinder)
        {
            string codeText = File.ReadAllText(codefile);

            TestLog.EmbedPlainText("The Code", codeText);

//            System.Diagnostics.Debugger.Break();

            IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(codeText));

            parser.Parse();

            if (parser.Errors.Count > 0)
            {
                throw new ApplicationException(string.Format("Expected no errors in the input code. Code was: {0} ErrorOutput: {1}", codeText,
                                                             parser.Errors.ErrorOutput));
            }

            var comments = parser.Lexer.SpecialTracker.RetrieveSpecials().OfType <Comment>();

            if (comments.Any(x => x.CommentText.TrimStart().StartsWith("Ignore")))
            {
                throw new ApplicationException("Ignored.");
            }

            var begin_comment = comments.FirstOrDefault(x => x.CommentText.Contains("BEGIN"));
            var end_comment   = comments.FirstOrDefault(x => x.CommentText.Contains("END"));

            if (begin_comment == null)
            {
                throw new ApplicationException("There was no comment containing the BEGIN keyword.");
            }
            if (end_comment == null)
            {
                throw new ApplicationException("There was no comment containing the END keyword.");
            }

            CloneDesc largest = null;

            cloneFinder.OnExtractedCandidate += (finder, args) =>
            {
                if (largest == null)
                {
                    largest = args.Candidate;
                }
                else if (args.Candidate.ReplacementInvocationInfo.ReplacementSectionStartLine >= begin_comment.StartPosition.Line &&
                         args.Candidate.ReplacementInvocationInfo.ReplacementSectionEndLine <= end_comment.EndPosition.Line &&
                         args.Candidate.PermutatedMethod.CountNodes() > largest.PermutatedMethod.CountNodes())
                {
                    largest = args.Candidate;
                }
            };


            var replacements = cloneFinder.GetCloneReplacements(parser.CompilationUnit);

            int clone_count = replacements.Clones.Count;

            TestLog.EmbedPlainText(clone_count + " clones found.");
            for (int i = 0; i < replacements.Clones.Count; i++)
            {
                TestLog.EmbedPlainText("   ***** Clone #" + i + " *****", replacements.Clones[i].ToString());
            }

            TestLog.EmbedPlainText("The largest permutation found within the markers was:", largest.PermutatedMethod.Print());

            var quickFixInfos = replacements.Clones.Where(Predicate(begin_comment, end_comment));

            Assert.IsTrue(quickFixInfos.Count() > 0, "None of the clones found (there were " + clone_count + ") fell inbetween the BEGIN/END markers.");

            var expected_call_snippet = begin_comment.CommentText.Substring(begin_comment.CommentText.IndexOf("BEGIN") + 5).Trim();

            if (!string.IsNullOrEmpty(expected_call_snippet))
            {
                var expected_call = ParseUtilCSharp.ParseStatement <Statement>(expected_call_snippet);
                var actual_cal    = ParseUtilCSharp.ParseStatement <Statement>(quickFixInfos.First().TextForACallToJanga);
                Assert.IsTrue(expected_call.MatchesPrint(actual_cal), "The expected call did not match the actual call.  \n\tExpected Call: " + expected_call.Print() + "\n\t" + "Actual Call: " + actual_cal.Print());
            }
        }