TestPython.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package unit;
  2. import python.KwArgs;
  3. import python.Syntax;
  4. import python.VarArgs;
  5. import sys.io.File;
  6. import sys.io.Process;
  7. // check compilation python classes
  8. import python.NativeArrayTools;
  9. import python.NativeStringTools;
  10. import python.lib.Codecs;
  11. import python.lib.FuncTools;
  12. import python.lib.Glob;
  13. import python.lib.Inspect;
  14. import python.lib.Json;
  15. import python.lib.Math;
  16. import python.lib.Msvcrt;
  17. import python.lib.Os;
  18. import python.lib.PPrint;
  19. import python.lib.Random;
  20. import python.lib.Re;
  21. import python.lib.Set;
  22. import python.lib.ShUtil;
  23. import python.lib.Subprocess;
  24. import python.lib.Sys;
  25. import python.lib.Tempfile;
  26. import python.lib.Termios;
  27. import python.lib.ThreadLowLevel;
  28. import python.lib.Time;
  29. import python.lib.Traceback;
  30. import python.lib.Tty;
  31. import python.lib.Tuple;
  32. import python.lib.datetime.DateTime;
  33. import python.lib.datetime.TimeDelta;
  34. import python.lib.datetime.Timezone;
  35. import python.lib.datetime.TzInfo;
  36. import python.lib.io.BufferedIOBase;
  37. import python.lib.io.BufferedRWPair;
  38. import python.lib.io.BufferedRandom;
  39. import python.lib.io.BufferedReader;
  40. import python.lib.io.BufferedWriter;
  41. import python.lib.io.BytesIO;
  42. import python.lib.io.FileIO;
  43. import python.lib.io.IOBase;
  44. import python.lib.io.RawIOBase;
  45. import python.lib.io.StringIO;
  46. import python.lib.io.TextIOBase;
  47. import python.lib.net.Address;
  48. import python.lib.net.Socket;
  49. import python.lib.subprocess.Popen;
  50. import python.lib.threading.Thread;
  51. import python.lib.xml.etree.ElementTree;
  52. import python.lib.json.JSONEncoder;
  53. private typedef T = {
  54. var value:Int;
  55. @:optional var maybeValue:Int;
  56. }
  57. private enum MyEnum {
  58. A(?x:Int, b:String);
  59. True;
  60. False;
  61. }
  62. private interface IA {}
  63. private class A implements IA { }
  64. private class B extends A {
  65. public function new() {}
  66. }
  67. class TestPython extends Test {
  68. public function testDoWhileAsExpression () {
  69. var x = 1;
  70. var z = function () return (do {
  71. x++;
  72. } while (x < 3));
  73. z();
  74. eq(3, x);
  75. }
  76. public function testKeywords () {
  77. var list = new Array();
  78. }
  79. public function testStringMethod() {
  80. var d:Dynamic = "foo";
  81. eq("FOO", d.toUpperCase());
  82. var o:{function toUpperCase():String;} = "foo";
  83. eq("FOO", o.toUpperCase());
  84. var d:Dynamic = "FOO";
  85. eq("foo", d.toLowerCase());
  86. var o:{function toLowerCase():String;} = "FOO";
  87. eq("foo", o.toLowerCase());
  88. }
  89. public function testOptionalStructureFields() {
  90. var v:T = haxe.Json.parse('{"value": 1 }');
  91. eq(1, v.value);
  92. eq(null, v.maybeValue);
  93. v.maybeValue = 12;
  94. eq(12, v.maybeValue);
  95. v.maybeValue += 9;
  96. eq(21, v.maybeValue);
  97. var v:T = haxe.Json.parse('{"value": 1 }');
  98. var d:Dynamic = v;
  99. eq(1, d.value);
  100. eq(null, d.maybeValue);
  101. d.maybeValue = 12;
  102. eq(12, d.maybeValue);
  103. d.maybeValue += 9;
  104. eq(21, d.maybeValue);
  105. }
  106. function testNonOptionalArgumentAfterOptionalArgument() {
  107. function skip(a:Int = 1, b:String) {
  108. return Std.string(a) + b;
  109. }
  110. eq("12a", skip(12, "a"));
  111. eq("1a", skip("a"));
  112. }
  113. function testNativeClosures() {
  114. var s = "foo";
  115. var f = s.toUpperCase;
  116. eq("FOO", f());
  117. var a = [];
  118. var f = a.push;
  119. f(12);
  120. eq(12, a[0]);
  121. }
  122. function testOptionalEnumArguments() {
  123. var a1 = 1;
  124. var a2 = null;
  125. switch(A("foo")) {
  126. case A(i, b):
  127. a1 = i;
  128. a2 = b;
  129. case _:
  130. }
  131. eq(null, a1);
  132. eq("foo", a2);
  133. }
  134. function testTypedCatch() {
  135. function throwMe(arg:Dynamic) {
  136. return try {
  137. throw arg;
  138. } catch(e:haxe.macro.Expr.ExprDef) {
  139. 'ExprDef:$e';
  140. } catch(s:String) {
  141. 'String:$s';
  142. } catch(e:Dynamic) {
  143. 'Other:$e';
  144. }
  145. }
  146. eq("ExprDef:EBreak", throwMe(haxe.macro.Expr.ExprDef.EBreak));
  147. eq("String:foo", throwMe("foo"));
  148. eq("Other:12", throwMe(12));
  149. }
  150. /*
  151. function testSys () {
  152. var p = new Process("/bin/ls", ["-l"]);
  153. trace(p.stdout.readLine());
  154. trace(p.stdout.readLine());
  155. }
  156. */
  157. function testUnderscoreAndReflection () {
  158. var x = { __v : 5 };
  159. eq(5, Reflect.field(x, "__v"));
  160. var x = { ___b : 5 };
  161. eq(5, Reflect.field(x, "___b"));
  162. var x = { __iter__ : 5 };
  163. eq(5, Reflect.field(x, "__iter__"));
  164. }
  165. function testMakeVarArgs () {
  166. var f = function (a:Array<Dynamic>) {
  167. return a[0] + a[1];
  168. }
  169. var g = Reflect.makeVarArgs(f);
  170. var res = g(1,2);
  171. eq(3, res);
  172. }
  173. function testKwArgs () {
  174. function x (args:KwArgs) {
  175. var a = args.get("a", 0);
  176. var b = args.get("b", 0);
  177. return a + b;
  178. }
  179. var res = x( python.lib.Dict.fromObject({ "a" : 1, "b" : 2}) );
  180. eq(3, res);
  181. var res2 = python.Syntax.callNamedUntyped(x, { a : 3, b : 5});
  182. eq(8, res2);
  183. }
  184. function testNonLocal() {
  185. try { }
  186. catch (e:Dynamic) {
  187. e = 1;
  188. }
  189. }
  190. var _s:String;
  191. var s(get, null):String;
  192. var s2(null, set_s2):String;
  193. var s3(get, set):String;
  194. function get_s() return s;
  195. function set_s2(s) return s2 = s;
  196. function get_s3() return _s;
  197. function set_s3(s) return _s = s;
  198. function testPropertyInit() {
  199. s += "a";
  200. s2 += "b";
  201. s3 += "c";
  202. eq("nulla", s);
  203. eq("nullb", s2);
  204. eq("nullc", s3);
  205. }
  206. function testIsViaParentInterface() {
  207. t(Std.is(new B(), IA));
  208. }
  209. // Syntax Tests
  210. function testPythonCodeStringInterpolation () {
  211. var z = 1;
  212. var a = (Syntax.pythonCode('[$z, ${2}]'):Array<Int>);
  213. eq(a[0], z);
  214. eq(a[1], 2);
  215. inline function test2 (x:Int) {
  216. x += 1;
  217. return (Syntax.pythonCode('$x'):Int);
  218. }
  219. inline function test3 (x:Int) {
  220. return (Syntax.pythonCode('[$x]'):Array<Int>);
  221. }
  222. var x = 1;
  223. eq(2, test2(x));
  224. eq(1, x);
  225. eq(1, test3(1)[0]);
  226. eq("foo1bar", Syntax.pythonCode("'foo' + str(" + x + ") + 'bar'"));
  227. }
  228. // Issue #2936
  229. function testArrayEq () {
  230. f([1,2,3] == [1,2,3]);
  231. f(([1,2,3]:Dynamic) == ([1,2,3]:Dynamic));
  232. f(([1,2,3]:Dynamic) == [1,2,3]);
  233. f([1,2,3] == ([1,2,3]:Dynamic));
  234. t([1,2,3] != [1,2,3]);
  235. t(([1,2,3]:Dynamic) != ([1,2,3]:Dynamic));
  236. t(([1,2,3]:Dynamic) != [1,2,3]);
  237. t([1,2,3] != ([1,2,3]:Dynamic));
  238. }
  239. }