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 bool CheckNamingOfTypeEndsWithTestSuffix(ICSharpTypeDeclaration declaration) { if (declaration.IsAbstract) { return(true); } var declaredClassName = declaration.DeclaredName; if (!declaredClassName.StartsWith(Enumerable.ToArray(BDDPrefixes))) { if (!declaredClassName.EndsWith(Settings.TestClassSuffixes())) { var testingWarning = new TestClassNameSuffixWarning(Settings.TestClassSuffix, declaration); AddHighlighting(declaration.GetNameDocumentRange(), testingWarning); return(false); } } return(true); }
private void CheckClassNamespaceOfTestMatchesClassUnderTest(ICSharpTypeDeclaration thisDeclaration, List <IClrDeclaredElement> declaredElements) { var thisProject = thisDeclaration.GetProject(); if (thisProject == null) { return; } var associatedProject = thisProject.GetAssociatedProjects(CurrentSourceFile.ToProjectFile()).FirstOrDefault(); if (associatedProject == null) { return; } ResharperHelper.RemoveElementsNotInProjects(declaredElements, new [] { associatedProject.Project }); var thisProjectsDefaultNamespace = thisProject.GetDefaultNamespace(); if (string.IsNullOrEmpty(thisProjectsDefaultNamespace)) { return; } var associatedProjectsDefaultNameSpace = associatedProject.Project.GetDefaultNamespace(); if (string.IsNullOrEmpty(associatedProjectsDefaultNameSpace)) { return; } //var nsToBeFoundShouldBe = associatedProject.Project.GetDefaultNamespace()+associatedProject.SubNamespace; var nsToBeFoundShouldBe = associatedProject.FullNamespace(); //Lookup the namespaces of the declaredElements we've found that possibly match this test IList <string> foundNameSpaces = new List <string>(); foreach (var declaredTestElement in declaredElements) { var cls = declaredTestElement as TypeElement; if (cls == null) { continue; } var ns = cls.OwnerNamespaceDeclaration(); if (nsToBeFoundShouldBe == ns) { return;//found a match ! } foundNameSpaces.Add(ns); } foreach (var ns in foundNameSpaces) { if (ns.StartsWith(associatedProjectsDefaultNameSpace)) { //TODO: Review this can be probably be replaced with associatedProject method calls var targetsubNameSpace = ns.Substring(associatedProjectsDefaultNameSpace.Length).TrimStart(new[] { '.' }); string suggestedNameSpace = thisProjectsDefaultNamespace.AppendIfNotNull(".", targetsubNameSpace); var targetFolder = thisProject.Location.Combine(targetsubNameSpace.Replace(".", @"\")); var highlight = new TestFileNameSpaceWarning(CurrentSourceFile.ToProjectFile(), thisDeclaration, suggestedNameSpace , thisProject, targetFolder); AddHighlighting(thisDeclaration.GetNameDocumentRange(), highlight); } } }
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); } } }