Skip to content

Commit

Permalink
Add new UseRawValue parameter to skip attribute value quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
agc93 committed Oct 26, 2019
1 parent 5874fda commit 940661e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,21 @@ public void Should_Add_Configuration_Attribute_If_Set()
Assert.Contains("[assembly: AssemblyConfiguration(\"TheConfiguration\")]", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Raw_Value()
{
// Given
var fixture = new AssemblyInfoFixture();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = "RawTestValue", UseRawValue = true } };

// When
var result = fixture.CreateAndReturnContent();

// Then
Assert.Contains("using Test.NameSpace;", result);
Assert.Contains("[assembly: TestAttribute(RawTestValue)]", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public AssemblyInfoCreatorData(AssemblyInfoSettings settings, bool isVisualBasic
{
foreach (var item in settings.CustomAttributes.Where(item => item != null))
{
AddCustomAttribute(item.Name, item.NameSpace, item.Value);
AddCustomAttribute(item.Name, item.NameSpace, item.Value, item.UseRawValue);
}
}
if (settings.MetaDataAttributes != null)
Expand Down Expand Up @@ -100,14 +100,14 @@ private void AddAttribute(string name, string @namespace, string value)
}
}

private void AddCustomAttribute(string name, string @namespace, object value)
private void AddCustomAttribute(string name, string @namespace, object value, bool isRawValue)
{
var attributeValue = AttributeValueToString(value);
var attributeValue = AttributeValueToString(value, isRawValue);

AddAttributeCore(CustomAttributes, name, @namespace, attributeValue);
}

private string AttributeValueToString(object value)
private string AttributeValueToString(object value, bool isRawValue)
{
switch (value)
{
Expand All @@ -123,7 +123,9 @@ private string AttributeValueToString(object value)
{
return stringValue == string.Empty
? string.Empty
: string.Concat("\"", value, "\"");
: isRawValue
? stringValue
: string.Concat("\"", stringValue.Replace("\"", "\\\""), "\"");
}
default:
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ public sealed class AssemblyInfoCustomAttribute
/// </summary>
/// <value>The value for the attribute.</value>
public object Value { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the value is raw or should be quoted in the created attribute.
/// </summary>
/// <value>
/// <c>true</c> if should be treated as raw; otherwise, <c>false</c>.
/// </value>
public bool UseRawValue { get; set; }
}
}

0 comments on commit 940661e

Please sign in to comment.