Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH2533: Add new RawValue parameter to skip attribute value quoting #2648

Merged
merged 1 commit into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}
}