From 439010a73b6c11cf6dcd1b14716b8b3b5b3cdda4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Fri, 23 Mar 2012 19:24:11 +0100 Subject: [PATCH] Added some inconsistent naming tests. --- .../ICSharpCode.NRefactory.CSharp.csproj | 1 + .../InconsistentNamingIssue/DefaultRules.cs | 14 +- .../IRenameNameHandler.cs | 40 ++++++ .../InconsistentNamingIssue.cs | 51 ++++--- .../InconsistentNamingIssue/NamingRule.cs | 1 - .../CodeIssues/InconsistentNamingTests.cs | 133 ++++++++++++++++++ .../ICSharpCode.NRefactory.Tests.csproj | 1 + 7 files changed, 217 insertions(+), 24 deletions(-) create mode 100644 ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/IRenameNameHandler.cs create mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index 6aca802374..d91ec01f6b 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -348,6 +348,7 @@ + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/DefaultRules.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/DefaultRules.cs index e7787c2726..b0f496b941 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/DefaultRules.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/DefaultRules.cs @@ -45,10 +45,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring }; // PascalCasing for types - yield return new NamingRule(AffectedEntity.Type) { + + yield return new NamingRule(AffectedEntity.Class | AffectedEntity.Struct | AffectedEntity.Enum | AffectedEntity.Delegate) { NamingStyle = NamingStyle.PascalCase }; - + yield return new NamingRule(AffectedEntity.Interface) { NamingStyle = NamingStyle.PascalCase, RequiredPrefixes = new [] { "I" } @@ -73,11 +74,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring yield return new NamingRule(AffectedEntity.Method) { NamingStyle = NamingStyle.PascalCase }; - + + // public fields Pascal Case yield return new NamingRule(AffectedEntity.Field) { NamingStyle = NamingStyle.PascalCase, VisibilityMask = Modifiers.Public | Modifiers.Protected | Modifiers.Internal }; + + // private fields Camel Case + yield return new NamingRule(AffectedEntity.Field) { + NamingStyle = NamingStyle.CamelCase, + VisibilityMask = Modifiers.Private + }; yield return new NamingRule(AffectedEntity.Property) { NamingStyle = NamingStyle.PascalCase diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/IRenameNameHandler.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/IRenameNameHandler.cs new file mode 100644 index 0000000000..1845c8ce02 --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/IRenameNameHandler.cs @@ -0,0 +1,40 @@ +// +// RenameNameHandler.cs +// +// Author: +// Mike Krüger +// +// Copyright (c) 2012 Xamarin +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Collections.Generic; +using ICSharpCode.NRefactory.TypeSystem; + +namespace ICSharpCode.NRefactory.CSharp.Refactoring +{ + public interface IRenameNameHandler + { + IEnumerable Rules { get; } + bool CanRunRenameDialog { get; } + void RunRenameDialog(IEntity entity); + void RunRenameDialog(IVariable entity); + } +} + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs index 1e11bf5dad..fca9c25ea0 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs @@ -29,11 +29,11 @@ using System.Linq; namespace ICSharpCode.NRefactory.CSharp.Refactoring { -/* [IssueDescription("Inconsistent Naming", + [IssueDescription("Inconsistent Naming", Description = "Name doesn't match the defined style for this entity.", Category = IssueCategories.ConstraintViolations, - Severity = Severity.Warning)]*/ - public class InconsistentNamingIssue + Severity = Severity.Warning)] + public class InconsistentNamingIssue : ICodeIssueProvider { public IEnumerable GetIssues(BaseRefactoringContext context) { @@ -53,8 +53,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring rules = new List (DefaultRules.Rules); } - void CheckName(AffectedEntity entity, Identifier identifier) + void CheckName(AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { + Console.WriteLine(entity); foreach (var rule in rules) { if (!rule.AffectedEntity.HasFlag(entity)) { continue; @@ -62,8 +63,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring if (!rule.IsValid(identifier.Name)) { IList suggestedNames; var msg = rule.GetErrorMessage(ctx, identifier.Name, out suggestedNames); - AddIssue(identifier, msg, suggestedNames.Select(n => new CodeAction(string.Format(ctx.TranslateString("Rename to '{0}'"), n), (Script script) => { + // TODO: Real rename. script.Replace(identifier, Identifier.Create(n)); }))); } @@ -74,8 +75,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring { base.VisitNamespaceDeclaration(namespaceDeclaration); foreach (var id in namespaceDeclaration.Identifiers) { - CheckName(AffectedEntity.Namespace, id); + CheckName(AffectedEntity.Namespace, id, Modifiers.None); + } + } + + Modifiers GetAccessibiltiy(EntityDeclaration decl, Modifiers defaultModifier) + { + var accessibility = (decl.Modifiers & Modifiers.VisibilityMask); + if (accessibility == Modifiers.None) { + return defaultModifier; } + return accessibility; } public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration) @@ -99,84 +109,85 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring throw new System.ArgumentOutOfRangeException(); } - CheckName(entity, typeDeclaration.NameToken); + CheckName(entity, typeDeclaration.NameToken, GetAccessibiltiy(typeDeclaration, typeDeclaration.Parent is TypeDeclaration ? Modifiers.Private : Modifiers.Internal)); } public override void VisitDelegateDeclaration(DelegateDeclaration delegateDeclaration) { base.VisitDelegateDeclaration(delegateDeclaration); - CheckName(AffectedEntity.Delegate, delegateDeclaration.NameToken); + CheckName(AffectedEntity.Delegate, delegateDeclaration.NameToken, GetAccessibiltiy(delegateDeclaration, delegateDeclaration.Parent is TypeDeclaration ? Modifiers.Private : Modifiers.Internal)); } public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration) { base.VisitPropertyDeclaration(propertyDeclaration); - CheckName(AffectedEntity.Property, propertyDeclaration.NameToken); + CheckName(AffectedEntity.Property, propertyDeclaration.NameToken, GetAccessibiltiy(propertyDeclaration, Modifiers.Private)); } public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) { base.VisitMethodDeclaration(methodDeclaration); - CheckName(AffectedEntity.Method, methodDeclaration.NameToken); + CheckName(AffectedEntity.Method, methodDeclaration.NameToken, GetAccessibiltiy(methodDeclaration, Modifiers.Private)); } public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration) { base.VisitFieldDeclaration(fieldDeclaration); - CheckName(AffectedEntity.Field, fieldDeclaration.NameToken); + foreach (var init in fieldDeclaration.Variables) { + CheckName(AffectedEntity.Field, init.NameToken, GetAccessibiltiy(fieldDeclaration, Modifiers.Private)); + } } public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration) { base.VisitFixedFieldDeclaration(fixedFieldDeclaration); - CheckName(AffectedEntity.Field, fixedFieldDeclaration.NameToken); + CheckName(AffectedEntity.Field, fixedFieldDeclaration.NameToken, GetAccessibiltiy(fixedFieldDeclaration, Modifiers.Private)); } public override void VisitEventDeclaration(EventDeclaration eventDeclaration) { base.VisitEventDeclaration(eventDeclaration); - CheckName(AffectedEntity.Event, eventDeclaration.NameToken); foreach (var init in eventDeclaration.Variables) { - CheckName(AffectedEntity.Event, init.NameToken); + CheckName(AffectedEntity.Event, init.NameToken, GetAccessibiltiy(eventDeclaration, Modifiers.Private)); } } public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration) { base.VisitCustomEventDeclaration(eventDeclaration); - CheckName(AffectedEntity.Event, eventDeclaration.NameToken); + CheckName(AffectedEntity.Event, eventDeclaration.NameToken, GetAccessibiltiy(eventDeclaration, Modifiers.Private)); } public override void VisitEnumMemberDeclaration(EnumMemberDeclaration enumMemberDeclaration) { base.VisitEnumMemberDeclaration(enumMemberDeclaration); - CheckName(AffectedEntity.EnumMember, enumMemberDeclaration.NameToken); + CheckName(AffectedEntity.EnumMember, enumMemberDeclaration.NameToken, GetAccessibiltiy(enumMemberDeclaration, Modifiers.Private)); } public override void VisitParameterDeclaration(ParameterDeclaration parameterDeclaration) { base.VisitParameterDeclaration(parameterDeclaration); - CheckName(parameterDeclaration.Parent is LambdaExpression ? AffectedEntity.LambdaParameter : AffectedEntity.Parameter, parameterDeclaration.NameToken); + CheckName(parameterDeclaration.Parent is LambdaExpression ? AffectedEntity.LambdaParameter : AffectedEntity.Parameter, parameterDeclaration.NameToken, Modifiers.None); } public override void VisitTypeParameterDeclaration(TypeParameterDeclaration typeParameterDeclaration) { base.VisitTypeParameterDeclaration(typeParameterDeclaration); - CheckName(AffectedEntity.TypeParameter, typeParameterDeclaration.NameToken); + CheckName(AffectedEntity.TypeParameter, typeParameterDeclaration.NameToken, Modifiers.None); } public override void VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement) { base.VisitVariableDeclarationStatement(variableDeclarationStatement); foreach (var init in variableDeclarationStatement.Variables) { - CheckName(AffectedEntity.LocalVariable, init.NameToken); + CheckName(AffectedEntity.LocalVariable, init.NameToken, Modifiers.None); } } public override void VisitLabelStatement(LabelStatement labelStatement) { base.VisitLabelStatement(labelStatement); - CheckName(AffectedEntity.Label, labelStatement.LabelToken); + CheckName(AffectedEntity.Label, labelStatement.LabelToken, Modifiers.None); } } diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingRule.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingRule.cs index 436af955f0..610421a70d 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingRule.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingRule.cs @@ -114,7 +114,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring { AffectedEntity = affectedEntity; VisibilityMask = Modifiers.VisibilityMask; - } static bool NoUnderscoreWithoutNumber(string id) diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs new file mode 100644 index 0000000000..fba2363e7e --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs @@ -0,0 +1,133 @@ +// +// InconsistentNamingTests.cs +// +// Author: +// Mike Krüger +// +// Copyright (c) 2012 Xamarin +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using NUnit.Framework; +using ICSharpCode.NRefactory.CSharp.Refactoring; +using ICSharpCode.NRefactory.CSharp.CodeActions; + +namespace ICSharpCode.NRefactory.CSharp.CodeIssues +{ + [TestFixture] + public class InconsistentNamingTests : InspectionActionTestBase + { + void CheckNaming (string input, string output) + { + TestRefactoringContext context; + var issues = GetIssues (new InconsistentNamingIssue (), input, out context); + Assert.Greater (issues.Count, 0); + CheckFix (context, issues [0], output); + } + + [Test] + public void TestNamespaceName () + { + var input = @"namespace anIssue {}"; + var output = @"namespace AnIssue {}"; + CheckNaming (input, output); + } + + [Test] + public void TestClassName () + { + var input = @"class anIssue {}"; + var output = @"class AnIssue {}"; + CheckNaming (input, output); + } + + [Test] + public void TestStructName () + { + var input = @"struct anIssue {}"; + var output = @"struct AnIssue {}"; + CheckNaming (input, output); + } + + [Test] + public void TestInterfaceName () + { + var input = @"interface anIssue {}"; + var output = @"interface IAnIssue {}"; + CheckNaming (input, output); + } + + [Test] + public void TestEnumName () + { + var input = @"enum anIssue {}"; + var output = @"enum AnIssue {}"; + CheckNaming (input, output); + } + + [Test] + public void TestDelegateName () + { + var input = @"delegate void anIssue ();"; + var output = @"delegate void AnIssue ();"; + CheckNaming (input, output); + } + + [Test] + public void TestPrivateFieldName () + { + var input = @"class AClass { int Field; }"; + var output = @"class AClass { int field; }"; + CheckNaming (input, output); + } + + [Test] + public void TestPublicFieldName () + { + var input = @"class AClass { public int field; }"; + var output = @"class AClass { public int Field; }"; + CheckNaming (input, output); + } + + [Test] + public void TestMethodName () + { + var input = @"class AClass { int method () {} }"; + var output = @"class AClass { int Method () {} }"; + CheckNaming (input, output); + } + + [Test] + public void TestPropertyName () + { + var input = @"class AClass { int property { get; set; } }"; + var output = @"class AClass { int Property { get; set; } }"; + CheckNaming (input, output); + } + + [Test] + public void TestParameterName () + { + var input = @"class AClass { int Method (int Param) {} }"; + var output = @"class AClass { int Method (int param) {} }"; + CheckNaming (input, output); + } + } +} + diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index 5285417f38..cade54b214 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -248,6 +248,7 @@ +