PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/icsharpcode/ILSpy
C# | 180 lines | 148 code | 14 blank | 18 comment | 0 complexity | cf38074d81cee62606b9e73cfdc9f238 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 CastExpressionTests
 26	{
 27		[Test]
 28		public void SimpleCastExpression()
 29		{
 30			ParseUtilCSharp.AssertExpression(
 31				"(MyObject)o",
 32				new CastExpression {
 33					Type = new SimpleType("MyObject"),
 34					Expression = new IdentifierExpression("o")
 35				});
 36		}
 37		
 38		[Test]
 39		public void ArrayCastExpression()
 40		{
 41			ParseUtilCSharp.AssertExpression(
 42				"(MyType[])o",
 43				new CastExpression {
 44					Type = new SimpleType("MyType").MakeArrayType(1),
 45					Expression = new IdentifierExpression("o")
 46				});
 47		}
 48		
 49		[Test]
 50		public void NullablePrimitiveCastExpression()
 51		{
 52			ParseUtilCSharp.AssertExpression(
 53				"(int?)o",
 54				new CastExpression {
 55					Type = new ComposedType { BaseType = new PrimitiveType("int"), HasNullableSpecifier = true },
 56					Expression = new IdentifierExpression("o")
 57				});
 58		}
 59		
 60		[Test]
 61		public void NullableCastExpression()
 62		{
 63			ParseUtilCSharp.AssertExpression(
 64				"(MyType?)o",
 65				new CastExpression {
 66					Type = new ComposedType { BaseType = new SimpleType("MyType"), HasNullableSpecifier = true },
 67					Expression = new IdentifierExpression("o")
 68				});
 69		}
 70		
 71		[Test]
 72		public void NullableTryCastExpression()
 73		{
 74			ParseUtilCSharp.AssertExpression(
 75				"o as int?",
 76				new AsExpression {
 77					Type = new ComposedType { BaseType = new PrimitiveType("int"), HasNullableSpecifier = true },
 78					Expression = new IdentifierExpression("o")
 79				});
 80		}
 81		
 82		[Test]
 83		public void GenericCastExpression()
 84		{
 85			ParseUtilCSharp.AssertExpression(
 86				"(List<string>)o",
 87				new CastExpression {
 88					Type = new SimpleType("List") { TypeArguments = { new PrimitiveType("string") } },
 89					Expression = new IdentifierExpression("o")
 90				});
 91		}
 92		
 93		[Test]
 94		public void GenericArrayCastExpression()
 95		{
 96			ParseUtilCSharp.AssertExpression(
 97				"(List<string>[])o",
 98				new CastExpression {
 99					Type = new ComposedType {
100						BaseType = new SimpleType("List") { TypeArguments = { new PrimitiveType("string") } },
101						ArraySpecifiers = { new ArraySpecifier(1) }
102					},
103					Expression = new IdentifierExpression("o")
104				});
105		}
106		
107		[Test]
108		public void GenericArrayAsCastExpression()
109		{
110			ParseUtilCSharp.AssertExpression(
111				"o as List<string>[]",
112				new AsExpression {
113					Type = new ComposedType {
114						BaseType = new SimpleType("List") { TypeArguments = { new PrimitiveType("string") } },
115						ArraySpecifiers = { new ArraySpecifier(1) }
116					},
117					Expression = new IdentifierExpression("o")
118				});
119		}
120		
121		[Test]
122		public void CastMemberReferenceOnParenthesizedExpression()
123		{
124			// yes, we really want to evaluate .Member on expr and THEN cast the result to MyType
125			ParseUtilCSharp.AssertExpression(
126				"(MyType)(expr).Member",
127				new CastExpression {
128					Type = new SimpleType("MyType"),
129					Expression = new ParenthesizedExpression { Expression = new IdentifierExpression("expr") }.Member("Member")
130				});
131		}
132		
133		[Test]
134		public void TryCastParenthesizedExpression()
135		{
136			ParseUtilCSharp.AssertExpression(
137				"(o) as string",
138				new AsExpression {
139					Expression = new ParenthesizedExpression { Expression = new IdentifierExpression("o") },
140					Type = new PrimitiveType("string")
141				});
142		}
143		
144		[Test]
145		public void CastNegation()
146		{
147			ParseUtilCSharp.AssertExpression(
148				"(uint)-negativeValue",
149				new CastExpression {
150					Type = new PrimitiveType("uint"),
151					Expression = new UnaryOperatorExpression(
152						UnaryOperatorType.Minus,
153						new IdentifierExpression("negativeValue")
154					)});
155		}
156		
157		[Test]
158		public void SubtractionIsNotCast()
159		{
160			ParseUtilCSharp.AssertExpression(
161				"(BigInt)-negativeValue",
162				new BinaryOperatorExpression {
163					Left = new ParenthesizedExpression { Expression = new IdentifierExpression("BigInt") },
164					Operator = BinaryOperatorType.Subtract,
165					Right = new IdentifierExpression("negativeValue")
166				});
167		}
168		
169		[Test]
170		public void IntMaxValueToBigInt()
171		{
172			ParseUtilCSharp.AssertExpression(
173				"(BigInt)int.MaxValue",
174				new CastExpression {
175					Type = new SimpleType("BigInt"),
176					Expression = new PrimitiveType("int").Member("MaxValue")
177				});
178		}
179	}
180}