提交 1a5cabbf 编写于 作者: N Neal Gafter 提交者: GitHub

Add further tests for pattern-matching in global code (#17092)

Fixes #14146
上级 4f8c86bc
......@@ -121,6 +121,16 @@ protected static void VerifyModelForDeclarationPatternDuplicateInSameScope(Seman
}
}
protected static void VerifyNotAPatternField(SemanticModel model, IdentifierNameSyntax reference)
{
var symbol = model.GetSymbolInfo(reference).Symbol;
Assert.NotEqual(SymbolKind.Field, symbol.Kind);
Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: reference.Identifier.ValueText).Single());
Assert.True(model.LookupNames(reference.SpanStart).Contains(reference.Identifier.ValueText));
}
protected static void VerifyNotAPatternLocal(SemanticModel model, IdentifierNameSyntax reference)
{
var symbol = model.GetSymbolInfo(reference).Symbol;
......@@ -301,6 +311,11 @@ protected static void VerifyModelNotSupported(SemanticModel model, params Identi
}
}
protected static void AssertNoGlobalStatements(SyntaxTree tree)
{
Assert.Empty(tree.GetRoot().DescendantNodes().OfType<GlobalStatementSyntax>());
}
#endregion helpers
}
}
......@@ -795,6 +795,13 @@ static bool Dummy(int x)
var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe);
CompileAndVerify(compilation, expectedOutput: @"1
True");
var tree = compilation.SyntaxTrees.Single();
var model = compilation.GetSemanticModel(tree);
var x1Decl = GetPatternDeclaration(tree, "x1");
var x1Ref = GetReferences(tree, "x1").Single();
VerifyModelForDeclarationPattern(model, x1Decl, x1Ref);
}
[Fact]
......@@ -902,6 +909,49 @@ static bool Print(object x)
}
}
[Fact]
public void Query_02()
{
var source =
@"
using System.Linq;
public class X
{
public static void Main()
{
Test1();
}
static void Test1()
{
var res = from x1 in new[] { 1 is var y1 && Print(y1) ? 2 : 0}
select Print(x1);
res.ToArray();
}
static bool Print(object x)
{
System.Console.WriteLine(x);
return true;
}
}
";
var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular);
CompileAndVerify(compilation, expectedOutput:
@"1
2");
var tree = compilation.SyntaxTrees.Single();
var model = compilation.GetSemanticModel(tree);
var yDecl = GetPatternDeclaration(tree, "y1");
var yRef = GetReferences(tree, "y1").Single();
VerifyModelForDeclarationPattern(model, yDecl, yRef);
}
[Fact]
public void ExpressionBodiedFunctions_01()
{
......@@ -2359,6 +2409,14 @@ static System.Collections.IEnumerable Dummy(object y, object z)
CompileAndVerify(compilation, expectedOutput:
@"3
3");
var tree = compilation.SyntaxTrees.Single();
var model = compilation.GetSemanticModel(tree);
var x1Decl = GetPatternDeclaration(tree, "x1");
var x1Ref = GetReferences(tree, "x1").ToArray();
Assert.Equal(2, x1Ref.Length);
VerifyModelForDeclarationPattern(model, x1Decl, x1Ref);
}
[Fact]
......@@ -2775,6 +2833,14 @@ static bool Dummy(object y, object z)
CompileAndVerify(compilation, expectedOutput:
@"System.InvalidOperationException
System.InvalidOperationException");
var tree = compilation.SyntaxTrees.Single();
var model = compilation.GetSemanticModel(tree);
var x1Decl = GetPatternDeclaration(tree, "x1");
var x1Ref = GetReferences(tree, "x1").ToArray();
Assert.Equal(2, x1Ref.Length);
VerifyModelForDeclarationPattern(model, x1Decl, x1Ref);
}
[Fact]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册