diff --git a/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs b/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs index bf668ed0950..1ed357814c7 100644 --- a/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs +++ b/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs @@ -655,6 +655,143 @@ public void OneAttributePositionalAndNamedParamsVisualBasic() File.Delete(task.OutputFile.ItemSpec); } + /// + /// Positional arguments can be non-string types. To specify the type of a parameter, + /// use metadata like "_Parameter1Type" that specifies the full name of the type. + /// + [Fact] + public void OneAttributePositionalWithType() { + WriteCodeFragment task = new WriteCodeFragment(); + MockEngine engine = new MockEngine(true); + task.BuildEngine = engine; + TaskItem attribute = new TaskItem("System.Runtime.InteropServices.CLSCompliantAttribute"); + attribute.SetMetadata("_Parameter1:System.Boolean", "True"); + task.AssemblyAttributes = new TaskItem[] { attribute }; + task.Language = "c#"; + task.OutputDirectory = new TaskItem(Path.GetTempPath()); + bool result = task.Execute(); + + Assert.True(result); + + string content = File.ReadAllText(task.OutputFile.ItemSpec); + Console.WriteLine(content); + + CheckContentCSharp(content, @"[assembly: System.Runtime.InteropServices.CLSCompliantAttribute(true)]"); + + File.Delete(task.OutputFile.ItemSpec); + } + + /// + /// Named arguments can be non-string types. To specify the type + /// of a parameter called "MyParameter", use metadata called + /// "MyParameterType" that specifies the full name of the type. + /// + [Fact] + public void OneAttributeNamedWithType() { + WriteCodeFragment task = new WriteCodeFragment(); + MockEngine engine = new MockEngine(true); + task.BuildEngine = engine; + TaskItem attribute = new TaskItem("TestAttribute"); + attribute.SetMetadata("BoolArgument:System.Boolean", "False"); + attribute.SetMetadata("Int32Argument:System.Int32", "42"); + task.AssemblyAttributes = new TaskItem[] { attribute }; + task.Language = "c#"; + task.OutputDirectory = new TaskItem(Path.GetTempPath()); + bool result = task.Execute(); + + Assert.True(result); + + string content = File.ReadAllText(task.OutputFile.ItemSpec); + Console.WriteLine(content); + + CheckContentCSharp(content, @"[assembly: TestAttribute(BoolArgument=false, Int32Argument=42)]"); + + File.Delete(task.OutputFile.ItemSpec); + } + + /// + /// This tests that the parameter name can be a single character and also specify a type name. + /// + [Fact] + public void SingleCharacterNamedWithType() { + WriteCodeFragment task = new WriteCodeFragment(); + MockEngine engine = new MockEngine(true); + task.BuildEngine = engine; + TaskItem attribute = new TaskItem("TestAttribute"); + attribute.SetMetadata("X:System.Int32", "99"); + task.AssemblyAttributes = new TaskItem[] { attribute }; + task.Language = "c#"; + task.OutputDirectory = new TaskItem(Path.GetTempPath()); + bool result = task.Execute(); + + Assert.True(result); + + string content = File.ReadAllText(task.OutputFile.ItemSpec); + Console.WriteLine(content); + + CheckContentCSharp(content, @"[assembly: TestAttribute(X=99)]"); + + File.Delete(task.OutputFile.ItemSpec); + } + + /// + /// An unknown type name for a parameter should cause a failure. + /// + [Fact] + public void UnknownParameterTypeName() { + WriteCodeFragment task = new WriteCodeFragment(); + MockEngine engine = new MockEngine(true); + task.BuildEngine = engine; + TaskItem attribute = new TaskItem("TestAttribute"); + attribute.SetMetadata("Parameter:Foo.Bar", "99"); + task.AssemblyAttributes = new TaskItem[] { attribute }; + task.Language = "c#"; + task.OutputFile = new TaskItem("foo"); + bool result = task.Execute(); + + Assert.False(result); + engine.AssertLogContains("MSB3715"); + } + + /// + /// A parameter value that cannot be converted to the specified type should cause a failure. + /// + [Fact] + public void ParameterCannotBeConvertedToType() { + WriteCodeFragment task = new WriteCodeFragment(); + MockEngine engine = new MockEngine(true); + task.BuildEngine = engine; + TaskItem attribute = new TaskItem("TestAttribute"); + attribute.SetMetadata("Parameter:System.Boolean", "99"); + task.AssemblyAttributes = new TaskItem[] { attribute }; + task.Language = "c#"; + task.OutputFile = new TaskItem("foo"); + bool result = task.Execute(); + + Assert.False(result); + engine.AssertLogContains("MSB3716"); + } + + /// + /// Parameter value that is too large for the data type should cause a failure. + /// + [Fact] + public void ParameterValueCausesOverflow() + { + WriteCodeFragment task = new WriteCodeFragment(); + MockEngine engine = new MockEngine(true); + task.BuildEngine = engine; + TaskItem attribute = new TaskItem("TestAttribute"); + attribute.SetMetadata("Parameter:System.Byte", "1000"); + task.AssemblyAttributes = new TaskItem[] { attribute }; + task.Language = "c#"; + task.OutputFile = new TaskItem("foo"); + bool result = task.Execute(); + + Assert.False(result); + engine.AssertLogContains("MSB3716"); + } + private static void CheckContentCSharp(string actualContent, params string[] expectedAttributes) { CheckContent( diff --git a/src/Tasks/Resources/Strings.resx b/src/Tasks/Resources/Strings.resx index a054ea6c65a..1fe62cb61fa 100644 --- a/src/Tasks/Resources/Strings.resx +++ b/src/Tasks/Resources/Strings.resx @@ -1,4 +1,4 @@ - +