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); }
public async Task <SolutionVersionWithMetrics> GetSolutionVersionWithMetrics() { using (var workspace = MSBuildWorkspace.Create()) { workspace.WorkspaceFailed += (o, e) => Console.WriteLine(e.Diagnostic.Message); var solution = await workspace.OpenSolutionAsync(_solutionLocation, new ConsoleProgressReporter()); var projects = solution.Projects.Where(x => x.FilePath.EndsWith(".csproj") && !x.Name.Contains("Tests")); // Project project = projects.First(); SolutionVersionWithMetrics solutionVersionWithMetrics = new SolutionVersionWithMetrics(); foreach (Project project in projects) { Compilation comp = project.GetCompilationAsync().Result; List <SyntaxTree> syntaxTrees = comp.SyntaxTrees.ToList(); Dictionary <INamedTypeSymbol, int> classExtensions = NumberOfChildren.GetClassExtensions(syntaxTrees, comp); Dictionary <INamedTypeSymbol, int> classCouplings = CouplingBetweenObjects.CalculateCouplings(syntaxTrees, comp); foreach (var syntaxTree in syntaxTrees) { string location = syntaxTree.FilePath; if (location.ToLower().Contains("test")) { continue; } SemanticModel semanticModel = comp.GetSemanticModel(syntaxTree); List <ClassDeclarationSyntax> classes = GetClassesFromRoot(syntaxTree.GetRoot()); string relativePath = location.Replace(@"\", "/"); var pathArr = relativePath.Split('/'); string fileAndParent = $@"{pathArr[pathArr.Length-2]}/{pathArr[pathArr.Length-1]}"; foreach (ClassDeclarationSyntax classDecl in classes) { string className = classDecl.Identifier.ValueText; string classNameWithHash = $"{className}-{fileAndParent.GetHashCode().ToString()}"; if (solutionVersionWithMetrics.ClassProcessed(classNameWithHash)) { continue; } ClassWithMetrics classMetricResults = GetMetricResults(classDecl, semanticModel, className, location, classExtensions, classCouplings); if (className == "LocalizableStrings") { Console.WriteLine(); } solutionVersionWithMetrics.AddClassWithMetrics( classNameWithHash , classMetricResults); } } } return(solutionVersionWithMetrics); } }