Skip to content

Commit

Permalink
Add some Global Using Directive tests (#52866)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyTs authored Apr 23, 2021
1 parent eb94fe9 commit f53f0ad
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4070,13 +4070,15 @@ class alias1 {}
var node = tree.GetRoot().DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
var model = comp.GetSemanticModel(tree);
Assert.Equal("C1", model.GetTypeInfo(node).Type.ToTestDisplayString());
Assert.Equal("alias1=C1", model.GetAliasInfo(node.Type).ToTestDisplayString());

comp = CreateCompilation(new[] { usings1 + source, globalUsings1 }, parseOptions: TestOptions.RegularPreview);
comp.GetDiagnostics().Where(d => d.Code is not ((int)ErrorCode.ERR_BadExternAlias or (int)ErrorCode.HDN_UnusedUsingDirective)).Verify(expected2);
tree = comp.SyntaxTrees[0];
node = tree.GetRoot().DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
model = comp.GetSemanticModel(tree);
Assert.Equal("C1", model.GetTypeInfo(node).Type.ToTestDisplayString());
Assert.Equal("alias1=C1", model.GetAliasInfo(node.Type).ToTestDisplayString());

comp = CreateCompilation(new[] { externAlias1 + globalUsings1 + usings1 + source }, parseOptions: TestOptions.RegularPreview);
comp.GetDiagnostics().Where(d => d.Code is not ((int)ErrorCode.ERR_BadExternAlias or (int)ErrorCode.ERR_DuplicateAlias or (int)ErrorCode.HDN_UnusedUsingDirective)).Verify(expected1);
Expand Down Expand Up @@ -4104,27 +4106,31 @@ class alias1 {}
node = tree.GetRoot().DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
model = comp.GetSemanticModel(tree);
Assert.Equal("C1", model.GetTypeInfo(node).Type.ToTestDisplayString());
Assert.Equal("alias1=C1", model.GetAliasInfo(node.Type).ToTestDisplayString());

comp = CreateCompilation(new[] { globalUsings1 + source, globalUsings2 }, parseOptions: TestOptions.RegularPreview);
comp.GetDiagnostics().Where(d => d.Code is not ((int)ErrorCode.ERR_BadExternAlias or (int)ErrorCode.HDN_UnusedUsingDirective)).Verify(expected3);
tree = comp.SyntaxTrees[0];
node = tree.GetRoot().DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
model = comp.GetSemanticModel(tree);
Assert.Equal("C1", model.GetTypeInfo(node).Type.ToTestDisplayString());
Assert.Equal("alias1=C1", model.GetAliasInfo(node.Type).ToTestDisplayString());

comp = CreateCompilation(new[] { source, globalUsings1 + globalUsings2 }, parseOptions: TestOptions.RegularPreview);
comp.GetDiagnostics().Where(d => d.Code is not ((int)ErrorCode.ERR_BadExternAlias or (int)ErrorCode.HDN_UnusedUsingDirective)).Verify(expected3);
tree = comp.SyntaxTrees[0];
node = tree.GetRoot().DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
model = comp.GetSemanticModel(tree);
Assert.Equal("C1", model.GetTypeInfo(node).Type.ToTestDisplayString());
Assert.Equal("alias1=C1", model.GetAliasInfo(node.Type).ToTestDisplayString());

comp = CreateCompilation(new[] { source, globalUsings1, globalUsings2 }, parseOptions: TestOptions.RegularPreview);
comp.GetDiagnostics().Where(d => d.Code is not ((int)ErrorCode.ERR_BadExternAlias or (int)ErrorCode.HDN_UnusedUsingDirective)).Verify(expected3);
tree = comp.SyntaxTrees[0];
node = tree.GetRoot().DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
model = comp.GetSemanticModel(tree);
Assert.Equal("C1", model.GetTypeInfo(node).Type.ToTestDisplayString());
Assert.Equal("alias1=C1", model.GetAliasInfo(node.Type).ToTestDisplayString());

comp = CreateCompilation(new[] { usings1 + usings2 + source }, parseOptions: TestOptions.RegularPreview);
comp.GetDiagnostics().Where(d => d.Code is not ((int)ErrorCode.ERR_BadExternAlias or (int)ErrorCode.HDN_UnusedUsingDirective)).Verify(
Expand All @@ -4136,6 +4142,7 @@ class alias1 {}
node = tree.GetRoot().DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
model = comp.GetSemanticModel(tree);
Assert.Equal("C3", model.GetTypeInfo(node).Type.ToTestDisplayString());
Assert.Equal("alias1=C3", model.GetAliasInfo(node.Type).ToTestDisplayString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,17 @@ public void TestNormalizeDeclaration1()
TestNormalizeDeclaration("using a.b;", "using a.b;");
TestNormalizeDeclaration("using A; using B; class C {}", "using A;\r\nusing B;\r\n\r\nclass C\r\n{\r\n}");

TestNormalizeDeclaration("global using a;", "global using a;");
TestNormalizeDeclaration("global using a=b;", "global using a = b;");
TestNormalizeDeclaration("global using a.b;", "global using a.b;");
TestNormalizeDeclaration("global using A; global using B; class C {}", "global using A;\r\nglobal using B;\r\n\r\nclass C\r\n{\r\n}");
TestNormalizeDeclaration("global using A; using B; class C {}", "global using A;\r\nusing B;\r\n\r\nclass C\r\n{\r\n}");
TestNormalizeDeclaration("using A; global using B; class C {}", "using A;\r\nglobal using B;\r\n\r\nclass C\r\n{\r\n}");

// namespace
TestNormalizeDeclaration("namespace a{}", "namespace a\r\n{\r\n}");
TestNormalizeDeclaration("namespace a{using b;}", "namespace a\r\n{\r\n using b;\r\n}");
TestNormalizeDeclaration("namespace a{global using b;}", "namespace a\r\n{\r\n global using b;\r\n}");
TestNormalizeDeclaration("namespace a{namespace b{}}", "namespace a\r\n{\r\n namespace b\r\n {\r\n }\r\n}");
TestNormalizeDeclaration("namespace a{}namespace b{}", "namespace a\r\n{\r\n}\r\n\r\nnamespace b\r\n{\r\n}");

Expand Down
226 changes: 226 additions & 0 deletions src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -922,5 +922,231 @@ public void GlobalUsingDirective_18()
}
EOF();
}

[Fact]
public void GlobalUsingDirective_19()
{
var test = @"
M();
global using ns1;
";

UsingTree(test, TestOptions.RegularPreview,
// (3,1): error CS1529: A using clause must precede all other elements defined in the namespace except extern alias declarations
// global using ns1;
Diagnostic(ErrorCode.ERR_UsingAfterElements, "global using ns1;").WithLocation(3, 1)
);

N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.GlobalStatement);
{
N(SyntaxKind.ExpressionStatement);
{
N(SyntaxKind.InvocationExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "M");
}
N(SyntaxKind.ArgumentList);
{
N(SyntaxKind.OpenParenToken);
N(SyntaxKind.CloseParenToken);
}
}
N(SyntaxKind.SemicolonToken);
}
}
N(SyntaxKind.EndOfFileToken);
}
EOF();
}

[Fact]
public void GlobalUsingDirective_20()
{
var test = @"
global using ns1;
using ns2;
M();
";

UsingTree(test, TestOptions.RegularPreview);

N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.UsingDirective);
{
N(SyntaxKind.GlobalKeyword);
N(SyntaxKind.UsingKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "ns1");
}
N(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.UsingDirective);
{
N(SyntaxKind.UsingKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "ns2");
}
N(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.GlobalStatement);
{
N(SyntaxKind.ExpressionStatement);
{
N(SyntaxKind.InvocationExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "M");
}
N(SyntaxKind.ArgumentList);
{
N(SyntaxKind.OpenParenToken);
N(SyntaxKind.CloseParenToken);
}
}
N(SyntaxKind.SemicolonToken);
}
}
N(SyntaxKind.EndOfFileToken);
}
EOF();
}

[Fact]
public void GlobalUsingDirective_21()
{
var test = @"
global using alias1 = ns1;
using alias2 = ns2;
M();
";

UsingTree(test, TestOptions.RegularPreview);

N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.UsingDirective);
{
N(SyntaxKind.GlobalKeyword);
N(SyntaxKind.UsingKeyword);
N(SyntaxKind.NameEquals);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "alias1");
}
N(SyntaxKind.EqualsToken);
}
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "ns1");
}
N(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.UsingDirective);
{
N(SyntaxKind.UsingKeyword);
N(SyntaxKind.NameEquals);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "alias2");
}
N(SyntaxKind.EqualsToken);
}
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "ns2");
}
N(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.GlobalStatement);
{
N(SyntaxKind.ExpressionStatement);
{
N(SyntaxKind.InvocationExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "M");
}
N(SyntaxKind.ArgumentList);
{
N(SyntaxKind.OpenParenToken);
N(SyntaxKind.CloseParenToken);
}
}
N(SyntaxKind.SemicolonToken);
}
}
N(SyntaxKind.EndOfFileToken);
}
EOF();
}

[Fact]
public void GlobalUsingDirective_22()
{
var test = @"
global using static ns1;
using static ns2;
M();
";

UsingTree(test, TestOptions.RegularPreview);

N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.UsingDirective);
{
N(SyntaxKind.GlobalKeyword);
N(SyntaxKind.UsingKeyword);
N(SyntaxKind.StaticKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "ns1");
}
N(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.UsingDirective);
{
N(SyntaxKind.UsingKeyword);
N(SyntaxKind.StaticKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "ns2");
}
N(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.GlobalStatement);
{
N(SyntaxKind.ExpressionStatement);
{
N(SyntaxKind.InvocationExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "M");
}
N(SyntaxKind.ArgumentList);
{
N(SyntaxKind.OpenParenToken);
N(SyntaxKind.CloseParenToken);
}
}
N(SyntaxKind.SemicolonToken);
}
}
N(SyntaxKind.EndOfFileToken);
}
EOF();
}
}
}

0 comments on commit f53f0ad

Please sign in to comment.