ExpressionTest_New.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // ExpressionTest_New.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  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_New {
  36. [Test]
  37. [ExpectedException (typeof (ArgumentNullException))]
  38. public void NullType ()
  39. {
  40. Expression.New (null as Type);
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentNullException))]
  44. public void NullConstructor ()
  45. {
  46. Expression.New (null as ConstructorInfo);
  47. }
  48. public class Foo {
  49. public Foo (string s)
  50. {
  51. }
  52. }
  53. public class Bar {
  54. public string Value { get; set; }
  55. public Bar ()
  56. {
  57. }
  58. }
  59. public struct Baz {
  60. }
  61. [Test]
  62. [ExpectedException (typeof (ArgumentException))]
  63. public void NoParameterlessConstructor ()
  64. {
  65. Expression.New (typeof (Foo));
  66. }
  67. [Test]
  68. [ExpectedException (typeof (ArgumentException))]
  69. public void ConstructorHasTooMuchParameters ()
  70. {
  71. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }));
  72. }
  73. [Test]
  74. [ExpectedException (typeof (ArgumentNullException))]
  75. public void HasNullArgument ()
  76. {
  77. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), null as Expression);
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentException))]
  81. public void HasWrongArgument ()
  82. {
  83. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), Expression.Constant (12));
  84. }
  85. [Test]
  86. public void NewFoo ()
  87. {
  88. var n = Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), Expression.Constant ("foo"));
  89. Assert.AreEqual (ExpressionType.New, n.NodeType);
  90. Assert.AreEqual (typeof (Foo), n.Type);
  91. Assert.AreEqual ("new Foo(\"foo\")", n.ToString ());
  92. }
  93. [Test]
  94. public void NewBar ()
  95. {
  96. var n = Expression.New (typeof (Bar));
  97. Assert.AreEqual ("new Bar()", n.ToString ());
  98. n = Expression.New (typeof (Bar).GetConstructor (Type.EmptyTypes));
  99. Assert.AreEqual ("new Bar()", n.ToString ());
  100. }
  101. }
  102. }