1
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
7
using ICSharpCode.NRefactory.VB.Parser;
8
using ICSharpCode.NRefactory.VB.Ast;
9
using ICSharpCode.NRefactory.VB.PrettyPrinter;
11
namespace ICSharpCode.NRefactory.VB.Tests.Ast
14
public class BinaryOperatorExpressionTests
16
void OperatorPrecedenceTest(string strongOperator, BinaryOperatorType strongOperatorType,
17
string weakOperator, BinaryOperatorType weakOperatorType)
19
string program = "a " + weakOperator + " b " + strongOperator + " c";
20
BinaryOperatorExpression boe;
21
boe = ParseUtil.ParseExpression<BinaryOperatorExpression>(program);
22
Assert.AreEqual(weakOperatorType, boe.Op);
23
Assert.IsTrue(boe.Left is SimpleNameExpression);
24
boe = (BinaryOperatorExpression)boe.Right;
25
Assert.AreEqual(strongOperatorType, boe.Op);
26
Assert.IsTrue(boe.Left is SimpleNameExpression);
27
Assert.IsTrue(boe.Right is SimpleNameExpression);
29
program = "a " + strongOperator + " b " + weakOperator + " c";
30
boe = ParseUtil.ParseExpression<BinaryOperatorExpression>(program);
31
Assert.AreEqual(weakOperatorType, boe.Op);
32
Assert.IsTrue(boe.Right is SimpleNameExpression);
33
boe = (BinaryOperatorExpression)boe.Left;
34
Assert.AreEqual(strongOperatorType, boe.Op);
35
Assert.IsTrue(boe.Left is SimpleNameExpression);
36
Assert.IsTrue(boe.Right is SimpleNameExpression);
39
void SameOperatorPrecedenceTest(string firstOperator, BinaryOperatorType firstOperatorType,
40
string secondOperator, BinaryOperatorType secondOperatorType)
42
string program = "a " + secondOperator + " b " + firstOperator + " c";
43
BinaryOperatorExpression boe;
44
boe = ParseUtil.ParseExpression<BinaryOperatorExpression>(program);
45
Assert.AreEqual(firstOperatorType, boe.Op);
46
Assert.IsTrue(boe.Right is SimpleNameExpression);
47
boe = (BinaryOperatorExpression)boe.Left;
48
Assert.AreEqual(secondOperatorType, boe.Op);
49
Assert.IsTrue(boe.Left is SimpleNameExpression);
50
Assert.IsTrue(boe.Right is SimpleNameExpression);
52
program = "a " + firstOperator + " b " + secondOperator + " c";
53
boe = ParseUtil.ParseExpression<BinaryOperatorExpression>(program);
54
Assert.AreEqual(secondOperatorType, boe.Op);
55
Assert.IsTrue(boe.Right is SimpleNameExpression);
56
boe = (BinaryOperatorExpression)boe.Left;
57
Assert.AreEqual(firstOperatorType, boe.Op);
58
Assert.IsTrue(boe.Left is SimpleNameExpression);
59
Assert.IsTrue(boe.Right is SimpleNameExpression);
63
void VBNetTestBinaryOperatorExpressionTest(string program, BinaryOperatorType op)
65
BinaryOperatorExpression boe = ParseUtil.ParseExpression<BinaryOperatorExpression>(program);
66
Assert.AreEqual(op, boe.Op);
68
Assert.IsTrue(boe.Left is SimpleNameExpression);
69
Assert.IsTrue(boe.Right is SimpleNameExpression);
73
public void VBOperatorPrecedenceTest()
75
OperatorPrecedenceTest("^", BinaryOperatorType.Power, "*", BinaryOperatorType.Multiply);
76
SameOperatorPrecedenceTest("*", BinaryOperatorType.Multiply, "/", BinaryOperatorType.Divide);
77
OperatorPrecedenceTest("/", BinaryOperatorType.Divide, "\\", BinaryOperatorType.DivideInteger);
78
OperatorPrecedenceTest("\\", BinaryOperatorType.DivideInteger, "Mod", BinaryOperatorType.Modulus);
79
OperatorPrecedenceTest("Mod", BinaryOperatorType.Modulus, "+", BinaryOperatorType.Add);
80
SameOperatorPrecedenceTest("+", BinaryOperatorType.Add, "-", BinaryOperatorType.Subtract);
81
OperatorPrecedenceTest("-", BinaryOperatorType.Subtract, "&", BinaryOperatorType.Concat);
82
OperatorPrecedenceTest("&", BinaryOperatorType.Concat, "<<", BinaryOperatorType.ShiftLeft);
83
SameOperatorPrecedenceTest("<<", BinaryOperatorType.ShiftLeft, ">>", BinaryOperatorType.ShiftRight);
84
OperatorPrecedenceTest("<<", BinaryOperatorType.ShiftLeft, "=", BinaryOperatorType.Equality);
85
SameOperatorPrecedenceTest("<>", BinaryOperatorType.InEquality, "=", BinaryOperatorType.Equality);
86
SameOperatorPrecedenceTest("<", BinaryOperatorType.LessThan, "=", BinaryOperatorType.Equality);
87
SameOperatorPrecedenceTest("<=", BinaryOperatorType.LessThanOrEqual, "=", BinaryOperatorType.Equality);
88
SameOperatorPrecedenceTest(">", BinaryOperatorType.GreaterThan, "=", BinaryOperatorType.Equality);
89
SameOperatorPrecedenceTest(">=", BinaryOperatorType.GreaterThanOrEqual, "=", BinaryOperatorType.Equality);
90
SameOperatorPrecedenceTest("Like", BinaryOperatorType.Like, "=", BinaryOperatorType.Equality);
91
SameOperatorPrecedenceTest("Is", BinaryOperatorType.ReferenceEquality, "=", BinaryOperatorType.Equality);
92
SameOperatorPrecedenceTest("IsNot", BinaryOperatorType.ReferenceInequality, "=", BinaryOperatorType.Equality);
93
OperatorPrecedenceTest("=", BinaryOperatorType.Equality, "And", BinaryOperatorType.BitwiseAnd);
94
SameOperatorPrecedenceTest("And", BinaryOperatorType.BitwiseAnd, "AndAlso", BinaryOperatorType.LogicalAnd);
95
OperatorPrecedenceTest("And", BinaryOperatorType.BitwiseAnd, "Or", BinaryOperatorType.BitwiseOr);
96
SameOperatorPrecedenceTest("Or", BinaryOperatorType.BitwiseOr, "OrElse", BinaryOperatorType.LogicalOr);
97
SameOperatorPrecedenceTest("Or", BinaryOperatorType.BitwiseOr, "Xor", BinaryOperatorType.ExclusiveOr);
101
public void VBNetPowerTest()
103
VBNetTestBinaryOperatorExpressionTest("a ^ b", BinaryOperatorType.Power);
107
public void VBNetConcatTest()
109
VBNetTestBinaryOperatorExpressionTest("a & b", BinaryOperatorType.Concat);
113
public void VBNetLogicalAndTest()
115
VBNetTestBinaryOperatorExpressionTest("a AndAlso b", BinaryOperatorType.LogicalAnd);
118
public void VBNetLogicalAndNotLazyTest()
120
VBNetTestBinaryOperatorExpressionTest("a And b", BinaryOperatorType.BitwiseAnd);
124
public void VBNetLogicalOrTest()
126
VBNetTestBinaryOperatorExpressionTest("a OrElse b", BinaryOperatorType.LogicalOr);
129
public void VBNetLogicalOrNotLazyTest()
131
VBNetTestBinaryOperatorExpressionTest("a Or b", BinaryOperatorType.BitwiseOr);
135
public void VBNetExclusiveOrTest()
137
VBNetTestBinaryOperatorExpressionTest("a Xor b", BinaryOperatorType.ExclusiveOr);
142
public void VBNetGreaterThanTest()
144
VBNetTestBinaryOperatorExpressionTest("a > b", BinaryOperatorType.GreaterThan);
148
public void VBNetGreaterThanOrEqualTest()
150
VBNetTestBinaryOperatorExpressionTest("a >= b", BinaryOperatorType.GreaterThanOrEqual);
154
public void VBNetEqualityTest()
156
VBNetTestBinaryOperatorExpressionTest("a = b", BinaryOperatorType.Equality);
160
public void VBNetInEqualityTest()
162
VBNetTestBinaryOperatorExpressionTest("a <> b", BinaryOperatorType.InEquality);
166
public void VBNetLessThanTest()
168
VBNetTestBinaryOperatorExpressionTest("a < b", BinaryOperatorType.LessThan);
172
public void VBNetLessThanOrEqualTest()
174
VBNetTestBinaryOperatorExpressionTest("a <= b", BinaryOperatorType.LessThanOrEqual);
178
public void VBNetAddTest()
180
VBNetTestBinaryOperatorExpressionTest("a + b", BinaryOperatorType.Add);
184
public void VBNetSubtractTest()
186
VBNetTestBinaryOperatorExpressionTest("a - b", BinaryOperatorType.Subtract);
190
public void VBNetMultiplyTest()
192
VBNetTestBinaryOperatorExpressionTest("a * b", BinaryOperatorType.Multiply);
196
public void VBNetDivideTest()
198
VBNetTestBinaryOperatorExpressionTest("a / b", BinaryOperatorType.Divide);
202
public void VBNetDivideIntegerTest()
204
VBNetTestBinaryOperatorExpressionTest("a \\ b", BinaryOperatorType.DivideInteger);
208
public void VBNetModulusTest()
210
VBNetTestBinaryOperatorExpressionTest("a Mod b", BinaryOperatorType.Modulus);
214
public void VBNetShiftLeftTest()
216
VBNetTestBinaryOperatorExpressionTest("a << b", BinaryOperatorType.ShiftLeft);
220
public void VBNetShiftRightTest()
222
VBNetTestBinaryOperatorExpressionTest("a >> b", BinaryOperatorType.ShiftRight);
226
public void VBNetISTest()
228
VBNetTestBinaryOperatorExpressionTest("a is b", BinaryOperatorType.ReferenceEquality);
232
public void VBNetISNotTest()
234
VBNetTestBinaryOperatorExpressionTest("a IsNot b", BinaryOperatorType.ReferenceInequality);
238
public void VBNetLikeTest()
240
VBNetTestBinaryOperatorExpressionTest("a Like b", BinaryOperatorType.Like);
244
public void VBNetNullCoalescingTest()
246
VBNetTestBinaryOperatorExpressionTest("If(a, b)", BinaryOperatorType.NullCoalescing);
250
public void VBNetDictionaryAccess()
252
BinaryOperatorExpression boe = ParseUtil.ParseExpression<BinaryOperatorExpression>("a!b");
253
Assert.AreEqual(BinaryOperatorType.DictionaryAccess, boe.Op);
254
Assert.IsTrue(boe.Left is SimpleNameExpression);
255
Assert.IsTrue(boe.Right is PrimitiveExpression);
259
public void VBNetWithDictionaryAccess()
261
BinaryOperatorExpression boe = ParseUtil.ParseExpression<BinaryOperatorExpression>("!b");
262
Assert.AreEqual(BinaryOperatorType.DictionaryAccess, boe.Op);
263
Assert.IsTrue(boe.Left.IsNull);
264
Assert.IsTrue(boe.Right is PrimitiveExpression);