private bool CheckNamingOfFileAgainstTypeAndCreateWarningIfNot(ICSharpTypeDeclaration declaration) { var declaredClassName = declaration.DeclaredName; if (declaredClassName.StartsWith(Enumerable.ToArray(BDDPrefixes))) { return(false); } var currentFileName = CurrentSourceFile.GetLocation().NameWithoutExtension; if (declaration.IsPartial && currentFileName.Contains(".")) { currentFileName = currentFileName.Substring(0, currentFileName.LastIndexOf('.')); } var testClassNameFromFileName = currentFileName.Replace(".", ""); if (testClassNameFromFileName != declaredClassName) { var testingWarning = new TestClassNameDoesNotMatchFileNameWarning(declaredClassName, testClassNameFromFileName, declaration); AddHighlighting(declaration.GetNameDocumentRange(), testingWarning); return(false); } return(true); }
private void CheckClassnameInFileNameActuallyExistsAndCreateWarningIfNot(ICSharpTypeDeclaration thisDeclaration) { if (thisDeclaration.IsAbstract) { return; } var currentFileName = CurrentSourceFile.GetLocation().NameWithoutExtension; var appropriateTestClassSuffixes = TestCopSettingsManager.Instance.Settings.GetAppropriateTestClassSuffixes(currentFileName); foreach (var testClassSuffix in appropriateTestClassSuffixes) { var className = currentFileName.Split(new[] { '.' }, 2)[0].RemoveTrailing(testClassSuffix); var declaredElements = ResharperHelper.FindClass(Solution, className); var currentProject = thisDeclaration.GetProject(); var currentDeclarationNamespace = thisDeclaration.OwnerNamespaceDeclaration != null ? thisDeclaration.OwnerNamespaceDeclaration.DeclaredName : ""; var associatedProjects = currentProject.GetAssociatedProjects(CurrentSourceFile.ToProjectFile()); if (associatedProjects == null || associatedProjects.Count == 0) { var highlight = new TestFileNameWarning( "Project for this test assembly was not found - check namespace of projects", thisDeclaration); AddHighlighting(thisDeclaration.GetNameDocumentRange(), highlight); return; } var filteredDeclaredElements = new List <IClrDeclaredElement>(declaredElements); ResharperHelper.RemoveElementsNotInProjects(filteredDeclaredElements, associatedProjects.Select(p => p.Project).ToList()); if (filteredDeclaredElements.Count == 0) { string message = string.Format( "The file name begins with {0} but no matching class exists in associated project", className); foreach (var declaredElement in declaredElements) { var cls = declaredElement as TypeElement; if (cls != null) { message += string.Format("\nHas it moved to {0}.{1} ?", cls.OwnerNamespaceDeclaration(), cls.GetClrName()); } } var highlight = new TestFileNameWarning(message, thisDeclaration); AddHighlighting(thisDeclaration.GetNameDocumentRange(), highlight); return; } if (Settings.CheckTestNamespaces) { CheckClassNamespaceOfTestMatchesClassUnderTest(thisDeclaration, declaredElements); } } }