提交 fa37d1b3 编写于 作者: J Jinu 提交者: GitHub

Adding Unit tests for IfStatement (#18060)

* Adding Unit tests for IfStatement

* Addressing PR feedback

* Addressing pr feedback

* Adding implicit embedded statement test case
上级 a703709e
......@@ -59,6 +59,7 @@
<Compile Include="Diagnostics\DiagnosticAnalyzerTests.AllInOne.cs" />
<Compile Include="Diagnostics\DiagnosticAnalyzerTests.cs" />
<Compile Include="Diagnostics\GetDiagnosticsTests.cs" />
<Compile Include="IOperation\IOperationTests_IIfStatement.cs" />
<Compile Include="IOperation\IOperationTests_ISymbolInitializer.cs" />
<Compile Include="IOperation\IOperationTests.cs" />
<Compile Include="Diagnostics\OperationAnalyzerTests.cs" />
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
public partial class IOperationTests : SemanticModelTestBase
{
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementSimpleIf()
{
string source = @"
class P
{
private void M()
{
bool condition=false;
/*<bind>*/if (true)
{
condition = true;
}/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: condition (OperationKind.LocalReferenceExpression, Type: System.Boolean)
Right: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementSimpleIfWithElse()
{
string source = @"
class P
{
private void M()
{
bool condition=false;
/*<bind>*/if (true)
{
condition = true;
}
else
{
condition = false;
}/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: condition (OperationKind.LocalReferenceExpression, Type: System.Boolean)
Right: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: condition (OperationKind.LocalReferenceExpression, Type: System.Boolean)
Right: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: False)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementSimpleIfWithConditionEvaluationTrue()
{
string source = @"
class P
{
private void M()
{
bool condition=false;
/*<bind>*/if (1==1)
{
condition = true;
}/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerEquals) (OperationKind.BinaryOperatorExpression, Type: System.Boolean, Constant: True)
Left: ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
Right: ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: condition (OperationKind.LocalReferenceExpression, Type: System.Boolean)
Right: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementSimpleIfNested1()
{
string source = @"
using System;
class P
{
private void M()
{
int m = 12;
int n = 18;
/*<bind>*/
if (m > 10)
{
if (n > 20)
Console.WriteLine(""Result1"");
}
else
{
Console.WriteLine(""Result2"");
}/*</bind>*/
}
}
";
var compilation = CreateCompilationWithMscorlib(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular);
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result1)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result2)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementSimpleIfNested2()
{
string source = @"
using System;
class P
{
private void M()
{
int m = 9;
int n = 7;
/*<bind>*/if (m > 10)
if (n > 20)
{
Console.WriteLine(""Result1"");
}
else
{
Console.WriteLine(""Result2"");
}/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result1)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result2)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithMultipleCondition()
{
string source = @"
using System;
class P
{
private void M()
{
int m = 9;
int n = 7;
int p = 5;
/*<bind>*/if (m >= n && m >= p)
{
Console.WriteLine(""Nothing is larger than m."");
}/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.BooleanConditionalAnd) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThanOrEqual) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThanOrEqual) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: p (OperationKind.LocalReferenceExpression, Type: System.Int32)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Nothing is larger than m.)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithElseIfCondition()
{
string source = @"
using System;
class P
{
private void M()
{
int m = 9;
int n = 7;
/*<bind>*/if (n > 20)
{
Console.WriteLine(""Result1"");
}
else if(m > 10)
{
Console.WriteLine(""Result2"");
}
else
{
Console.WriteLine(""Result3"");
}/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result1)
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result2)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result3)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithElseIfConditionOutVar()
{
string source = @"
class P
{
private void M()
{
var s = """";
/*<bind>*/if (int.TryParse(s, out var i))
System.Console.WriteLine($""i ={ i}, s ={ s}"");
else
System.Console.WriteLine($""i ={ i}, s ={ s}"");/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: IInvocationExpression (static System.Boolean System.Int32.TryParse(System.String s, out System.Int32 result)) (OperationKind.InvocationExpression, Type: System.Boolean)
IArgument (Matching Parameter: s) (OperationKind.Argument)
ILocalReferenceExpression: s (OperationKind.LocalReferenceExpression, Type: System.String)
IArgument (Matching Parameter: result) (OperationKind.Argument)
ILocalReferenceExpression: i (OperationKind.LocalReferenceExpression, Type: System.Int32)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
IOperation: (OperationKind.None)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
IOperation: (OperationKind.None)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithOutVar()
{
string source = @"
class P
{
private void M()
{
/*<bind>*/if (true)
System.Console.WriteLine(A());/*</bind>*/
}
private int A()
{
var s = """";
if (int.TryParse(s, out var i))
{
return i;
}
else
{
return -1;
}
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.Int32 value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
IInvocationExpression ( System.Int32 P.A()) (OperationKind.InvocationExpression, Type: System.Int32)
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementExplictEmbeddedOutVar()
{
string source = @"
class P
{
private void M()
{
var s = ""data"";
/*<bind>*/if (true)
{
A(int.TryParse(s, out var i));
}/*</bind>*/
}
private void A(bool flag)
{
if (flag)
{
System.Console.WriteLine(""Result1"");
}
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IBlockStatement (1 statements, 1 locals) (OperationKind.BlockStatement)
Local_1: System.Int32 i
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression ( void P.A(System.Boolean flag)) (OperationKind.InvocationExpression, Type: System.Void)
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P)
IArgument (Matching Parameter: flag) (OperationKind.Argument)
IInvocationExpression (static System.Boolean System.Int32.TryParse(System.String s, out System.Int32 result)) (OperationKind.InvocationExpression, Type: System.Boolean)
IArgument (Matching Parameter: s) (OperationKind.Argument)
ILocalReferenceExpression: s (OperationKind.LocalReferenceExpression, Type: System.String)
IArgument (Matching Parameter: result) (OperationKind.Argument)
ILocalReferenceExpression: i (OperationKind.LocalReferenceExpression, Type: System.Int32)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementImplicitEmbeddedOutVar()
{
string source = @"
class Program
{
static void Main(string[] args)
{
object o = 25;
/*<bind>*/if (true)
A(o is int i, 1);/*</bind>*/
}
private static void A(bool flag, int number)
{
if (flag)
{
System.Console.WriteLine(new string('*', number));
}
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IBlockStatement (1 statements, 1 locals) (OperationKind.BlockStatement)
Local_1: System.Int32 i
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void Program.A(System.Boolean flag, System.Int32 number)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: flag) (OperationKind.Argument)
IOperation: (OperationKind.None)
IArgument (Matching Parameter: number) (OperationKind.Argument)
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithConditionPattern()
{
string source = @"
using System;
class P
{
private void M()
{
object obj = ""pattern"";
/*<bind>*/if (obj is string str)
{
Console.WriteLine(str);
}/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: IOperation: (OperationKind.None)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILocalReferenceExpression: str (OperationKind.LocalReferenceExpression, Type: System.String)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithPattern()
{
string source = @"
class Program
{
static void Main(string[] args)
{
/*<bind>*/if (true)
A(25);/*</bind>*/
}
private static void A(object o)
{
if (o is null) return;
if (!(o is int i)) return;
System.Console.WriteLine(new string('*', i));
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void Program.A(System.Object o)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: o) (OperationKind.Argument)
IConversionExpression (ConversionKind.Cast, Implicit) (OperationKind.ConversionExpression, Type: System.Object)
ILiteralExpression (Text: 25) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 25)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithEmbeddedPattern()
{
string source = @"
class Program
{
static void Main(string[] args)
{
object o = 25;
/*<bind>*/if (true)
{
A(o is int i, 1);
}/*</bind>*/
}
private static void A(bool flag, int number)
{
if (flag)
{
System.Console.WriteLine(new string('*', number));
}
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IBlockStatement (1 statements, 1 locals) (OperationKind.BlockStatement)
Local_1: System.Int32 i
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void Program.A(System.Boolean flag, System.Int32 number)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: flag) (OperationKind.Argument)
IOperation: (OperationKind.None)
IArgument (Matching Parameter: number) (OperationKind.Argument)
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithElseMissing()
{
string source = @"
using System;
class P
{
private void M()
{
object obj = ""pattern"";
/*<bind>*/if (obj is string str)
{
Console.WriteLine(str);
}
else
/*</bind>*/ }
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement, IsInvalid)
Condition: IOperation: (OperationKind.None)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static void System.Console.WriteLine(System.String value)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILocalReferenceExpression: str (OperationKind.LocalReferenceExpression, Type: System.String)
IExpressionStatement (OperationKind.ExpressionStatement, IsInvalid)
IInvalidExpression (OperationKind.InvalidExpression, Type: ?, IsInvalid)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithConditionMissing()
{
string source = @"
using System;
class P
{
private void M()
{
int a = 1;
/*<bind>*/if ()
{
a = 2;
}
else
{
a = 3;
}/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement, IsInvalid)
Condition: IConversionExpression (ConversionKind.Invalid, Implicit) (OperationKind.ConversionExpression, Type: System.Boolean, IsInvalid)
IInvalidExpression (OperationKind.InvalidExpression, Type: ?, IsInvalid)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: a (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 2) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: a (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 3) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 3)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithStatementMissing()
{
string source = @"
using System;
class P
{
private void M()
{
int a = 1;
/*<bind>*/if (a==1)
else
/*</bind>*/
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement, IsInvalid)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerEquals) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: a (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
IExpressionStatement (OperationKind.ExpressionStatement, IsInvalid)
IInvalidExpression (OperationKind.InvalidExpression, Type: ?, IsInvalid)
IExpressionStatement (OperationKind.ExpressionStatement, IsInvalid)
IInvalidExpression (OperationKind.InvalidExpression, Type: ?, IsInvalid)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithFuncCall()
{
string source = @"
using System;
class P
{
private void M()
{
/*<bind>*/if (true)
A();
else
B();/*</bind>*/
}
private void A()
{
Console.WriteLine(""A"");
}
private void B()
{
Console.WriteLine(""B"");
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression ( void P.A()) (OperationKind.InvocationExpression, Type: System.Void)
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression ( void P.B()) (OperationKind.InvocationExpression, Type: System.Void)
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
[Fact, WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")]
public void IIfstatementWithDynamic()
{
string source = @"
using System;
class C
{
public static int F<T>(dynamic d, Type t, T x) where T : struct
{
/*<bind>*/if (d.GetType() == t && ((T)d).Equals(x))
{
return 1;
}/*</bind>*/
return 2;
}
}
";
string expectedOperationTree = @"
IIfStatement (OperationKind.IfStatement)
Condition: IUnaryOperatorExpression (UnaryOperationKind.DynamicTrue) (OperationKind.UnaryOperatorExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.DynamicAnd) (OperationKind.BinaryOperatorExpression, Type: dynamic)
Left: IBinaryOperatorExpression (BinaryOperationKind.Invalid) (OperationKind.BinaryOperatorExpression, Type: dynamic)
Left: IOperation: (OperationKind.None)
Right: IParameterReferenceExpression: t (OperationKind.ParameterReferenceExpression, Type: System.Type)
Right: IInvocationExpression (virtual System.Boolean System.ValueType.Equals(System.Object obj)) (OperationKind.InvocationExpression, Type: System.Boolean)
Instance Receiver: IConversionExpression (ConversionKind.CSharp, Explicit) (OperationKind.ConversionExpression, Type: T)
IParameterReferenceExpression: d (OperationKind.ParameterReferenceExpression, Type: dynamic)
IArgument (Matching Parameter: obj) (OperationKind.Argument)
IConversionExpression (ConversionKind.Cast, Implicit) (OperationKind.ConversionExpression, Type: System.Object)
IParameterReferenceExpression: x (OperationKind.ParameterReferenceExpression, Type: T)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IReturnStatement (OperationKind.ReturnStatement)
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
";
VerifyOperationTreeForTest<IfStatementSyntax>(source, expectedOperationTree);
}
}
}
......@@ -96,6 +96,7 @@
<Compile Include="Diagnostics\DiagnosticAnalyzerTests.vb" />
<Compile Include="Diagnostics\DiagnosticTests.vb" />
<Compile Include="Diagnostics\GetDiagnosticsTests.vb" />
<Compile Include="IOperation\IOperationTests_IIfStatement.vb" />
<Compile Include="IOperation\IOperationTests_ISymbolInitializer.vb" />
<Compile Include="IOperation\IOperationTests.vb" />
<Compile Include="Diagnostics\OperationAnalyzerTests.vb" />
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports Roslyn.Test.Utilities
Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics
Partial Public Class IOperationTests
Inherits SemanticModelTestBase
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementSingleLineIf()
Dim source = <![CDATA[
Module Program
Sub Main(args As String())
Dim count As Integer = 0
Dim returnValue As Integer = -1
If count > 0 Then returnValue = count'BIND:"If count > 0 Then returnValue = count"
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 0) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 0)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: returnValue (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
]]>.Value
VerifyOperationTreeForTest(Of SingleLineIfStatementSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementMultiLineIf()
Dim source = <![CDATA[
Module Program
Sub Main(args As String())
Dim count As Integer = 0
Dim returnValue As Integer = 1
If count > 0 Then'BIND:"If count > 0 Then"
returnValue = count
End If
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 0) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 0)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: returnValue (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementSingleLineIfAndElse()
Dim source = <![CDATA[
Module Program
Sub Main(args As String())
Dim count As Integer
Dim data As Integer
If count > 10 Then data = data + count Else data = data - count'BIND:"If count > 10 Then data = data + count Else data = data - count"
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: data (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: IBinaryOperatorExpression (BinaryOperationKind.IntegerAdd) (OperationKind.BinaryOperatorExpression, Type: System.Int32)
Left: ILocalReferenceExpression: data (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: data (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: IBinaryOperatorExpression (BinaryOperationKind.IntegerSubtract) (OperationKind.BinaryOperatorExpression, Type: System.Int32)
Left: ILocalReferenceExpression: data (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
]]>.Value
VerifyOperationTreeForTest(Of SingleLineIfStatementSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementSingleLineIfAndElseNested()
Dim source = <![CDATA[
Module Program
Sub Main(args As String())
Dim m As Integer = 12
Dim n As Integer = 18
Dim returnValue As Integer = -1
If m > 10 Then If n > 20 Then returnValue = n'BIND:"If m > 10 Then If n > 20 Then returnValue = n"
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: returnValue (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
]]>.Value
VerifyOperationTreeForTest(Of SingleLineIfStatementSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementSimpleIfWithConditionEvaluationTrue()
Dim source = <![CDATA[
Class P
Private Sub M()
Dim condition As Boolean = False
If 1 = 1 Then'BIND:"If 1 = 1 Then"
condition = True
End If
End Sub
End Class
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerEquals) (OperationKind.BinaryOperatorExpression, Type: System.Boolean, Constant: True)
Left: ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
Right: ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: condition (OperationKind.LocalReferenceExpression, Type: System.Boolean)
Right: ILiteralExpression (Text: True) (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementSimpleIfWithConditionConstantFalse()
Dim source = <![CDATA[
Class P
Private Sub M()
Dim condition As Boolean = True
If False Then'BIND:"If False Then"
condition = False
End If
End Sub
End Class
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: ILiteralExpression (Text: False) (OperationKind.LiteralExpression, Type: System.Boolean, Constant: False)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: condition (OperationKind.LocalReferenceExpression, Type: System.Boolean)
Right: ILiteralExpression (Text: False) (OperationKind.LiteralExpression, Type: System.Boolean, Constant: False)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementSingleLineWithOperator()
Dim source = <![CDATA[
Module Program
Sub Main(args As String())
Dim m As Integer = 12
Dim n As Integer = 18
Dim returnValue As Integer = -1
If (m > 10 And n > 20) Then returnValue = n'BIND:"If (m > 10 And n > 20) Then returnValue = n"
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.BooleanAnd) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
Right: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: returnValue (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
]]>.Value
VerifyOperationTreeForTest(Of SingleLineIfStatementSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementMultiLineIfWithElse()
Dim source = <![CDATA[
Module Program
Sub Main(args As String())
Dim count As Integer = 0
Dim returnValue As Integer = -1
If count > 0 Then'BIND:"If count > 0 Then"
returnValue = count
Else
returnValue = -1
End If
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 0) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 0)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: returnValue (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: count (OperationKind.LocalReferenceExpression, Type: System.Int32)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IAssignmentExpression (OperationKind.AssignmentExpression, Type: System.Int32)
Left: ILocalReferenceExpression: returnValue (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: IUnaryOperatorExpression (UnaryOperationKind.IntegerMinus) (OperationKind.UnaryOperatorExpression, Type: System.Int32, Constant: -1)
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementSimpleIfNested1()
Dim source = <![CDATA[
Imports System
Module Program
Sub Main(args As String())
Dim m As Integer = 12
Dim n As Integer = 18
Dim returnValue As Integer = -1
If (m > 10) Then'BIND:"If (m > 10) Then"
If (n > 20) Then
Console.WriteLine("Result 1")
End If
Else
Console.WriteLine("Result 2")
End If
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result 1)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result 2)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementIfNested2()
Dim source = <![CDATA[
Imports System
Module Program
Sub Main(args As String())
Dim m As Integer = 12
Dim n As Integer = 18
Dim returnValue As Integer = -1
If (m > 10) Then'BIND:"If (m > 10) Then"
If (n > 20) Then
Console.WriteLine("Result 1")
Else
Console.WriteLine("Result 2")
End If
End If
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result 1)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result 2)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementWithMultipleCondition()
Dim source = <![CDATA[
Imports System
Module Program
Sub Main(args As String())
Dim m As Integer = 9
Dim n As Integer = 7
Dim p As Integer = 5
If (m >= n AndAlso m >= p) Then'BIND:"If (m >= n AndAlso m >= p) Then"
Console.WriteLine("Nothing Is larger than m.")
End If
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.BooleanConditionalAnd) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThanOrEqual) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThanOrEqual) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILocalReferenceExpression: p (OperationKind.LocalReferenceExpression, Type: System.Int32)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Nothing Is larger than m.)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementWithElseIfCondition()
Dim source = <![CDATA[
Imports System
Module Program
Sub Main(args As String())
Dim m As Integer = 9
Dim n As Integer = 7
If (m > 20) Then'BIND:"If (m > 20) Then"
Console.WriteLine("Result1")
ElseIf (n > 10) Then
Console.WriteLine("Result2")
Else
Console.WriteLine("Result3")
End If
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result1)
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result2)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result3)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementWithElseIfSingleLine()
Dim source = <![CDATA[
Module Program
Sub Main(args As String())
Dim m As Integer = 9
Dim n As Integer = 7
If (m > 20) Then System.Console.WriteLine("Result1") Else If (n > 10) Then System.Console.WriteLine("Result2") Else System.Console.WriteLine("Result3") End If'BIND:"If (m > 20) Then System.Console.WriteLine("Result1") Else If (n > 10) Then System.Console.WriteLine("Result2") Else System.Console.WriteLine("Result3")"
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result1)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: n (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 10) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result2)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result3)
]]>.Value
VerifyOperationTreeForTest(Of SingleLineIfStatementSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementWithElseMissing()
Dim source = <![CDATA[
Imports System
Module Program
Sub Main(args As String())
Dim m As Integer = 9
If (m > 20) Then'BIND:"If (m > 20) Then"
Console.WriteLine("Result1")
Else
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerGreaterThan) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 20) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 20)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result1)
IBlockStatement (0 statements) (OperationKind.BlockStatement)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementWithConditionMissing()
Dim source = <![CDATA[
Imports System
Module Program
Sub Main(args As String())
Dim m As Integer = 9
If () Then'BIND:"If () Then"
Console.WriteLine("Result1")
End If
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement, IsInvalid)
Condition: IConversionExpression (ConversionKind.Basic, Implicit) (OperationKind.ConversionExpression, Type: System.Boolean, IsInvalid)
IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: ?, IsInvalid)
IInvalidExpression (OperationKind.InvalidExpression, Type: ?, IsInvalid)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (Matching Parameter: value) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: Result1)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementWithStatementMissing()
Dim source = <![CDATA[
Imports System
Module Program
Sub Main(args As String())
Dim m As Integer = 9
If (m = 9) Then'BIND:"If (m = 9) Then"
Else
End If
End Sub
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean)
IBinaryOperatorExpression (BinaryOperationKind.IntegerEquals) (OperationKind.BinaryOperatorExpression, Type: System.Boolean)
Left: ILocalReferenceExpression: m (OperationKind.LocalReferenceExpression, Type: System.Int32)
Right: ILiteralExpression (Text: 9) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 9)
IBlockStatement (0 statements) (OperationKind.BlockStatement)
IBlockStatement (0 statements) (OperationKind.BlockStatement)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
<Fact(), WorkItem(17601, "https://github.com/dotnet/roslyn/issues/17601")>
Public Sub IIfstatementWithFuncCall()
Dim source = <![CDATA[
Module Module1
Sub Main()
If (True) Then'BIND:"If (True) Then"
A()
Else
B()
End If
End Sub
Function A() As String
Return "A"
End Function
Function B() As String
Return "B"
End Function
End Module
]]>.Value
Dim expectedOperationTree = <![CDATA[
IIfStatement (OperationKind.IfStatement)
Condition: IParenthesizedExpression (OperationKind.ParenthesizedExpression, Type: System.Boolean, Constant: True)
ILiteralExpression (Text: True) (OperationKind.LiteralExpression, Type: System.Boolean, Constant: True)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Function Module1.A() As System.String) (OperationKind.InvocationExpression, Type: System.String)
IBlockStatement (1 statements) (OperationKind.BlockStatement)
IExpressionStatement (OperationKind.ExpressionStatement)
IInvocationExpression (static Function Module1.B() As System.String) (OperationKind.InvocationExpression, Type: System.String)
]]>.Value
VerifyOperationTreeForTest(Of MultiLineIfBlockSyntax)(source, expectedOperationTree)
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册