Browse Source

GenerateProperty: do not propose to generate a setter if the field is readonly.

newNRvisualizers
Daniel Grunwald 13 years ago
parent
commit
0f82920267
  1. 4
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs
  2. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs
  3. 28
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/GeneratePropertyTests.cs

4
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs

@ -1,4 +1,4 @@
// //
// GenerateProperty.cs // GenerateProperty.cs
// //
// Author: // Author:
@ -51,7 +51,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
} }
var field = initializer.Parent as FieldDeclaration; var field = initializer.Parent as FieldDeclaration;
if (field == null) { if (field == null || field.HasModifier(Modifiers.Readonly) || field.HasModifier(Modifiers.Const)) {
yield break; yield break;
} }
var resolveResult = context.Resolve(initializer) as MemberResolveResult; var resolveResult = context.Resolve(initializer) as MemberResolveResult;

6
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs

@ -84,7 +84,11 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
protected static void TestWrongContext<T> (string input) where T : ICodeActionProvider, new () protected static void TestWrongContext<T> (string input) where T : ICodeActionProvider, new ()
{ {
ICodeActionProvider action = new T (); TestWrongContext (new T(), input);
}
protected static void TestWrongContext (ICodeActionProvider action, string input)
{
var context = TestRefactoringContext.Create (input); var context = TestRefactoringContext.Create (input);
bool isValid = action.GetActions (context).Any (); bool isValid = action.GetActions (context).Any ();
if (!isValid) if (!isValid)

28
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/GeneratePropertyTests.cs

@ -1,4 +1,4 @@
// //
// GeneratePropertyTests.cs // GeneratePropertyTests.cs
// //
// Author: // Author:
@ -107,5 +107,31 @@ class TestClass
int myField, myOtherField; int myField, myOtherField;
}"); }");
} }
[Test]
public void CannotGeneratePropertyForReadOnlyField()
{
TestWrongContext (
new GeneratePropertyAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" readonly int $myField;" + Environment.NewLine +
"}"
);
}
[Test]
public void CannotGeneratePropertyForConst()
{
TestWrongContext (
new GeneratePropertyAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" const int $myField = 0;" + Environment.NewLine +
"}"
);
}
} }
} }
Loading…
Cancel
Save