Browse Source

Added some inconsistent naming tests.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
439010a73b
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 14
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/DefaultRules.cs
  3. 40
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/IRenameNameHandler.cs
  4. 51
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs
  5. 1
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingRule.cs
  6. 133
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs
  7. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -348,6 +348,7 @@ @@ -348,6 +348,7 @@
<Compile Include="Refactoring\CodeIssues\InconsistentNamingIssue\AffectedEntity.cs" />
<Compile Include="Refactoring\CodeIssues\InconsistentNamingIssue\DefaultRules.cs" />
<Compile Include="Refactoring\CodeIssues\InconsistentNamingIssue\InconsistentNamingIssue.cs" />
<Compile Include="Refactoring\CodeIssues\InconsistentNamingIssue\IRenameNameHandler.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

14
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/DefaultRules.cs

@ -45,10 +45,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -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 @@ -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

40
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/IRenameNameHandler.cs

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
//
// RenameNameHandler.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <http://xamarin.com>
//
// 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<NamingRule> Rules { get; }
bool CanRunRenameDialog { get; }
void RunRenameDialog(IEntity entity);
void RunRenameDialog(IVariable entity);
}
}

51
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs

@ -29,11 +29,11 @@ using System.Linq; @@ -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<CodeIssue> GetIssues(BaseRefactoringContext context)
{
@ -53,8 +53,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -53,8 +53,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
rules = new List<NamingRule> (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 @@ -62,8 +63,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (!rule.IsValid(identifier.Name)) {
IList<string> 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 @@ -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 @@ -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);
}
}

1
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingRule.cs

@ -114,7 +114,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -114,7 +114,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
AffectedEntity = affectedEntity;
VisibilityMask = Modifiers.VisibilityMask;
}
static bool NoUnderscoreWithoutNumber(string id)

133
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs

@ -0,0 +1,133 @@ @@ -0,0 +1,133 @@
//
// InconsistentNamingTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <http://xamarin.com>
//
// 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);
}
}
}

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -248,6 +248,7 @@ @@ -248,6 +248,7 @@
<Compile Include="CSharp\CodeActions\RemoveRegionTests.cs" />
<Compile Include="CSharp\CodeActions\GeneratePropertyTests.cs" />
<Compile Include="CSharp\Inspector\InconsistentNamingIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\InconsistentNamingTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save