PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_TypeIs.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 130 lines | 93 code | 17 blank | 20 comment | 0 complexity | fd8668763457bdef8ec3a5dde5f3c910 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. //
  19. // Authors:
  20. // Federico Di Gregorio <fog@initd.org>
  21. using System;
  22. using System.Reflection;
  23. using System.Linq;
  24. using System.Linq.Expressions;
  25. using NUnit.Framework;
  26. namespace MonoTests.System.Linq.Expressions
  27. {
  28. [TestFixture]
  29. public class ExpressionTest_TypeIs
  30. {
  31. [Test]
  32. [ExpectedException (typeof (ArgumentNullException))]
  33. public void Arg1Null ()
  34. {
  35. Expression.TypeIs (null, typeof (int));
  36. }
  37. [Test]
  38. [ExpectedException (typeof (ArgumentNullException))]
  39. public void Arg2Null ()
  40. {
  41. Expression.TypeIs (Expression.Constant (1), null);
  42. }
  43. [Test]
  44. public void Numeric ()
  45. {
  46. TypeBinaryExpression expr = Expression.TypeIs (Expression.Constant (1), typeof (int));
  47. Assert.AreEqual (ExpressionType.TypeIs, expr.NodeType, "TypeIs#01");
  48. Assert.AreEqual (typeof (bool), expr.Type, "TypeIs#02");
  49. Assert.AreEqual ("(1 Is Int32)", expr.ToString(), "TypeIs#03");
  50. }
  51. [Test]
  52. public void String ()
  53. {
  54. TypeBinaryExpression expr = Expression.TypeIs (Expression.Constant (1), typeof (string));
  55. Assert.AreEqual (ExpressionType.TypeIs, expr.NodeType, "TypeIs#04");
  56. Assert.AreEqual (typeof (bool), expr.Type, "TypeIs#05");
  57. Assert.AreEqual ("(1 Is String)", expr.ToString(), "TypeIs#06");
  58. }
  59. [Test]
  60. public void UserDefinedClass ()
  61. {
  62. TypeBinaryExpression expr = Expression.TypeIs (Expression.Constant (new OpClass()), typeof (OpClass));
  63. Assert.AreEqual (ExpressionType.TypeIs, expr.NodeType, "TypeIs#07");
  64. Assert.AreEqual (typeof (bool), expr.Type, "TypeIs#08");
  65. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) Is OpClass)", expr.ToString(), "TypeIs#09");
  66. }
  67. struct Foo {
  68. }
  69. class Bar {
  70. }
  71. class Baz : Bar {
  72. }
  73. static Func<TType, bool> CreateTypeIs<TType, TCandidate> ()
  74. {
  75. var p = Expression.Parameter (typeof (TType), "p");
  76. return Expression.Lambda<Func<TType, bool>> (
  77. Expression.TypeIs (p, typeof (TCandidate)), p).Compile ();
  78. }
  79. [Test]
  80. public void CompiledTypeIs ()
  81. {
  82. var foo_is_bar = CreateTypeIs<Foo, Bar> ();
  83. var foo_is_foo = CreateTypeIs<Foo, Foo> ();
  84. var bar_is_bar = CreateTypeIs<Bar, Bar> ();
  85. var bar_is_foo = CreateTypeIs<Bar, Foo> ();
  86. var baz_is_bar = CreateTypeIs<Baz, Bar> ();
  87. Assert.IsTrue (foo_is_foo (new Foo ()));
  88. Assert.IsFalse (foo_is_bar (new Foo ()));
  89. Assert.IsTrue (bar_is_bar (new Bar ()));
  90. Assert.IsFalse (bar_is_foo (new Bar ()));
  91. Assert.IsTrue (baz_is_bar (new Baz ()));
  92. }
  93. [Test]
  94. [Category ("NotDotNet")]
  95. [ExpectedException (typeof (ArgumentException))]
  96. public void TypeIsVoid ()
  97. {
  98. Expression.TypeIs ("yoyo".ToConstant (), typeof (void));
  99. }
  100. public static void TacTac ()
  101. {
  102. }
  103. [Test]
  104. public void VoidIsObject ()
  105. {
  106. var vio = Expression.Lambda<Func<bool>> (
  107. Expression.TypeIs (
  108. Expression.Call (GetType ().GetMethod ("TacTac")),
  109. typeof (object))).Compile ();
  110. Assert.IsFalse (vio ());
  111. }
  112. }
  113. }