ExpressionTest_OrElse.cs 9.7 KB

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