2
0

ExpressionTest_OrElse.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <[email protected]>
  21. // Jb Evain <[email protected]>
  22. using System;
  23. using System.Reflection;
  24. using System.Linq;
  25. using System.Linq.Expressions;
  26. using NUnit.Framework;
  27. namespace MonoTests.System.Linq.Expressions
  28. {
  29. [TestFixture]
  30. [Category("SRE")]
  31. public class ExpressionTest_OrElse
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentNullException))]
  35. public void Arg1Null ()
  36. {
  37. Expression.OrElse (null, Expression.Constant (1));
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg2Null ()
  42. {
  43. Expression.OrElse (Expression.Constant (1), null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (InvalidOperationException))]
  47. public void NoOperatorClass ()
  48. {
  49. Expression.OrElse (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (InvalidOperationException))]
  53. public void Double ()
  54. {
  55. Expression.OrElse (Expression.Constant (1.0), Expression.Constant (2.0));
  56. }
  57. [Test]
  58. [ExpectedException (typeof (InvalidOperationException))]
  59. public void Integer ()
  60. {
  61. Expression.OrElse (Expression.Constant (1), Expression.Constant (2));
  62. }
  63. [Test]
  64. [ExpectedException (typeof (InvalidOperationException))]
  65. public void MismatchedTypes ()
  66. {
  67. Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (true));
  68. }
  69. [Test]
  70. public void Boolean ()
  71. {
  72. BinaryExpression expr = Expression.OrElse (Expression.Constant (true), Expression.Constant (false));
  73. Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#01");
  74. Assert.AreEqual (typeof (bool), expr.Type, "OrElse#02");
  75. Assert.IsNull (expr.Method, "OrElse#03");
  76. }
  77. [Test]
  78. public void UserDefinedClass ()
  79. {
  80. // We can use the simplest version of GetMethod because we already know only one
  81. // exists in the very simple class we're using for the tests.
  82. MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseOr");
  83. BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  84. Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
  85. Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
  86. Assert.AreEqual (mi, expr.Method, "OrElse#07");
  87. Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
  88. }
  89. public class BrokenMethod {
  90. public static int operator | (BrokenMethod a, BrokenMethod b)
  91. {
  92. return 1;
  93. }
  94. }
  95. public class BrokenMethod2 {
  96. public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
  97. {
  98. return null;
  99. }
  100. }
  101. [Test]
  102. [ExpectedException(typeof(ArgumentException))]
  103. public void MethodInfoReturnType ()
  104. {
  105. Expression.OrElse (Expression.Constant (new BrokenMethod ()),
  106. Expression.Constant (new BrokenMethod ()));
  107. }
  108. [Test]
  109. [ExpectedException(typeof(ArgumentException))]
  110. public void MethodInfoReturnType2 ()
  111. {
  112. Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
  113. Expression.Constant (1));
  114. }
  115. [Test]
  116. public void OrElseNotLifted ()
  117. {
  118. var b = Expression.OrElse (
  119. Expression.Constant (true, typeof (bool)),
  120. Expression.Constant (true, typeof (bool)));
  121. Assert.AreEqual (typeof (bool), b.Type);
  122. Assert.IsFalse (b.IsLifted);
  123. Assert.IsFalse (b.IsLiftedToNull);
  124. }
  125. [Test]
  126. public void OrElseTest ()
  127. {
  128. var a = Expression.Parameter (typeof (bool), "a");
  129. var b = Expression.Parameter (typeof (bool), "b");
  130. var l = Expression.Lambda<Func<bool, bool, bool>> (
  131. Expression.OrElse (a, b), a, b);
  132. var be = l.Body as BinaryExpression;
  133. Assert.IsNotNull (be);
  134. Assert.AreEqual (typeof (bool), be.Type);
  135. Assert.IsFalse (be.IsLifted);
  136. Assert.IsFalse (be.IsLiftedToNull);
  137. var c = l.Compile ();
  138. Assert.AreEqual (true, c (true, true), "o1");
  139. Assert.AreEqual (true, c (true, false), "o2");
  140. Assert.AreEqual (true, c (false, true), "o3");
  141. Assert.AreEqual (false, c (false, false), "o4");
  142. }
  143. [Test]
  144. public void OrElseLifted ()
  145. {
  146. var b = Expression.OrElse (
  147. Expression.Constant (null, typeof (bool?)),
  148. Expression.Constant (null, typeof (bool?)));
  149. Assert.AreEqual (typeof (bool?), b.Type);
  150. Assert.IsTrue (b.IsLifted);
  151. Assert.IsTrue (b.IsLiftedToNull);
  152. }
  153. [Test]
  154. public void OrElseTestNullable ()
  155. {
  156. var a = Expression.Parameter (typeof (bool?), "a");
  157. var b = Expression.Parameter (typeof (bool?), "b");
  158. var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
  159. Expression.OrElse (a, b), a, b);
  160. var be = l.Body as BinaryExpression;
  161. Assert.IsNotNull (be);
  162. Assert.AreEqual (typeof (bool?), be.Type);
  163. Assert.IsTrue (be.IsLifted);
  164. Assert.IsTrue (be.IsLiftedToNull);
  165. var c = l.Compile ();
  166. Assert.AreEqual (true, c (true, true), "o1");
  167. Assert.AreEqual (true, c (true, false), "o2");
  168. Assert.AreEqual (true, c (false, true), "o3");
  169. Assert.AreEqual (false, c (false, false), "o4");
  170. Assert.AreEqual (true, c (true, null), "o5");
  171. Assert.AreEqual (null, c (false, null), "o6");
  172. Assert.AreEqual (null, c (null, false), "o7");
  173. Assert.AreEqual (true, c (true, null), "o8");
  174. Assert.AreEqual (null, c (null, null), "o9");
  175. }
  176. [Test]
  177. public void OrElseBoolItem ()
  178. {
  179. var i = Expression.Parameter (typeof (Item<bool>), "i");
  180. var and = Expression.Lambda<Func<Item<bool>, bool>> (
  181. Expression.OrElse (
  182. Expression.Property (i, "Left"),
  183. Expression.Property (i, "Right")), i).Compile ();
  184. var item = new Item<bool> (true, false);
  185. Assert.AreEqual (true, and (item));
  186. Assert.IsTrue (item.LeftCalled);
  187. Assert.IsFalse (item.RightCalled);
  188. }
  189. [Test]
  190. public void OrElseNullableBoolItem ()
  191. {
  192. var i = Expression.Parameter (typeof (Item<bool?>), "i");
  193. var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
  194. Expression.OrElse (
  195. Expression.Property (i, "Left"),
  196. Expression.Property (i, "Right")), i).Compile ();
  197. var item = new Item<bool?> (true, false);
  198. Assert.AreEqual ((bool?) true, and (item));
  199. Assert.IsTrue (item.LeftCalled);
  200. Assert.IsFalse (item.RightCalled);
  201. }
  202. struct Slot {
  203. public int Value;
  204. public Slot (int val)
  205. {
  206. this.Value = val;
  207. }
  208. public static Slot operator | (Slot a, Slot b)
  209. {
  210. return new Slot (a.Value | b.Value);
  211. }
  212. public static bool operator true (Slot a)
  213. {
  214. return a.Value != 0;
  215. }
  216. public static bool operator false (Slot a)
  217. {
  218. return a.Value == 0;
  219. }
  220. }
  221. [Test]
  222. public void UserDefinedOrElse ()
  223. {
  224. var l = Expression.Parameter (typeof (Slot), "l");
  225. var r = Expression.Parameter (typeof (Slot), "r");
  226. var method = typeof (Slot).GetMethod ("op_BitwiseOr");
  227. var node = Expression.OrElse (l, r, method);
  228. Assert.IsFalse (node.IsLifted);
  229. Assert.IsFalse (node.IsLiftedToNull);
  230. Assert.AreEqual (method, node.Method);
  231. var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
  232. Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
  233. Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
  234. }
  235. [Test]
  236. public void UserDefinedOrElseShortCircuit ()
  237. {
  238. var i = Expression.Parameter (typeof (Item<Slot>), "i");
  239. var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
  240. Expression.OrElse (
  241. Expression.Property (i, "Left"),
  242. Expression.Property (i, "Right")), i).Compile ();
  243. var item = new Item<Slot> (new Slot (1), new Slot (0));
  244. Assert.AreEqual (new Slot (1), orelse (item));
  245. Assert.IsTrue (item.LeftCalled);
  246. Assert.IsFalse (item.RightCalled);
  247. }
  248. [Test]
  249. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
  250. public void UserDefinedLiftedOrElseShortCircuit ()
  251. {
  252. var i = Expression.Parameter (typeof (Item<Slot?>), "i");
  253. var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
  254. Expression.OrElse (
  255. Expression.Property (i, "Left"),
  256. Expression.Property (i, "Right")), i).Compile ();
  257. var item = new Item<Slot?> (new Slot (1), null);
  258. Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
  259. Assert.IsTrue (item.LeftCalled);
  260. Assert.IsFalse (item.RightCalled);
  261. }
  262. struct Incomplete {
  263. public int Value;
  264. public Incomplete (int val)
  265. {
  266. Value = val;
  267. }
  268. public static Incomplete operator | (Incomplete a, Incomplete b)
  269. {
  270. return new Incomplete (a.Value | b.Value);
  271. }
  272. }
  273. [Test]
  274. [ExpectedException (typeof (ArgumentException))]
  275. public void IncompleteUserDefinedOrElse ()
  276. {
  277. var l = Expression.Parameter (typeof (Incomplete), "l");
  278. var r = Expression.Parameter (typeof (Incomplete), "r");
  279. var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
  280. Expression.OrElse (l, r, method);
  281. }
  282. }
  283. }