Browse Source

Implement EnvDTE.CodeAttribute.

pull/28/head
Matt Ward 14 years ago
parent
commit
2578b8c77d
  1. 1
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  2. 34
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute.cs
  3. 19
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute2.cs
  4. 23
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArgument.cs
  5. 65
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArguments.cs
  6. 2
      src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj
  7. 136
      src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeAttribute2Tests.cs
  8. 38
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/AttributeHelper.cs

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

@ -77,6 +77,7 @@ @@ -77,6 +77,7 @@
<Compile Include="Src\EnvDTE\CodeAttribute.cs" />
<Compile Include="Src\EnvDTE\CodeAttribute2.cs" />
<Compile Include="Src\EnvDTE\CodeAttributeArgument.cs" />
<Compile Include="Src\EnvDTE\CodeAttributeArguments.cs" />
<Compile Include="Src\EnvDTE\CodeClass.cs" />
<Compile Include="Src\EnvDTE\CodeClass2.cs" />
<Compile Include="Src\EnvDTE\CodeDelegate.cs" />

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

@ -2,15 +2,45 @@ @@ -2,15 +2,45 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Linq;
using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.PackageManagement.EnvDTE
{
public class CodeAttribute : CodeElement
{
public CodeAttribute()
IAttribute attribute;
public CodeAttribute(IAttribute attribute)
{
this.attribute = attribute;
}
public virtual string FullName {
get { return attribute.AttributeType.FullyQualifiedName; }
}
public virtual string Value { get; set; }
public virtual string Value {
get { return GetValue(); }
set { }
}
string GetValue()
{
return String.Join(", ", GetArgumentValues());
}
string[] GetArgumentValues()
{
return attribute
.PositionalArguments
.Select(arg => GetArgumentValue(arg))
.ToArray();
}
string GetArgumentValue(object argument)
{
return new CodeAttributeArgument(String.Empty, argument).Value;
}
}
}

19
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttribute2.cs

@ -2,21 +2,28 @@ @@ -2,21 +2,28 @@
// 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 CodeAttribute2 : CodeAttribute
{
public CodeAttribute2()
{
}
CodeAttributeArguments arguments;
IAttribute attribute;
public virtual string FullName {
get { throw new NotImplementedException(); }
public CodeAttribute2(IAttribute attribute)
: base(attribute)
{
this.attribute = attribute;
}
public virtual CodeElements Arguments {
get { throw new NotImplementedException(); }
get {
if (arguments == null) {
arguments = new CodeAttributeArguments(attribute);
}
return arguments;
}
}
}
}

23
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeAttributeArgument.cs

@ -5,18 +5,31 @@ using System; @@ -5,18 +5,31 @@ using System;
namespace ICSharpCode.PackageManagement.EnvDTE
{
public class CodeAttributeArgument : MarshalByRefObject
public class CodeAttributeArgument : CodeElement
{
public CodeAttributeArgument()
string name;
string value;
public CodeAttributeArgument(string name, object value)
{
this.name = name;
this.value = GetValue(value);
}
string GetValue(object value)
{
if (value is string) {
return String.Format("\"{0}\"", value);
}
return value.ToString();
}
public virtual string Name {
get { throw new NotImplementedException(); }
public override string Name {
get { return name; }
}
public virtual string Value {
get { throw new NotImplementedException(); }
get { return value; }
}
}
}

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

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
// 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 CodeAttributeArguments : CodeElements
{
List<CodeElement> elements = new List<CodeElement>();
IAttribute attribute;
public CodeAttributeArguments(IAttribute attribute)
{
this.attribute = attribute;
GetCodeElements();
}
void GetCodeElements()
{
foreach (object arg in attribute.PositionalArguments) {
AddAttributeArgument(String.Empty, arg);
}
foreach (KeyValuePair<string, object> namedArg in attribute.NamedArguments) {
AddAttributeArgument(namedArg.Key, namedArg.Value);
}
}
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);
}
}
}

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

@ -72,12 +72,14 @@ @@ -72,12 +72,14 @@
<Compile Include="..\..\..\..\Main\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Src\EnvDTE\CodeAttribute2Tests.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\NamespaceNameTests.cs" />
<Compile Include="Src\EnvDTE\SolutionTests.cs" />
<Compile Include="Src\Helpers\AttributeHelper.cs" />
<Compile Include="Src\Helpers\CodeElementsHelpers.cs" />
<Compile Include="Src\Helpers\FakeSelectProjectsService.cs" />
<Compile Include="Src\Helpers\FakeSolutionPackageRepositoryFactory.cs" />

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

@ -0,0 +1,136 @@ @@ -0,0 +1,136 @@
// 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.Linq;
using ICSharpCode.PackageManagement.EnvDTE;
using NUnit.Framework;
using PackageManagement.Tests.Helpers;
namespace PackageManagement.Tests.EnvDTE
{
[TestFixture]
public class CodeAttribute2Tests
{
CodeAttribute2 codeAttribute;
AttributeHelper helper;
void CreateAttribute()
{
codeAttribute = new CodeAttribute2(helper.Attribute);
}
void CreateMSBuildAttribute(string fullName)
{
helper = new AttributeHelper();
helper.CreateAttribute(fullName);
}
[Test]
public void FullName_AttributeIsDataAnnotationsDisplayColumnAttribute_ReturnsDisplayColumnAttributeFullyQualifiedName()
{
CreateMSBuildAttribute("System.ComponentModel.DataAnnotations.DisplayColumn");
CreateAttribute();
string name = codeAttribute.FullName;
Assert.AreEqual("System.ComponentModel.DataAnnotations.DisplayColumn", name);
}
[Test]
public void Value_AttributeHasOneStringPositionalArgument_ReturnsStringInQuotes()
{
CreateMSBuildAttribute("Test.MyAttribute");
helper.AddPositionalArguments("StringValue");
CreateAttribute();
string attributeValue = codeAttribute.Value;
Assert.AreEqual("\"StringValue\"", attributeValue);
}
[Test]
public void Value_AttributeHasOneBooleanPositionalArgument_ReturnsBooleanValue()
{
CreateMSBuildAttribute("Test.MyAttribute");
helper.AddPositionalArguments(true);
CreateAttribute();
string attributeValue = codeAttribute.Value;
Assert.AreEqual("True", attributeValue);
}
[Test]
public void Value_AttributeHasStringAndBooleanPositionalArgument_ReturnsArgumentCommandSeparated()
{
CreateMSBuildAttribute("Test.MyAttribute");
helper.AddPositionalArguments("Test", true);
CreateAttribute();
string attributeValue = codeAttribute.Value;
Assert.AreEqual("\"Test\", True", attributeValue);
}
[Test]
public void Arguments_AttributeHasOneStringPositionalArgument_ReturnsOneAttributeArgumentWithNoName()
{
CreateMSBuildAttribute("Test.MyAttribute");
helper.AddPositionalArguments("StringValue");
CreateAttribute();
CodeElements args = codeAttribute.Arguments;
CodeAttributeArgument attributeArg = args.ToList().FirstOrDefault() as CodeAttributeArgument;
Assert.AreEqual(1, args.Count);
Assert.AreEqual(String.Empty, attributeArg.Name);
Assert.AreEqual("\"StringValue\"", attributeArg.Value);
}
[Test]
public void Arguments_AttributeHasOneStringNamedArgument_ReturnsOneAttributeArgumentWithName()
{
CreateMSBuildAttribute("Test.MyAttribute");
helper.AddNamedArgument("Name", "StringValue");
CreateAttribute();
CodeElements args = codeAttribute.Arguments;
CodeAttributeArgument attributeArg = args.ToList().FirstOrDefault() as CodeAttributeArgument;
Assert.AreEqual("Name", attributeArg.Name);
Assert.AreEqual("\"StringValue\"", attributeArg.Value);
}
[Test]
public void Arguments_GetArgumentByItemIndexWhenTwoPositionalArguments_ReturnsArgumentAtIndex()
{
CreateMSBuildAttribute("Test.MyAttribute");
helper.AddPositionalArguments("StringValue", false);
CreateAttribute();
CodeElements args = codeAttribute.Arguments;
CodeAttributeArgument arg = args.Item(2) as CodeAttributeArgument;
Assert.AreEqual("False", arg.Value);
}
[Test]
public void Arguments_GetArgumentByItemNameWhenTwoNamedArguments_ReturnsArgument()
{
CreateMSBuildAttribute("Test.MyAttribute");
helper.AddNamedArgument("One", "OneValue");
helper.AddNamedArgument("Two", false);
CreateAttribute();
CodeElements args = codeAttribute.Arguments;
CodeAttributeArgument arg = args.Item("Two") as CodeAttributeArgument;
Assert.AreEqual("False", arg.Value);
}
}
}

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

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
// 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 ICSharpCode.SharpDevelop.Dom;
using Rhino.Mocks;
namespace PackageManagement.Tests.Helpers
{
public class AttributeHelper
{
public IAttribute Attribute;
public IReturnType AttributeType;
public List<object> PositionalArguments = new List<object>();
public Dictionary<string, object> NamedArguments = new Dictionary<string, object>();
public void CreateAttribute(string fullName)
{
AttributeType = MockRepository.GenerateStub<IReturnType>();
AttributeType.Stub(at => at.FullyQualifiedName).Return(fullName);
Attribute = MockRepository.GenerateStub<IAttribute>();
Attribute.Stub(a => a.AttributeType).Return(AttributeType);
Attribute.Stub(a => a.PositionalArguments).Return(PositionalArguments);
Attribute.Stub(a => a.NamedArguments).Return(NamedArguments);
}
public void AddPositionalArguments(params object[] args)
{
PositionalArguments.AddRange(args);
}
public void AddNamedArgument(string name, object value)
{
NamedArguments.Add(name, value);
}
}
}
Loading…
Cancel
Save