1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
21
using ICSharpCode.NRefactory.PatternMatching;
22
using NUnit.Framework;
24
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
27
public class VariableDeclarationStatementTests
30
public void VariableDeclarationStatementTest()
32
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("int a = 5;");
33
Assert.AreEqual(1, lvd.Variables.Count());
34
Assert.AreEqual("a", lvd.Variables.First ().Name);
36
Assert.AreEqual("int", type.ToString ());
37
Assert.AreEqual(5, ((PrimitiveExpression)lvd.Variables.First ().Initializer).Value);
41
public void VoidPointerVariableDeclarationTest()
43
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("void *a;");
44
Assert.IsTrue(new VariableDeclarationStatement(new PrimitiveType("void").MakePointerType(), "a").IsMatch(lvd));
48
public void ComplexGenericVariableDeclarationStatementTest()
50
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("Generic<Namespace.Printable, G<Printable[]> > where = new Generic<Namespace.Printable, G<Printable[]>>();");
51
AstType type = new SimpleType("Generic") {
53
new MemberType { Target = new SimpleType("Namespace"), MemberName = "Printable" },
54
new SimpleType("G") { TypeArguments = { new SimpleType("Printable").MakeArrayType() } }
56
Assert.IsTrue(new VariableDeclarationStatement(type, "where", new ObjectCreateExpression { Type = type.Clone() }).IsMatch(lvd));
60
public void NestedGenericVariableDeclarationStatementTest()
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") }
69
MemberName = "InnerInnerClass"
71
Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
75
public void GenericWithArrayVariableDeclarationStatementTest1()
77
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int>[] a;");
78
AstType type = new SimpleType("G") {
79
TypeArguments = { new PrimitiveType("int") }
81
Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
85
public void GenericWithArrayVariableDeclarationStatementTest2()
87
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int[]> a;");
88
AstType type = new SimpleType("G") {
89
TypeArguments = { new PrimitiveType("int").MakeArrayType() }
91
Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
95
public void GenericVariableDeclarationStatementTest2()
97
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<G<int> > a;");
98
AstType type = new SimpleType("G") {
100
new SimpleType("G") { TypeArguments = { new PrimitiveType("int") } }
103
Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
107
public void GenericVariableDeclarationStatementTest2WithoutSpace()
109
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<G<int>> a;");
110
AstType type = new SimpleType("G") {
112
new SimpleType("G") { TypeArguments = { new PrimitiveType("int") } }
115
Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
119
public void GenericVariableDeclarationStatementTest()
121
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int> a;");
122
AstType type = new SimpleType("G") {
123
TypeArguments = { new PrimitiveType("int") }
125
Assert.IsTrue(new VariableDeclarationStatement(type, "a").IsMatch(lvd));
129
public void SimpleVariableDeclarationStatementTest()
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));
136
public void SimpleVariableDeclarationStatementTest1()
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));
143
public void NullableVariableDeclarationStatementTest1()
145
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("int? a;");
146
Assert.IsTrue(new VariableDeclarationStatement(new PrimitiveType("int").MakeNullableType(), "a").IsMatch(lvd));
150
public void NullableVariableDeclarationStatementTest2()
152
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime? a;");
153
Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakeNullableType(), "a").IsMatch(lvd));
156
[Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
157
public void NullableVariableDeclarationStatementTest3()
159
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime?[] a;");
160
Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakeNullableType().MakeArrayType(), "a").IsMatch(lvd));
164
public void NullableVariableDeclarationStatementTest4()
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));
174
public void PositionTestWithoutModifier()
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);
184
public void PositionTestWithModifier()
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);
195
public void NestedArray()
197
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime[,][] a;");
198
Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakeArrayType(1).MakeArrayType(2), "a").IsMatch(lvd));
202
public void NestedPointers()
204
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime*** a;");
205
Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakePointerType().MakePointerType().MakePointerType(), "a").IsMatch(lvd));
208
[Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
209
public void ArrayOfPointers()
211
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime*[] a;");
212
Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakePointerType().MakeArrayType(), "a").IsMatch(lvd));
215
[Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
216
public void ArrayOfNullables()
218
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime?[] a;");
219
Assert.IsTrue(new VariableDeclarationStatement(new SimpleType("DateTime").MakeNullableType().MakeArrayType(), "a").IsMatch(lvd));
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"
234
IsDoubleColon = false,
235
MemberName = "String",
238
new VariableInitializer("a")
244
public void ArrayDeclarationWithInitializer()
246
ParseUtilCSharp.AssertStatement(
248
new VariableDeclarationStatement {
249
Type = new PrimitiveType("int").MakeArrayType(),
251
new VariableInitializer {
253
Initializer = new ArrayInitializerExpression {
254
Elements = { new PrimitiveExpression(0) }