PageRenderTime 27ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/ObjectCreateExpressionTests.cs

http://github.com/icsharpcode/ILSpy
C# | 252 lines | 222 code | 13 blank | 17 comment | 0 complexity | fb5dbd0e7918aac3c0c2b9516cc48705 MD5 | raw file
  1// Copyright (c) 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
 19using System;
 20using NUnit.Framework;
 21
 22namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
 23{
 24	[TestFixture]
 25	public class ObjectCreateExpressionTests
 26	{
 27		[Test]
 28		public void Simple()
 29		{
 30			ParseUtilCSharp.AssertExpression(
 31				"new MyObject(1, 2, 3)",
 32				new ObjectCreateExpression {
 33					Type = new SimpleType("MyObject"),
 34					Arguments = {
 35						new PrimitiveExpression(1),
 36						new PrimitiveExpression(2),
 37						new PrimitiveExpression(3)
 38					}});
 39		}
 40		
 41		[Test]
 42		public void Nullable()
 43		{
 44			ParseUtilCSharp.AssertExpression(
 45				"new IntPtr?(1)",
 46				new ObjectCreateExpression {
 47					Type = new SimpleType("IntPtr").MakeNullableType(),
 48					Arguments = { new PrimitiveExpression(1) }
 49				});
 50		}
 51		
 52		[Test]
 53		public void ObjectInitializer()
 54		{
 55			ParseUtilCSharp.AssertExpression(
 56				"new Point() { X = 0, Y = 1 }",
 57				new ObjectCreateExpression {
 58					Type = new SimpleType("Point"),
 59					Initializer = new ArrayInitializerExpression {
 60						Elements = {
 61							new NamedExpression("X", new PrimitiveExpression(0)),
 62							new NamedExpression("Y", new PrimitiveExpression(1))
 63						}
 64					}});
 65		}
 66		
 67		[Test]
 68		public void ObjectInitializerWithoutParenthesis()
 69		{
 70			ParseUtilCSharp.AssertExpression(
 71				"new Point { X = 0, Y = 1 }",
 72				new ObjectCreateExpression {
 73					Type = new SimpleType("Point"),
 74					Initializer = new ArrayInitializerExpression {
 75						Elements = {
 76							new NamedExpression("X", new PrimitiveExpression(0)),
 77							new NamedExpression("Y", new PrimitiveExpression(1))
 78						}
 79					}});
 80		}
 81		
 82		[Test]
 83		public void ObjectInitializerWithTrailingComma()
 84		{
 85			ParseUtilCSharp.AssertExpression(
 86				"new Point() { X = 0, Y = 1, }",
 87				new ObjectCreateExpression {
 88					Type = new SimpleType("Point"),
 89					Initializer = new ArrayInitializerExpression {
 90						Elements = {
 91							new NamedExpression("X", new PrimitiveExpression(0)),
 92							new NamedExpression("Y", new PrimitiveExpression(1))
 93						}
 94					}});
 95		}
 96		
 97		[Test]
 98		public void NestedObjectInitializer()
 99		{
100			ParseUtilCSharp.AssertExpression(
101				"new Rectangle { P1 = new Point { X = 0, Y = 1 }, P2 = new Point { X = 2, Y = 3 } }",
102				new ObjectCreateExpression {
103					Type = new SimpleType("Rectangle"),
104					Initializer = new ArrayInitializerExpression {
105						Elements = {
106							new NamedExpression(
107								"P1",
108								new ObjectCreateExpression {
109									Type = new SimpleType("Point"),
110									Initializer = new ArrayInitializerExpression {
111										Elements = {
112											new NamedExpression("X", new PrimitiveExpression(0)),
113											new NamedExpression("Y", new PrimitiveExpression(1))
114										}
115									}}),
116							new NamedExpression(
117								"P2",
118								new ObjectCreateExpression {
119									Type = new SimpleType("Point"),
120									Initializer = new ArrayInitializerExpression {
121										Elements = {
122											new NamedExpression("X", new PrimitiveExpression(2)),
123											new NamedExpression("Y", new PrimitiveExpression(3))
124										}
125									}})
126						}}});
127		}
128		
129		[Test]
130		public void NestedObjectInitializerForPreinitializedProperty()
131		{
132			ParseUtilCSharp.AssertExpression(
133				"new Rectangle { P1 = { X = 0, Y = 1 }, P2 = { X = 2, Y = 3 } }",
134				new ObjectCreateExpression {
135					Type = new SimpleType("Rectangle"),
136					Initializer = new ArrayInitializerExpression {
137						Elements = {
138							new NamedExpression(
139								"P1",
140								new ArrayInitializerExpression {
141									Elements = {
142										new NamedExpression("X", new PrimitiveExpression(0)),
143										new NamedExpression("Y", new PrimitiveExpression(1))
144									}
145								}),
146							new NamedExpression(
147								"P2",
148								new ArrayInitializerExpression {
149									Elements = {
150										new NamedExpression("X", new PrimitiveExpression(2)),
151										new NamedExpression("Y", new PrimitiveExpression(3))
152									}
153								})
154						}}});
155		}
156		
157		[Test]
158		public void CollectionInitializer()
159		{
160			ParseUtilCSharp.AssertExpression(
161				"new List<int> { 0, 1, 2 }",
162				new ObjectCreateExpression {
163					Type = new SimpleType("List", new PrimitiveType("int")),
164					Initializer = new ArrayInitializerExpression {
165						Elements = {
166							new ArrayInitializerExpression(new PrimitiveExpression(0)),
167							new ArrayInitializerExpression(new PrimitiveExpression(1)),
168							new ArrayInitializerExpression(new PrimitiveExpression(2))
169						}}});
170		}
171		
172		
173		[Test]
174		public void DictionaryInitializer()
175		{
176			ParseUtilCSharp.AssertExpression(
177				"new Dictionary<string, int> { { \"a\", 0 }, { \"b\", 1 } }",
178				new ObjectCreateExpression {
179					Type = new SimpleType("Dictionary", new PrimitiveType("string"), new PrimitiveType("int")),
180					Initializer = new ArrayInitializerExpression {
181						Elements = {
182							new ArrayInitializerExpression {
183								Elements = { new PrimitiveExpression("a"), new PrimitiveExpression(0) }
184							},
185							new ArrayInitializerExpression {
186								Elements = { new PrimitiveExpression("b"), new PrimitiveExpression(1) }
187							}
188						}}});
189		}
190		
191		[Test]
192		public void ComplexCollectionInitializer()
193		{
194			ParseUtilCSharp.AssertExpression(
195				@"new List<Contact> {
196					new Contact {
197						Name = ""Chris"",
198						PhoneNumbers = { ""206-555-0101"" }
199					},
200					new Contact(additionalParameter) {
201						Name = ""Bob"",
202						PhoneNumbers = { ""650-555-0199"", ""425-882-8080"" }
203					}
204				}",
205				new ObjectCreateExpression {
206					Type = new SimpleType("List", new SimpleType("Contact")),
207					Initializer = new ArrayInitializerExpression(
208						new ArrayInitializerExpression(
209							new ObjectCreateExpression {
210								Type = new SimpleType("Contact"),
211								Initializer = new ArrayInitializerExpression {
212									Elements = {
213										new NamedExpression("Name", new PrimitiveExpression("Chris")),
214										new NamedExpression("PhoneNumbers", new ArrayInitializerExpression () {
215											Elements = { new ArrayInitializerExpression(new PrimitiveExpression("206-555-0101")) }
216										})
217									}}}),
218						new ArrayInitializerExpression(
219							new ObjectCreateExpression {
220								Type = new SimpleType("Contact"),
221								Arguments = { new IdentifierExpression("additionalParameter") },
222								Initializer = new ArrayInitializerExpression {
223									Elements = {
224										new NamedExpression("Name", new PrimitiveExpression("Bob")),
225										new NamedExpression("PhoneNumbers", new ArrayInitializerExpression () {
226											Elements = {
227												new ArrayInitializerExpression(new PrimitiveExpression("650-555-0199")),
228												new ArrayInitializerExpression(new PrimitiveExpression("425-882-8080"))
229											}
230										})
231									}}})
232						)});
233		}
234		
235		[Test]
236		public void AssignmentInCollectionInitializer()
237		{
238			ParseUtilCSharp.AssertExpression(
239				@"new List<int> { { a = 1 } }",
240				new ObjectCreateExpression {
241					Type = new SimpleType("List", new PrimitiveType("int")),
242					Initializer = new ArrayInitializerExpression {
243						Elements = {
244							new ArrayInitializerExpression {
245								Elements = {
246									new AssignmentExpression(new IdentifierExpression("a"), new PrimitiveExpression(1))
247								}
248							}
249						}}});
250		}
251	}
252}