8 changed files with 305 additions and 13 deletions
@ -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); |
||||
} |
||||
} |
||||
} |
||||
@ -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); |
||||
} |
||||
} |
||||
} |
||||
@ -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…
Reference in new issue