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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.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) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.IO;
 
21
using NUnit.Framework;
 
22
 
 
23
namespace ICSharpCode.NRefactory.CSharp
 
24
{
 
25
        [TestFixture]
 
26
        public class InsertParenthesesVisitorTests
 
27
        {
 
28
                CSharpFormattingOptions policy;
 
29
                
 
30
                [SetUp]
 
31
                public void SetUp()
 
32
                {
 
33
                        policy = FormattingOptionsFactory.CreateMono ();
 
34
                }
 
35
                
 
36
                string InsertReadable(Expression expr)
 
37
                {
 
38
                        expr = expr.Clone();
 
39
                        expr.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });
 
40
                        StringWriter w = new StringWriter();
 
41
                        w.NewLine = " ";
 
42
                        expr.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "" }, policy));
 
43
                        return w.ToString();
 
44
                }
 
45
                
 
46
                string InsertRequired(Expression expr)
 
47
                {
 
48
                        expr = expr.Clone();
 
49
                        expr.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = false });
 
50
                        StringWriter w = new StringWriter();
 
51
                        w.NewLine = " ";
 
52
                        expr.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "" }, policy));
 
53
                        return w.ToString();
 
54
                }
 
55
                
 
56
                [Test]
 
57
                public void EqualityInAssignment()
 
58
                {
 
59
                        Expression expr = new AssignmentExpression(
 
60
                                new IdentifierExpression("cond"),
 
61
                                new BinaryOperatorExpression(
 
62
                                        new IdentifierExpression("a"),
 
63
                                        BinaryOperatorType.Equality,
 
64
                                        new IdentifierExpression("b")
 
65
                                )
 
66
                        );
 
67
                        
 
68
                        Assert.AreEqual("cond = a == b", InsertRequired(expr));
 
69
                        Assert.AreEqual("cond = (a == b)", InsertReadable(expr));
 
70
                }
 
71
                
 
72
                [Test]
 
73
                public void TrickyCast1()
 
74
                {
 
75
                        Expression expr = new UnaryOperatorExpression(
 
76
                                UnaryOperatorType.Minus, new IdentifierExpression("a")
 
77
                        ).CastTo(new PrimitiveType("int"));
 
78
                        
 
79
                        Assert.AreEqual("(int)-a", InsertRequired(expr));
 
80
                        Assert.AreEqual("(int)(-a)", InsertReadable(expr));
 
81
                }
 
82
                
 
83
                [Test]
 
84
                public void TrickyCast2()
 
85
                {
 
86
                        Expression expr = new UnaryOperatorExpression(
 
87
                                UnaryOperatorType.Minus, new IdentifierExpression("a")
 
88
                        ).CastTo(new SimpleType("MyType"));
 
89
                        
 
90
                        Assert.AreEqual("(MyType)(-a)", InsertRequired(expr));
 
91
                        Assert.AreEqual("(MyType)(-a)", InsertReadable(expr));
 
92
                }
 
93
                
 
94
                [Test]
 
95
                public void TrickyCast3()
 
96
                {
 
97
                        Expression expr = new UnaryOperatorExpression(
 
98
                                UnaryOperatorType.Not, new IdentifierExpression("a")
 
99
                        ).CastTo(new SimpleType("MyType"));
 
100
                        
 
101
                        Assert.AreEqual("(MyType)!a", InsertRequired(expr));
 
102
                        Assert.AreEqual("(MyType)(!a)", InsertReadable(expr));
 
103
                }
 
104
                
 
105
                [Test]
 
106
                public void TrickyCast4()
 
107
                {
 
108
                        Expression expr = new PrimitiveExpression(int.MinValue).CastTo(new SimpleType("MyType"));
 
109
                        
 
110
                        Assert.AreEqual("(MyType)(-2147483648)", InsertRequired(expr));
 
111
                        Assert.AreEqual("(MyType)(-2147483648)", InsertReadable(expr));
 
112
                }
 
113
                
 
114
                [Test]
 
115
                public void TrickyCast5()
 
116
                {
 
117
                        Expression expr = new PrimitiveExpression(-1.0).CastTo(new SimpleType("MyType"));
 
118
                        
 
119
                        Assert.AreEqual("(MyType)(-1.0)", InsertRequired(expr));
 
120
                        Assert.AreEqual("(MyType)(-1.0)", InsertReadable(expr));
 
121
                }
 
122
                
 
123
                [Test]
 
124
                public void TrickyCast6()
 
125
                {
 
126
                        Expression expr = new PrimitiveExpression(int.MinValue).CastTo(new PrimitiveType("double"));
 
127
                        
 
128
                        Assert.AreEqual("(double)-2147483648", InsertRequired(expr));
 
129
                        Assert.AreEqual("(double)-2147483648", InsertReadable(expr));
 
130
                }
 
131
                
 
132
                [Test]
 
133
                public void CastAndInvoke()
 
134
                {
 
135
                        Expression expr = new IdentifierExpression("a")
 
136
                                .CastTo(new PrimitiveType("string"))
 
137
                                .Member("Length");
 
138
                        
 
139
                        Assert.AreEqual("((string)a).Length", InsertRequired(expr));
 
140
                        Assert.AreEqual("((string)a).Length", InsertReadable(expr));
 
141
                }
 
142
                
 
143
                [Test]
 
144
                public void DoubleNegation()
 
145
                {
 
146
                        Expression expr = new UnaryOperatorExpression(
 
147
                                UnaryOperatorType.Minus,
 
148
                                new UnaryOperatorExpression(UnaryOperatorType.Minus, new IdentifierExpression("a"))
 
149
                        );
 
150
                        
 
151
                        Assert.AreEqual("- -a", InsertRequired(expr));
 
152
                        Assert.AreEqual("-(-a)", InsertReadable(expr));
 
153
                }
 
154
                
 
155
                [Test]
 
156
                public void AdditionWithConditional()
 
157
                {
 
158
                        Expression expr = new BinaryOperatorExpression {
 
159
                                Left = new IdentifierExpression("a"),
 
160
                                Operator = BinaryOperatorType.Add,
 
161
                                Right = new ConditionalExpression {
 
162
                                        Condition = new BinaryOperatorExpression {
 
163
                                                Left = new IdentifierExpression("b"),
 
164
                                                Operator = BinaryOperatorType.Equality,
 
165
                                                Right = new PrimitiveExpression(null)
 
166
                                        },
 
167
                                        TrueExpression = new IdentifierExpression("c"),
 
168
                                        FalseExpression = new IdentifierExpression("d")
 
169
                                }
 
170
                        };
 
171
                        
 
172
                        Assert.AreEqual("a + (b == null ? c : d)", InsertRequired(expr));
 
173
                        Assert.AreEqual("a + ((b == null) ? c : d)", InsertReadable(expr));
 
174
                }
 
175
                
 
176
                [Test]
 
177
                public void TypeTestInConditional()
 
178
                {
 
179
                        Expression expr = new ConditionalExpression {
 
180
                                Condition = new IdentifierExpression("a").IsType(
 
181
                                        new ComposedType {
 
182
                                                BaseType = new PrimitiveType("int"),
 
183
                                                HasNullableSpecifier = true
 
184
                                        }
 
185
                                ),
 
186
                                TrueExpression = new IdentifierExpression("b"),
 
187
                                FalseExpression = new IdentifierExpression("c")
 
188
                        };
 
189
                        
 
190
                        Assert.AreEqual("a is int? ? b : c", InsertRequired(expr));
 
191
                        Assert.AreEqual("(a is int?) ? b : c", InsertReadable(expr));
 
192
                        
 
193
                        policy.SpaceBeforeConditionalOperatorCondition = false;
 
194
                        policy.SpaceAfterConditionalOperatorCondition = false;
 
195
                        policy.SpaceBeforeConditionalOperatorSeparator = false;
 
196
                        policy.SpaceAfterConditionalOperatorSeparator = false;
 
197
                        
 
198
                        Assert.AreEqual("a is int? ?b:c", InsertRequired(expr));
 
199
                        Assert.AreEqual("(a is int?)?b:c", InsertReadable(expr));
 
200
                }
 
201
                
 
202
                [Test]
 
203
                public void MethodCallOnQueryExpression()
 
204
                {
 
205
                        Expression expr = new QueryExpression {
 
206
                                Clauses = {
 
207
                                        new QueryFromClause {
 
208
                                                Identifier = "a",
 
209
                                                Expression = new IdentifierExpression("b")
 
210
                                        },
 
211
                                        new QuerySelectClause {
 
212
                                                Expression = new IdentifierExpression("a").Invoke("c")
 
213
                                        }
 
214
                                }
 
215
                        }.Invoke("ToArray");
 
216
                        
 
217
                        Assert.AreEqual("( from a in b select a.c ()).ToArray ()", InsertRequired(expr));
 
218
                        Assert.AreEqual("( from a in b select a.c ()).ToArray ()", InsertReadable(expr));
 
219
                }
 
220
                
 
221
                [Test]
 
222
                public void SumOfQueries()
 
223
                {
 
224
                        QueryExpression query = new QueryExpression {
 
225
                                Clauses = {
 
226
                                        new QueryFromClause {
 
227
                                                Identifier = "a",
 
228
                                                Expression = new IdentifierExpression("b")
 
229
                                        },
 
230
                                        new QuerySelectClause {
 
231
                                                Expression = new IdentifierExpression("a")
 
232
                                        }
 
233
                                }
 
234
                        };
 
235
                        Expression expr = new BinaryOperatorExpression(
 
236
                                query,
 
237
                                BinaryOperatorType.Add,
 
238
                                query.Clone()
 
239
                        );
 
240
                        
 
241
                        Assert.AreEqual("( from a in b select a) + " +
 
242
                                        " from a in b select a", InsertRequired(expr));
 
243
                        Assert.AreEqual("( from a in b select a) + " +
 
244
                                        "( from a in b select a)", InsertReadable(expr));
 
245
                }
 
246
                
 
247
                [Test]
 
248
                public void QueryInTypeTest()
 
249
                {
 
250
                        Expression expr = new QueryExpression {
 
251
                                Clauses = {
 
252
                                        new QueryFromClause {
 
253
                                                Identifier = "a",
 
254
                                                Expression = new IdentifierExpression("b")
 
255
                                        },
 
256
                                        new QuerySelectClause {
 
257
                                                Expression = new IdentifierExpression("a")
 
258
                                        }
 
259
                                }
 
260
                        }.IsType(new PrimitiveType("int"));
 
261
                        
 
262
                        Assert.AreEqual("( from a in b select a) is int", InsertRequired(expr));
 
263
                        Assert.AreEqual("( from a in b select a) is int", InsertReadable(expr));
 
264
                }
 
265
                
 
266
                [Test]
 
267
                public void PrePost()
 
268
                {
 
269
                        Expression expr = new UnaryOperatorExpression(
 
270
                                UnaryOperatorType.Increment,
 
271
                                new UnaryOperatorExpression(
 
272
                                        UnaryOperatorType.PostIncrement,
 
273
                                        new IdentifierExpression("a")
 
274
                                )
 
275
                        );
 
276
                        
 
277
                        Assert.AreEqual("++a++", InsertRequired(expr));
 
278
                        Assert.AreEqual("++(a++)", InsertReadable(expr));
 
279
                }
 
280
                
 
281
                [Test]
 
282
                public void PostPre()
 
283
                {
 
284
                        Expression expr = new UnaryOperatorExpression(
 
285
                                UnaryOperatorType.PostIncrement,
 
286
                                new UnaryOperatorExpression(
 
287
                                        UnaryOperatorType.Increment,
 
288
                                        new IdentifierExpression("a")
 
289
                                )
 
290
                        );
 
291
                        
 
292
                        Assert.AreEqual("(++a)++", InsertRequired(expr));
 
293
                        Assert.AreEqual("(++a)++", InsertReadable(expr));
 
294
                }
 
295
                
 
296
                [Test]
 
297
                public void Logical1()
 
298
                {
 
299
                        Expression expr = new BinaryOperatorExpression(
 
300
                                new BinaryOperatorExpression(
 
301
                                        new IdentifierExpression("a"),
 
302
                                        BinaryOperatorType.ConditionalAnd,
 
303
                                        new IdentifierExpression("b")
 
304
                                ),
 
305
                                BinaryOperatorType.ConditionalAnd,
 
306
                                new IdentifierExpression("c")
 
307
                        );
 
308
                        
 
309
                        Assert.AreEqual("a && b && c", InsertRequired(expr));
 
310
                        Assert.AreEqual("a && b && c", InsertReadable(expr));
 
311
                }
 
312
                
 
313
                [Test]
 
314
                public void Logical2()
 
315
                {
 
316
                        Expression expr = new BinaryOperatorExpression(
 
317
                                new IdentifierExpression("a"),
 
318
                                BinaryOperatorType.ConditionalAnd,
 
319
                                new BinaryOperatorExpression(
 
320
                                        new IdentifierExpression("b"),
 
321
                                        BinaryOperatorType.ConditionalAnd,
 
322
                                        new IdentifierExpression("c")
 
323
                                )
 
324
                        );
 
325
                        
 
326
                        Assert.AreEqual("a && (b && c)", InsertRequired(expr));
 
327
                        Assert.AreEqual("a && (b && c)", InsertReadable(expr));
 
328
                }
 
329
                
 
330
                [Test]
 
331
                public void Logical3()
 
332
                {
 
333
                        Expression expr = new BinaryOperatorExpression(
 
334
                                new IdentifierExpression("a"),
 
335
                                BinaryOperatorType.ConditionalOr,
 
336
                                new BinaryOperatorExpression(
 
337
                                        new IdentifierExpression("b"),
 
338
                                        BinaryOperatorType.ConditionalAnd,
 
339
                                        new IdentifierExpression("c")
 
340
                                )
 
341
                        );
 
342
                        
 
343
                        Assert.AreEqual("a || b && c", InsertRequired(expr));
 
344
                        Assert.AreEqual("a || (b && c)", InsertReadable(expr));
 
345
                }
 
346
                
 
347
                [Test]
 
348
                public void Logical4()
 
349
                {
 
350
                        Expression expr = new BinaryOperatorExpression(
 
351
                                new IdentifierExpression("a"),
 
352
                                BinaryOperatorType.ConditionalAnd,
 
353
                                new BinaryOperatorExpression(
 
354
                                        new IdentifierExpression("b"),
 
355
                                        BinaryOperatorType.ConditionalOr,
 
356
                                        new IdentifierExpression("c")
 
357
                                )
 
358
                        );
 
359
                        
 
360
                        Assert.AreEqual("a && (b || c)", InsertRequired(expr));
 
361
                        Assert.AreEqual("a && (b || c)", InsertReadable(expr));
 
362
                }
 
363
                
 
364
                [Test]
 
365
                public void ArrayCreationInIndexer()
 
366
                {
 
367
                        Expression expr = new IndexerExpression {
 
368
                                Target = new ArrayCreateExpression {
 
369
                                        Type = new PrimitiveType("int"),
 
370
                                        Arguments = { new PrimitiveExpression(1) }
 
371
                                },
 
372
                                Arguments = { new PrimitiveExpression(0) }
 
373
                        };
 
374
                        
 
375
                        Assert.AreEqual("(new int[1]) [0]", InsertRequired(expr));
 
376
                        Assert.AreEqual("(new int[1]) [0]", InsertReadable(expr));
 
377
                }
 
378
                
 
379
                [Test]
 
380
                public void ArrayCreationWithInitializerInIndexer()
 
381
                {
 
382
                        Expression expr = new IndexerExpression {
 
383
                                Target = new ArrayCreateExpression {
 
384
                                        Type = new PrimitiveType("int"),
 
385
                                        Arguments = { new PrimitiveExpression(1) },
 
386
                                        Initializer = new ArrayInitializerExpression {
 
387
                                                Elements = { new PrimitiveExpression(42) }
 
388
                                        }
 
389
                                },
 
390
                                Arguments = { new PrimitiveExpression(0) }
 
391
                        };
 
392
                        
 
393
                        Assert.AreEqual("new int[1] { 42 } [0]", InsertRequired(expr));
 
394
                        Assert.AreEqual("(new int[1] { 42 }) [0]", InsertReadable(expr));
 
395
                }
 
396
        }
 
397
}