~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.VB.Tests/Parser/Expressions/BinaryOperatorExpressionTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.IO;
 
6
using NUnit.Framework;
 
7
using ICSharpCode.NRefactory.VB.Parser;
 
8
using ICSharpCode.NRefactory.VB.Ast;
 
9
using ICSharpCode.NRefactory.VB.PrettyPrinter;
 
10
 
 
11
namespace ICSharpCode.NRefactory.VB.Tests.Ast
 
12
{
 
13
        [TestFixture]
 
14
        public class BinaryOperatorExpressionTests
 
15
        {
 
16
                void OperatorPrecedenceTest(string strongOperator, BinaryOperatorType strongOperatorType,
 
17
                                            string weakOperator, BinaryOperatorType weakOperatorType)
 
18
                {
 
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);
 
28
                        
 
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);
 
37
                }
 
38
                
 
39
                void SameOperatorPrecedenceTest(string firstOperator, BinaryOperatorType firstOperatorType,
 
40
                                                string secondOperator, BinaryOperatorType secondOperatorType)
 
41
                {
 
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);
 
51
                        
 
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);
 
60
                }
 
61
                
 
62
                #region VB.NET
 
63
                void VBNetTestBinaryOperatorExpressionTest(string program, BinaryOperatorType op)
 
64
                {
 
65
                        BinaryOperatorExpression boe = ParseUtil.ParseExpression<BinaryOperatorExpression>(program);
 
66
                        Assert.AreEqual(op, boe.Op);
 
67
                        
 
68
                        Assert.IsTrue(boe.Left is SimpleNameExpression);
 
69
                        Assert.IsTrue(boe.Right is SimpleNameExpression);
 
70
                }
 
71
                
 
72
                [Test]
 
73
                public void VBOperatorPrecedenceTest()
 
74
                {
 
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);
 
98
                }
 
99
                
 
100
                [Test]
 
101
                public void VBNetPowerTest()
 
102
                {
 
103
                        VBNetTestBinaryOperatorExpressionTest("a ^ b", BinaryOperatorType.Power);
 
104
                }
 
105
                
 
106
                [Test]
 
107
                public void VBNetConcatTest()
 
108
                {
 
109
                        VBNetTestBinaryOperatorExpressionTest("a & b", BinaryOperatorType.Concat);
 
110
                }
 
111
                
 
112
                [Test]
 
113
                public void VBNetLogicalAndTest()
 
114
                {
 
115
                        VBNetTestBinaryOperatorExpressionTest("a AndAlso b", BinaryOperatorType.LogicalAnd);
 
116
                }
 
117
                [Test]
 
118
                public void VBNetLogicalAndNotLazyTest()
 
119
                {
 
120
                        VBNetTestBinaryOperatorExpressionTest("a And b", BinaryOperatorType.BitwiseAnd);
 
121
                }
 
122
                
 
123
                [Test]
 
124
                public void VBNetLogicalOrTest()
 
125
                {
 
126
                        VBNetTestBinaryOperatorExpressionTest("a OrElse b", BinaryOperatorType.LogicalOr);
 
127
                }
 
128
                [Test]
 
129
                public void VBNetLogicalOrNotLazyTest()
 
130
                {
 
131
                        VBNetTestBinaryOperatorExpressionTest("a Or b", BinaryOperatorType.BitwiseOr);
 
132
                }
 
133
                
 
134
                [Test]
 
135
                public void VBNetExclusiveOrTest()
 
136
                {
 
137
                        VBNetTestBinaryOperatorExpressionTest("a Xor b", BinaryOperatorType.ExclusiveOr);
 
138
                }
 
139
                
 
140
                
 
141
                [Test]
 
142
                public void VBNetGreaterThanTest()
 
143
                {
 
144
                        VBNetTestBinaryOperatorExpressionTest("a > b", BinaryOperatorType.GreaterThan);
 
145
                }
 
146
                
 
147
                [Test]
 
148
                public void VBNetGreaterThanOrEqualTest()
 
149
                {
 
150
                        VBNetTestBinaryOperatorExpressionTest("a >= b", BinaryOperatorType.GreaterThanOrEqual);
 
151
                }
 
152
                
 
153
                [Test]
 
154
                public void VBNetEqualityTest()
 
155
                {
 
156
                        VBNetTestBinaryOperatorExpressionTest("a = b", BinaryOperatorType.Equality);
 
157
                }
 
158
                
 
159
                [Test]
 
160
                public void VBNetInEqualityTest()
 
161
                {
 
162
                        VBNetTestBinaryOperatorExpressionTest("a <> b", BinaryOperatorType.InEquality);
 
163
                }
 
164
                
 
165
                [Test]
 
166
                public void VBNetLessThanTest()
 
167
                {
 
168
                        VBNetTestBinaryOperatorExpressionTest("a < b", BinaryOperatorType.LessThan);
 
169
                }
 
170
                
 
171
                [Test]
 
172
                public void VBNetLessThanOrEqualTest()
 
173
                {
 
174
                        VBNetTestBinaryOperatorExpressionTest("a <= b", BinaryOperatorType.LessThanOrEqual);
 
175
                }
 
176
                
 
177
                [Test]
 
178
                public void VBNetAddTest()
 
179
                {
 
180
                        VBNetTestBinaryOperatorExpressionTest("a + b", BinaryOperatorType.Add);
 
181
                }
 
182
                
 
183
                [Test]
 
184
                public void VBNetSubtractTest()
 
185
                {
 
186
                        VBNetTestBinaryOperatorExpressionTest("a - b", BinaryOperatorType.Subtract);
 
187
                }
 
188
                
 
189
                [Test]
 
190
                public void VBNetMultiplyTest()
 
191
                {
 
192
                        VBNetTestBinaryOperatorExpressionTest("a * b", BinaryOperatorType.Multiply);
 
193
                }
 
194
                
 
195
                [Test]
 
196
                public void VBNetDivideTest()
 
197
                {
 
198
                        VBNetTestBinaryOperatorExpressionTest("a / b", BinaryOperatorType.Divide);
 
199
                }
 
200
                
 
201
                [Test]
 
202
                public void VBNetDivideIntegerTest()
 
203
                {
 
204
                        VBNetTestBinaryOperatorExpressionTest("a \\ b", BinaryOperatorType.DivideInteger);
 
205
                }
 
206
                
 
207
                [Test]
 
208
                public void VBNetModulusTest()
 
209
                {
 
210
                        VBNetTestBinaryOperatorExpressionTest("a Mod b", BinaryOperatorType.Modulus);
 
211
                }
 
212
                
 
213
                [Test]
 
214
                public void VBNetShiftLeftTest()
 
215
                {
 
216
                        VBNetTestBinaryOperatorExpressionTest("a << b", BinaryOperatorType.ShiftLeft);
 
217
                }
 
218
                
 
219
                [Test]
 
220
                public void VBNetShiftRightTest()
 
221
                {
 
222
                        VBNetTestBinaryOperatorExpressionTest("a >> b", BinaryOperatorType.ShiftRight);
 
223
                }
 
224
                
 
225
                [Test]
 
226
                public void VBNetISTest()
 
227
                {
 
228
                        VBNetTestBinaryOperatorExpressionTest("a is b", BinaryOperatorType.ReferenceEquality);
 
229
                }
 
230
                
 
231
                [Test]
 
232
                public void VBNetISNotTest()
 
233
                {
 
234
                        VBNetTestBinaryOperatorExpressionTest("a IsNot b", BinaryOperatorType.ReferenceInequality);
 
235
                }
 
236
                
 
237
                [Test]
 
238
                public void VBNetLikeTest()
 
239
                {
 
240
                        VBNetTestBinaryOperatorExpressionTest("a Like b", BinaryOperatorType.Like);
 
241
                }
 
242
                
 
243
                [Test]
 
244
                public void VBNetNullCoalescingTest()
 
245
                {
 
246
                        VBNetTestBinaryOperatorExpressionTest("If(a, b)", BinaryOperatorType.NullCoalescing);
 
247
                }
 
248
                
 
249
                [Test]
 
250
                public void VBNetDictionaryAccess()
 
251
                {
 
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);
 
256
                }
 
257
                
 
258
                [Test]
 
259
                public void VBNetWithDictionaryAccess()
 
260
                {
 
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);
 
265
                }
 
266
                #endregion
 
267
        }
 
268
}