Skip to content

Commit

Permalink
Error tests for locations
Browse files Browse the repository at this point in the history
  • Loading branch information
chsienki committed Feb 11, 2022
1 parent 4e31b6d commit 7d10648
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3302,7 +3302,7 @@ A catch() block after a catch (System.Exception e) block can catch non-CLS excep
<value>'{0}' does not contain a constructor that takes {1} arguments</value>
</data>
<data name="ERR_GlobalAttributesNotFirst" xml:space="preserve">
<value>Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations</value>
<value>Assembly, module, and main attributes must precede all other elements defined in a file except using clauses and extern alias declarations</value>
</data>
<data name="ERR_ExpressionExpected" xml:space="preserve">
<value>Expected expression</value>
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

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

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

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

45 changes: 45 additions & 0 deletions src/Compilers/CSharp/Test/Emit2/Attributes/AttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5149,6 +5149,51 @@ public class MyAttribute : Attribute { public MyAttribute(string name) {} }
CompileAndVerify(source, expectedOutput: "Hello World");
}

[Fact]
public void TestMainAttributesAfterCode()
{
var source = CreateCompilation(@"
using System;
Console.WriteLine(""Hello World"");

[main: My(""one"")]
public class MyAttribute : Attribute { public MyAttribute(string name) {} }
");
source.VerifyDiagnostics(
// (5,2): error CS1730: Assembly, module, and main attributes must precede all other elements defined in a file except using clauses and extern alias declarations
// [main: My("one")]
Diagnostic(ErrorCode.ERR_GlobalAttributesNotFirst, "main").WithLocation(5, 2)
);
}

[Theory]
[InlineData("All", true)]
[InlineData("Method", true)]
[InlineData("Class", false)]
[InlineData("Field", false)]
[InlineData("Assembly", false)]
[InlineData("Module", false)]
public void TestMainAttributesWithSpecificLocation(string location, bool valid)
{
var source = CreateCompilation($@"
using System;

[main: My(""one"")]
Console.WriteLine(""Hello World"");

[AttributeUsage(AttributeTargets.{location}, AllowMultiple = true)]
public class MyAttribute : Attribute {{ public MyAttribute(string name) {{}} }}
");

source.VerifyDiagnostics(valid ? Array.Empty<DiagnosticDescription>() :
new[]
{
// (4,8): error CS0592: Attribute 'My' is not valid on this declaration type. It is only valid on '...' declarations.
// [main: My("one")]
Diagnostic(ErrorCode.ERR_AttributeOnBadSymbolType, "My").WithArguments("My", location.ToLower()).WithLocation(4, 8)
});
}

#endregion

#region Error Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class A : Attribute { }
";

CreateCompilation(new[] { source1, source2 }).VerifyDiagnostics(
// (4,6): error CS1730: Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations
// (4,6): error CS1730: Assembly, module, and main attributes must precede all other elements defined in a file except using clauses and extern alias declarations
Diagnostic(ErrorCode.ERR_GlobalAttributesNotFirst, "assembly"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5871,7 +5871,7 @@ class MyAttribute : System.Attribute
var comp = CreateCompilation(text, options: TestOptions.DebugExe, parseOptions: DefaultParseOptions);

comp.VerifyDiagnostics(
// (4,2): error CS1730: Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations
// (4,2): error CS1730: Assembly, module, and main attributes must precede all other elements defined in a file except using clauses and extern alias declarations
// [module: MyAttribute]
Diagnostic(ErrorCode.ERR_GlobalAttributesNotFirst, "module").WithLocation(4, 2)
);
Expand Down

0 comments on commit 7d10648

Please sign in to comment.