/// <summary> /// Searches the class files. /// </summary> private void SearchClassFiles() { if (!System.IO.Directory.Exists(this.Directory)) { return; } Logger.DebugFormat(" " + Resources.IndexingClasses, new DirectoryInfo(this.Directory).FullName); foreach (var file in SafeDirectorySearcher.EnumerateFiles(this.Directory, "*.cs", SearchOption.AllDirectories)) { foreach (var classInFile in SourceCodeAnalyzer.GetClassesInFile(file)) { HashSet <string> filesOfClass = null; if (!this.filesByClassName.TryGetValue(classInFile, out filesOfClass)) { filesOfClass = new HashSet <string>(); this.filesByClassName.Add(classInFile, filesOfClass); } filesOfClass.Add(file); } } }
public void GetClassesInFile_AllClassesAreReturned() { var classes = SourceCodeAnalyzer.GetClassesInFile(Path.Combine(FileManager.GetReportDirectory(), "TestClasses", "TestClass.cs")); Assert.IsNotNull(classes, "Classes must not be null."); Assert.IsTrue(classes.Contains("Test.TestClass"), "Classes does not contain root class"); Assert.IsTrue(classes.Contains("Test.TestClassNestedClass"), "Classes does not contain nested class"); }
public void FindProperty_SearchNonExistingProperty_PositionIsNull() { PropertyElement propertyElement = new PropertyElement("TestNamespace.AnalyzerTestClass", "get_DoesNotExist"); var propertyPosition = SourceCodeAnalyzer.FindSourceElement(elementClassFile, propertyElement); Assert.IsNull(propertyPosition, "PropertyPosition is not null."); }
public void FindProperty_SearchExistingProperty_PositionMustNotBeNullAndSupplyCorrectLinenumber() { PropertyElement propertyElement = new PropertyElement("TestNamespace.AnalyzerTestClass", "get_AutoProperty"); var propertyPosition = SourceCodeAnalyzer.FindSourceElement(elementClassFile, propertyElement); Assert.IsNotNull(propertyPosition, "PropertyPosition must not be null."); Assert.AreEqual(46, propertyPosition.Start, "Start line number does not match."); Assert.AreEqual(46, propertyPosition.End, "End line number does not match."); }
public void FindMethod_SearchNonExistingGenericMethod_PositionIsNull() { PartCoverMethodElement partCoverMethodElement = new PartCoverMethodElement( "TestNamespace.AnalyzerTestClass", "GenericMethod", "void (int)"); var methodPosition = SourceCodeAnalyzer.FindSourceElement(elementClassFile, partCoverMethodElement); Assert.IsNull(methodPosition, "MethodPosition is not null."); }
/// <summary> /// Searches the given source element (e.g. property) and updates the report if element can be found in source code files. /// </summary> /// <param name="sourceElement">The source element.</param> /// <param name="filenameByFileIdDictionary">Dictionary containing all files used in the report by their corresponding id.</param> /// <param name="fileIdsOfClass">The file ids of class.</param> /// <param name="reportElement">The report element.</param> /// <param name="updateReportElement">Action that updates the report element.</param> /// <param name="filesContainer">The files container.</param> /// <returns><c>true</c> if source element has been found.</returns> protected bool SearchElement( SourceElement sourceElement, Dictionary <string, string> filenameByFileIdDictionary, IEnumerable <string> fileIdsOfClass, XContainer reportElement, Action <XContainer, SourceElementPosition, string> updateReportElement, XContainer filesContainer) { Func <bool> searchSourceElement = () => { foreach (var fileId in fileIdsOfClass) { var elementPosition = SourceCodeAnalyzer.FindSourceElement(filenameByFileIdDictionary[fileId], sourceElement); if (elementPosition != null) { updateReportElement(reportElement, elementPosition, fileId); return(true); } } return(false); }; // Search files from module first if (!searchSourceElement()) { // Property has not been found in classes of module, now search the common directory if (this.ClassSearcher == null) { this.ClassSearcher = this.classSearcherFactory.CreateClassSearcher(CommonDirectorySearcher.GetCommonDirectory(filenameByFileIdDictionary.Values)); } fileIdsOfClass = this.TryToFindFileIdsOfClass( this.ClassSearcher, sourceElement.Classname, filenameByFileIdDictionary, filesContainer); // Property has not been found in common directory, now search the global directory if (!searchSourceElement()) { fileIdsOfClass = this.TryToFindFileIdsOfClass( this.globalClassSearcher, sourceElement.Classname, filenameByFileIdDictionary, filesContainer); return(searchSourceElement()); } } return(true); }
public void FindMethod_SearchExistingConstructor_PositionMustNotBeNullAndSupplyCorrectLinenumber() { PartCoverMethodElement partCoverMethodElement = new PartCoverMethodElement( "TestNamespace.AnalyzerTestClass", ".ctor", "void ()"); var methodPosition = SourceCodeAnalyzer.FindSourceElement(elementClassFile, partCoverMethodElement); Assert.IsNotNull(methodPosition, "MethodPosition must not be null."); Assert.AreEqual(10, methodPosition.Start, "Start line number does not match."); Assert.AreEqual(12, methodPosition.End, "End line number does not match."); }
public void FindMethod_SearchExistingMethod_PositionMustNotBeNullAndSupplyCorrectLinenumber() { PartCoverMethodElement partCoverMethodElement = new PartCoverMethodElement( "TestNamespace.AnalyzerTestClass", "DoSomething", "string (string, string[], System.Guid, string, string, System.Decimal, int, long, stringint, ref int, float, double, bool, unsigned byte, char, object, byte, short, unsigned int, unsigned long, unsigned short, ICSharpCode.NRefactory.Ast.INode)"); var methodPosition = SourceCodeAnalyzer.FindSourceElement(elementClassFile, partCoverMethodElement); Assert.IsNotNull(methodPosition, "MethodPosition must not be null."); Assert.AreEqual(37, methodPosition.Start, "Start line number does not match."); Assert.AreEqual(40, methodPosition.End, "End line number does not match."); }