-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues in
AttributeStatement
(#4526)
Fixes #4525
- Loading branch information
1 parent
3fdc695
commit fa68969
Showing
11 changed files
with
215 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...csharp/generator/Microsoft.Generator.CSharp/test/Providers/ProviderWithAttributesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Providers; | ||
using Microsoft.Generator.CSharp.Statements; | ||
using Microsoft.Generator.CSharp.Tests.Common; | ||
using NUnit.Framework; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Microsoft.Generator.CSharp.Tests.Providers | ||
{ | ||
public class ProviderWithAttributesTests | ||
{ | ||
public ProviderWithAttributesTests() | ||
{ | ||
MockHelpers.LoadMockPlugin(); | ||
} | ||
|
||
private class ProviderWithAttribute : TypeProvider | ||
{ | ||
public ProviderWithAttribute() | ||
{ | ||
} | ||
|
||
protected override string BuildName() => "ProviderWithAttributes"; | ||
|
||
protected override string BuildRelativeFilePath() => "."; | ||
|
||
protected override IReadOnlyList<AttributeStatement> BuildAttributes() | ||
{ | ||
return [ | ||
new(typeof(ObsoleteAttribute)), | ||
new(typeof(ObsoleteAttribute), Literal("This is obsolete")), | ||
new(typeof(ObsoleteAttribute), Literal("This is obsolete"), Literal(true)), | ||
new(typeof(ObsoleteAttribute), [new KeyValuePair<string, ValueExpression>("DiagnosticId", Literal("TypeSpecGenerator001"))]), | ||
new(typeof(ObsoleteAttribute), [new KeyValuePair<string, ValueExpression>("DiagnosticId", Literal("TypeSpecGenerator001")), new KeyValuePair<string, ValueExpression>("UrlFormat", Literal("my-format"))]), | ||
new(typeof(ObsoleteAttribute), [Literal("This is obsolete"), Literal(true)], | ||
[new KeyValuePair<string, ValueExpression>("DiagnosticId", Literal("TypeSpecGenerator001")), new KeyValuePair<string, ValueExpression>("UrlFormat", Literal("my-format"))]), | ||
]; | ||
} | ||
} | ||
|
||
[Test] | ||
public void ValidateAttributes() | ||
{ | ||
var provider = new ProviderWithAttribute(); | ||
var writer = CodeModelPlugin.Instance.GetWriter(provider); | ||
var content = writer.Write(); | ||
Assert.AreEqual(Helpers.GetExpectedFromFile(), content.Content); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...enerator.CSharp/test/Providers/TestData/ProviderWithAttributesTests/ValidateAttributes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
|
||
namespace Sample | ||
{ | ||
/// <summary></summary> | ||
[global::System.ObsoleteAttribute] | ||
[global::System.ObsoleteAttribute("This is obsolete")] | ||
[global::System.ObsoleteAttribute("This is obsolete", true)] | ||
[global::System.ObsoleteAttribute(DiagnosticId = "TypeSpecGenerator001")] | ||
[global::System.ObsoleteAttribute(DiagnosticId = "TypeSpecGenerator001", UrlFormat = "my-format")] | ||
[global::System.ObsoleteAttribute("This is obsolete", true, DiagnosticId = "TypeSpecGenerator001", UrlFormat = "my-format")] | ||
public partial class ProviderWithAttributes | ||
{ | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...nt-csharp/generator/Microsoft.Generator.CSharp/test/Statements/AttributeStatementTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Statements; | ||
using Microsoft.Generator.CSharp.Tests.Common; | ||
using NUnit.Framework; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Microsoft.Generator.CSharp.Tests.Statements | ||
{ | ||
public class AttributeStatementTests | ||
{ | ||
public AttributeStatementTests() | ||
{ | ||
MockHelpers.LoadMockPlugin(); | ||
} | ||
|
||
[Test] | ||
public void AttributeStatementWithNoArgument() | ||
{ | ||
var attributeStatement = new AttributeStatement(typeof(ObsoleteAttribute)); | ||
|
||
using var writer = new CodeWriter(); | ||
attributeStatement.Write(writer); | ||
|
||
Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.ToString(false)); | ||
} | ||
|
||
[Test] | ||
public void AttributeStatementWithOneArgument() | ||
{ | ||
var attributeStatement = new AttributeStatement(typeof(ObsoleteAttribute), Literal("This is obsolete")); | ||
|
||
using var writer = new CodeWriter(); | ||
attributeStatement.Write(writer); | ||
|
||
Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.ToString(false)); | ||
} | ||
|
||
[Test] | ||
public void AttributeStatementWithMultipleArguments() | ||
{ | ||
var attributeStatement = new AttributeStatement(typeof(ObsoleteAttribute), Literal("This is obsolete"), Literal(true)); | ||
|
||
using var writer = new CodeWriter(); | ||
attributeStatement.Write(writer); | ||
|
||
Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.ToString(false)); | ||
} | ||
|
||
[Test] | ||
public void AttributeStatementWithOneNamedArgument() | ||
{ | ||
var attributeStatement = new AttributeStatement(typeof(ObsoleteAttribute), | ||
[ | ||
new KeyValuePair<string, ValueExpression>("DiagnosticId", Literal("TypeSpecGenerator001")) | ||
]); | ||
|
||
using var writer = new CodeWriter(); | ||
attributeStatement.Write(writer); | ||
|
||
Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.ToString(false)); | ||
} | ||
|
||
[Test] | ||
public void AttributeStatementWithNamedArguments() | ||
{ | ||
var attributeStatement = new AttributeStatement(typeof(ObsoleteAttribute), | ||
[ | ||
new KeyValuePair<string, ValueExpression>("DiagnosticId", Literal("TypeSpecGenerator001")), | ||
new KeyValuePair<string, ValueExpression>("UrlFormat", Literal("my-format")) | ||
]); | ||
|
||
using var writer = new CodeWriter(); | ||
attributeStatement.Write(writer); | ||
|
||
Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.ToString(false)); | ||
} | ||
|
||
[Test] | ||
public void AttributeStatementWithArgumentsAndNamedArguments() | ||
{ | ||
var attributeStatement = new AttributeStatement(typeof(ObsoleteAttribute), [Literal("This is obsolete"), Literal(true)], | ||
[ | ||
new KeyValuePair<string, ValueExpression>("DiagnosticId", Literal("TypeSpecGenerator001")), | ||
new KeyValuePair<string, ValueExpression>("UrlFormat", Literal("my-format")) | ||
]); | ||
|
||
using var writer = new CodeWriter(); | ||
attributeStatement.Write(writer); | ||
|
||
Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.ToString(false)); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ents/TestData/AttributeStatementTests/AttributeStatementWithArgumentsAndNamedArguments.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[global::System.ObsoleteAttribute("This is obsolete", true, DiagnosticId = "TypeSpecGenerator001", UrlFormat = "my-format")] |
1 change: 1 addition & 0 deletions
1
...st/Statements/TestData/AttributeStatementTests/AttributeStatementWithMultipleArguments.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[global::System.ObsoleteAttribute("This is obsolete", true)] |
1 change: 1 addition & 0 deletions
1
.../test/Statements/TestData/AttributeStatementTests/AttributeStatementWithNamedArguments.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[global::System.ObsoleteAttribute(DiagnosticId = "TypeSpecGenerator001", UrlFormat = "my-format")] |
1 change: 1 addition & 0 deletions
1
...harp/test/Statements/TestData/AttributeStatementTests/AttributeStatementWithNoArgument.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[global::System.ObsoleteAttribute] |
1 change: 1 addition & 0 deletions
1
...arp/test/Statements/TestData/AttributeStatementTests/AttributeStatementWithOneArgument.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[global::System.ObsoleteAttribute("This is obsolete")] |
1 change: 1 addition & 0 deletions
1
...est/Statements/TestData/AttributeStatementTests/AttributeStatementWithOneNamedArgument.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[global::System.ObsoleteAttribute(DiagnosticId = "TypeSpecGenerator001")] |