Skip to content

Commit

Permalink
Allow parameter type name to be specified for WriteCodeFragment task.
Browse files Browse the repository at this point in the history
  • Loading branch information
reduckted committed Mar 13, 2021
1 parent 38da84d commit 289e3b4
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 2 deletions.
137 changes: 137 additions & 0 deletions src/Tasks.UnitTests/WriteCodeFragment_Tests.cs
Expand Up @@ -655,6 +655,143 @@ public void OneAttributePositionalAndNamedParamsVisualBasic()
File.Delete(task.OutputFile.ItemSpec);
}

/// <summary>
/// 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.
/// </summary>
[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);
}

/// <summary>
/// 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.
/// </summary>
[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);
}

/// <summary>
/// This tests that the parameter name can be a single character and also specify a type name.
/// </summary>
[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);
}

/// <summary>
/// An unknown type name for a parameter should cause a failure.
/// </summary>
[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");
}

/// <summary>
/// A parameter value that cannot be converted to the specified type should cause a failure.
/// </summary>
[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");
}

/// <summary>
/// Parameter value that is too large for the data type should cause a failure.
/// </summary>
[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(
Expand Down
10 changes: 9 additions & 1 deletion src/Tasks/Resources/Strings.resx
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Expand Down Expand Up @@ -2374,6 +2374,14 @@
<value>MSB3714: The parameter "{0}" was supplied, but not all previously numbered parameters.</value>
<comment>{StrBegin="MSB3714: "}</comment>
</data>
<data name="WriteCodeFragment.ParameterTypeNotFound" xml:space="preserve">
<value>MSB3715: The type "{0}" was not found.</value>
<comment>{StrBegin="MSB3715: "}</comment>
</data>
<data name="WriteCodeFragment.CouldNotConvertValue" xml:space="preserve">
<value>MSB3716: The parameter value "{0}" could not be converted to "{1}". {2}</value>
<comment>{StrBegin="MSB3716: "}</comment>
</data>
<data name="WriteCodeFragment.NoWorkToDo" xml:space="preserve">
<value>No output file was written because no code was specified to create.</value>
</data>
Expand Down
10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.en.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 289e3b4

Please sign in to comment.