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