Skip to content

Commit

Permalink
(GH-2638) Add ability to pass bool and null values
Browse files Browse the repository at this point in the history
When creating CustomAttributes for AssemblyInfo files, it is useful to
be able to generate attributes with boolean values, as well as attributes
that don't have a value.  This is a "simple" solution to allow this to
happen, a broader fix would be to use Roslyn to fully understand all
attributes in the file.
  • Loading branch information
gep13 committed Oct 15, 2019
1 parent 18ed6a4 commit 8546aa3
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,51 @@ public void Should_Add_CustomAttributes_If_Set()
Assert.Contains("[assembly: TestAttribute(\"TestValue\")]", result);
}

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

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

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

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

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

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

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

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

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

[Fact]
public void Should_Add_MetadataAttributes_If_Set()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,51 @@ public void Should_Add_CustomAttributes_If_Set()
Assert.Contains("Imports Test.NameSpace", result);
Assert.Contains("<Assembly: TestAttribute(\"TestValue\")>", result);
}

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

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

// Then
Assert.Contains("Imports Test.NameSpace", result);
Assert.Contains("<Assembly: TestAttribute(True)>", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Null_Value()
{
// Given
var fixture = new AssemblyInfoFixture_VB();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = null } };

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

// Then
Assert.Contains("Imports Test.NameSpace", result);
Assert.Contains("<Assembly: TestAttribute()>", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Empty_Value()
{
// Given
var fixture = new AssemblyInfoFixture_VB();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = string.Empty } };

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

// Then
Assert.Contains("Imports Test.NameSpace", result);
Assert.Contains("<Assembly: TestAttribute()>", result);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Cake.Core;

Expand Down Expand Up @@ -99,11 +100,35 @@ private void AddAttribute(string name, string @namespace, string value)
}
}

private void AddCustomAttribute(string name, string @namespace, string value)
private void AddCustomAttribute(string name, string @namespace, object value)
{
if (value != null)
var attributeValue = AttributeValueToString(value);

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

private string AttributeValueToString(object value)
{
switch (value)
{
AddAttributeCore(CustomAttributes, name, @namespace, string.Concat("\"", value, "\""));
case null:
{
return string.Empty;
}
case bool boolValue:
{
return boolValue ? _trueStringValue : _falseStringValue;
}
case string stringValue:
{
return stringValue == string.Empty
? string.Empty
: string.Concat("\"", value, "\"");
}
default:
{
return Convert.ToString(value, CultureInfo.InvariantCulture);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand All @@ -25,6 +25,6 @@ public sealed class AssemblyInfoCustomAttribute
/// Gets or sets the value.
/// </summary>
/// <value>The value for the attribute.</value>
public string Value { get; set; }
public object Value { get; set; }
}
}

0 comments on commit 8546aa3

Please sign in to comment.