ExpressionTest_Utils.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // ExpressionTest_Utils.cs
  3. //
  4. // Author:
  5. // Federico Di Gregorio <[email protected]>
  6. // Jb Evain ([email protected])
  7. //
  8. // (C) 2008 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Reflection;
  31. using System.Linq;
  32. using System.Linq.Expressions;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Linq.Expressions
  35. {
  36. public class OpClass
  37. {
  38. public static OpClass operator + (OpClass a, OpClass b)
  39. {
  40. return a;
  41. }
  42. public static OpClass operator - (OpClass a, OpClass b)
  43. {
  44. return a;
  45. }
  46. public static OpClass operator * (OpClass a, OpClass b)
  47. {
  48. return a;
  49. }
  50. public static OpClass operator / (OpClass a, OpClass b)
  51. {
  52. return a;
  53. }
  54. public static OpClass operator % (OpClass a, OpClass b)
  55. {
  56. return a;
  57. }
  58. public static OpClass operator & (OpClass a, OpClass b)
  59. {
  60. return a;
  61. }
  62. public static OpClass operator | (OpClass a, OpClass b)
  63. {
  64. return a;
  65. }
  66. public static OpClass operator ^ (OpClass a, OpClass b)
  67. {
  68. return a;
  69. }
  70. public static OpClass operator >> (OpClass a, int b)
  71. {
  72. return a;
  73. }
  74. public static OpClass operator << (OpClass a, int b)
  75. {
  76. return a;
  77. }
  78. public static bool operator true (OpClass a)
  79. {
  80. return false;
  81. }
  82. public static bool operator false (OpClass a)
  83. {
  84. return false;
  85. }
  86. public static bool operator > (OpClass a, OpClass b)
  87. {
  88. return false;
  89. }
  90. public static bool operator < (OpClass a, OpClass b)
  91. {
  92. return false;
  93. }
  94. public static bool operator >= (OpClass a, OpClass b)
  95. {
  96. return false;
  97. }
  98. public static bool operator <= (OpClass a, OpClass b)
  99. {
  100. return false;
  101. }
  102. public static OpClass operator + (OpClass a)
  103. {
  104. return a;
  105. }
  106. public static OpClass operator - (OpClass a)
  107. {
  108. return a;
  109. }
  110. public static OpClass operator ! (OpClass a)
  111. {
  112. return a;
  113. }
  114. public static OpClass operator ~ (OpClass a)
  115. {
  116. return a;
  117. }
  118. public static void WrongUnaryReturnVoid (OpClass a)
  119. {
  120. }
  121. public static OpClass WrongUnaryParameterCount (OpClass a, OpClass b)
  122. {
  123. return a;
  124. }
  125. public OpClass WrongUnaryNotStatic (OpClass a)
  126. {
  127. return a;
  128. }
  129. public static bool operator == (OpClass a, OpClass b)
  130. {
  131. return ((object) a) == ((object) b);
  132. }
  133. public static bool operator != (OpClass a, OpClass b)
  134. {
  135. return ((object) a) != ((object) b);
  136. }
  137. //
  138. // Required when you have == or !=
  139. //
  140. public override bool Equals (object o)
  141. {
  142. return ((object)this) == o;
  143. }
  144. public override int GetHashCode ()
  145. {
  146. return 1;
  147. }
  148. }
  149. public class NoOpClass
  150. {
  151. // No user-defined operators here (we use this class to test for exceptions.)
  152. }
  153. public class MemberClass
  154. {
  155. public int TestField1 = 0;
  156. public readonly int TestField2 = 1;
  157. public int TestProperty1 { get { return TestField1; }}
  158. public int TestProperty2 { get { return TestField1; } set { TestField1 = value; }}
  159. public int TestMethod (int i) { return TestField1 + i; }
  160. public T TestGenericMethod<T> (T arg) { return arg; }
  161. public delegate int TestDelegate(int i);
  162. public event TestDelegate TestEvent;
  163. public void DoNothing ()
  164. {
  165. // Just to avoid a compiler warning
  166. if (TestEvent != null)
  167. return;
  168. }
  169. public static int StaticField = 0;
  170. public static int StaticProperty { get { return StaticField; } set { StaticField = value; }}
  171. public static int StaticMethod (int i) { return 1 + i; }
  172. public static T StaticGenericMethod<T> (T arg) { return arg; }
  173. public static MethodInfo GetMethodInfo ()
  174. {
  175. return typeof (MemberClass).GetMethod ("TestMethod");
  176. }
  177. public static FieldInfo GetRoFieldInfo ()
  178. {
  179. return typeof (MemberClass).GetField ("TestField1");
  180. }
  181. public static FieldInfo GetRwFieldInfo ()
  182. {
  183. return typeof (MemberClass).GetField ("TestField2");
  184. }
  185. public static PropertyInfo GetRoPropertyInfo ()
  186. {
  187. return typeof (MemberClass).GetProperty ("TestProperty1");
  188. }
  189. public static PropertyInfo GetRwPropertyInfo ()
  190. {
  191. return typeof (MemberClass).GetProperty ("TestProperty2");
  192. }
  193. public static EventInfo GetEventInfo ()
  194. {
  195. return typeof (MemberClass).GetEvent ("TestEvent");
  196. }
  197. public static FieldInfo GetStaticFieldInfo ()
  198. {
  199. return typeof (MemberClass).GetField ("StaticField");
  200. }
  201. public static PropertyInfo GetStaticPropertyInfo ()
  202. {
  203. return typeof (MemberClass).GetProperty ("StaticProperty");
  204. }
  205. }
  206. public struct OpStruct
  207. {
  208. public static OpStruct operator + (OpStruct a, OpStruct b)
  209. {
  210. return a;
  211. }
  212. public static OpStruct operator - (OpStruct a, OpStruct b)
  213. {
  214. return a;
  215. }
  216. public static OpStruct operator * (OpStruct a, OpStruct b)
  217. {
  218. return a;
  219. }
  220. public static OpStruct operator & (OpStruct a, OpStruct b)
  221. {
  222. return a;
  223. }
  224. }
  225. static class ExpressionExtensions {
  226. public static ConstantExpression ToConstant<T> (this T t)
  227. {
  228. return Expression.Constant (t);
  229. }
  230. public static void AssertThrows (this Action action, Type type)
  231. {
  232. try {
  233. action ();
  234. Assert.Fail ();
  235. } catch (Exception e) {
  236. if (e.GetType () != type)
  237. Assert.Fail ();
  238. }
  239. }
  240. }
  241. class Item<T> {
  242. bool left_called;
  243. T left;
  244. public T Left {
  245. get {
  246. left_called = true;
  247. return left;
  248. }
  249. }
  250. public bool LeftCalled {
  251. get { return left_called; }
  252. }
  253. bool right_called;
  254. T right;
  255. public T Right {
  256. get {
  257. right_called = true;
  258. return right;
  259. }
  260. }
  261. public bool RightCalled {
  262. get { return right_called; }
  263. }
  264. public Item (T left, T right)
  265. {
  266. this.left = left;
  267. this.right = right;
  268. }
  269. }
  270. }