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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/ArrayCreateExpressionTests.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.Linq;
 
21
using NUnit.Framework;
 
22
 
 
23
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
 
24
{
 
25
        [TestFixture]
 
26
        public class ArrayCreateExpressionTests
 
27
        {
 
28
                [Test]
 
29
                public void ArrayCreateExpressionTest1()
 
30
                {
 
31
                        ParseUtilCSharp.AssertExpression(
 
32
                                "new int[5]",
 
33
                                new ArrayCreateExpression {
 
34
                                        Type = new PrimitiveType("int"),
 
35
                                        Arguments = { new PrimitiveExpression(5) }
 
36
                                });
 
37
                }
 
38
                
 
39
                [Test]
 
40
                public void MultidimensionalNestedArray()
 
41
                {
 
42
                        ParseUtilCSharp.AssertExpression(
 
43
                                "new int[5,2][,,][]",
 
44
                                new ArrayCreateExpression {
 
45
                                        Type = new PrimitiveType("int"),
 
46
                                        Arguments = { new PrimitiveExpression(5), new PrimitiveExpression(2) },
 
47
                                        AdditionalArraySpecifiers = {
 
48
                                                new ArraySpecifier(3),
 
49
                                                new ArraySpecifier(1)
 
50
                                        }
 
51
                                });
 
52
                }
 
53
                
 
54
                [Test]
 
55
                public void ArrayWithImplicitSize()
 
56
                {
 
57
                        ParseUtilCSharp.AssertExpression(
 
58
                                "new int[] { 1 }",
 
59
                                new ArrayCreateExpression {
 
60
                                        Type = new PrimitiveType("int"),
 
61
                                        AdditionalArraySpecifiers = {
 
62
                                                new ArraySpecifier(0)
 
63
                                        },
 
64
                                        Initializer = new ArrayInitializerExpression {
 
65
                                                Elements = { new PrimitiveExpression(1) }
 
66
                                        }
 
67
                                });
 
68
                }
 
69
                
 
70
                [Test]
 
71
                public void ArrayWithImplicitSize2D()
 
72
                {
 
73
                        ParseUtilCSharp.AssertExpression(
 
74
                                "new int[,] { { 1 } }",
 
75
                                new ArrayCreateExpression {
 
76
                                        Type = new PrimitiveType("int"),
 
77
                                        AdditionalArraySpecifiers = { new ArraySpecifier (2) },
 
78
                                        Initializer = new ArrayInitializerExpression {
 
79
                                                Elements = {
 
80
                                                        new ArrayInitializerExpression {
 
81
                                                                Elements = { new PrimitiveExpression(1) }
 
82
                                                        }
 
83
                                                }
 
84
                                        }
 
85
                                });
 
86
                }
 
87
                
 
88
                [Test]
 
89
                public void ImplicitlyTypedArrayCreateExpression()
 
90
                {
 
91
                        ParseUtilCSharp.AssertExpression(
 
92
                                "new[] { 1, 10, 100, 1000 }",
 
93
                                new ArrayCreateExpression {
 
94
                                        AdditionalArraySpecifiers = {
 
95
                                                new ArraySpecifier(0)
 
96
                                        },
 
97
                                        Initializer = new ArrayInitializerExpression {
 
98
                                                Elements = {
 
99
                                                        new PrimitiveExpression(1),
 
100
                                                        new PrimitiveExpression(10),
 
101
                                                        new PrimitiveExpression(100),
 
102
                                                        new PrimitiveExpression(1000)
 
103
                                                }
 
104
                                        }}
 
105
                        );
 
106
                }
 
107
                
 
108
                [Test]
 
109
                public void ImplicitlyTypedArrayCreateExpression2D()
 
110
                {
 
111
                        ParseUtilCSharp.AssertExpression(
 
112
                                "new [,] { { 1, 10 }, { 100, 1000 } }",
 
113
                                new ArrayCreateExpression {
 
114
                                        AdditionalArraySpecifiers = {
 
115
                                                new ArraySpecifier(2),
 
116
                                        },
 
117
                                        Initializer = new ArrayInitializerExpression {
 
118
                                                Elements = {
 
119
                                                        new ArrayInitializerExpression {
 
120
                                                                Elements = {
 
121
                                                                        new PrimitiveExpression(1),
 
122
                                                                        new PrimitiveExpression(10)
 
123
                                                                }
 
124
                                                        },
 
125
                                                        new ArrayInitializerExpression {
 
126
                                                                Elements = {
 
127
                                                                        new PrimitiveExpression(100),
 
128
                                                                        new PrimitiveExpression(1000)
 
129
                                                                }
 
130
                                                        }
 
131
                                                }
 
132
                                        }});
 
133
                }
 
134
                
 
135
                [Test]
 
136
                public void AssignmentInArrayInitializer()
 
137
                {
 
138
                        ParseUtilCSharp.AssertExpression(
 
139
                                "new [] { a = 10 }",
 
140
                                new ArrayCreateExpression {
 
141
                                        AdditionalArraySpecifiers = {
 
142
                                                new ArraySpecifier(0)
 
143
                                        },
 
144
                                        Initializer = new ArrayInitializerExpression {
 
145
                                                Elements = {
 
146
                                                        new AssignmentExpression(new IdentifierExpression("a"), new PrimitiveExpression(10))
 
147
                                                }
 
148
                                        }});
 
149
                }
 
150
 
 
151
                [Test]
 
152
                public void EmptyArrayCreation()
 
153
                {
 
154
                        var ace = ParseUtilCSharp.ParseExpression<ArrayCreateExpression>("new [] { }");
 
155
                        Assert.AreEqual(new Role[] {
 
156
                                                Roles.LBrace,
 
157
                                                Roles.RBrace
 
158
                                        }, ace.Initializer.Children.Select(c => c.Role).ToArray());
 
159
                }
 
160
 
 
161
                [Test]
 
162
                public void ArrayInitializerWithCommaAtEnd()
 
163
                {
 
164
                        var ace = ParseUtilCSharp.ParseExpression<ArrayCreateExpression>("new [] { 1, }");
 
165
                        Assert.AreEqual(new Role[] {
 
166
                                                Roles.LBrace,
 
167
                                                Roles.Expression,
 
168
                                                Roles.Comma,
 
169
                                                Roles.RBrace
 
170
                                        }, ace.Initializer.Children.Select(c => c.Role).ToArray());
 
171
                }
 
172
        }
 
173
}