Skip to content
Browse files

Implement Getter and Setter properties on EnvDTE.CodeProperty.

  • Loading branch information...
1 parent bbf32f7 commit 1dbdbf2e647e02ec21dae502006aa5d469431644 @mrward mrward committed Jun 3, 2012
View
3 src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
@@ -87,6 +87,8 @@
<Compile Include="Src\EnvDTE\CodeElementsInNamespace.cs" />
<Compile Include="Src\EnvDTE\CodeElementsList.cs" />
<Compile Include="Src\EnvDTE\CodeFunction.cs" />
+ <Compile Include="Src\EnvDTE\CodeGetterFunction.cs" />
+ <Compile Include="Src\EnvDTE\CodeGetterOrSetterFunction.cs" />
<Compile Include="Src\EnvDTE\CodeInterface.cs" />
<Compile Include="Src\EnvDTE\CodeModel.cs" />
<Compile Include="Src\EnvDTE\CodeModelLanguageConstants.cs" />
@@ -95,6 +97,7 @@
<Compile Include="Src\EnvDTE\CodeProperty.cs" />
<Compile Include="Src\EnvDTE\CodeProperty2.cs" />
<Compile Include="Src\EnvDTE\CodePropertyParameters.cs" />
+ <Compile Include="Src\EnvDTE\CodeSetterFunction.cs" />
<Compile Include="Src\EnvDTE\CodeStruct.cs" />
<Compile Include="Src\EnvDTE\CodeType.cs" />
<Compile Include="Src\EnvDTE\CodeTypeBaseTypes.cs" />
View
7 src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeFunction.cs
@@ -17,7 +17,12 @@ public CodeFunction()
{
}
- public vsCMAccess Access {
+ public CodeFunction(IProperty property)
+ : base(property)
+ {
+ }
+
+ public virtual vsCMAccess Access {
get { return GetAccess(); }
set { }
}
View
16 src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeGetterFunction.cs
@@ -0,0 +1,16 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using ICSharpCode.SharpDevelop.Dom;
+
+namespace ICSharpCode.PackageManagement.EnvDTE
+{
+ public class CodeGetterFunction : CodeGetterOrSetterFunction
+ {
+ public CodeGetterFunction(IProperty property)
+ : base(property, property.GetterModifiers)
+ {
+ }
+ }
+}
View
29 src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeGetterOrSetterFunction.cs
@@ -0,0 +1,29 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using ICSharpCode.SharpDevelop.Dom;
+
+namespace ICSharpCode.PackageManagement.EnvDTE
+{
+ public class CodeGetterOrSetterFunction : CodeFunction
+ {
+ ModifierEnum modifier;
+
+ public CodeGetterOrSetterFunction(IProperty property, ModifierEnum modifier)
+ : base(property)
+ {
+ this.modifier = modifier;
+ }
+
+ public override vsCMAccess Access {
+ get {
+ if (modifier == ModifierEnum.None) {
+ return base.Access;
+ }
+ return vsCMAccess.vsCMAccessPrivate;
+ }
+ set { }
+ }
+ }
+}
View
29 src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeProperty.cs
@@ -8,8 +8,6 @@ namespace ICSharpCode.PackageManagement.EnvDTE
{
public class CodeProperty : CodeElement
{
- CodeElements attributes;
-
public CodeProperty()
{
}
@@ -32,24 +30,35 @@ public CodeProperty(IProperty property)
}
public virtual CodeElements Attributes {
- get {
- if (attributes == null) {
- attributes = new CodeAttributes(Property);
- }
- return attributes;
- }
+ get { return new CodeAttributes(Property); }
}
public virtual CodeTypeRef Type {
get { throw new NotImplementedException(); }
}
public virtual CodeFunction Getter {
- get { throw new NotImplementedException(); }
+ get { return GetGetter(); }
+ }
+
+ CodeFunction GetGetter()
+ {
+ if (Property.CanGet) {
+ return new CodeGetterFunction(Property);
+ }
+ return null;
}
public virtual CodeFunction Setter {
- get { throw new NotImplementedException(); }
+ get { return GetSetter(); }
+ }
+
+ CodeFunction GetSetter()
+ {
+ if (Property.CanSet) {
+ return new CodeSetterFunction(Property);
+ }
+ return null;
}
}
}
View
16 src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeSetterFunction.cs
@@ -0,0 +1,16 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using ICSharpCode.SharpDevelop.Dom;
+
+namespace ICSharpCode.PackageManagement.EnvDTE
+{
+ public class CodeSetterFunction : CodeGetterOrSetterFunction
+ {
+ public CodeSetterFunction(IProperty property)
+ : base(property, property.SetterModifiers)
+ {
+ }
+ }
+}
View
2 src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj
@@ -88,7 +88,7 @@
<Compile Include="Src\EnvDTE\SolutionTests.cs" />
<Compile Include="Src\Helpers\AttributeHelper.cs" />
<Compile Include="Src\Helpers\ClassHelper.cs" />
- <Compile Include="Src\Helpers\CodeElementsHelpers.cs" />
+ <Compile Include="Src\Helpers\CodeElementsExtensions.cs" />
<Compile Include="Src\Helpers\FieldHelper.cs" />
<Compile Include="Src\Helpers\FakeSelectProjectsService.cs" />
<Compile Include="Src\Helpers\FakeSolutionPackageRepositoryFactory.cs" />
View
120 src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeProperty2Tests.cs
@@ -120,5 +120,125 @@ public void ReadWrite_PropertyHasSetterOnly_ReturnsWriteOnlyProperty()
Assert.AreEqual(vsCMPropertyKind.vsCMPropertyKindWriteOnly, kind);
}
+
+ [Test]
+ public void Parameters_PropertyIsIndexerWithOneParameter_ReturnsOneParameter()
+ {
+ helper.CreatePublicProperty("MyProperty");
+ helper.AddParameterToProperty("item");
+ CreateCodeProperty2();
+
+ CodeElements parameters = property.Parameters;
+ CodeParameter parameter = parameters.FirstCodeParameterOrDefault();
+
+ Assert.AreEqual(1, parameters.Count);
+ Assert.AreEqual("item", parameter.Name);
+ }
+
+ [Test]
+ public void Getter_PublicGetter_ReturnsPublicGetterCodeFunction()
+ {
+ helper.CreatePublicProperty("MyProperty");
+ helper.HasGetterOnly();
+ helper.GetterModifierIsNone();
+ CreateCodeProperty2();
+
+ CodeFunction getter = property.Getter;
+ vsCMAccess access = getter.Access;
+
+ Assert.AreEqual(vsCMAccess.vsCMAccessPublic, access);
+ }
+
+ [Test]
+ public void Getter_PrivateGetter_ReturnsPrivateGetterCodeFunction()
+ {
+ helper.CreatePrivateProperty("MyProperty");
+ helper.HasGetterOnly();
+ helper.GetterModifierIsNone();
+ CreateCodeProperty2();
+
+ CodeFunction getter = property.Getter;
+ vsCMAccess access = getter.Access;
+
+ Assert.AreEqual(vsCMAccess.vsCMAccessPrivate, access);
+ }
+
+ [Test]
+ public void Getter_NoGetter_ReturnsNull()
+ {
+ helper.CreatePublicProperty("MyProperty");
+ CreateCodeProperty2();
+
+ CodeFunction getter = property.Getter;
+
+ Assert.IsNull(getter);
+ }
+
+ [Test]
+ public void Getter_PublicPropertyButPrivateGetter_ReturnsPrivateGetterCodeFunction()
+ {
+ helper.CreatePublicProperty("MyProperty");
+ helper.HasGetterAndSetter();
+ helper.GetterModifierIsPrivate();
+ CreateCodeProperty2();
+
+ CodeFunction getter = property.Getter;
+ vsCMAccess access = getter.Access;
+
+ Assert.AreEqual(vsCMAccess.vsCMAccessPrivate, access);
+ }
+
+ [Test]
+ public void Setter_PublicSetter_ReturnsPublicSetterCodeFunction()
+ {
+ helper.CreatePublicProperty("MyProperty");
+ helper.HasSetterOnly();
+ helper.SetterModifierIsNone();
+ CreateCodeProperty2();
+
+ CodeFunction setter = property.Setter;
+ vsCMAccess access = setter.Access;
+
+ Assert.AreEqual(vsCMAccess.vsCMAccessPublic, access);
+ }
+
+ [Test]
+ public void Setter_PrivateSetter_ReturnsPrivateSetterCodeFunction()
+ {
+ helper.CreatePrivateProperty("MyProperty");
+ helper.HasSetterOnly();
+ helper.SetterModifierIsNone();
+ CreateCodeProperty2();
+
+ CodeFunction setter = property.Setter;
+ vsCMAccess access = setter.Access;
+
+ Assert.AreEqual(vsCMAccess.vsCMAccessPrivate, access);
+ }
+
+ [Test]
+ public void Setter_NoSetter_ReturnsNull()
+ {
+ helper.CreatePublicProperty("MyProperty");
+ CreateCodeProperty2();
+
+ CodeFunction setter = property.Setter;
+
+ Assert.IsNull(setter);
+ }
+
+ [Test]
+ public void Setter_PublicPropertyButPrivateSetter_ReturnsPrivateSetterCodeFunction()
+ {
+ helper.CreatePublicProperty("MyProperty");
+ helper.HasGetterAndSetter();
+ helper.SetterModifierIsPrivate();
+ CreateCodeProperty2();
+
+ CodeFunction setter = property.Setter;
+ vsCMAccess access = setter.Access;
+
+ Assert.AreEqual(vsCMAccess.vsCMAccessPrivate, access);
+ }
}
}
View
77 src/AddIns/Misc/PackageManagement/Test/Src/Helpers/CodeElementsHelpers.cs
@@ -1,77 +0,0 @@
-// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
-// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
-
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using ICSharpCode.PackageManagement.EnvDTE;
-
-namespace PackageManagement.Tests.Helpers
-{
- public static class CodeElementsHelpers
- {
- public static List<CodeElement> ToList(this CodeElements codeElements)
- {
- var list = new List<CodeElement>();
- foreach (CodeElement codeElement in codeElements) {
- list.Add(codeElement);
- }
- return list;
- }
-
- public static CodeElement FirstOrDefault(this CodeElements codeElements)
- {
- return ToList(codeElements).FirstOrDefault();
- }
-
- public static CodeFunction FirstCodeFunctionOrDefault(this CodeElements codeElements)
- {
- return codeElements.FirstOrDefault() as CodeFunction;
- }
-
- public static CodeClass2 FirstCodeClass2OrDefault(this CodeElements codeElements)
- {
- return codeElements.FirstOrDefault() as CodeClass2;
- }
-
- public static CodeInterface FirstCodeInterfaceOrDefault(this CodeElements codeElements)
- {
- return codeElements.FirstOrDefault() as CodeInterface;
- }
-
- public static CodeAttributeArgument FirstCodeAttributeArgumentOrDefault(this CodeElements codeElements)
- {
- return codeElements.FirstOrDefault() as CodeAttributeArgument;
- }
-
- public static CodeNamespace FirstCodeNamespaceOrDefault(this CodeElements codeElements)
- {
- return codeElements.FirstOrDefault() as CodeNamespace;
- }
-
- public static CodeNamespace LastCodeNamespaceOrDefault(this CodeElements codeElements)
- {
- return codeElements.LastOrDefault() as CodeNamespace;
- }
-
- public static CodeElement LastOrDefault(this CodeElements codeElements)
- {
- return codeElements.ToList().LastOrDefault();
- }
-
- public static CodeAttribute2 FirstCodeAttribute2OrDefault(this CodeElements codeElements)
- {
- return codeElements.FirstOrDefault() as CodeAttribute2;
- }
-
- public static CodeProperty2 FirstCodeProperty2OrDefault(this CodeElements codeElements)
- {
- return codeElements.FirstOrDefault() as CodeProperty2;
- }
-
- public static CodeVariable FirstCodeVariableOrDefault(this CodeElements codeElements)
- {
- return codeElements.FirstOrDefault() as CodeVariable;
- }
- }
-}
View
44 src/AddIns/Misc/PackageManagement/Test/Src/Helpers/PropertyHelper.cs
@@ -11,25 +11,28 @@ namespace PackageManagement.Tests.Helpers
public class PropertyHelper
{
public IProperty Property;
- public List<IAttribute> Attributes = new List<IAttribute>();
public ProjectContentHelper ProjectContentHelper = new ProjectContentHelper();
+ List<IAttribute> attributes = new List<IAttribute>();
+ List<IParameter> parameters = new List<IParameter>();
+
/// <summary>
/// Property name should include class prefix (e.g. "Class1.MyProperty")
/// </summary>
public void CreateProperty(string fullyQualifiedName)
{
Property = MockRepository.GenerateMock<IProperty, IEntity>();
Property.Stub(p => p.FullyQualifiedName).Return(fullyQualifiedName);
- Property.Stub(p => p.Attributes).Return(Attributes);
+ Property.Stub(p => p.Attributes).Return(attributes);
+ Property.Stub(p => p.Parameters).Return(parameters);
Property.Stub(p => p.ProjectContent).Return(ProjectContentHelper.FakeProjectContent);
}
public void AddAttribute(string fullName, string shortName)
{
var attributeHelper = new AttributeHelper();
attributeHelper.CreateAttribute(fullName, shortName);
- Attributes.Add(attributeHelper.Attribute);
+ attributes.Add(attributeHelper.Attribute);
}
public void AddParentClass(string className)
@@ -76,5 +79,40 @@ public void HasSetterOnly()
HasGetter = false;
HasSetter = true;
}
+
+ public void AddParameterToProperty(string name)
+ {
+ IParameter parameter = MockRepository.GenerateStub<IParameter>();
+ parameter.Stub(p => p.Name).Return(name);
+ parameters.Add(parameter);
+ }
+
+ public void GetterModifierIsPrivate()
+ {
+ GetterModifier = ModifierEnum.Private;
+ }
+
+ ModifierEnum GetterModifier {
+ set { Property.Stub(p => p.GetterModifiers).Return(value); }
+ }
+
+ public void GetterModifierIsNone()
+ {
+ GetterModifier = ModifierEnum.None;
+ }
+
+ public void SetterModifierIsPrivate()
+ {
+ SetterModifier = ModifierEnum.Private;
+ }
+
+ ModifierEnum SetterModifier {
+ set { Property.Stub(p => p.SetterModifiers).Return(value); }
+ }
+
+ public void SetterModifierIsNone()
+ {
+ GetterModifier = ModifierEnum.None;
+ }
}
}

0 comments on commit 1dbdbf2

Please sign in to comment.
Something went wrong with that request. Please try again.