/Languages/IronPython/IronPythonTest/ClrType.cs

https://github.com/kumaryu/IronLanguages-main · C# · 338 lines · 236 code · 85 blank · 17 comment · 0 complexity · ed6e7109c5098d468bfd0641856017c9 MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using IronPython.Runtime.Types;
  17. using Microsoft.Scripting;
  18. using IronPython.Runtime.Exceptions;
  19. using IronPython.Runtime;
  20. namespace IronPythonTest.interop.net.type.clrtype {
  21. ///--Helpers--
  22. public class Factory {
  23. public static T Get<T>() where T : new() {
  24. return new T();
  25. }
  26. }
  27. ///--Positive Scenarios--
  28. #region sanity derived
  29. public class SanityDerived : PythonExceptions.BaseException {
  30. public SanityDerived(PythonType pt) : base(pt) { }
  31. }
  32. #endregion
  33. #region sanity
  34. public class Sanity : IPythonObject {
  35. private PythonType _pythonType;
  36. private PythonDictionary _dict;
  37. public Sanity(PythonType param) {
  38. _pythonType = param;
  39. }
  40. #region IPythonObject Members
  41. PythonDictionary IPythonObject.Dict {
  42. get { return _dict; }
  43. }
  44. PythonDictionary IPythonObject.SetDict(PythonDictionary dict) {
  45. System.Threading.Interlocked.CompareExchange<PythonDictionary>(ref _dict, dict, null);
  46. return _dict;
  47. }
  48. bool IPythonObject.ReplaceDict(PythonDictionary dict) {
  49. return false;
  50. }
  51. PythonType IPythonObject.PythonType {
  52. get { return _pythonType; }
  53. }
  54. void IPythonObject.SetPythonType(PythonType newType) {
  55. _pythonType = newType;
  56. }
  57. object[] IPythonObject.GetSlots() { return null; }
  58. object[] IPythonObject.GetSlotsCreate() { return null; }
  59. #endregion
  60. }
  61. #endregion
  62. #region sanity non-standard constructor
  63. public class SanityUniqueConstructor : IPythonObject {
  64. private PythonType _pythonType;
  65. private PythonDictionary _dict;
  66. public SanityUniqueConstructor(PythonType param, Int32 param2) { }
  67. #region IPythonObject Members
  68. PythonDictionary IPythonObject.Dict {
  69. get { return _dict; }
  70. }
  71. PythonDictionary IPythonObject.SetDict(PythonDictionary dict) {
  72. System.Threading.Interlocked.CompareExchange<PythonDictionary>(ref _dict, dict, null);
  73. return _dict;
  74. }
  75. bool IPythonObject.ReplaceDict(PythonDictionary dict) {
  76. return false;
  77. }
  78. PythonType IPythonObject.PythonType {
  79. get { return _pythonType; }
  80. }
  81. void IPythonObject.SetPythonType(PythonType newType) {
  82. _pythonType = newType;
  83. }
  84. object[] IPythonObject.GetSlots() { return null; }
  85. object[] IPythonObject.GetSlotsCreate() { return null; }
  86. #endregion
  87. }
  88. #endregion
  89. #region sanity constructor overloads
  90. public class SanityConstructorOverloads : IPythonObject {
  91. private PythonType _pythonType;
  92. private PythonDictionary _dict;
  93. public Object MyField;
  94. public SanityConstructorOverloads(PythonType param) {
  95. _pythonType = param;
  96. param.Name = "first";
  97. }
  98. public SanityConstructorOverloads(PythonType param, Int32 param2) {
  99. _pythonType = param;
  100. param.Name = "second";
  101. }
  102. #region IPythonObject Members
  103. PythonDictionary IPythonObject.Dict {
  104. get { return _dict; }
  105. }
  106. PythonDictionary IPythonObject.SetDict(PythonDictionary dict) {
  107. System.Threading.Interlocked.CompareExchange<PythonDictionary>(ref _dict, dict, null);
  108. return _dict;
  109. }
  110. bool IPythonObject.ReplaceDict(PythonDictionary dict) {
  111. return false;
  112. }
  113. PythonType IPythonObject.PythonType {
  114. get { return _pythonType; }
  115. }
  116. void IPythonObject.SetPythonType(PythonType newType) {
  117. _pythonType = newType;
  118. }
  119. object[] IPythonObject.GetSlots() { return null; }
  120. object[] IPythonObject.GetSlotsCreate() { return null; }
  121. #endregion
  122. }
  123. #endregion
  124. #region sanity generic
  125. public class SanityGeneric<T> : IPythonObject {
  126. private PythonType _pythonType;
  127. private PythonDictionary _dict;
  128. public SanityGeneric(PythonType param) {
  129. _pythonType = param;
  130. }
  131. #region IPythonObject Members
  132. PythonDictionary IPythonObject.Dict {
  133. get { return _dict; }
  134. }
  135. PythonDictionary IPythonObject.SetDict(PythonDictionary dict) {
  136. System.Threading.Interlocked.CompareExchange<PythonDictionary>(ref _dict, dict, null);
  137. return _dict;
  138. }
  139. bool IPythonObject.ReplaceDict(PythonDictionary dict) {
  140. return false;
  141. }
  142. PythonType IPythonObject.PythonType {
  143. get { return _pythonType; }
  144. }
  145. void IPythonObject.SetPythonType(PythonType newType) {
  146. _pythonType = newType;
  147. }
  148. object[] IPythonObject.GetSlots() { return null; }
  149. object[] IPythonObject.GetSlotsCreate() { return null; }
  150. #endregion
  151. }
  152. #endregion
  153. #region sanity generic constructor
  154. public class SanityGenericConstructor<T> : IPythonObject {
  155. private PythonType _pythonType;
  156. private PythonDictionary _dict;
  157. public SanityGenericConstructor(T param) {
  158. _pythonType = (PythonType)(Object)param;
  159. }
  160. #region IPythonObject Members
  161. PythonDictionary IPythonObject.Dict {
  162. get { return _dict; }
  163. }
  164. PythonDictionary IPythonObject.SetDict(PythonDictionary dict) {
  165. System.Threading.Interlocked.CompareExchange<PythonDictionary>(ref _dict, dict, null);
  166. return _dict;
  167. }
  168. bool IPythonObject.ReplaceDict(PythonDictionary dict) {
  169. return false;
  170. }
  171. PythonType IPythonObject.PythonType {
  172. get { return _pythonType; }
  173. }
  174. void IPythonObject.SetPythonType(PythonType newType) {
  175. _pythonType = newType;
  176. }
  177. object[] IPythonObject.GetSlots() { return null; }
  178. object[] IPythonObject.GetSlotsCreate() { return null; }
  179. #endregion
  180. }
  181. #endregion
  182. #region sanity constructor but no IPythonObject
  183. public class SanityNoIPythonObject {
  184. public SanityNoIPythonObject(PythonType pt) {
  185. }
  186. }
  187. #endregion
  188. #region sanity with parameterless constructor
  189. public class SanityParameterlessConstructor : IPythonObject {
  190. private static PythonType _PythonType;
  191. public static int WhichConstructor;
  192. private PythonDictionary _dict;
  193. public SanityParameterlessConstructor(PythonType param) {
  194. _PythonType = param;
  195. WhichConstructor = 1;
  196. }
  197. public SanityParameterlessConstructor() {
  198. WhichConstructor = 2;
  199. }
  200. #region IPythonObject Members
  201. PythonDictionary IPythonObject.Dict {
  202. get { return _dict; }
  203. }
  204. PythonDictionary IPythonObject.SetDict(PythonDictionary dict) {
  205. System.Threading.Interlocked.CompareExchange<PythonDictionary>(ref _dict, dict, null);
  206. return _dict;
  207. }
  208. bool IPythonObject.ReplaceDict(PythonDictionary dict) {
  209. return false;
  210. }
  211. PythonType IPythonObject.PythonType {
  212. get { return _PythonType; }
  213. }
  214. void IPythonObject.SetPythonType(PythonType newType) {
  215. _PythonType = newType;
  216. }
  217. object[] IPythonObject.GetSlots() { return null; }
  218. object[] IPythonObject.GetSlotsCreate() { return null; }
  219. #endregion
  220. }
  221. #endregion
  222. ///--Negative Scenarios--
  223. #region negative empty
  224. public class NegativeEmpty { }
  225. #endregion
  226. #region negative no constructor, but implements IPythonOjbect
  227. public class NegativeNoConstructor : IPythonObject {
  228. #region IPythonObject Members
  229. public PythonDictionary Dict {
  230. get { throw new NotImplementedException(); }
  231. }
  232. public PythonDictionary SetDict(PythonDictionary dict) {
  233. throw new NotImplementedException();
  234. }
  235. public bool ReplaceDict(PythonDictionary dict) {
  236. throw new NotImplementedException();
  237. }
  238. public PythonType PythonType {
  239. get { return TypeCache.Int32; }
  240. }
  241. public void SetPythonType(PythonType newType) {
  242. throw new NotImplementedException();
  243. }
  244. public object[] GetSlots() {
  245. throw new NotImplementedException();
  246. }
  247. public object[] GetSlotsCreate() {
  248. throw new NotImplementedException();
  249. }
  250. #endregion
  251. }
  252. #endregion
  253. }