Browse Source

Implement EnvDTE.CodeType.Attributes.

pull/28/head
Matt Ward 14 years ago
parent
commit
9aa770e5b9
  1. 2
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  2. 18
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute.cs
  3. 36
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArguments.cs
  4. 34
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributes.cs
  5. 35
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsInNamespace.cs
  6. 51
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsList.cs
  7. 9
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeType.cs
  8. 2
      src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj
  9. 33
      src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttribute2Tests.cs
  10. 76
      src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttributesTests.cs
  11. 57
      src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeTypeTests.cs
  12. 13
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/AttributeHelper.cs

2
src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj

@ -78,12 +78,14 @@ @@ -78,12 +78,14 @@
<Compile Include="Src\EnvDTE\CodeAttribute2.cs" />
<Compile Include="Src\EnvDTE\CodeAttributeArgument.cs" />
<Compile Include="Src\EnvDTE\CodeAttributeArguments.cs" />
<Compile Include="Src\EnvDTE\CodeAttributes.cs" />
<Compile Include="Src\EnvDTE\CodeClass.cs" />
<Compile Include="Src\EnvDTE\CodeClass2.cs" />
<Compile Include="Src\EnvDTE\CodeDelegate.cs" />
<Compile Include="Src\EnvDTE\CodeElement.cs" />
<Compile Include="Src\EnvDTE\CodeElements.cs" />
<Compile Include="Src\EnvDTE\CodeElementsInNamespace.cs" />
<Compile Include="Src\EnvDTE\CodeElementsList.cs" />
<Compile Include="Src\EnvDTE\CodeFunction.cs" />
<Compile Include="Src\EnvDTE\CodeInterface.cs" />
<Compile Include="Src\EnvDTE\CodeModel.cs" />

18
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute.cs

@ -10,12 +10,30 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -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; }
}

36
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArguments.cs

@ -9,18 +9,17 @@ using ICSharpCode.SharpDevelop.Dom; @@ -9,18 +9,17 @@ using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.PackageManagement.EnvDTE
{
public class CodeAttributeArguments : CodeElements
public class CodeAttributeArguments : CodeElementsList
{
List<CodeElement> elements = new List<CodeElement>();
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 @@ -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));
}
}
}

34
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributes.cs

@ -0,0 +1,34 @@ @@ -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));
}
}
}

35
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsInNamespace.cs

@ -10,9 +10,8 @@ using ICSharpCode.SharpDevelop.Dom; @@ -10,9 +10,8 @@ using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.PackageManagement.EnvDTE
{
public class CodeElementsInNamespace : CodeElements
public class CodeElementsInNamespace : CodeElementsList
{
List<CodeElement> codeElements = new List<CodeElement>();
IProjectContent projectContent;
NamespaceName namespaceName;
@ -58,37 +57,5 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -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);
}
}
}

51
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeElementsList.cs

@ -0,0 +1,51 @@ @@ -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<CodeElement> elements = new List<CodeElement>();
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);
}
}
}

9
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeType.cs

@ -8,6 +8,8 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -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 @@ -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;
}
}
}
}

2
src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj

@ -73,10 +73,12 @@ @@ -73,10 +73,12 @@
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Src\EnvDTE\CodeAttribute2Tests.cs" />
<Compile Include="Src\EnvDTE\CodeAttributesTests.cs" />
<Compile Include="Src\EnvDTE\CodeClass2Tests.cs" />
<Compile Include="Src\EnvDTE\CodeElementsInNamespaceTests.cs" />
<Compile Include="Src\EnvDTE\CodeModelTests.cs" />
<Compile Include="Src\EnvDTE\CodeNamespaceTests.cs" />
<Compile Include="Src\EnvDTE\CodeTypeTests.cs" />
<Compile Include="Src\EnvDTE\NamespaceNameTests.cs" />
<Compile Include="Src\EnvDTE\SolutionTests.cs" />
<Compile Include="Src\Helpers\AttributeHelper.cs" />

33
src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttribute2Tests.cs

@ -21,20 +21,36 @@ namespace PackageManagement.Tests.EnvDTE @@ -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 @@ -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);
}
}
}

76
src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttributesTests.cs

@ -0,0 +1,76 @@ @@ -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<CodeElement> GetEnumerator()
{
return attributes.ToList();
}
[Test]
public void GetEnumerator_ClassHasOneAttribute_ReturnsOneAttribute()
{
CreateMSBuildClass();
AddAttributeToClass("TestAttribute");
CreateCodeAttributes();
List<CodeElement> 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);
}
}
}

57
src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeTypeTests.cs

@ -0,0 +1,57 @@ @@ -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);
}
}
}

13
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/AttributeHelper.cs

@ -16,9 +16,15 @@ namespace PackageManagement.Tests.Helpers @@ -16,9 +16,15 @@ namespace PackageManagement.Tests.Helpers
public Dictionary<string, object> NamedArguments = new Dictionary<string, object>();
public void CreateAttribute(string fullName)
{
CreateAttribute(fullName, fullName);
}
public void CreateAttribute(string fullName, string shortName)
{
AttributeType = MockRepository.GenerateStub<IReturnType>();
AttributeType.Stub(at => at.FullyQualifiedName).Return(fullName);
AttributeType.Stub(at => at.Name).Return(shortName);
Attribute = MockRepository.GenerateStub<IAttribute>();
Attribute.Stub(a => a.AttributeType).Return(AttributeType);
Attribute.Stub(a => a.PositionalArguments).Return(PositionalArguments);
@ -34,5 +40,12 @@ namespace PackageManagement.Tests.Helpers @@ -34,5 +40,12 @@ namespace PackageManagement.Tests.Helpers
{
NamedArguments.Add(name, value);
}
public void AddAttributeToClass(IClass @class)
{
var attributes = new List<IAttribute>();
attributes.Add(Attribute);
@class.Stub(c => c.Attributes).Return(attributes);
}
}
}

Loading…
Cancel
Save