From 9aa770e5b90c327a99c6256c2bfc23559d27f044 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sun, 27 May 2012 12:29:37 +0100 Subject: [PATCH] Implement EnvDTE.CodeType.Attributes. --- .../Project/PackageManagement.csproj | 2 + .../Project/Src/EnvDTE/CodeAttribute.cs | 18 +++++ .../Src/EnvDTE/CodeAttributeArguments.cs | 36 +-------- .../Project/Src/EnvDTE/CodeAttributes.cs | 34 +++++++++ .../Src/EnvDTE/CodeElementsInNamespace.cs | 35 +-------- .../Project/Src/EnvDTE/CodeElementsList.cs | 51 +++++++++++++ .../Project/Src/EnvDTE/CodeType.cs | 9 ++- .../Test/PackageManagement.Tests.csproj | 2 + .../Test/Src/EnvDTE/CodeAttribute2Tests.cs | 33 +++++++- .../Test/Src/EnvDTE/CodeAttributesTests.cs | 76 +++++++++++++++++++ .../Test/Src/EnvDTE/CodeTypeTests.cs | 57 ++++++++++++++ .../Test/Src/Helpers/AttributeHelper.cs | 13 ++++ 12 files changed, 296 insertions(+), 70 deletions(-) create mode 100644 src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributes.cs create mode 100644 src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsList.cs create mode 100644 src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttributesTests.cs create mode 100644 src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeTypeTests.cs diff --git a/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj b/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj index 59bc4fe82e..f41bb9bc4a 100644 --- a/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj +++ b/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj @@ -78,12 +78,14 @@ + + diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute.cs index 2b0707cf54..a696288d5d 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute.cs @@ -10,12 +10,30 @@ namespace ICSharpCode.PackageManagement.EnvDTE public class CodeAttribute : CodeElement { IAttribute attribute; + static readonly string AttributeEndName = "Attribute"; public CodeAttribute(IAttribute attribute) { this.attribute = attribute; } + public override string Name { + get { return GetShortName(); } + } + + string GetShortName() + { + return GetShortName(attribute.AttributeType.Name); + } + + string GetShortName(string name) + { + if (name.EndsWith(AttributeEndName)) { + return name.Substring(0, name.Length - AttributeEndName.Length); + } + return name; + } + public virtual string FullName { get { return attribute.AttributeType.FullyQualifiedName; } } diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArguments.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArguments.cs index 95b0c83ca9..0392ceb558 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArguments.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArguments.cs @@ -9,18 +9,17 @@ using ICSharpCode.SharpDevelop.Dom; namespace ICSharpCode.PackageManagement.EnvDTE { - public class CodeAttributeArguments : CodeElements + public class CodeAttributeArguments : CodeElementsList { - List elements = new List(); IAttribute attribute; public CodeAttributeArguments(IAttribute attribute) { this.attribute = attribute; - GetCodeElements(); + AddCodeElements(); } - void GetCodeElements() + void AddCodeElements() { foreach (object arg in attribute.PositionalArguments) { AddAttributeArgument(String.Empty, arg); @@ -32,34 +31,7 @@ namespace ICSharpCode.PackageManagement.EnvDTE void AddAttributeArgument(string name, object value) { - elements.Add(new CodeAttributeArgument(name, value)); - } - - public int Count { - get { return elements.Count; } - } - - public IEnumerator GetEnumerator() - { - return elements.GetEnumerator(); - } - - public CodeElement Item(object index) - { - if (index is int) { - return Item((int)index); - } - return Item((string)index); - } - - CodeElement Item(int index) - { - return elements[index - 1]; - } - - CodeElement Item(string name) - { - return elements.Single(item => item.Name == name); + AddCodeElement(new CodeAttributeArgument(name, value)); } } } diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributes.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributes.cs new file mode 100644 index 0000000000..a3bd7761ef --- /dev/null +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributes.cs @@ -0,0 +1,34 @@ +// 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; +using System.Collections.Generic; +using System.Linq; +using ICSharpCode.SharpDevelop.Dom; + +namespace ICSharpCode.PackageManagement.EnvDTE +{ + public class CodeAttributes : CodeElementsList + { + IEntity entity; + + public CodeAttributes(IEntity entity) + { + this.entity = entity; + GetAttributes(); + } + + void GetAttributes() + { + foreach (IAttribute attribute in entity.Attributes) { + AddAttribute(attribute); + } + } + + void AddAttribute(IAttribute attribute) + { + AddCodeElement(new CodeAttribute2(attribute)); + } + } +} diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsInNamespace.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsInNamespace.cs index 53edd646e6..aaf2c53102 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsInNamespace.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsInNamespace.cs @@ -10,9 +10,8 @@ using ICSharpCode.SharpDevelop.Dom; namespace ICSharpCode.PackageManagement.EnvDTE { - public class CodeElementsInNamespace : CodeElements + public class CodeElementsInNamespace : CodeElementsList { - List codeElements = new List(); IProjectContent projectContent; NamespaceName namespaceName; @@ -58,37 +57,5 @@ namespace ICSharpCode.PackageManagement.EnvDTE { AddCodeElement(new CodeClass2(projectContent, c)); } - - void AddCodeElement(CodeElement codeElement) - { - codeElements.Add(codeElement); - } - - public int Count { - get { return codeElements.Count; } - } - - public IEnumerator GetEnumerator() - { - return codeElements.GetEnumerator(); - } - - public CodeElement Item(object index) - { - if (index is int) { - return Item((int)index); - } - return Item((string)index); - } - - CodeElement Item(int index) - { - return codeElements[index - 1]; - } - - CodeElement Item(string name) - { - return codeElements.Single(element => element.Name == name); - } } } diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsList.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsList.cs new file mode 100644 index 0000000000..d0bb79e858 --- /dev/null +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsList.cs @@ -0,0 +1,51 @@ +// 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; +using System.Collections.Generic; +using System.Linq; + +namespace ICSharpCode.PackageManagement.EnvDTE +{ + public abstract class CodeElementsList : CodeElements + { + List elements = new List(); + + public CodeElementsList() + { + } + + protected virtual void AddCodeElement(CodeElement element) + { + elements.Add(element); + } + + public int Count { + get { return elements.Count; } + } + + public IEnumerator GetEnumerator() + { + return elements.GetEnumerator(); + } + + public CodeElement Item(object index) + { + if (index is int) { + return Item((int)index); + } + return Item((string)index); + } + + CodeElement Item(int index) + { + return elements[index - 1]; + } + + CodeElement Item(string name) + { + return elements.Single(item => item.Name == name); + } + } +} diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeType.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeType.cs index 6b7809ada4..931c8b1c53 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeType.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeType.cs @@ -8,6 +8,8 @@ namespace ICSharpCode.PackageManagement.EnvDTE { public class CodeType : CodeElement { + CodeAttributes attributes; + public CodeType(IProjectContent projectContent, IClass c) : base(c) { @@ -44,7 +46,12 @@ namespace ICSharpCode.PackageManagement.EnvDTE } public virtual CodeElements Attributes { - get { throw new NotImplementedException(); } + get { + if (attributes == null) { + attributes = new CodeAttributes(Class); + } + return attributes; + } } } } diff --git a/src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj b/src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj index 5b7e68a164..aa70e15478 100644 --- a/src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj +++ b/src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj @@ -73,10 +73,12 @@ Properties\GlobalAssemblyInfo.cs + + diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttribute2Tests.cs b/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttribute2Tests.cs index 83c5f74234..57e37ab71d 100644 --- a/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttribute2Tests.cs +++ b/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttribute2Tests.cs @@ -21,20 +21,36 @@ namespace PackageManagement.Tests.EnvDTE } void CreateMSBuildAttribute(string fullName) + { + CreateMSBuildAttribute(fullName, fullName); + } + + void CreateMSBuildAttribute(string fullName, string shortName) { helper = new AttributeHelper(); - helper.CreateAttribute(fullName); + helper.CreateAttribute(fullName, shortName); } [Test] public void FullName_AttributeIsDataAnnotationsDisplayColumnAttribute_ReturnsDisplayColumnAttributeFullyQualifiedName() { - CreateMSBuildAttribute("System.ComponentModel.DataAnnotations.DisplayColumn"); + CreateMSBuildAttribute("System.ComponentModel.DataAnnotations.DisplayColumnAttribute"); CreateAttribute(); string name = codeAttribute.FullName; - Assert.AreEqual("System.ComponentModel.DataAnnotations.DisplayColumn", name); + Assert.AreEqual("System.ComponentModel.DataAnnotations.DisplayColumnAttribute", name); + } + + [Test] + public void Name_AttributeIsDataAnnotationsDisplayColumnAttribute_ReturnsShortDisplayColumnAttributeNameWithoutTheAttributePart() + { + CreateMSBuildAttribute("System.ComponentModel.DataAnnotations.DisplayColumnAttribute", "DisplayColumnAttribute"); + CreateAttribute(); + + string name = codeAttribute.Name; + + Assert.AreEqual("DisplayColumn", name); } [Test] @@ -132,5 +148,16 @@ namespace PackageManagement.Tests.EnvDTE Assert.AreEqual("False", arg.Value); } + + [Test] + public void Name_AttributeIsNotLastPartOfName_ReturnsShortNameContainingAttributePart() + { + CreateMSBuildAttribute("Tests.TestAttributeColumn", "TestAttributeColumn"); + CreateAttribute(); + + string name = codeAttribute.Name; + + Assert.AreEqual("TestAttributeColumn", name); + } } } diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttributesTests.cs b/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttributesTests.cs new file mode 100644 index 0000000000..3843959773 --- /dev/null +++ b/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttributesTests.cs @@ -0,0 +1,76 @@ +// 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; +using ICSharpCode.SharpDevelop.Dom; +using NUnit.Framework; +using PackageManagement.Tests.Helpers; + +namespace PackageManagement.Tests.EnvDTE +{ + [TestFixture] + public class CodeAttributesTests + { + IClass fakeClass; + CodeAttributes attributes; + ProjectContentHelper helper; + + [SetUp] + public void Init() + { + helper = new ProjectContentHelper(); + } + + void CreateCodeAttributes() + { + attributes = new CodeAttributes(fakeClass); + } + + void CreateMSBuildClass() + { + fakeClass = helper.AddClassToProjectContent("MyClass"); + } + + void AddAttributeToClass(string name) + { + var attributeHelper = new AttributeHelper(); + attributeHelper.CreateAttribute(name); + attributeHelper.AddAttributeToClass(fakeClass); + } + + List GetEnumerator() + { + return attributes.ToList(); + } + + [Test] + public void GetEnumerator_ClassHasOneAttribute_ReturnsOneAttribute() + { + CreateMSBuildClass(); + AddAttributeToClass("TestAttribute"); + CreateCodeAttributes(); + + List attributeList = GetEnumerator(); + + CodeAttribute2 attribute = attributeList.FirstOrDefault() as CodeAttribute2; + + Assert.AreEqual(1, attributeList.Count); + Assert.AreEqual("Test", attribute.Name); + } + + [Test] + public void Item_GetItemByNameWhenClassHasOneAttribute_ReturnsOneAttribute() + { + CreateMSBuildClass(); + AddAttributeToClass("TestAttribute"); + CreateCodeAttributes(); + + CodeAttribute2 attribute = attributes.Item("Test") as CodeAttribute2; + + Assert.AreEqual("Test", attribute.Name); + } + } +} diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeTypeTests.cs b/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeTypeTests.cs new file mode 100644 index 0000000000..52df31e32c --- /dev/null +++ b/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeTypeTests.cs @@ -0,0 +1,57 @@ +// 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.PackageManagement.EnvDTE; +using ICSharpCode.SharpDevelop.Dom; +using NUnit.Framework; +using PackageManagement.Tests.Helpers; + +namespace PackageManagement.Tests.EnvDTE +{ + [TestFixture] + public class CodeTypeTests + { + CodeType codeType; + ProjectContentHelper helper; + IClass fakeClass; + + void CreateProjectContent() + { + helper = new ProjectContentHelper(); + } + + void CreateClass(string name) + { + fakeClass = helper.AddClassToProjectContent(name); + } + + void CreateCodeType() + { + codeType = new CodeType(helper.FakeProjectContent, fakeClass); + } + + void AddAttributeToClass(string name) + { + var attributeHelper = new AttributeHelper(); + attributeHelper.CreateAttribute(name); + attributeHelper.AddAttributeToClass(fakeClass); + } + + [Test] + public void Attributes_ClassHasOneAttribute_ReturnsOneAttribute() + { + CreateProjectContent(); + CreateClass("TestClass"); + AddAttributeToClass("TestAttribute"); + CreateCodeType(); + + CodeElements attributes = codeType.Attributes; + + CodeAttribute2 attribute = attributes.Item(1) as CodeAttribute2; + + Assert.AreEqual(1, attributes.Count); + Assert.AreEqual("Test", attribute.Name); + } + } +} diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/AttributeHelper.cs b/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/AttributeHelper.cs index b330b76ad9..30f5694aa0 100644 --- a/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/AttributeHelper.cs +++ b/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/AttributeHelper.cs @@ -16,9 +16,15 @@ namespace PackageManagement.Tests.Helpers public Dictionary NamedArguments = new Dictionary(); public void CreateAttribute(string fullName) + { + CreateAttribute(fullName, fullName); + } + + public void CreateAttribute(string fullName, string shortName) { AttributeType = MockRepository.GenerateStub(); AttributeType.Stub(at => at.FullyQualifiedName).Return(fullName); + AttributeType.Stub(at => at.Name).Return(shortName); Attribute = MockRepository.GenerateStub(); Attribute.Stub(a => a.AttributeType).Return(AttributeType); Attribute.Stub(a => a.PositionalArguments).Return(PositionalArguments); @@ -34,5 +40,12 @@ namespace PackageManagement.Tests.Helpers { NamedArguments.Add(name, value); } + + public void AddAttributeToClass(IClass @class) + { + var attributes = new List(); + attributes.Add(Attribute); + @class.Stub(c => c.Attributes).Return(attributes); + } } }