コード例 #1
0
ファイル: Methods.cs プロジェクト: harold4/bitdiffer
        public void Method_Override()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "ChildClass", "SomeMethod");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public override void SomeMethod()", mi.ToString());
        }
コード例 #2
0
        public void Generics_Parameters_Extraction()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "GenericClass<T>", "GenericListParameter");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public string GenericListParameter(System.Collections.Generic.List<T>)", mi.ToString());
        }
コード例 #3
0
ファイル: Operators.cs プロジェクト: harold4/bitdiffer
        public void Operator_Extraction()
        {
            MethodDetail mi = ExtractOperator(Subjects.One, "Operators", "op_Equality");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public static bool op_Equality(BitDiffer.Tests.Subject.Operators, BitDiffer.Tests.Subject.Operators)", mi.ToString());
        }
コード例 #4
0
ファイル: Methods.cs プロジェクト: harold4/bitdiffer
        public void Method_Virtual()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "DerivedClass", "SomeMethod");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public virtual void SomeMethod()", mi.ToString());
        }
コード例 #5
0
        public void Generics_Method_Extraction()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "NormalClass", "Foo<T>");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public string Foo<T>(T)", mi.ToString());
        }
コード例 #6
0
ファイル: Methods.cs プロジェクト: harold4/bitdiffer
        public void Method_WithAttributes()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "BasicClass", "MethodWithAttribute");

            Assert.AreEqual(Status.Present, mi.Status);
            CheckForAttribute(mi);
        }
コード例 #7
0
        private Task ApplyEvent(IEvent @event, Type eventType)
        {
            var methodDetail = methodCache.GetOrAdd(eventType, (t) =>
            {
                var aggregateType         = GetAggregateType();
                var aggregateTypeDetail   = TypeAnalyzer.GetTypeDetail(aggregateType);
                MethodDetail methodDetail = null;
                foreach (var method in aggregateTypeDetail.MethodDetails)
                {
                    if (!method.MethodInfo.IsStatic && method.ParametersInfo.Count == 1 && method.ParametersInfo[0].ParameterType == t)
                    {
                        if (methodDetail != null)
                        {
                            throw new Exception($"Multiple aggregate event methods found in {aggregateType.Name} to accept {t.Name}");
                        }
                        methodDetail = method;
                    }
                }
                if (methodDetail == null)
                {
                    throw new Exception($"No aggregate event methods found in {aggregateType.Name} to accept {t.Name}");
                }
                return(methodDetail);
            });

            return(methodDetail.CallerAsync(this, new object[] { @event }));
        }
コード例 #8
0
        public void Generics_Method_Restricted_Extraction()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "NormalClass", "FooRestricted<T>");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public string FooRestricted<T>(T) where T : System.ComponentModel.INotifyPropertyChanged", mi.ToString());
        }
コード例 #9
0
        public void Generics_Nullables_Extraction()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "NormalClass", "NullableParameters");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public string NullableParameters(bool?, int?)", mi.ToString());
        }
コード例 #10
0
        public static string GetJSON(ICallHirearchy ch, MethodDetail method)
        {
            NodeCollection collection = new NodeCollection();
            var            items      = ch.TracePath(method);

            if (items == null)
            {
                Console.WriteLine("No Callers detected");
                return(collection.ToString());
            }

            List <MethodDetail> itemsDisplayed = new List <MethodDetail>();

            foreach (var item in items)
            {
                Node n = null;
                if (item.Count == 1)
                {
                    n = GetNode(collection, item[0]);
                }
                else
                {
                    for (int i = 1; i < item.Count; i++)
                    {
                        n = GetNode(collection, item[i - 1]);
                        n.TraverseTo.Add(GetNodeKey(item[i]));
                    }
                }
            }

            return(collection.ToString());
        }
コード例 #11
0
ファイル: Methods.cs プロジェクト: harold4/bitdiffer
        public void Method_Static()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "StaticClass", "SimpleStaticMethod");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public static void SimpleStaticMethod()", mi.ToString());
        }
コード例 #12
0
ファイル: Methods.cs プロジェクト: harold4/bitdiffer
        public void Method_Abstract()
        {
            MethodDetail mi = ExtractMethod(Subjects.One, "AbstractClass", "SimpleAbstractMethod");

            Assert.AreEqual(Status.Present, mi.Status);
            Assert.AreEqual("public abstract void SimpleAbstractMethod()", mi.ToString());
        }
コード例 #13
0
        protected MethodDetail ExtractOperator(string assemblyFile, string typeName, string OperatorName, DiffConfig config)
        {
            ClassDetail ci = ExtractClass(assemblyFile, typeName, config);

            MethodDetail value = ListOperations.FindOrReturnMissing(ci.FilterChildren <OperatorDetail>(), OperatorName);

            Log.Verbose("Extracted Operator : {0}", value);
            return(value);
        }
コード例 #14
0
ファイル: MutantExecutor.cs プロジェクト: mdaniyalkhan/MuTest
        private static void PrintMethodSummary(MethodDetail method, StringBuilder mutationProcessLog)
        {
            if (method.Mutants.Any())
            {
                method.CalculateMutationScore();

                mutationProcessLog.Append(method.MutationScore.ToString().PrintWithPreTagWithMarginImportant(color: Colors.BlueViolet));
                mutationProcessLog.Append(
                    $"Method Coverage: Mutation({method.MutationScore.Mutation}) Line({method.LinesCovered}{method.LineCoverage}) Branch({method.BlocksCovered}{method.BranchCoverage})"
                    .PrintWithPreTagWithMarginImportant(color: Colors.Blue));
            }
        }
コード例 #15
0
        private static Node GetNode(NodeCollection collection, MethodDetail item)
        {
            Node   n;
            string key = GetNodeKey(item);

            if (!collection.TryGetValue(key, out n))
            {
                n = new Node(key);
                collection.Add(n.MethodName, n);
            }
            return(n);
        }
コード例 #16
0
        public List <MethodDetail> GetMethodDetails(string sourceCode)
        {
            var methods = GetTheMethods(sourceCode);

            var methodDetails = new List <MethodDetail>();

            // This is example code, you don't want to really use it this way
            var dummyMethodOne = new MethodDetail();

            //            dummyMethodOne.MethodName = "Not the Name";
            //          dummyMethodOne.MethodAccessModifier = AccessModifier.Private;
            //        methodDetails.Add(dummyMethodOne);

            foreach (var method in methods)
            {
                System.Diagnostics.Debug.WriteLine(method.Identifier.Text);
                Console.WriteLine("Access Modifier: {0} Method {1}", GetModiferForMethod(method), method.Identifier);
                dummyMethodOne.MethodName           = method.Identifier.Text;
                dummyMethodOne.MethodAccessModifier = GetModiferForMethod(method);
                methodDetails.Add(dummyMethodOne);
            }

            return(methodDetails);
        }
コード例 #17
0
        private void AssertOnlyChange(ClassDetail cd, string declaration, ChangeType changeType)
        {
            bool found = false;

            foreach (ICanAlign child in cd.Children)
            {
                if (child is MethodDetail)
                {
                    MethodDetail method = (MethodDetail)child;

                    if ((method.GetTextDeclaration() == declaration) || (((MethodDetail)method.NavigateBackward).GetTextDeclaration() == declaration))
                    {
                        Assert.AreEqual(changeType, method.Change);
                        found = true;
                    }
                    else
                    {
                        Assert.AreEqual(ChangeType.None, method.Change);
                    }
                }
            }

            Assert.IsTrue(found, string.Format("Method '{0}' was not found in {1}", declaration, cd.Name));
        }
コード例 #18
0
 public void AddGetProperty(String name, MethodDetail detail) {
    Property property = properties.get(name);
    if(property == null) {
       property = new Property();
       properties.put(name, property);
    }
    if(property. != null) {
       throw new IllegalStateException("The property '"+name+"' is defined twice in "+details.FullyQualifiedName);
    }
    if(detail == null) {
       throw new IllegalStateException("Can not set a null property");
    }
    property.AddGetMethod(detail);
 }
コード例 #19
0
 public void AddSetMethod(MethodDetail detail) {
    this.set = detail;
 }
コード例 #20
0
 public void AddGetMethod(MethodDetail detail) {
    this.get = detail;
 }
コード例 #21
0
        public void FindCoverage(SourceClassDetail source, CoverageDS codeCoverage)
        {
            if (codeCoverage != null)
            {
                source.ExternalCoveredClasses.Clear();
                source.ExternalCoveredClasses.AddRange(FindExternalCoveredClasses(source, codeCoverage));
                var parentClassName = string.Join(".", source.Claz.Syntax.Ancestors <ClassDeclarationSyntax>().Select(x => x.ClassName()));
                var className       = $"{parentClassName}.{source.Claz.Syntax.ClassName()}".TrimStart('.');
                var coverages       = codeCoverage
                                      .Class
                                      .Where(x => x.NamespaceTableRow.NamespaceName == source.Claz.Syntax.NameSpace() &&
                                             (x.ClassName == className ||
                                              x.ClassName.StartsWith($"{className}.{GenericMethodStart}") ||
                                              x.ClassName.StartsWith($"{className}{GenericMethodStart}"))).ToList();

                if (coverages.Any())
                {
                    source.Coverage = new Coverage
                    {
                        LinesCovered     = (uint)coverages.Sum(x => x.LinesCovered),
                        LinesNotCovered  = (uint)coverages.Sum(x => x.LinesNotCovered),
                        BlocksCovered    = (uint)coverages.Sum(x => x.BlocksCovered),
                        BlocksNotCovered = (uint)coverages.Sum(x => x.BlocksNotCovered)
                    };

                    var methodsWithCoverage = new List <MethodDetail>();
                    PrintClassCoverage(source, className);
                    foreach (var coverage in coverages)
                    {
                        var coverageClassName = coverage.ClassName;
                        var methods           = codeCoverage
                                                .Method.Where(x => x.ClassKeyName == coverage.ClassKeyName)
                                                .ToList();

                        foreach (CoverageDSPriv.MethodRow mCoverage in methods)
                        {
                            var methodFullName = mCoverage.MethodFullName;
                            if (methodFullName.StartsWith(GenericMethodStart) && methodFullName.Contains(GenericMethodEnd))
                            {
                                var startIndex = methodFullName.IndexOf(GenericMethodStart, StringComparison.InvariantCulture) + 1;
                                var length     = methodFullName.IndexOf(GenericMethodEnd, StringComparison.InvariantCulture) - 1;
                                methodFullName = $"{methodFullName.Substring(startIndex, length)}(";
                            }

                            var numberOfOverloadedMethods = source.MethodDetails.Where(x =>
                                                                                       methodFullName.StartsWith($"{x.Method.MethodName()}(") ||
                                                                                       methodFullName.StartsWith($"set_{x.Method.MethodName()}(") ||
                                                                                       methodFullName.StartsWith($"get_{x.Method.MethodName()}(")).ToList();
                            MethodDetail methodDetail = null;
                            if (numberOfOverloadedMethods.Count == 1)
                            {
                                methodDetail = numberOfOverloadedMethods.First();
                            }

                            if (methodDetail == null)
                            {
                                methodDetail = source.MethodDetails
                                               .FirstOrDefault(x =>
                                                               x.Method.MethodWithParameterTypes() == methodFullName.Replace("System.", string.Empty));
                            }

                            string methodName;
                            if (methodDetail == null && coverageClassName.Contains(GenericMethodStart))
                            {
                                var startIndex = coverageClassName.IndexOf(GenericMethodStart, StringComparison.InvariantCulture);
                                var endIndex   = coverageClassName.IndexOf(GenericMethodEnd, StringComparison.InvariantCulture);
                                methodName   = coverageClassName.Substring(startIndex + 1, endIndex - startIndex - 1);
                                methodDetail = source.MethodDetails.FirstOrDefault(x => x.Method.MethodName().Equals(methodName));
                            }

                            if (methodDetail != null)
                            {
                                if (methodDetail.Coverage == null)
                                {
                                    methodDetail.Coverage = new Coverage();
                                }

                                methodDetail.Coverage = new Coverage
                                {
                                    LinesCovered     = methodDetail.Coverage.LinesCovered + mCoverage.LinesCovered,
                                    LinesNotCovered  = methodDetail.Coverage.LinesNotCovered + mCoverage.LinesNotCovered,
                                    BlocksCovered    = methodDetail.Coverage.BlocksCovered + mCoverage.BlocksCovered,
                                    BlocksNotCovered = methodDetail.Coverage.BlocksNotCovered + mCoverage.BlocksNotCovered
                                };

                                methodDetail.Lines.AddRange(mCoverage.GetLinesRows());
                                methodsWithCoverage.Add(methodDetail);
                            }
                        }
                    }

                    methodsWithCoverage = methodsWithCoverage.GroupBy(x => x.Method.MethodWithParameterTypes()).Select(x =>
                    {
                        var methodDetail = new MethodDetail
                        {
                            Coverage   = x.Last().Coverage,
                            Method     = x.First().Method,
                            MethodName = x.First().MethodName
                        };

                        methodDetail.Lines.AddRange(x.First().Lines);
                        return(methodDetail);
                    }).ToList();
                    foreach (var methodDetail in methodsWithCoverage)
                    {
                        var methodName = methodDetail.Method.MethodWithParameterTypes();
                        Output += $"{methodName} {methodDetail.Coverage.ToString().Print(color: Colors.BlueViolet)}".PrintWithPreTagWithMarginImportant();
                    }
                }

                PrintExternalCoveredClasses(source);
            }
        }
コード例 #22
0
        private StringBuilder FindMissedMutants(SyntaxNode testClassDeclaration,
                                                MethodDetail methodDetail,
                                                IList <AssertsAnalyzer.AssertMapper> asserts)
        {
            var    testBuilder           = new StringBuilder(Constants.PreStartWithMargin);
            double numberOfMutants       = 0;
            double numberOfMissedMutants = 0;
            var    testClass             = testClassDeclaration.ToFullString();

            foreach (var methodNode in methodDetail.Method.DescendantNodes())
            {
                if (methodNode is InvocationExpressionSyntax invocation)
                {
                    foreach (var argument in invocation.ArgumentList.Arguments)
                    {
                        numberOfMutants++;
                        var argExpression    = argument.Expression.ToString().Encode();
                        var methodExpression = invocation.GetMethod().ToString().Encode().Print(color: Constants.Colors.Red);
                        if ((argExpression.StartsWith(@"""") ||
                             argExpression.StartsWith(@"'") ||
                             argExpression.IsNumeric()) &&
                            !testClass.Contains(argExpression))
                        {
                            testBuilder
                            .AppendLine($"{(argExpression + " - ").Print(color: Constants.Colors.Blue)}{methodExpression}");
                            numberOfMissedMutants++;
                        }
                        else if (argExpression.Contains(".") &&
                                 !testClass.Contains(argExpression.Split('.').Last()))
                        {
                            testBuilder
                            .AppendLine(
                                $"{(argExpression + " - ").Print(color: Constants.Colors.Blue)}{methodExpression}");
                            numberOfMissedMutants++;
                        }
                    }
                }
            }

            foreach (var assertMapper in asserts)
            {
                numberOfMutants++;
                var expected = assertMapper.Right.RemoveUnnecessaryWords();
                var actual   = assertMapper.Left.RemoveUnnecessaryWords();

                var isNullOrWhiteSpace = string.IsNullOrWhiteSpace(expected.Replace("\"", string.Empty)) || expected == "null";
                if (isNullOrWhiteSpace)
                {
                    if (!testClass.Contains(actual))
                    {
                        testBuilder.AppendLine(assertMapper.Print().Encode().Print(color: Constants.Colors.Red));
                        numberOfMissedMutants++;
                    }
                }
                else if (!testClass.Contains(expected))
                {
                    testBuilder.AppendLine(assertMapper.Print().Encode().Print(color: Constants.Colors.Red));
                    numberOfMissedMutants++;
                }
            }

            var coverage = 1 - numberOfMissedMutants / numberOfMutants;

            testBuilder.AppendLine($"Mutants Coverage: [{numberOfMissedMutants} Missed/{numberOfMutants}]({coverage:P})".PrintWithPreTagImportant(color: Constants.Colors.Orange));
            testBuilder.AppendLine(Constants.PreEnd);

            _totalNumberOfMutants       += numberOfMutants;
            _totalNumberOfMissedMutants += numberOfMissedMutants;
            return(testBuilder);
        }
コード例 #23
0
        private static string GetNodeKey(MethodDetail item)
        {
            string key = item.FriendlyName.Replace(item.DeclaringTypeName.FullName + "::", string.Empty);

            return(key);
        }
コード例 #24
0
        private void FilterMutants(MethodDetail method)
        {
            if (MutantsAtSpecificLines.Any())
            {
                foreach (var mutant in method.Mutants)
                {
                    if (!MutantsAtSpecificLines.Contains(mutant.Mutation.Location.GetValueOrDefault()))
                    {
                        mutant.ResultStatus = MutantStatus.Skipped;
                    }
                }

                return;
            }

            var idList = new List <string>();

            if (!string.IsNullOrWhiteSpace(MutantFilterId))
            {
                if (MutantFilterId.Contains(','))
                {
                    idList.AddRange(MutantFilterId.Split(','));
                }
                else
                {
                    idList.Add(MutantFilterId.Trim());
                }
            }

            foreach (var key in idList)
            {
                var mutant = method.Mutants.FirstOrDefault(x => x.Id.ToString().Equals(key));
                if (mutant != null)
                {
                    mutant.ResultStatus = MutantStatus.Skipped;
                }
            }

            var regExList = new List <string>();

            if (!string.IsNullOrWhiteSpace(MutantFilterRegEx))
            {
                if (MutantFilterRegEx.Contains(','))
                {
                    regExList.AddRange(MutantFilterRegEx.Split(','));
                }
                else
                {
                    regExList.Add(MutantFilterRegEx);
                }
            }

            var spRegExList = new List <string>();

            if (!string.IsNullOrWhiteSpace(SpecificFilterRegEx))
            {
                if (SpecificFilterRegEx.Contains(','))
                {
                    spRegExList.AddRange(SpecificFilterRegEx.Split(','));
                }
                else
                {
                    spRegExList.Add(SpecificFilterRegEx);
                }
            }

            foreach (var regEx in regExList)
            {
                method.Mutants.ForEach(x =>
                {
                    if (x.ResultStatus != MutantStatus.Skipped)
                    {
                        if (Regex.IsMatch(x.Mutation.OriginalNode.ToString(), regEx))
                        {
                            x.ResultStatus = MutantStatus.Skipped;
                        }
                        else
                        {
                            var parentInvocation = x
                                                   .Mutation
                                                   .OriginalNode
                                                   .Ancestors <InvocationExpressionSyntax>().FirstOrDefault();

                            var objInitializer = x
                                                 .Mutation
                                                 .OriginalNode
                                                 .Ancestors <ObjectCreationExpressionSyntax>().FirstOrDefault();

                            if (parentInvocation != null && Regex.IsMatch(parentInvocation.ToString(), regEx) ||
                                objInitializer != null && Regex.IsMatch(objInitializer.ToString(), regEx))
                            {
                                x.ResultStatus = MutantStatus.Skipped;
                            }
                        }
                    }
                });
            }

            foreach (var regEx in spRegExList)
            {
                method.Mutants.ForEach(x =>
                {
                    if (x.ResultStatus != MutantStatus.Skipped)
                    {
                        var parentInvocation = x
                                               .Mutation
                                               .OriginalNode
                                               .Ancestors <InvocationExpressionSyntax>().FirstOrDefault();

                        var objInitializer = x
                                             .Mutation
                                             .OriginalNode
                                             .Ancestors <ObjectCreationExpressionSyntax>().FirstOrDefault();

                        if (!(parentInvocation != null && Regex.IsMatch(parentInvocation.ToString(), regEx) ||
                              objInitializer != null && Regex.IsMatch(objInitializer.ToString(), regEx) ||
                              Regex.IsMatch(x.Mutation.OriginalNode.ToString(), regEx)))
                        {
                            x.ResultStatus = MutantStatus.Skipped;
                        }
                    }
                });
            }
        }