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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/VariableDeclarationStatementTests.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 ICSharpCode.NRefactory.PatternMatching;
 
22
using NUnit.Framework;
 
23
 
 
24
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
 
25
{
 
26
        [TestFixture]
 
27
        public class VariableDeclarationStatementTests
 
28
        {
 
29
                [Test]
 
30
                public void VariableDeclarationStatementTest()
 
31
                {
 
32
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("int a = 5;");
 
33
                        Assert.AreEqual(1, lvd.Variables.Count());
 
34
                        Assert.AreEqual("a", lvd.Variables.First ().Name);
 
35
                        var type = lvd.Type;
 
36
                        Assert.AreEqual("int", type.ToString ());
 
37
                        Assert.AreEqual(5, ((PrimitiveExpression)lvd.Variables.First ().Initializer).Value);
 
38
                }
 
39
                
 
40
                [Test]
 
41
                public void VoidPointerVariableDeclarationTest()
 
42
                {
 
43
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("void *a;");
 
44
                        Assert.IsTrue(new VariableDeclarationStatement(new PrimitiveType("void").MakePointerType(), "a").IsMatch(lvd));
 
45
                }
 
46
                
 
47
                [Test]
 
48
                public void ComplexGenericVariableDeclarationStatementTest()
 
49
                {
 
50
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("Generic<Namespace.Printable, G<Printable[]> > where = new Generic<Namespace.Printable, G<Printable[]>>();");
 
51
                        AstType type = new SimpleType("Generic") {
 
52
                                TypeArguments = {
 
53
                                        new MemberType { Target = new SimpleType("Namespace"), MemberName = "Printable" },
 
54
                                        new SimpleType("G") { TypeArguments = { new SimpleType("Printable").MakeArrayType() } }
 
55
                                }};
 
56
                        Assert.IsTrue(new VariableDeclarationStatement(type, "where", new ObjectCreateExpression { Type = type.Clone() }).IsMatch(lvd));
 
57
                }
 
58
                
 
59
                [Test]
 
60
                public void NestedGenericVariableDeclarationStatementTest()
 
61
                {
 
62
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("MyType<string>.InnerClass<int>.InnerInnerClass a;");
 
63
                        AstType type = new MemberType {
 
64
                                Target = new MemberType {
 
65
                                        Target = new SimpleType("MyType") { TypeArguments = { new PrimitiveType("string") } },
 
66
                                        MemberName = "InnerClass",
 
67
                                        TypeArguments = { new PrimitiveType("int") }
 
68
                                },
 
69
                                MemberName = "InnerInnerClass"
 
70
                        };
 
71
                        Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
 
72
                }
 
73
                
 
74
                [Test]
 
75
                public void GenericWithArrayVariableDeclarationStatementTest1()
 
76
                {
 
77
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int>[] a;");
 
78
                        AstType type = new SimpleType("G") {
 
79
                                TypeArguments = { new PrimitiveType("int") }
 
80
                        }.MakeArrayType();
 
81
                        Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
 
82
                }
 
83
                
 
84
                [Test]
 
85
                public void GenericWithArrayVariableDeclarationStatementTest2()
 
86
                {
 
87
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int[]> a;");
 
88
                        AstType type = new SimpleType("G") {
 
89
                                TypeArguments = { new PrimitiveType("int").MakeArrayType() }
 
90
                        };
 
91
                        Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
 
92
                }
 
93
                
 
94
                [Test]
 
95
                public void GenericVariableDeclarationStatementTest2()
 
96
                {
 
97
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<G<int> > a;");
 
98
                        AstType type = new SimpleType("G") {
 
99
                                TypeArguments = {
 
100
                                        new SimpleType("G") { TypeArguments = { new PrimitiveType("int") } }
 
101
                                }
 
102
                        };
 
103
                        Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
 
104
                }
 
105
                
 
106
                [Test]
 
107
                public void GenericVariableDeclarationStatementTest2WithoutSpace()
 
108
                {
 
109
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<G<int>> a;");
 
110
                        AstType type = new SimpleType("G") {
 
111
                                TypeArguments = {
 
112
                                        new SimpleType("G") { TypeArguments = { new PrimitiveType("int") } }
 
113
                                }
 
114
                        };
 
115
                        Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
 
116
                }
 
117
                
 
118
                [Test]
 
119
                public void GenericVariableDeclarationStatementTest()
 
120
                {
 
121
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int> a;");
 
122
                        AstType type = new SimpleType("G") {
 
123
                                TypeArguments = { new PrimitiveType("int") }
 
124
                        };
 
125
                        Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
 
126
                }
 
127
                
 
128
                [Test]
 
129
                public void SimpleVariableDeclarationStatementTest()
 
130
                {
 
131
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("MyVar var = new MyVar();");
 
132
                        Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("MyVar"), "var", new ObjectCreateExpression { Type = new SimpleType("MyVar") }).IsMatch(lvd));
 
133
                }
 
134
                
 
135
                [Test]
 
136
                public void SimpleVariableDeclarationStatementTest1()
 
137
                {
 
138
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("yield yield = new yield();");
 
139
                        Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("yield"), "yield", new ObjectCreateExpression { Type = new SimpleType("yield") }).IsMatch(lvd));
 
140
                }
 
141
                
 
142
                [Test]
 
143
                public void NullableVariableDeclarationStatementTest1()
 
144
                {
 
145
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("int? a;");
 
146
                        Assert.IsTrue(new VariableDeclarationStatement(new PrimitiveType("int").MakeNullableType(), "a").IsMatch(lvd));
 
147
                }
 
148
                
 
149
                [Test]
 
150
                public void NullableVariableDeclarationStatementTest2()
 
151
                {
 
152
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime? a;");
 
153
                        Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakeNullableType(), "a").IsMatch(lvd));
 
154
                }
 
155
                
 
156
                [Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
 
157
                public void NullableVariableDeclarationStatementTest3()
 
158
                {
 
159
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime?[] a;");
 
160
                        Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakeNullableType().MakeArrayType(), "a").IsMatch(lvd));
 
161
                }
 
162
                
 
163
                [Test]
 
164
                public void NullableVariableDeclarationStatementTest4()
 
165
                {
 
166
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("SomeStruct<int?>? a;");
 
167
                        AstType type = new SimpleType("SomeStruct") {
 
168
                                TypeArguments = { new PrimitiveType("int").MakeNullableType() }
 
169
                        }.MakeNullableType();
 
170
                        Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
 
171
                }
 
172
                
 
173
                [Test]
 
174
                public void PositionTestWithoutModifier()
 
175
                {
 
176
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("\ndouble w = 7;");
 
177
                        Assert.AreEqual(2, lvd.StartLocation.Line);
 
178
                        Assert.AreEqual(1, lvd.StartLocation.Column);
 
179
                        Assert.AreEqual(2, lvd.EndLocation.Line);
 
180
                        Assert.AreEqual(14, lvd.EndLocation.Column);
 
181
                }
 
182
                
 
183
                [Test]
 
184
                public void PositionTestWithModifier()
 
185
                {
 
186
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("\nconst double w = 7;");
 
187
                        Assert.AreEqual(Modifiers.Const, lvd.Modifiers);
 
188
                        Assert.AreEqual(2, lvd.StartLocation.Line);
 
189
                        Assert.AreEqual(1, lvd.StartLocation.Column);
 
190
                        Assert.AreEqual(2, lvd.EndLocation.Line);
 
191
                        Assert.AreEqual(20, lvd.EndLocation.Column);
 
192
                }
 
193
                
 
194
                [Test]
 
195
                public void NestedArray()
 
196
                {
 
197
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime[,][] a;");
 
198
                        Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakeArrayType(1).MakeArrayType(2), "a").IsMatch(lvd));
 
199
                }
 
200
                
 
201
                [Test]
 
202
                public void NestedPointers()
 
203
                {
 
204
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime*** a;");
 
205
                        Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakePointerType().MakePointerType().MakePointerType(), "a").IsMatch(lvd));
 
206
                }
 
207
                
 
208
                [Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
 
209
                public void ArrayOfPointers()
 
210
                {
 
211
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime*[] a;");
 
212
                        Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakePointerType().MakeArrayType(), "a").IsMatch(lvd));
 
213
                }
 
214
                
 
215
                [Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
 
216
                public void ArrayOfNullables()
 
217
                {
 
218
                        VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime?[] a;");
 
219
                        Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakeNullableType().MakeArrayType(), "a").IsMatch(lvd));
 
220
                }
 
221
                
 
222
                [Test]
 
223
                public void Global()
 
224
                {
 
225
                        ParseUtilCSharp.AssertStatement(
 
226
                                "global::System.String a;",
 
227
                                new VariableDeclarationStatement {
 
228
                                        Type = new MemberType {
 
229
                                                Target = new MemberType {
 
230
                                                        Target = new SimpleType("global"),
 
231
                                                        IsDoubleColon = true,
 
232
                                                        MemberName = "System"
 
233
                                                },
 
234
                                                IsDoubleColon = false,
 
235
                                                MemberName = "String",
 
236
                                        },
 
237
                                        Variables = {
 
238
                                                new VariableInitializer("a")
 
239
                                        }
 
240
                                });
 
241
                }
 
242
                
 
243
                [Test]
 
244
                public void ArrayDeclarationWithInitializer()
 
245
                {
 
246
                        ParseUtilCSharp.AssertStatement(
 
247
                                "int[] a = { 0 };",
 
248
                                new VariableDeclarationStatement {
 
249
                                        Type = new PrimitiveType("int").MakeArrayType(),
 
250
                                        Variables = {
 
251
                                                new VariableInitializer {
 
252
                                                        Name = "a",
 
253
                                                        Initializer = new ArrayInitializerExpression {
 
254
                                                                Elements = { new PrimitiveExpression(0) }
 
255
                                                        }
 
256
                                                }
 
257
                                        }});
 
258
                }
 
259
        }
 
260
}