public void NOCTester(string treeKey)
        {
            SyntaxTree tree = _treeDictionary[treeKey];

            int result = int.Parse(treeKey.Split("-")[0]);

            CSharpCompilation compilation = CSharpCompilation.Create("Trivial")
                                            .AddReferences(
                MetadataReference.CreateFromFile(
                    typeof(object).Assembly.Location))
                                            .AddSyntaxTrees(tree);

            SemanticModel model = compilation.GetSemanticModel(tree);

            IEnumerable <ClassDeclarationSyntax> classDeclarations = MetricRunner.GetClassesFromRoot(tree.GetRoot());

            var outgoingCouplings = CouplingBetweenObjects.CalculateCouplings(new List <SyntaxTree> {
                tree
            }, compilation);


            int noc = CouplingBetweenObjects.GetCount(classDeclarations.First(), model, outgoingCouplings);

            Assert.IsTrue(noc == result);
        }
예제 #2
0
        private static ClassWithMetrics GetMetricResults(ClassDeclarationSyntax classDecl, SemanticModel semanticModel, string className, string location, Dictionary <INamedTypeSymbol, int> classExtensions, Dictionary <INamedTypeSymbol, int> classCouplings)
        {
            ClassWithMetrics classMetricResults = new ClassWithMetrics(className, location);

            var lambdaMetrics = LambdaMetrics.GetValueList(classDecl, semanticModel);

            classMetricResults.AddMetric(Measure.LambdaCount, lambdaMetrics.LambdaCount);
            classMetricResults.AddMetric(Measure.LambdaFieldVariableUsageCount, lambdaMetrics.FieldVariableUsageCount);
            classMetricResults.AddMetric(Measure.LambdaLocalVariableUsageCount, lambdaMetrics.LocalVariableUsageCount);
            classMetricResults.AddMetric(Measure.LambdaSideEffectCount, lambdaMetrics.SideEffects);


            int sourceLinesOfCode = SourceLinesOfCode.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.SourceLinesOfCode, sourceLinesOfCode);

            int commentDensity = CommentDensity.GetCount(classDecl, sourceLinesOfCode);

            classMetricResults.AddMetric(Measure.CommentDensity, commentDensity);

            int cyclomaticComplexity = CyclomaticComplexity.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.CyclomaticComplexity, cyclomaticComplexity);

            int weightedMethodsPerClass = WeightedMethodsPerClass.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.WeightedMethodsPerClass, weightedMethodsPerClass);

            int depthOfInheritanceTree = DepthOfInheritanceTree.GetCount(classDecl, semanticModel);

            classMetricResults.AddMetric(Measure.DepthOfInheritanceTree, depthOfInheritanceTree);

            int numberOfChildren = NumberOfChildren.GetCount(classDecl, semanticModel, classExtensions);

            classMetricResults.AddMetric(Measure.NumberOfChildren, numberOfChildren);

            int couplingBetweenObjects = CouplingBetweenObjects.GetCount(classDecl, semanticModel, classCouplings);

            classMetricResults.AddMetric(Measure.CouplingBetweenObjects, couplingBetweenObjects);

            int responseForAClass = ResponseForAClass.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.ResponseForAClass, responseForAClass);

            int lackOfCohesionOfMethods = LackOfCohesionOfMethods.GetCount(classDecl, semanticModel);

            classMetricResults.AddMetric(Measure.LackOfCohesionOfMethods, lackOfCohesionOfMethods);

            int sourceLinesOfLambda = SourceLinesOfLambda.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.SourceLinesOfLambda, sourceLinesOfLambda);

            int lambdaScore = (int)((double)sourceLinesOfLambda / sourceLinesOfCode * 100);

            classMetricResults.AddMetric(Measure.LambdaScore, lambdaScore);

            int unterminatedCollections = UnterminatedCollections.GetCount(classDecl, semanticModel);

            classMetricResults.AddMetric(Measure.UnterminatedCollections, unterminatedCollections);

            return(classMetricResults);
        }