/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Compilers/CSharp/Test/Syntax/Parsing/SingleLineDeclarationParsingTests.cs
858 строк
28 KB
CyrusNajmabadi
Move IDS_FeatureXXX checks out of the parser (#65413)
18 ноя 2022, 07:01
Не верифицирован
18 ноя 2022, 07:01
dc04e49
Код
Авторство
О чём код?
// 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. #nullable disable using System; using System.Linq; using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Test.Utilities; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; using Xunit; using Xunit.Abstractions; namespace Microsoft.CodeAnalysis.CSharp.UnitTests { public class SingleLineDeclarationParsingTests : ParsingTests { public SingleLineDeclarationParsingTests(ITestOutputHelper output) : base(output) { } protected override SyntaxTree ParseTree(string text, CSharpParseOptions options) { return SyntaxFactory.ParseSyntaxTree(text, options); } [Fact] public void NamespaceWithNoNameSemicolonOrBraces() { UsingNode( @"namespace", TestOptions.RegularPreview, // (1,10): error CS1001: Identifier expected // namespace Diagnostic(ErrorCode.ERR_IdentifierExpected, "").WithLocation(1, 10), // (1,10): error CS1514: { expected // namespace Diagnostic(ErrorCode.ERR_LbraceExpected, "").WithLocation(1, 10), // (1,10): error CS1513: } expected // namespace Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(1, 10)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.NamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); M(SyntaxKind.IdentifierName); { M(SyntaxKind.IdentifierToken); } M(SyntaxKind.OpenBraceToken); M(SyntaxKind.CloseBraceToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithNoSemicolonOrBraces1() { UsingNode( @"namespace A", TestOptions.RegularPreview, // (1,12): error CS1514: { expected // namespace A Diagnostic(ErrorCode.ERR_LbraceExpected, "").WithLocation(1, 12), // (1,12): error CS1513: } expected // namespace A Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(1, 12)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.NamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } M(SyntaxKind.OpenBraceToken); M(SyntaxKind.CloseBraceToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithNoSemicolonOrBraces2() { UsingNode( @"namespace A.", TestOptions.RegularPreview, // (1,13): error CS1001: Identifier expected // namespace A. Diagnostic(ErrorCode.ERR_IdentifierExpected, "").WithLocation(1, 13), // (1,13): error CS1514: { expected // namespace A. Diagnostic(ErrorCode.ERR_LbraceExpected, "").WithLocation(1, 13), // (1,13): error CS1513: } expected // namespace A. Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(1, 13)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.NamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.QualifiedName); { N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.DotToken); M(SyntaxKind.IdentifierName); { M(SyntaxKind.IdentifierToken); } } M(SyntaxKind.OpenBraceToken); M(SyntaxKind.CloseBraceToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithNoSemicolonOrBraces3() { UsingNode( @"namespace A.B", TestOptions.RegularPreview, // (1,14): error CS1514: { expected // namespace A.B Diagnostic(ErrorCode.ERR_LbraceExpected, "").WithLocation(1, 14), // (1,14): error CS1513: } expected // namespace A.B Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(1, 14)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.NamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.QualifiedName); { N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.DotToken); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "B"); } } M(SyntaxKind.OpenBraceToken); M(SyntaxKind.CloseBraceToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithSemicolon1() { UsingNode( @"namespace A;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithSemicolon_CSharp9() { var test = @"namespace A;"; CreateCompilation(test, parseOptions: TestOptions.Regular9).VerifyDiagnostics( // (1,1): error CS8773: Feature 'file-scoped namespace' is not available in C# 9.0. Please use language version 10.0 or greater. // namespace A; Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "namespace").WithArguments("file-scoped namespace", "10.0").WithLocation(1, 1)); UsingNode(test, TestOptions.Regular9); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithSemicolon2() { UsingNode( @"namespace A.;", TestOptions.RegularPreview, // (1,13): error CS1001: Identifier expected // namespace A.; Diagnostic(ErrorCode.ERR_IdentifierExpected, ";").WithLocation(1, 13)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.QualifiedName); { N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.DotToken); M(SyntaxKind.IdentifierName); { M(SyntaxKind.IdentifierToken); } } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithSemicolon3() { UsingNode( @"namespace A.B;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.QualifiedName); { N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.DotToken); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "B"); } } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithSemicolonAndOpenBrace() { UsingNode( @"namespace A; {", TestOptions.RegularPreview, // (1,14): error CS1022: Type or namespace definition, or end-of-file expected // namespace A; { Diagnostic(ErrorCode.ERR_EOFExpected, "{").WithLocation(1, 14)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithSemicolonAndBraces() { UsingNode( @"namespace A; { }", TestOptions.RegularPreview, // (1,14): error CS1022: Type or namespace definition, or end-of-file expected // namespace A; { } Diagnostic(ErrorCode.ERR_EOFExpected, "{").WithLocation(1, 14), // (1,16): error CS1022: Type or namespace definition, or end-of-file expected // namespace A; { } Diagnostic(ErrorCode.ERR_EOFExpected, "}").WithLocation(1, 16)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithSemicolonAndCloseBrace() { UsingNode( @"namespace A; }", TestOptions.RegularPreview, // (1,14): error CS1022: Type or namespace definition, or end-of-file expected // namespace A; } Diagnostic(ErrorCode.ERR_EOFExpected, "}").WithLocation(1, 14)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void MultipleFileScopedNamespaces() { UsingNode( @"namespace A; namespace B;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "B"); } N(SyntaxKind.SemicolonToken); } } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void FileScopedNamespaceNestedInNormalNamespace() { UsingNode( @"namespace A { namespace B; }", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.NamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.OpenBraceToken); N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "B"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.CloseBraceToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NormalAndFileScopedNamespace1() { UsingNode( @"namespace A; namespace B { }", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); N(SyntaxKind.NamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "B"); } N(SyntaxKind.OpenBraceToken); N(SyntaxKind.CloseBraceToken); } } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NormalAndFileScopedNamespace2() { UsingNode( @"namespace A { } namespace B;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.NamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.OpenBraceToken); N(SyntaxKind.CloseBraceToken); } N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "B"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithPrecedingUsing() { UsingNode( @"using X; namespace A;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.UsingDirective); { N(SyntaxKind.UsingKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "X"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithFollowingUsing() { UsingNode( @"namespace A; using X;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); N(SyntaxKind.UsingDirective); { N(SyntaxKind.UsingKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "X"); } N(SyntaxKind.SemicolonToken); } } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithPrecedingType() { UsingNode( @"class X { } namespace A;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.ClassDeclaration); { N(SyntaxKind.ClassKeyword); N(SyntaxKind.IdentifierToken, "X"); N(SyntaxKind.OpenBraceToken); N(SyntaxKind.CloseBraceToken); } N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithFollowingType() { UsingNode( @"namespace A; class X { }", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); N(SyntaxKind.ClassDeclaration); { N(SyntaxKind.ClassKeyword); N(SyntaxKind.IdentifierToken, "X"); N(SyntaxKind.OpenBraceToken); N(SyntaxKind.CloseBraceToken); } } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithPrecedingExtern() { UsingNode( @"extern alias X; namespace A;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.ExternAliasDirective); { N(SyntaxKind.ExternKeyword); N(SyntaxKind.AliasKeyword); N(SyntaxKind.IdentifierToken, "X"); N(SyntaxKind.SemicolonToken); } N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithFollowingExtern() { UsingNode( @"namespace A; extern alias X;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); N(SyntaxKind.ExternAliasDirective); { N(SyntaxKind.ExternKeyword); N(SyntaxKind.AliasKeyword); N(SyntaxKind.IdentifierToken, "X"); N(SyntaxKind.SemicolonToken); } } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithExtraSemicolon() { UsingNode( @"namespace A;;", TestOptions.RegularPreview, // (1,13): error CS1022: Type or namespace definition, or end-of-file expected // namespace A;; Diagnostic(ErrorCode.ERR_EOFExpected, ";").WithLocation(1, 13)); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithGenericName() { UsingNode( @"namespace A<X>;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.GenericName); { N(SyntaxKind.IdentifierToken, "A"); N(SyntaxKind.TypeArgumentList); { N(SyntaxKind.LessThanToken); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "X"); } N(SyntaxKind.GreaterThanToken); } } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithAlias() { UsingNode( @"namespace A::B;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.AliasQualifiedName); { N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.ColonColonToken); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "B"); } } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithModifiers() { UsingNode( @"public namespace A;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.PublicKeyword); N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceWithAttributes() { UsingNode( @"[X] namespace A;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.AttributeList); { N(SyntaxKind.OpenBracketToken); N(SyntaxKind.Attribute); { N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "X"); } } N(SyntaxKind.CloseBracketToken); } N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); } N(SyntaxKind.EndOfFileToken); } EOF(); } [Fact] public void NamespaceFollowedByVariable() { UsingNode( @"namespace A; int x;", TestOptions.RegularPreview); N(SyntaxKind.CompilationUnit); { N(SyntaxKind.FileScopedNamespaceDeclaration); { N(SyntaxKind.NamespaceKeyword); N(SyntaxKind.IdentifierName); { N(SyntaxKind.IdentifierToken, "A"); } N(SyntaxKind.SemicolonToken); N(SyntaxKind.FieldDeclaration); { N(SyntaxKind.VariableDeclaration); { N(SyntaxKind.PredefinedType); { N(SyntaxKind.IntKeyword); } N(SyntaxKind.VariableDeclarator); { N(SyntaxKind.IdentifierToken, "x"); } } N(SyntaxKind.SemicolonToken); } } N(SyntaxKind.EndOfFileToken); } EOF(); } } }