PageRenderTime 67ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/Ruby/Tests/Interop/net/method/fixtures/classes.rb

http://github.com/IronLanguages/main
Ruby | 1549 lines | 1274 code | 235 blank | 40 comment | 58 complexity | bbdc50bf87a836e2b33dc801e82b533f MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require File.dirname(__FILE__) + "/../../bcl/fixtures/classes"
  2. reference "System.dll"
  3. csc <<-EOL
  4. using Microsoft.Scripting.Math;
  5. using Microsoft.Scripting.Utils;
  6. using System.Collections.Generic;
  7. EOL
  8. def method_block(type, prefix, suffix = "", &blk)
  9. val = <<-EOL
  10. public #{type} #{prefix}SignatureOverload#{suffix}() { #{blk.call "SO void" }; }
  11. public #{type} #{prefix}SignatureOverload#{suffix}(string foo) { #{blk.call "SO string"}; }
  12. public #{type} #{prefix}SignatureOverload#{suffix}(int foo) { #{blk.call "SO int"}; }
  13. public #{type} #{prefix}SignatureOverload#{suffix}(string foo, params int[] bar) { #{blk.call "SO string params(int[])"}; }
  14. public #{type} #{prefix}SignatureOverload#{suffix}(string foo, params string[] bar) { #{blk.call "SO string params(string[])"}; }
  15. public #{type} #{prefix}SignatureOverload#{suffix}(string foo, int bar, int baz) { #{ blk.call "SO string int int"};}
  16. public #{type} #{prefix}SignatureOverload#{suffix}(params int[] args) { #{blk.call "SO params(int[])"};}
  17. public #{type} #{prefix}SignatureOverload#{suffix}(ref string foo) { #{blk.call "SO ref string"}; }
  18. public #{type} #{prefix}SignatureOverload#{suffix}(out int foo) { foo = 1;#{blk.call "SO out int"}; }
  19. public #{type} #{prefix}SignatureOverload#{suffix}(string foo, ref string bar) { #{blk.call "SO string ref"}; }
  20. public #{type} #{prefix}SignatureOverload#{suffix}(ref string foo, string bar) { #{blk.call "SO ref string"}; }
  21. public #{type} #{prefix}SignatureOverload#{suffix}(out string foo, ref string bar) { foo = "out"; #{blk.call "SO out ref"}; }
  22. EOL
  23. end
  24. @methods_string = <<-EOL
  25. #region private methods
  26. private string Private1Generic0Arg<T>() {
  27. return "private generic no args";
  28. }
  29. private string Private1Generic1Arg<T>(T arg0) {
  30. return Public1Generic1Arg<T>(arg0);
  31. }
  32. private string Private1Generic2Arg<T>(T arg0, string arg1) {
  33. return Public1Generic2Arg<T>(arg0, arg1);
  34. }
  35. private string Private2Generic2Arg<T, U>(T arg0, U arg1) {
  36. return Public2Generic2Arg<T, U>(arg0, arg1);
  37. }
  38. private string Private2Generic3Arg<T, U>(T arg0, U arg1, string arg2) {
  39. return Public2Generic3Arg<T, U>(arg0, arg1, arg2);
  40. }
  41. private string Private3Generic3Arg<T, U, V>(T arg0, U arg1, V arg2) {
  42. return Public3Generic3Arg<T, U, V>(arg0, arg1, arg2);
  43. }
  44. private string Private3Generic4Arg<T, U, V>(T arg0, U arg1, V arg2, string arg3) {
  45. return Public3Generic4Arg<T, U, V>(arg0, arg1, arg2, arg3);
  46. }
  47. #endregion
  48. #region protected methods
  49. protected string Protected1Generic0Arg<T>() {
  50. return "protected generic no args";
  51. }
  52. protected string Protected1Generic1Arg<T>(T arg0) {
  53. return Public1Generic1Arg<T>(arg0);
  54. }
  55. protected string Protected1Generic2Arg<T>(T arg0, string arg1) {
  56. return Public1Generic2Arg<T>(arg0, arg1);
  57. }
  58. protected string Protected2Generic2Arg<T, U>(T arg0, U arg1) {
  59. return Public2Generic2Arg<T, U>(arg0, arg1);
  60. }
  61. protected string Protected2Generic3Arg<T, U>(T arg0, U arg1, string arg2) {
  62. return Public2Generic3Arg<T, U>(arg0, arg1, arg2);
  63. }
  64. protected string Protected3Generic3Arg<T, U, V>(T arg0, U arg1, V arg2) {
  65. return Public3Generic3Arg<T, U, V>(arg0, arg1, arg2);
  66. }
  67. protected string Protected3Generic4Arg<T, U, V>(T arg0, U arg1, V arg2, string arg3) {
  68. return Public3Generic4Arg<T, U, V>(arg0, arg1, arg2, arg3);
  69. }
  70. #endregion
  71. #region public methods
  72. public string Public1Generic0Arg<T>() {
  73. return "public generic no args";
  74. }
  75. public string Public1Generic1Arg<T>(T arg0) {
  76. return arg0.ToString();
  77. }
  78. public string Public1Generic2Arg<T>(T arg0, string arg1) {
  79. return System.String.Format("{0} {1}", arg0, arg1);
  80. }
  81. public string Public2Generic2Arg<T, U>(T arg0, U arg1) {
  82. return Public1Generic2Arg<T>(arg0, arg1.ToString());
  83. }
  84. public string Public2Generic3Arg<T, U>(T arg0, U arg1, string arg2) {
  85. return System.String.Format("{0} {1} {2}", arg0, arg1, arg2);
  86. }
  87. public string Public3Generic3Arg<T, U, V>(T arg0, U arg1, V arg2) {
  88. return Public2Generic3Arg<T, U>(arg0, arg1, arg2.ToString());
  89. }
  90. public string Public3Generic4Arg<T, U, V>(T arg0, U arg1, V arg2, string arg3) {
  91. return System.String.Format("{0} {1} {2} {3}", arg0, arg1, arg2, arg3);
  92. }
  93. #endregion
  94. #region Constrained methods
  95. public T StructConstraintMethod<T>(T arg0)
  96. where T : struct {
  97. return arg0;
  98. }
  99. public T ClassConstraintMethod<T>(T arg0)
  100. where T : class {
  101. return arg0;
  102. }
  103. public T ConstructorConstraintMethod<T>()
  104. where T : new() {
  105. return new T();
  106. }
  107. public T TypeConstraintMethod<T, TBase>(T arg0)
  108. where T : TBase {
  109. return arg0;
  110. }
  111. #endregion
  112. EOL
  113. @conflicting_method_string = <<-EOL
  114. public string Public1Generic2Arg<T>(T arg0, K arg1) {
  115. return Public2Generic2Arg<T, K>(arg0, arg1);
  116. }
  117. public string ConflictingGenericMethod<K>(K arg0) {
  118. return arg0.ToString();
  119. }
  120. EOL
  121. csc <<-EOL
  122. public abstract partial class AbstractClassWithMethods {
  123. public abstract string PublicMethod();
  124. protected abstract string ProtectedMethod();
  125. }
  126. public partial class Klass{
  127. public static int StaticVoidMethod() {
  128. return 1;
  129. }
  130. private int _foo;
  131. public int Foo {
  132. get { return _foo; }
  133. }
  134. public Klass() {
  135. _foo = 10;
  136. }
  137. }
  138. public partial class SubKlass : Klass {}
  139. #pragma warning disable 693
  140. public partial class GenericClassWithMethods<K> {
  141. #{@methods_string}
  142. #{@conflicting_method_string}
  143. }
  144. public partial class GenericClass2Params<K, J> {
  145. #{@methods_string}
  146. #{@conflicting_method_string}
  147. }
  148. #pragma warning restore 693
  149. public partial class ClassWithIndexer {
  150. public int[,] Values = new int[,] { {0, 10}, {20, 30} };
  151. public int this[int i, int j] {
  152. get { return Values[i,j]; }
  153. set { Values[i,j] = value; }
  154. }
  155. }
  156. internal partial class PartialClassWithMethods {
  157. internal int Foo(){ return 1; }
  158. }
  159. public partial class ClassWithOverloads {
  160. public string Overloaded() { return "empty"; }
  161. public string Overloaded(int arg) { return "one arg"; }
  162. public string Overloaded(int arg1, int arg2) { return "two args"; }
  163. public string Tracker { get; set;}
  164. public string PublicProtectedOverload(){
  165. return "public overload";
  166. }
  167. protected string PublicProtectedOverload(string str) {
  168. return "protected overload";
  169. }
  170. #{method_block("void", "Void") {|el| "Tracker = \"#{el}\""}}
  171. #{method_block("string", "Ref") {|el| "return \"#{el}\""} }
  172. #{method_block("string[]", "RefArray") {|el| "return new string[]{\"#{el}\"}"} }
  173. #{method_block("int", "Val") {|el| "Tracker = \"#{el}\";return 1"} }
  174. #{method_block("int[]", "ValArray") {|el| "Tracker = \"#{el}\";return new int[]{1}" }}
  175. #{method_block("string", "Generic", "<T>") {|el| "return \"#{el}\" "}}
  176. }
  177. public class DerivedFromImplementsIInterface : ImplementsIInterface {}
  178. public struct StructImplementsIInterface : IInterface { public void m() {}}
  179. public partial class ClassWithMethods {
  180. public ClassWithMethods() {
  181. Tracker = new ArrayList();
  182. }
  183. public string PublicMethod() { return "public";}
  184. protected string ProtectedMethod() { return "protected";}
  185. private string PrivateMethod() { return "private";}
  186. public ArrayList Tracker { get; set;}
  187. private static ArrayList _staticTracker = new ArrayList();
  188. public static ArrayList StaticTracker { get { return _staticTracker;}}
  189. #{@methods_string}
  190. public void Reset() { Tracker.Clear(); }
  191. public static void StaticReset() { StaticTracker.Clear(); }
  192. public int SummingMethod(int a, int b){
  193. return a+b;
  194. }
  195. // no args
  196. public string NoArg() { return "NoArg";}
  197. //primitive types
  198. // 1. Ruby native
  199. public string Int32Arg(Int32 arg) { Tracker.Add(arg); return "Int32Arg";} // Fixnum
  200. public string DoubleArg(Double arg) { Tracker.Add(arg); return "DoubleArg";} // Float
  201. public string BigIntegerArg(BigInteger arg) { Tracker.Add(arg); return "BigIntegerArg";} // Bignum
  202. public string StringArg(String arg) { Tracker.Add(arg); return "StringArg";} // String
  203. public string BooleanArg(Boolean arg) { Tracker.Add(arg); return "BooleanArg";} // TrueClass/FalseClass/NilClass
  204. public string ObjectArg(object arg) { Tracker.Add(arg); return "ObjectArg";} // Object
  205. // 2. not Ruby native
  206. // 2.1 -- signed
  207. public string SByteArg(SByte arg) { Tracker.Add(arg); return "SByteArg";}
  208. public string Int16Arg(Int16 arg) { Tracker.Add(arg); return "Int16Arg";}
  209. public string Int64Arg(Int64 arg) { Tracker.Add(arg); return "Int64Arg";}
  210. public string SingleArg(Single arg) { Tracker.Add(arg); return "SingleArg";}
  211. // 2.2 -- unsigned
  212. public string ByteArg(Byte arg) { Tracker.Add(arg); return "ByteArg";}
  213. public string UInt16Arg(UInt16 arg) { Tracker.Add(arg); return "UInt16Arg";}
  214. public string UInt32Arg(UInt32 arg) { Tracker.Add(arg); return "UInt32Arg";}
  215. public string UInt64Arg(UInt64 arg) { Tracker.Add(arg); return "UInt64Arg";}
  216. // 2.3 -- special
  217. public string CharArg(Char arg) { Tracker.Add(arg); return "CharArg";}
  218. public string DecimalArg(Decimal arg) { Tracker.Add(arg); return "DecimalArg";}
  219. //
  220. // Reference type or value type
  221. //
  222. public string IInterfaceArg(IInterface arg) { Tracker.Add(arg); return "IInterfaceArg";}
  223. public string ImplementsIInterfaceArg(ImplementsIInterface arg) { Tracker.Add(arg); return "ImplementsIInterfaceArg";}
  224. public string DerivedFromImplementsIInterfaceArg(DerivedFromImplementsIInterface arg) { Tracker.Add(arg); return "DerivedFromImplementsIInterfaceArg";}
  225. public string CStructArg(CStruct arg) { Tracker.Add(arg); return "CStructArg";}
  226. public string StructImplementsIInterfaceArg(StructImplementsIInterface arg) { Tracker.Add(arg); return "StructImplementsIInterfaceArg";}
  227. public string AbstractClassArg(AbstractClass arg) { Tracker.Add(arg); return "AbstractClassArg";}
  228. public string DerivedFromAbstractArg(DerivedFromAbstract arg) { Tracker.Add(arg); return "DerivedFromAbstractArg";}
  229. public string CustomEnumArg(CustomEnum arg) { Tracker.Add(arg); return "CustomEnumArg";}
  230. public string EnumIntArg(EnumInt arg) { Tracker.Add(arg); return "EnumIntArg";}
  231. //
  232. // array
  233. //
  234. public string Int32ArrArg(Int32[] arg) { Tracker.Add(arg); return "Int32ArrArg";}
  235. public string ObjectArrArg(object[] arg) { Tracker.Add(arg); return "ObjectArrArg";}
  236. public string IInterfaceArrArg(IInterface[] arg) { Tracker.Add(arg); return "IInterfaceArrArg";}
  237. //
  238. // params array
  239. //
  240. public string ParamsInt32ArrArg(params Int32[] arg) { Tracker.Add(arg); return "ParamsInt32ArrArg";}
  241. public string ParamsIInterfaceArrArg(params IInterface[] arg) { Tracker.Add(arg); return "ParamsIInterfaceArrArg";}
  242. public string ParamsCStructArrArg(params CStruct[] arg) { Tracker.Add(arg); return "ParamsCStructArrArg";}
  243. public string Int32ArgParamsInt32ArrArg(Int32 arg, params Int32[] arg2) { Tracker.Add(arg); return "Int32ArgParamsInt32ArrArg";}
  244. public string IInterfaceArgParamsIInterfaceArrArg(IInterface arg, params IInterface[] arg2) { Tracker.Add(arg); return "IInterfaceArgParamsIInterfaceArrArg";}
  245. //
  246. // collections/generics
  247. //
  248. public string IListOfIntArg(IList<int> arg) { Tracker.Add(arg); return "IListOfIntArg";}
  249. public string IListOfObjArg(IList<object> arg) { Tracker.Add(arg); return "IListOfObjArg";}
  250. public string ArrayArg(Array arg) { Tracker.Add(arg); return "ArrayArg";}
  251. public string IEnumerableOfIntArg(IEnumerable<int> arg) { Tracker.Add(arg); return "IEnumerableOfIntArg";}
  252. public string IEnumeratorOfIntArg(IEnumerator<int> arg) { Tracker.Add(arg); return "IEnumeratorOfIntArg";}
  253. public string IEnumerableArg(IEnumerable arg) { Tracker.Add(arg); return "IEnumerableArg";}
  254. public string IEnumeratorArg(IEnumerator arg) { Tracker.Add(arg); return "IEnumeratorArg";}
  255. public string ArrayListArg(ArrayList arg) { Tracker.Add(arg); return "ArrayListArg";}
  256. public string IDictionaryOfObjectObjectArg(IDictionary<object, object> arg) { Tracker.Add(arg); return "IDictionaryOfObjectObjectArg";}
  257. public string IDictionaryOfIntStringArg(IDictionary<int, string> arg) { Tracker.Add(arg); return "IDictionaryOfIntStringArg";}
  258. public string DictionaryOfObjectObjectArg(Dictionary<object, object> arg) { Tracker.Add(arg); return "DictionaryOfObjectObjectArg";}
  259. public string DictionaryOfIntStringArg(Dictionary<int, string> arg) { Tracker.Add(arg); return "DictionaryOfIntStringArg";}
  260. // Nullable
  261. public string NullableInt32Arg(Int32? arg) { Tracker.Add(arg); return "NullableInt32Arg";}
  262. // ByRef, Out
  263. public string RefInt32Arg(ref Int32 arg) { arg = 1; Tracker.Add(arg); return "RefInt32Arg";}
  264. public string OutInt32Arg(out Int32 arg) { arg = 2; Tracker.Add(arg); return "OutInt32Arg";}
  265. // Default Value
  266. public string DefaultInt32Arg([DefaultParameterValue(10)] Int32 arg) { Tracker.Add(arg); return "DefaultInt32Arg";}
  267. public string Int32ArgDefaultInt32Arg(Int32 arg, [DefaultParameterValue(10)] Int32 arg2) { Tracker.Add(arg); Tracker.Add(arg2); return "Int32ArgDefaultInt32Arg";}
  268. // static
  269. public static string StaticMethodNoArg() { StaticTracker.Add(null); return "StaticMethodNoArg";}
  270. public static string StaticMethodClassWithMethodsArg(ClassWithMethods arg) {StaticTracker.Add(arg); return "StaticMethodClassWithMethodsArg";}
  271. public string ClassWithMethodsArg(ClassWithMethods arg) {Tracker.Add(arg); return "ClassWithMethodsArg";}
  272. // generic method
  273. public string GenericArg<T>(T arg) {Tracker.Add(arg); return String.Format("GenericArg[{0}]", typeof(T));}
  274. // out on non-byref
  275. public string OutNonByRefInt32Arg([Out] int arg) {arg = 1; Tracker.Add(arg); return "OutNonByRefInt32Arg";}
  276. // what does passing in nil mean?
  277. public string ParamsIInterfaceArrTestArg(params IInterface[] args) { Tracker.Add(args == null); Tracker.Add(args); return "ParamsIInterfaceArrTestArg";}
  278. // ref, out, ...
  279. public string RefOutInt32Args(ref int arg1, out int arg2, int arg3) {arg1=arg2=arg3; Tracker.Add(arg1); Tracker.Add(arg2); Tracker.Add(arg3); return "RefOutInt32Args";}
  280. public string RefInt32OutArgs(ref int arg1, int arg2, out int arg3) {arg3=arg1=arg2; Tracker.Add(arg1); Tracker.Add(arg2); Tracker.Add(arg3); return "RefInt32OutArgs";}
  281. public string Int32RefOutArgs(int arg1, ref int arg2, out int arg3) {arg2=arg3=arg1; Tracker.Add(arg1); Tracker.Add(arg2); Tracker.Add(arg3); return "Int32RefOutArgs";}
  282. // eight args
  283. public string EightArgs(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8) {
  284. Tracker.Add(arg1);
  285. Tracker.Add(arg2);
  286. Tracker.Add(arg3);
  287. Tracker.Add(arg4);
  288. Tracker.Add(arg5);
  289. Tracker.Add(arg6);
  290. Tracker.Add(arg7);
  291. Tracker.Add(arg8);
  292. return "EightArgs";
  293. }
  294. public string IDictionaryOfIntIntArg(IDictionary<int, int> arg){ Tracker.Add(arg); return "IDictionaryOfIntIntArg";}
  295. public string HashtableArg(Hashtable arg) { Tracker.Add(arg); return "HashtableArg";}
  296. public string ListOfIntArg(List<int> arg) { Tracker.Add(arg); return "ListOfIntArg";}
  297. // iterator support
  298. public string IEnumerableIteratingArg(IEnumerable arg) {
  299. IEnumerator ienum = arg.GetEnumerator();
  300. while (ienum.MoveNext())
  301. Tracker.Add(ienum.Current);
  302. return "IEnumerableIteratingArg";
  303. }
  304. public string IEnumeratorIteratingArg(IEnumerator arg) {
  305. while (arg.MoveNext())
  306. Tracker.Add(arg.Current);
  307. return "IEnumeratorIteratingArg";
  308. }
  309. public string IListArg(IList arg) { Tracker.Add(arg); Tracker.Add(arg.Count); return "IListArg";}
  310. public string IEnumerableOfCharIteratingArg(IEnumerable<Char> arg) {
  311. IEnumerator ienum = arg.GetEnumerator();
  312. while (ienum.MoveNext())
  313. Tracker.Add(ienum.Current);
  314. return "IEnumerableOfCharIteratingArg";
  315. }
  316. public string IEnumeratorOfCharIteratingArg(IEnumerator<Char> arg) {
  317. while (arg.MoveNext())
  318. Tracker.Add(arg.Current);
  319. return "IEnumeratorOfCharIteratingArg";
  320. }
  321. public string IListOfCharArg(IList<Char> arg) { Tracker.Add(arg); Tracker.Add(arg.Count); return "IListOfCharArg";}
  322. public string IEnumerableOfIntIteratingArg(IEnumerable<int> arg) {
  323. IEnumerator ienum = arg.GetEnumerator();
  324. while (ienum.MoveNext())
  325. Tracker.Add(ienum.Current);
  326. return "IEnumerableOfIntIteratingArg";
  327. }
  328. public string IEnumeratorOfIntIteratingArg(IEnumerator<int> arg) {
  329. while (arg.MoveNext())
  330. Tracker.Add(arg.Current);
  331. return "IEnumeratorOfIntIteratingArg";
  332. }
  333. public string IListOfIntArg2(IList<int> arg) { Tracker.Add(arg); Tracker.Add(arg.Count); return "IListOfIntArg2";}
  334. // delegate
  335. public string DelegateArg(Delegate arg) {
  336. IntIntDelegate d = (IntIntDelegate)arg;
  337. Tracker.Add(d(10));
  338. return "DelegateArg";
  339. }
  340. public string IntIntDelegateArg(IntIntDelegate arg) { Tracker.Add(arg(10)); return "IntIntDelegateArg";}
  341. // byte array
  342. public string RefByteArrArg(ref Byte[] arg) { Tracker.Add(arg); return "RefByteArrArg";}
  343. public string ByteArrRefByteArrArg(Byte[] input, ref Byte[] arg) { arg = input; Tracker.Add(arg); return "ByteArrRefByteArrArg";}
  344. // keywords
  345. public string KeywordsArgs(int arg1, object arg2, ref string arg3) { arg3 = arg3.ToUpper(); Tracker.Add(arg3); return "KeywordsArgs";}
  346. //more ref/out
  347. public string RefStructImplementsIInterfaceArg(ref StructImplementsIInterface arg) { arg = new StructImplementsIInterface(); Tracker.Add(arg); return "RefStructImplementsIInterfaceArg";}
  348. public string OutStructImplementsIInterfaceArg(out StructImplementsIInterface arg) { arg = new StructImplementsIInterface(); Tracker.Add(arg); return "OutStructImplementsIInterfaceArg";}
  349. public string RefImplementsIInterfaceArg(ref ImplementsIInterface arg) { Tracker.Add(arg); return "RefImplementsIInterfaceArg";}
  350. public string OutImplementsIInterfaceArg(out ImplementsIInterface arg) { arg = new ImplementsIInterface(); Tracker.Add(arg); return "OutImplementsIInterfaceArg";}
  351. public string RefBooleanArg(ref Boolean arg) { Tracker.Add(arg); return "RefBooleanArg";}
  352. public string OutBooleanArg(out Boolean arg) { arg = true; Tracker.Add(arg); return "OutBooleanArg";}
  353. public string RefInt32Int32OutInt32Arg(ref int arg1, int arg2, out int arg3) {
  354. arg3 = arg1 + arg2;
  355. arg1 = 100;
  356. Tracker.Add(arg1);
  357. Tracker.Add(arg2);
  358. Tracker.Add(arg3);
  359. return "RefInt32Int32OutInt32Arg";
  360. }
  361. }
  362. public partial class GenericClassWithMethods<K> {
  363. public ArrayList Tracker { get; set;}
  364. public GenericClassWithMethods() {
  365. Tracker = new ArrayList();
  366. }
  367. public void Reset() { Tracker.Clear();}
  368. public string GenericArg(K arg) { Tracker.Add(arg); return "GenericArg";}
  369. }
  370. public delegate int IntIntDelegate(int arg);
  371. public class VirtualMethodBaseClass {
  372. public virtual string VirtualMethod() { return "virtual"; }
  373. }
  374. public class VirtualMethodOverrideNew : VirtualMethodBaseClass {
  375. new public virtual string VirtualMethod() { return "new"; }
  376. }
  377. public class VirtualMethodOverrideOverride : VirtualMethodBaseClass {
  378. public override string VirtualMethod() { return "override"; }
  379. }
  380. public class ClassWithNullableMethods {
  381. public ClassWithNullableMethods() {
  382. Tracker = new ArrayList();
  383. }
  384. public ArrayList Tracker { get; set;}
  385. public void Reset() { Tracker.Clear(); }
  386. public Int16? Int16NullableProperty {get; set;}
  387. public Int32? Int32NullableProperty {get; set;}
  388. public Int64? Int64NullableProperty {get; set;}
  389. public UInt16? UInt16NullableProperty {get; set;}
  390. public UInt32? UInt32NullableProperty {get; set;}
  391. public UInt64? UInt64NullableProperty {get; set;}
  392. public Byte? ByteNullableProperty {get; set;}
  393. public SByte? SByteNullableProperty {get; set;}
  394. public Decimal? DecimalNullableProperty {get; set;}
  395. public Single? SingleNullableProperty {get; set;}
  396. public Double? DoubleNullableProperty {get; set;}
  397. public Char? CharNullableProperty {get; set;}
  398. public CustomEnum? CustomEnumNullableProperty {get; set;}
  399. public Boolean? BooleanNullableProperty {get; set;}
  400. public string Int16NullableArg(Int16? arg) { Tracker.Add(arg); return "Int16NullableArg"; }
  401. public string Int32NullableArg(Int32? arg) { Tracker.Add(arg); return "Int32NullableArg"; }
  402. public string Int64NullableArg(Int64? arg) { Tracker.Add(arg); return "Int64NullableArg"; }
  403. public string UInt16NullableArg(UInt16? arg) { Tracker.Add(arg); return "UInt16NullableArg"; }
  404. public string UInt32NullableArg(UInt32? arg) { Tracker.Add(arg); return "UInt32NullableArg"; }
  405. public string UInt64NullableArg(UInt64? arg) { Tracker.Add(arg); return "UInt64NullableArg"; }
  406. public string ByteNullableArg(Byte? arg) { Tracker.Add(arg); return "ByteNullableArg"; }
  407. public string SByteNullableArg(SByte? arg) { Tracker.Add(arg); return "SByteNullableArg"; }
  408. public string DecimalNullableArg(Decimal? arg) { Tracker.Add(arg); return "DecimalNullableArg"; }
  409. public string SingleNullableArg(Single? arg) { Tracker.Add(arg); return "SingleNullableArg"; }
  410. public string DoubleNullableArg(Double? arg) { Tracker.Add(arg); return "DoubleNullableArg"; }
  411. public string CharNullableArg(Char? arg) { Tracker.Add(arg); return "CharNullableArg"; }
  412. public string CustomEnumNullableArg(CustomEnum? arg) { Tracker.Add(arg); return "CustomEnumNullableArg"; }
  413. public string BooleanNullableArg(Boolean? arg) { Tracker.Add(arg); return "BooleanNullableArg"; }
  414. }
  415. public class StaticClassWithNullableMethods {
  416. private static ArrayList _tracker = new ArrayList();
  417. public static ArrayList Tracker { get { return _tracker;}}
  418. public static void Reset() {
  419. Tracker.Clear();
  420. StaticInt16NullableProperty = null;
  421. StaticInt32NullableProperty = null;
  422. StaticInt64NullableProperty = null;
  423. StaticUInt16NullableProperty = null;
  424. StaticUInt32NullableProperty = null;
  425. StaticUInt64NullableProperty = null;
  426. StaticByteNullableProperty = null;
  427. StaticSByteNullableProperty = null;
  428. StaticDecimalNullableProperty = null;
  429. StaticSingleNullableProperty = null;
  430. StaticDoubleNullableProperty = null;
  431. StaticCharNullableProperty = null;
  432. StaticCustomEnumNullableProperty = null;
  433. StaticBooleanNullableProperty = null;
  434. }
  435. public static Int16? StaticInt16NullableProperty {get; set;}
  436. public static Int32? StaticInt32NullableProperty {get; set;}
  437. public static Int64? StaticInt64NullableProperty {get; set;}
  438. public static UInt16? StaticUInt16NullableProperty {get; set;}
  439. public static UInt32? StaticUInt32NullableProperty {get; set;}
  440. public static UInt64? StaticUInt64NullableProperty {get; set;}
  441. public static Byte? StaticByteNullableProperty {get; set;}
  442. public static SByte? StaticSByteNullableProperty {get; set;}
  443. public static Decimal? StaticDecimalNullableProperty {get; set;}
  444. public static Single? StaticSingleNullableProperty {get; set;}
  445. public static Double? StaticDoubleNullableProperty {get; set;}
  446. public static Char? StaticCharNullableProperty {get; set;}
  447. public static CustomEnum? StaticCustomEnumNullableProperty {get; set;}
  448. public static Boolean? StaticBooleanNullableProperty {get; set;}
  449. public static string StaticInt16NullableArg(Int16? arg) { Tracker.Add(arg); return "StaticInt16NullableArg"; }
  450. public static string StaticInt32NullableArg(Int32? arg) { Tracker.Add(arg); return "StaticInt32NullableArg"; }
  451. public static string StaticInt64NullableArg(Int64? arg) { Tracker.Add(arg); return "StaticInt64NullableArg"; }
  452. public static string StaticUInt16NullableArg(UInt16? arg) { Tracker.Add(arg); return "StaticUInt16NullableArg"; }
  453. public static string StaticUInt32NullableArg(UInt32? arg) { Tracker.Add(arg); return "StaticUInt32NullableArg"; }
  454. public static string StaticUInt64NullableArg(UInt64? arg) { Tracker.Add(arg); return "StaticUInt64NullableArg"; }
  455. public static string StaticByteNullableArg(Byte? arg) { Tracker.Add(arg); return "StaticByteNullableArg"; }
  456. public static string StaticSByteNullableArg(SByte? arg) { Tracker.Add(arg); return "StaticSByteNullableArg"; }
  457. public static string StaticDecimalNullableArg(Decimal? arg) { Tracker.Add(arg); return "StaticDecimalNullableArg"; }
  458. public static string StaticSingleNullableArg(Single? arg) { Tracker.Add(arg); return "StaticSingleNullableArg"; }
  459. public static string StaticDoubleNullableArg(Double? arg) { Tracker.Add(arg); return "StaticDoubleNullableArg"; }
  460. public static string StaticCharNullableArg(Char? arg) { Tracker.Add(arg); return "StaticCharNullableArg"; }
  461. public static string StaticCustomEnumNullableArg(CustomEnum? arg) { Tracker.Add(arg); return "StaticCustomEnumNullableArg"; }
  462. public static string StaticBooleanNullableArg(Boolean? arg) { Tracker.Add(arg); return "StaticBooleanNullableArg"; }
  463. }
  464. public class GenericTypeInference {
  465. public static string Tx<T>(T x) {
  466. return typeof(T).ToString();
  467. }
  468. public static string TxTy<T>(T x, T y) {
  469. return typeof(T).ToString();
  470. }
  471. public static string TxTyTz<T>(T x, T y, T z) {
  472. return typeof(T).ToString();
  473. }
  474. public static string TParamsArrx<T>(params T[] x) {
  475. return typeof(T).ToString();
  476. }
  477. public static string TxTParamsArry<T>(T x, params T[] y) {
  478. return typeof(T).ToString();
  479. }
  480. public static string TRefx<T>(ref T x) {
  481. x = default(T);
  482. return typeof(T).ToString();
  483. }
  484. public static string TxClass<T>(T x)
  485. where T : class {
  486. return typeof(T).ToString();
  487. }
  488. public static string TxStruct<T>(T x)
  489. where T : struct {
  490. return typeof(T).ToString();
  491. }
  492. public static string TxIList<T>(T x)
  493. where T : IList {
  494. return typeof(T).ToString();
  495. }
  496. public static string TxUyTConstrainedToU<T, U>(T x, U y)
  497. where T : U {
  498. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  499. }
  500. public static string ObjxUyTConstrainedToU<T, U>(object x, U y)
  501. where T : U {
  502. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  503. }
  504. public static string TxObjyTConstrainedToU<T, U>(T x, object y)
  505. where T : U {
  506. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  507. }
  508. public static string TxUyVzTConstrainedToUConstrainedToV<T, U, V>(T x, U y, V z)
  509. where T : U
  510. where U : V {
  511. return String.Format("{0}, {1}, {2}", typeof(T).ToString(), typeof(U).ToString());
  512. }
  513. public static string TxUyVzTConstrainedToUConstrainedToClass<T, U, V>(T x, U y, V z)
  514. where T : U
  515. where U : class {
  516. return String.Format("{0}, {1}, {2}", typeof(T).ToString(), typeof(U).ToString());
  517. }
  518. public static string TxUyVzTConstrainedToUConstrainedToIList<T, U, V>(T x, U y, V z)
  519. where T : U
  520. where U : IList {
  521. return String.Format("{0}, {1}, {2}", typeof(T).ToString(), typeof(U).ToString());
  522. }
  523. public static string TObjectx<T>(object x){
  524. return typeof(T).ToString();
  525. }
  526. public static string TxObjecty<T, U>(T x, object y) {
  527. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  528. }
  529. public static string TxFuncTy<T>(T x, Func<T> y) {
  530. return typeof(T).ToString();
  531. }
  532. public static string TxActionTy<T>(T x, Action<T> y) {
  533. return typeof(T).ToString();
  534. }
  535. public static string TxIListTy<T>(T x, IList<T> y) {
  536. return typeof(T).ToString();
  537. }
  538. public static string TxDictionaryTIListTy<T>(T x, Dictionary<T, IList<T>> y){
  539. return typeof(T).ToString();
  540. }
  541. public static string TxIEnumerableTy<T>(T x, IEnumerable<T> y) {
  542. return typeof(T).ToString();
  543. }
  544. public static string TxTConstrainedToIEnumerable<T>(T x) {
  545. return typeof(T).ToString();
  546. }
  547. public static string TxUyTConstrainedToIListU<T, U>(T x, U y) {
  548. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  549. }
  550. public static string TxUy<T, U>(T x, U y) {
  551. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  552. }
  553. public static string IEnumerableTx<T>(IEnumerable<T> x) {
  554. return typeof(T).ToString();
  555. }
  556. public static string IEnumerableTxFuncTBooly<T>(IEnumerable<T> x, Func<T, bool> y) {
  557. return typeof(T).ToString();
  558. }
  559. public static string IEnumerableTxFuncTIntBooly<T>(IEnumerable<T> x, Func<T, bool> y) {
  560. return typeof(T).ToString();
  561. }
  562. public static string ListTx<T>(List<T> x) {
  563. return typeof(T).ToString();
  564. }
  565. public static string ListListTx<T>(List<List<T>> x) {
  566. return typeof(T).ToString();
  567. }
  568. public static string DictionaryTTx<T>(Dictionary<T,T> x) {
  569. return typeof(T).ToString();
  570. }
  571. public static string ListTxClass<T>(List<T> x)
  572. where T : class {
  573. return typeof(T).ToString();
  574. }
  575. public static string ListTxStruct<T>(List<T> x)
  576. where T : struct {
  577. return typeof(T).ToString();
  578. }
  579. public static string ListTxNew<T>(List<T> x)
  580. where T : new() {
  581. return typeof(T).ToString();
  582. }
  583. public static string DictionaryDictionaryTTDictionaryTTx<T>(Dictionary<Dictionary<T,T>, Dictionary<T,T>> x) {
  584. return typeof(T).ToString();
  585. }
  586. public static string FuncTBoolxStruct<T>(Func<T, bool> x)
  587. where T : struct {
  588. return typeof(T).ToString();
  589. }
  590. public static string FuncTBoolxIList<T>(Func<T, bool> x)
  591. where T : IList {
  592. return typeof(T).ToString();
  593. }
  594. public static string IListTxIListTy<T>(IList<T> x, IList<T> y) {
  595. return typeof(T).ToString();
  596. }
  597. }
  598. public class SelfEnumerable : IEnumerable<SelfEnumerable> {
  599. #region IEnumerable<Test> Members
  600. IEnumerator<SelfEnumerable> IEnumerable<SelfEnumerable>.GetEnumerator() {
  601. throw new NotImplementedException();
  602. }
  603. #endregion
  604. #region IEnumerable Members
  605. IEnumerator IEnumerable.GetEnumerator() {
  606. throw new NotImplementedException();
  607. }
  608. #endregion
  609. }
  610. public class GenericTypeInferenceInstance {
  611. public string Tx<T>(T x) {
  612. return typeof(T).ToString();
  613. }
  614. public string TxTy<T>(T x, T y) {
  615. return typeof(T).ToString();
  616. }
  617. public string TxTyTz<T>(T x, T y, T z) {
  618. return typeof(T).ToString();
  619. }
  620. public string TParamsArrx<T>(params T[] x) {
  621. return typeof(T).ToString();
  622. }
  623. public string TxTParamsArry<T>(T x, params T[] y) {
  624. return typeof(T).ToString();
  625. }
  626. public string TRefx<T>(ref T x) {
  627. x = default(T);
  628. return typeof(T).ToString();
  629. }
  630. public string TxClass<T>(T x)
  631. where T : class {
  632. return typeof(T).ToString();
  633. }
  634. public string TxStruct<T>(T x)
  635. where T : struct {
  636. return typeof(T).ToString();
  637. }
  638. public string TxIList<T>(T x)
  639. where T : IList {
  640. return typeof(T).ToString();
  641. }
  642. public string TxUyTConstrainedToU<T, U>(T x, U y)
  643. where T : U {
  644. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  645. }
  646. public string ObjxUyTConstrainedToU<T, U>(object x, U y)
  647. where T : U {
  648. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  649. }
  650. public string TxObjyTConstrainedToU<T, U>(T x, object y)
  651. where T : U {
  652. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  653. }
  654. public string TxUyVzTConstrainedToUConstrainedToV<T, U, V>(T x, U y, V z)
  655. where T : U
  656. where U : V {
  657. return String.Format("{0}, {1}, {2}", typeof(T).ToString(), typeof(U).ToString());
  658. }
  659. public string TxUyVzTConstrainedToUConstrainedToClass<T, U, V>(T x, U y, V z)
  660. where T : U
  661. where U : class {
  662. return String.Format("{0}, {1}, {2}", typeof(T).ToString(), typeof(U).ToString());
  663. }
  664. public string TxUyVzTConstrainedToUConstrainedToIList<T, U, V>(T x, U y, V z)
  665. where T : U
  666. where U : IList {
  667. return String.Format("{0}, {1}, {2}", typeof(T).ToString(), typeof(U).ToString());
  668. }
  669. public string TObjectx<T>(object x){
  670. return typeof(T).ToString();
  671. }
  672. public string TxObjecty<T, U>(T x, object y) {
  673. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  674. }
  675. public string TxFuncTy<T>(T x, Func<T> y) {
  676. return typeof(T).ToString();
  677. }
  678. public string TxActionTy<T>(T x, Action<T> y) {
  679. return typeof(T).ToString();
  680. }
  681. public string TxIListTy<T>(T x, IList<T> y) {
  682. return typeof(T).ToString();
  683. }
  684. public string TxDictionaryTIListTy<T>(T x, Dictionary<T, IList<T>> y){
  685. return typeof(T).ToString();
  686. }
  687. public string TxIEnumerableTy<T>(T x, IEnumerable<T> y) {
  688. return typeof(T).ToString();
  689. }
  690. public string TxTConstrainedToIEnumerable<T>(T x) {
  691. return typeof(T).ToString();
  692. }
  693. public string TxUyTConstrainedToIListU<T, U>(T x, U y) {
  694. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  695. }
  696. public string TxUy<T, U>(T x, U y) {
  697. return String.Format("{0}, {1}", typeof(T).ToString(), typeof(U).ToString());
  698. }
  699. public string IEnumerableTx<T>(IEnumerable<T> x) {
  700. return typeof(T).ToString();
  701. }
  702. public string IEnumerableTxFuncTBooly<T>(IEnumerable<T> x, Func<T, bool> y) {
  703. return typeof(T).ToString();
  704. }
  705. public string IEnumerableTxFuncTIntBooly<T>(IEnumerable<T> x, Func<T, bool> y) {
  706. return typeof(T).ToString();
  707. }
  708. public string ListTx<T>(List<T> x) {
  709. return typeof(T).ToString();
  710. }
  711. public string ListListTx<T>(List<List<T>> x) {
  712. return typeof(T).ToString();
  713. }
  714. public string DictionaryTTx<T>(Dictionary<T,T> x) {
  715. return typeof(T).ToString();
  716. }
  717. public string ListTxClass<T>(List<T> x)
  718. where T : class {
  719. return typeof(T).ToString();
  720. }
  721. public string ListTxStruct<T>(List<T> x)
  722. where T : struct {
  723. return typeof(T).ToString();
  724. }
  725. public string ListTxNew<T>(List<T> x)
  726. where T : new() {
  727. return typeof(T).ToString();
  728. }
  729. public string DictionaryDictionaryTTDictionaryTTx<T>(Dictionary<Dictionary<T,T>, Dictionary<T,T>> x) {
  730. return typeof(T).ToString();
  731. }
  732. public string FuncTBoolxStruct<T>(Func<T, bool> x)
  733. where T : struct {
  734. return typeof(T).ToString();
  735. }
  736. public string FuncTBoolxIList<T>(Func<T, bool> x)
  737. where T : IList {
  738. return typeof(T).ToString();
  739. }
  740. public string IListTxIListTy<T>(IList<T> x, IList<T> y) {
  741. return typeof(T).ToString();
  742. }
  743. }
  744. EOL
  745. no_csc do
  746. include System::Collections
  747. include System::Collections::Generic
  748. TE = TypeError
  749. AE = ArgumentError
  750. OE = System::OverflowException
  751. RE = RangeError
  752. SAF = System::Array[Fixnum]
  753. SAO = System::Array[Object]
  754. SAI = System::Array[IInterface]
  755. SAC = System::Array[CStruct]
  756. DObjObj = Dictionary[Object, Object]
  757. DIntStr = Dictionary[Fixnum, System::String]
  758. DIntInt = Dictionary[Fixnum, Fixnum]
  759. module BindingSpecs
  760. class ImplementsEnumerable
  761. include Enumerable
  762. def initialize
  763. @store = [1,2,3]
  764. end
  765. def reset
  766. @store = [1,2,3]
  767. end
  768. def each
  769. @store.each {|i| yield i}
  770. end
  771. end
  772. class MyString < String; end
  773. class RubyImplementsIInterface
  774. include IInterface
  775. def m; end
  776. end
  777. class RubyDerivedFromImplementsIInterface < ImplementsIInterface; end
  778. class RubyDerivedFromDerivedFromImplementsIInterface < DerivedFromImplementsIInterface; end
  779. class RubyDerivedFromDerivedFromAbstractAndImplementsIInterface < DerivedFromAbstract
  780. include IInterface
  781. def m; end
  782. end
  783. class RubyDerivedFromAbstract < AbstractClass; end
  784. class RubyDerivedFromDerivedFromAbstract < DerivedFromAbstract; end
  785. module Convert
  786. class ToI
  787. def to_i; 1; end
  788. end
  789. class ToInt
  790. def to_int; 1; end
  791. end
  792. class ToIntToI
  793. def to_i; 1; end
  794. def to_int; 2; end
  795. end
  796. class ToS
  797. def to_s; "to_s" end
  798. end
  799. class ToStr
  800. def to_str; "to_str" end
  801. end
  802. class ToStrToS
  803. def to_s; "to_s" end
  804. def to_str; "to_str" end
  805. end
  806. end
  807. class TestListOfInt
  808. include IEnumerable.of(Fixnum)
  809. def initialize
  810. @store = []
  811. end
  812. def get_enumerator
  813. TestListEnumeratorOfInt.new(@store)
  814. end
  815. def <<(val)
  816. @store << val
  817. self
  818. end
  819. class TestListEnumeratorOfInt
  820. include IEnumerator.of(Fixnum)
  821. attr_reader :list
  822. def initialize(list)
  823. @list = list
  824. @position = -1
  825. end
  826. def move_next
  827. @position += 1
  828. valid?
  829. end
  830. def reset
  831. @position = -1
  832. end
  833. def valid?
  834. @position != -1 && @position < @list.length
  835. end
  836. def current
  837. if valid?
  838. @list[@position]
  839. else
  840. raise System::InvalidOperationException.new
  841. end
  842. end
  843. end
  844. end
  845. class TestListOfChar
  846. include IEnumerable.of(System::Char)
  847. def initialize
  848. @store = []
  849. end
  850. def get_enumerator
  851. TestListEnumeratorOfChar.new(@store)
  852. end
  853. def <<(val)
  854. @store << val
  855. self
  856. end
  857. class TestListEnumeratorOfChar
  858. include IEnumerator.of(System::Char)
  859. attr_reader :list
  860. def initialize(list)
  861. @list = list
  862. @position = -1
  863. end
  864. def move_next
  865. @position += 1
  866. valid?
  867. end
  868. def reset
  869. @position = -1
  870. end
  871. def valid?
  872. @position != -1 && @position < @list.length
  873. end
  874. def current
  875. if valid?
  876. @list[@position]
  877. else
  878. raise System::InvalidOperationException.new
  879. end
  880. end
  881. end
  882. end
  883. end
  884. class Helper
  885. def self.binds(target, meth, input, result)
  886. lambda do
  887. target = eval target
  888. meth_call = (input == "NoArg" ? lambda { target.send(meth)} : lambda {target.send(meth, @values[input])})
  889. if result.class == Class && result < Exception
  890. meth_call.should raise_error result
  891. elsif result.class == Regexp
  892. res, ref = meth_call.call
  893. (res =~ result).should == 0
  894. else
  895. res, ref = meth_call.call
  896. res.should == result
  897. end
  898. end
  899. end
  900. def self.passes(target, meth, input, result)
  901. lambda do
  902. target = eval target
  903. value = @values[input]
  904. meth_call = (input == "NoArg" ? lambda { target.send(meth)} : lambda {target.send(meth, value)})
  905. res, ref = meth_call.call
  906. result = Helper.result(meth,value, input)
  907. target.tracker.should == [*result]
  908. if ref
  909. if result.is_a? ArrayList
  910. ref.should == result[0]
  911. else
  912. ref.should == result
  913. end
  914. end
  915. end
  916. end
  917. def self.run_matrix(results, input, keys)
  918. results[:OutInt32Arg] ||= TE
  919. keys.each do |meth|
  920. result = results[meth] || TE
  921. it "binds '#{meth}' for '#{input}' with '#{result.to_s}' (ClassWithMethods)", &(binds("@target", meth, input, result))
  922. it "binds '#{meth}' for '#{input}' with '#{result.to_s}' (RubyClassWithMethods)", &(binds("@target2", meth, input, result))
  923. next if result.class == Class && result < Exception
  924. it "passes the correct input (#{input}) into method (#{meth}) (ClassWithMethods)", &(passes("@target", meth, input, result))
  925. it "passes the correct input (#{input}) into method (#{meth}) (RubyClassWithMethods)", &(passes("@target2", meth, input, result))
  926. end
  927. end
  928. def self.property_binds(target, meth, input, result)
  929. lambda do
  930. target = eval target
  931. value = @values[input]
  932. method = lambda {target.send("#{meth}=", value)}
  933. if result.class == Class && result < Exception
  934. method.should raise_error result
  935. else
  936. target.send(meth).should == nil
  937. method.call
  938. target.send(meth).should == value
  939. end
  940. end
  941. end
  942. def self.run_property_matrix(results, input, keys, targets_one_and_two = true)
  943. keys.each do |meth|
  944. result = results[meth] || TE
  945. it "binds property '#{meth}' with '#{input}' on (ClassWithMethods)", &(property_binds("@target", meth, input, result))
  946. next unless targets_one_and_two
  947. it "binds property '#{meth}' with '#{input}' on (RubyClassWithMethods)", &(property_binds("@target2", meth, input, result))
  948. end
  949. end
  950. class ConversionHelper
  951. class << self
  952. def convert_for_BooleanArg(v)
  953. !!v
  954. end
  955. alias_method :convert_for_RefBooleanArg, :convert_for_BooleanArg
  956. def convert_for_SingleArg(v)
  957. case v
  958. when System::UInt32, System::Int32
  959. System::Single.parse(v.to_s)
  960. when System::Char
  961. System::Single.induced_from(int32(v))
  962. when Float
  963. System::Convert.to_single(v)
  964. end
  965. end
  966. def convert_for_DoubleArg(v)
  967. System::Double.induced_from(int32(v)) if v.is_a? System::Char
  968. end
  969. def convert_for_DecimalArg(v)
  970. System::Decimal.induced_from(int32(v)) if v.is_a? System::Char
  971. end
  972. def convert_for_CharArg(v)
  973. case v
  974. when System::String
  975. v[0]
  976. when Symbol
  977. v.to_s[0..0]
  978. when String
  979. v[0..0]
  980. end
  981. end
  982. def convert_for_IEnumerableArg(v)
  983. if test_value(v)
  984. v.to_int
  985. else
  986. [v]
  987. end
  988. end
  989. alias_method :convert_for_IListArg, :convert_for_IEnumerableArg
  990. alias_method :convert_for_IListOfObjArg, :convert_for_IEnumerableArg
  991. alias_method :convert_for_IListOfIntArg, :convert_for_IEnumerableArg
  992. alias_method :convert_for_IEnumerableOfIntArg, :convert_for_IEnumerableArg
  993. alias_method :convert_for_ArrayArg, :convert_for_IEnumerableArg
  994. alias_method :convert_for_ArrayListArg, :convert_for_IEnumerableArg
  995. alias_method :convert_for_Int32ArrArg, :convert_for_IEnumerableArg
  996. alias_method :convert_for_ParamsInt32ArrArg, :convert_for_IEnumerableArg
  997. alias_method :convert_for_ParamsCStructArrArg, :convert_for_IEnumerableArg
  998. alias_method :convert_for_HashtableArg, :convert_for_IEnumerableArg
  999. def convert_for_RefInt32Arg(v)
  1000. 1
  1001. end
  1002. def convert_for_Int32ArgDefaultInt32Arg(v)
  1003. [Fixnum.induced_from(v.to_int), 10]
  1004. end
  1005. def convert_for_Int32Arg(v)
  1006. if test_value(v)
  1007. v.to_int
  1008. end
  1009. end
  1010. alias_method :convert_for_BigIntegerArg, :convert_for_Int32Arg
  1011. alias_method :convert_for_DefaultInt32Arg, :convert_for_Int32Arg
  1012. alias_method :convert_for_Int32ArgParamsInt32ArrArg, :convert_for_Int32Arg
  1013. def convert_for_ParamsIInterfaceArrTestArg(v)
  1014. if v == nil
  1015. [true, [v]]
  1016. else
  1017. [false, [v]]
  1018. end
  1019. end
  1020. def convert_for_IListArg(v)
  1021. [v,v.size]
  1022. end
  1023. alias_method :convert_for_IListOfCharArg, :convert_for_IListArg
  1024. alias_method :convert_for_IListOfIntArg2, :convert_for_IListArg
  1025. def convert_for_EnumIntArg(v)
  1026. if v.is_a?(CustomEnum)
  1027. EnumInt.new
  1028. end
  1029. end
  1030. def convert_for_CustomEnumArg(v)
  1031. if v.is_a?(EnumInt)
  1032. CustomEnum.new
  1033. end
  1034. end
  1035. [System::Byte, System::SByte, System::Int16, System::UInt16, System::UInt32, System::Int64, System::UInt64].each do |val|
  1036. define_method("convert_for_#{val.name.gsub("System::","")}Arg") {|v| val.induced_from(v.to_int) if test_value(v)}
  1037. end
  1038. def convert_for_IEnumerableIteratingArg(v)
  1039. case v
  1040. when Hash
  1041. KeyValuePair.of(Object,Object).new(1,1)
  1042. when DObjObj
  1043. [KeyValuePair.of(Object,Object).new(1,1),KeyValuePair.of(Object,Object).new(2,2)]
  1044. when DIntInt
  1045. [KeyValuePair.of(Fixnum,Fixnum).new(1,1),KeyValuePair.of(Fixnum,Fixnum).new(2,2)]
  1046. when DIntStr
  1047. [KeyValuePair.of(Fixnum, System::String).new(1,"1".to_clr_string),KeyValuePair.of(Fixnum, System::String).new(2,"2".to_clr_string)]
  1048. when Hashtable
  1049. [DictionaryEntry.new(2,2), DictionaryEntry.new(1,1)]
  1050. when System::String
  1051. convert_for_IEnumerableOfCharIteratingArg(v)
  1052. end
  1053. end
  1054. def convert_for_ListOfIntArg(v)
  1055. if [List[Fixnum], System::Array[Fixnum], List[Object], Array,
  1056. System::Array[System::Char], ArrayList, List[System::Char],
  1057. System::Array[System::Byte], List[System::Byte], Hashtable,
  1058. DIntInt, DIntStr, DObjObj, Hash].any? {|e| v.is_a?(e)}
  1059. ArrayList.new << v
  1060. end
  1061. end
  1062. alias_method :convert_for_GenericArg, :convert_for_ListOfIntArg
  1063. alias_method :convert_for_IDictionaryOfIntIntArg, :convert_for_ListOfIntArg
  1064. def convert_for_RefByteArrArg(v)
  1065. if v.is_a? String
  1066. res = System::Array.of(System::Byte).new(v.size)
  1067. i = 0
  1068. v.each_byte {|e| res[i] = System::Byte.induced_from(e); i+=1}
  1069. ArrayList.new << res
  1070. elsif v.is_a? System::Array.of(System::Byte)
  1071. ArrayList.new << v
  1072. end
  1073. end
  1074. def convert_for_IEnumerableOfCharIteratingArg(v)
  1075. if v.is_a? System::String
  1076. v.to_s.split(//).map {|e| e.to_clr_string}
  1077. end
  1078. end
  1079. def convert_for_IEnumeratorIteratingArg(v)
  1080. if [TestList::TestListEnumerator, BindingSpecs::TestListOfInt::TestListEnumeratorOfInt, BindingSpecs::TestListOfChar::TestListEnumeratorOfChar].any? {|e| v.is_a? e}
  1081. v.list
  1082. end
  1083. end
  1084. def convert_for_IEnumeratorOfIntIteratingArg(v)
  1085. if [BindingSpecs::TestListOfInt::TestListEnumeratorOfInt].any? {|e| v.is_a? e}
  1086. v.list
  1087. end
  1088. end
  1089. def convert_for_IEnumeratorOfCharIteratingArg(v)
  1090. if [BindingSpecs::TestListOfChar::TestListEnumeratorOfChar].any? {|e| v.is_a? e}
  1091. v.list
  1092. end
  1093. end
  1094. def convert_for_DelegateArg(v)
  1095. case v
  1096. when IntIntDelegate
  1097. 11
  1098. when Proc
  1099. 12
  1100. when Method
  1101. 14
  1102. end
  1103. end
  1104. alias_method :convert_for_IntIntDelegateArg, :convert_for_DelegateArg
  1105. def convert_for_StringArg(arg)
  1106. case arg
  1107. when NilClass
  1108. nil
  1109. when Symbol
  1110. arg.to_s
  1111. when Fixnum
  1112. arg.to_sym.to_s
  1113. else
  1114. arg.to_str
  1115. end
  1116. end
  1117. def method_missing(meth, arg)
  1118. if meth =~ /convert_for_.*?Arg/
  1119. arg
  1120. end
  1121. end
  1122. private
  1123. def int32(v)
  1124. System::Convert.to_int32(v)
  1125. end
  1126. def test_value(value)
  1127. value.is_a?(Symbol) || value.is_a?(BindingSpecs::Convert::ToInt) || value.is_a?(BindingSpecs::Convert::ToIntToI)
  1128. end
  1129. end
  1130. end
  1131. def self.result(meth, value, input)
  1132. result = if input == "NoArg"
  1133. case meth.to_s
  1134. when /Params(?:Int32|CStruct|IInterface)ArrArg/
  1135. [[]]
  1136. when /DefaultInt32Arg/
  1137. [10]
  1138. when /NoArg/
  1139. []
  1140. when /OutInt32Arg/
  1141. [2]
  1142. when /ParamsIInterfaceArrTestArg/
  1143. [false, []]
  1144. when /OutBoolean/
  1145. true
  1146. when /OutImplementsIInterface/
  1147. ImplementsIInterface.new
  1148. when /OutStructImplementsIInterface/
  1149. StructImplementsIInterface.new
  1150. when /OutNonByRefInt32Arg/
  1151. 1
  1152. else
  1153. nil
  1154. end
  1155. else
  1156. ConversionHelper.send("convert_for_#{meth}", value)
  1157. end
  1158. result.nil? ? result = value : nil
  1159. result
  1160. end
  1161. def self.args
  1162. # TODO: More BigIntegerValues near boundaries
  1163. # TODO: More NullableIntegerValues near boundaries
  1164. obj = Object.new
  1165. class << obj
  1166. include Enumerable
  1167. def each
  1168. [1,2,3].each {|i| yield i}
  1169. end
  1170. def m;1;end
  1171. end
  1172. dobj = DObjObj.new
  1173. dobj[1] = 1
  1174. dobj[2] = 2
  1175. dint = DIntStr.new
  1176. dint[1] = "1".to_clr_string
  1177. dint[2] = "2".to_clr_string
  1178. dii = DIntInt.new
  1179. dii[1] = 1
  1180. dii[2] = 2
  1181. ht = Hashtable.new
  1182. ht[1] = 1
  1183. ht[2] = 2
  1184. tl1 = TestList.new << 1 << 2 << 3
  1185. tl2 = BindingSpecs::TestListOfInt.new << 1 << 2 << 3
  1186. tl3 = BindingSpecs::TestListOfChar.new << System::Char.new("a") << System::Char.new("b") << System::Char.new("c")
  1187. tl4 = TestList.new
  1188. clr_values = {}
  1189. # Fixnum Float
  1190. clr_types = [System::Int32, System::Double, System::SByte, System::Int16, System::Int64, System::Single, System::Decimal]
  1191. unsigned_clr_types = [System::Byte, System::UInt16, System::UInt32, System::UInt64]
  1192. (clr_types + unsigned_clr_types).each do |e|
  1193. clr_values["#{e.name}MaxValue"] = e.MaxValue
  1194. clr_values["#{e.name}MaxValueMinusOne"] = e.induced_from((e.MaxValue - 1))
  1195. clr_values["#{e.name}MinValue"] = e.MinValue
  1196. clr_values["#{e.name}MinValuePlusOne"] = e.induced_from((e.MinValue + 1))
  1197. end
  1198. other = { "nil" => nil, "true" => true, "false" => false, "obj" => Object.new,
  1199. "BigIntegerOne" => Bignum.One, "BigIntegerZero" => Bignum.Zero,
  1200. "Int32?Null" => System::Nullable[Fixnum].new, "Int32?One" => System::Nullable[Fixnum].new(1), "Int32?MinusOne" => System::Nullable[Fixnum].new(-1),
  1201. "" => "", "a" => "a", "abc" => "abc",
  1202. "System::String''" => System::String.new(""), "System::String'a'" => System::String.new("a"), "System::String'abc'" => System::String.new("abc"),
  1203. "MyString''" => BindingSpecs::MyString.new(""), "MyString'a'" => BindingSpecs::MyString.new("a"), "MyString'abc'" => BindingSpecs::MyString.new("abc"),
  1204. :a => :a, :abc => :abc,
  1205. "Convert::ToI" => BindingSpecs::Convert::ToI.new, "Convert::ToInt" => BindingSpecs::Convert::ToInt.new, "Convert::ToIntToI" => BindingSpecs::Convert::ToIntToI.new,
  1206. "Convert::ToS" => BindingSpecs::Convert::ToS.new, "Convert::ToStr" => BindingSpecs::Convert::ToStr.new, "Convert::ToStrToS" => BindingSpecs::Convert::ToStrToS.new,
  1207. "System::CharMaxValue" => System::Char.MaxValue, "System::CharMinValue" => System::Char.MinValue
  1208. }
  1209. other.merge! clr_values
  1210. types = [BindingSpecs::RubyImplementsIInterface, ImplementsIInterface, BindingSpecs::RubyDerivedFromImplementsIInterface, DerivedFromImplementsIInterface,
  1211. BindingSpecs::RubyDerivedFromDerivedFromImplementsIInterface, BindingSpecs::RubyDerivedFromDerivedFromAbstractAndImplementsIInterface, DerivedFromAbstract,
  1212. BindingSpecs::RubyDerivedFromAbstract,BindingSpecs::RubyDerivedFromDerivedFromAbstract,
  1213. Class, Object, CStruct, StructImplementsIInterface, EnumInt, CustomEnum].inject({"anonymous class" => Class.new, "anonymous classInstance" => Class.new.new, "metaclass" => Object.new.metaclass, "AbstractClass" => AbstractClass}) do |result, klass|
  1214. result[klass.name] = klass
  1215. begin
  1216. result["#{klass.name}Instance"] = klass.new
  1217. rescue Exception => e
  1218. puts "Exception raised during instantiation of #{klass.name}"
  1219. puts e
  1220. end
  1221. result
  1222. end
  1223. types.merge! other
  1224. types.merge!({"ClassWithMethods" => ClassWithMethods.new, "int" => 1, "hash" => {1=> 1},
  1225. "Dictionary[obj,obj]" => dobj, "Dictionary[int,str]" => dint, "Dictionary[int,int]" => dii,
  1226. "hashtable" => ht, "List[int]" => ( List[Fixnum].new << 1 << 2 << 3 ),
  1227. "List[obj]" => ( List[Object].new << 1 << 2 << 3), "Array[int]" => System::Array.of(Fixnum).new(2,3),
  1228. "String" => "String", "Array" => [Object.new, 1, :blue], "Array[Char]" => System::Array.of(System::Char).new(2, System::Char.new("a")),
  1229. "System::String" => "System::String".to_clr_string, "Array[System::String]" => System::Array.of(System::String).new(2, "a".to_clr_string),
  1230. "ArrayList" => (ArrayList.new << 1 << 2 << 3), "List[Char]" => ( List[System::Char].new << System::Char.new("a") << System::Char.new("b") << System::Char.new("c")),
  1231. "IEnumerator" => tl1.get_enumerator, "IEnumerator[int]" => tl2.get_enumerator, "IEnumerator[Char]" => tl3.get_enumerator,
  1232. "IntIntDelegate" => IntIntDelegate.new {|a| a+1 }, "lambda" => lambda {|a| a+2},
  1233. "proc" => proc {|a| a+2}, "method" => method(:test_method),
  1234. "unboundmethod" => method(:test_method).unbind, "bool" => true, "Array[byte]" => System::Array.of(System::Byte).new(2, System::Byte.MinValue),
  1235. "List[byte]" => (List[System::Byte].new << System::Byte.parse("1") << System::Byte.parse("2") << System::Byte.parse("3")),
  1236. "self" => "self", "class" => "class", "this" => "this", "public" => "public",
  1237. "RubyImplementsIInterfaceInstance" => BindingSpecs::RubyImplementsIInterface.new,
  1238. "Int16?Value" => System::Nullable[System::Int16].new(1),
  1239. "Int32?Value" => System::Nullable[System::Int32].new(1),
  1240. "Int64?Value" => System::Nullable[System::Int64].new(1),
  1241. "UInt16?Value" => System::Nullable[System::UInt16].new(1),
  1242. "UInt32?Value" => System::Nullable[System::UInt32].new(1),
  1243. "UInt64?Value" => System::Nullable[System::UInt64].new(1),
  1244. "Byte?Value" => System::Nullable[System::Byte].new(1),
  1245. "SByte?Value" => System::Nullable[System::SByte].new(1),
  1246. "Decimal?Value" => System::Nullable[System::Decimal].new(1),
  1247. "Single?Value" => System::Nullable[System::Single].new(1),
  1248. "Char?Value" => System::Nullable[System::Char].new("a"),
  1249. "Double?Value" => System::Nullable[System::Double].new(1),
  1250. "Boolean?Value" => System::Nullable[System::Boolean].new(1),
  1251. "CustomEnum?Value" => System::Nullable[CustomEnum].new(CustomEnum.A),
  1252. "obj" => Object.new,
  1253. "monkeypatched" => obj,
  1254. "ArrayInstanceEmpty" => [], "ArrayInstance" => [1,2,3],
  1255. "HashInstanceEmpty" => {}, "HashInstance" => {1=>2,3=>4,5=>6},
  1256. "ImplementsEnumerableInstance" => BindingSpecs::ImplementsEnumerable.new,
  1257. "StringInstanceEmpty" => "", "StringInstance" => "abc",
  1258. #[] [2,2]
  1259. "System::Array[Fixnum]InstanceEmpty" => SAF.new(0), "System::Array[Fixnum]Instance" => SAF.new(2,2),
  1260. #[] [Object.new,Object.new]
  1261. "System::Array[Object]InstanceEmpty" => SAO.new(0), "System::Array[Object]Instance" => SAO.new(2,Object.new),
  1262. "System::Array[IInterface]InstanceEmpty" => SAI.new(0), "System::Array[IInterface]Instance" => SAI.new(2, BindingSpecs::RubyImplementsIInterface.new),
  1263. "System::Array[CStruct]InstanceEmpty" => SAC.new(0), "System::Array[CStruct]Instance" => SAC.new(2, CStruct.new),
  1264. "ArrayListInstanceEmpty" => ArrayList.new, "ArrayListInstance" => (ArrayList.new << 1 << 2 << 3),
  1265. #{} {1=>1,2=>2}
  1266. "Dictionary[Object,Object]InstanceEmpty" => DObjObj.new, "Dictionary[Object,Object]Instance" => dobj,
  1267. #{} {1=>"1",2=>"2"}
  1268. "Dictionary[Fixnum,String]InstanceEmpty" => DIntStr.new, "Dictionary[Fixnum,String]Instance" => dint,
  1269. "TestListInstanceEmpty" => tl4, "TestListInstance" => tl1,
  1270. "TestListEnumeratorInstanceEmpty" => tl4.get_enumerator, "TestListEnumeratorInstance" => tl1.get_enumerator,
  1271. "CStructInstance" => CStruct.new,
  1272. "Int32Instance" => 1,
  1273. "IInterfaceInstance" => BindingSpecs::RubyImplementsIInterface.new,
  1274. "System::Collections::Generic::List[Fixnum]InstanceEmpty" => List[Fixnum].new, "System::Collections::Generic::List[Fixnum]Instance" => ( List[Fixnum].new << 1 << 2 << 3 ),
  1275. "System::Collections::Generic::List[Object]InstanceEmpty" => List[Object].new, "System::Collections::Generic::List[Object]Instance" => ( List[Object].new << Object.new << Object.new << Object.new ),
  1276. })
  1277. #TODO: Add the byref types when make_by_ref_type works
  1278. #byrefStructImplementsIInterfaceInstance
  1279. #byrefRubyImplementsIInterface
  1280. #byrefImpelemntsIInterface, byrefbool
  1281. #ref_types = {"ByRefInt" => Fixnum.get_type.make_by_ref_type, "Array[ByRefByte]" => System::Array.of(System::Byte.get_type.make_by_ref_type),
  1282. #"List[ByRefByte]" => List[System::Byte.get_type.make_by_ref_type]}
  1283. types
  1284. end
  1285. def self.test_method(a)
  1286. a+4
  1287. end
  1288. private
  1289. #returns random number between the given values. Using 0 for min value will
  1290. #give an abnormlly high probability of 0 as the result.
  1291. def self.rand_range(klass)
  1292. klass.induced_from(rand * (rand> 0.5 ? klass.MinValue : klass.MaxValue))
  1293. end
  1294. end
  1295. class RubyClassWithMethods < ClassWithMethods; end
  1296. class RubyClassWithNullableMethods < ClassWithNullableMethods; end
  1297. class RubyStaticClassWithNullableMethods < StaticClassWithNullableMethods; end
  1298. class RubyGenericTypeInference < GenericTypeInference
  1299. def self.call_long_method(meth)
  1300. method(meth).of(String).call("blah")
  1301. end
  1302. def self.call_short_method(meth)
  1303. send(meth, "blah")
  1304. end
  1305. end
  1306. class RubyGenericTypeInferenceInstance < GenericTypeInferenceInstance
  1307. def call_long_method(meth)
  1308. method(meth).of(String).call("blah")
  1309. end
  1310. def call_short_method(meth)
  1311. send(meth, "blah")
  1312. end
  1313. end
  1314. end