PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/web/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_Condition.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 105 lines | 64 code | 14 blank | 27 comment | 0 complexity | 98f8b54b2b5e2812de5988db0faf1ebc 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. //
  2. // ExpressionTest.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@novell.com)
  6. //
  7. // (C) 2008 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Reflection;
  30. using System.Linq;
  31. using System.Linq.Expressions;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Linq.Expressions {
  34. [TestFixture]
  35. public class ExpressionTest_Condition {
  36. [Test]
  37. [ExpectedException (typeof (ArgumentNullException))]
  38. public void Arg1Null ()
  39. {
  40. Expression.Condition (null, Expression.Constant (1), Expression.Constant (0));
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentNullException))]
  44. public void Arg2Null ()
  45. {
  46. Expression.Condition (Expression.Equal (Expression.Constant (42), Expression.Constant (42)), null, Expression.Constant (0));
  47. }
  48. [Test]
  49. [ExpectedException (typeof (ArgumentNullException))]
  50. public void Arg3Null ()
  51. {
  52. Expression.Condition (Expression.Equal (Expression.Constant (42), Expression.Constant (42)), Expression.Constant (1), null);
  53. }
  54. [Test]
  55. [ExpectedException (typeof (ArgumentException))]
  56. public void TestNotBool ()
  57. {
  58. Expression.Condition (Expression.Constant (42), Expression.Constant (1), Expression.Constant (0));
  59. }
  60. [Test]
  61. [ExpectedException (typeof (ArgumentException))]
  62. public void TrueBlockTypeNotFalseBlockType ()
  63. {
  64. Expression.Condition (Expression.Constant (42), Expression.Constant (1.1), Expression.Constant (0));
  65. }
  66. [Test]
  67. public void TestSimpleConditional ()
  68. {
  69. var cond = Expression.Condition (Expression.GreaterThan (Expression.Constant (2), Expression.Constant (1)), Expression.Constant (1), Expression.Constant (0));
  70. Assert.AreEqual (typeof (bool), cond.Test.Type);
  71. Assert.AreEqual ("IIF((2 > 1), 1, 0)", cond.ToString ());
  72. }
  73. [Test]
  74. public void CompileConditional ()
  75. {
  76. var parameters = new [] { Expression.Parameter (typeof (int), "number") };
  77. var l = Expression.Lambda<Func<int, string>> (
  78. Expression.Condition (
  79. Expression.GreaterThanOrEqual (
  80. parameters [0],
  81. Expression.Constant (0)),
  82. Expression.Constant ("+"),
  83. Expression.Constant ("-")),
  84. parameters);
  85. var gt = l.Compile ();
  86. Assert.AreEqual ("+", gt (1));
  87. Assert.AreEqual ("+", gt (0));
  88. Assert.AreEqual ("-", gt (-1));
  89. }
  90. }
  91. }