TestPython.hx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. @:pythonImport("native_python.sample", "A")
  68. extern class ExternClass {
  69. function new();
  70. function f(v:Int):Int;
  71. }
  72. @:pythonImport("native_python.sample", "A.Nested")
  73. extern class ExternNestedClass {
  74. function new();
  75. function f(v:Int):Int;
  76. }
  77. @:pythonImport("native_python.sample")
  78. extern class ExternModule {
  79. static function f(v:Int):Int;
  80. }
  81. @:pythonImport("inexistant", "AZAZA", ignoreError=true)
  82. extern class InexistantExtern1 {}
  83. @:pythonImport("inexistant", "AZAZA.ZAZA", ignoreError=true)
  84. extern class InexistantExtern2 {}
  85. @:pythonImport("inexistant", ignoreError=true)
  86. extern class InexistantExtern3 {}
  87. class TestPython extends Test {
  88. public function testDoWhileAsExpression () {
  89. var x = 1;
  90. var z = function () return (do {
  91. x++;
  92. } while (x < 3));
  93. z();
  94. eq(3, x);
  95. }
  96. public function testKeywords () {
  97. var list = new Array();
  98. }
  99. public function testStringMethod() {
  100. var d:Dynamic = "foo";
  101. eq("FOO", d.toUpperCase());
  102. var o:{function toUpperCase():String;} = "foo";
  103. eq("FOO", o.toUpperCase());
  104. var d:Dynamic = "FOO";
  105. eq("foo", d.toLowerCase());
  106. var o:{function toLowerCase():String;} = "FOO";
  107. eq("foo", o.toLowerCase());
  108. }
  109. public function testOptionalStructureFields() {
  110. var v:T = haxe.Json.parse('{"value": 1 }');
  111. eq(1, v.value);
  112. eq(null, v.maybeValue);
  113. v.maybeValue = 12;
  114. eq(12, v.maybeValue);
  115. v.maybeValue += 9;
  116. eq(21, v.maybeValue);
  117. var v:T = haxe.Json.parse('{"value": 1 }');
  118. var d:Dynamic = v;
  119. eq(1, d.value);
  120. eq(null, d.maybeValue);
  121. d.maybeValue = 12;
  122. eq(12, d.maybeValue);
  123. d.maybeValue += 9;
  124. eq(21, d.maybeValue);
  125. }
  126. function testNonOptionalArgumentAfterOptionalArgument() {
  127. function skip(a:Int = 1, b:String) {
  128. return Std.string(a) + b;
  129. }
  130. eq("12a", skip(12, "a"));
  131. eq("1a", skip("a"));
  132. }
  133. function testNativeClosures() {
  134. var s = "foo";
  135. var f = s.toUpperCase;
  136. eq("FOO", f());
  137. var a = [];
  138. var f = a.push;
  139. f(12);
  140. eq(12, a[0]);
  141. }
  142. function testOptionalEnumArguments() {
  143. var a1 = 1;
  144. var a2 = null;
  145. switch(A("foo")) {
  146. case A(i, b):
  147. a1 = i;
  148. a2 = b;
  149. case _:
  150. }
  151. eq(null, a1);
  152. eq("foo", a2);
  153. }
  154. function testTypedCatch() {
  155. function throwMe(arg:Dynamic) {
  156. return try {
  157. throw arg;
  158. } catch(e:haxe.macro.Expr.ExprDef) {
  159. 'ExprDef:$e';
  160. } catch(s:String) {
  161. 'String:$s';
  162. } catch(e:Dynamic) {
  163. 'Other:$e';
  164. }
  165. }
  166. eq("ExprDef:EBreak", throwMe(haxe.macro.Expr.ExprDef.EBreak));
  167. eq("String:foo", throwMe("foo"));
  168. eq("Other:12", throwMe(12));
  169. }
  170. /*
  171. function testSys () {
  172. var p = new Process("/bin/ls", ["-l"]);
  173. trace(p.stdout.readLine());
  174. trace(p.stdout.readLine());
  175. }
  176. */
  177. function testUnderscoreAndReflection () {
  178. var x = { __v : 5 };
  179. eq(5, Reflect.field(x, "__v"));
  180. var x = { ___b : 5 };
  181. eq(5, Reflect.field(x, "___b"));
  182. var x = { __iter__ : 5 };
  183. eq(5, Reflect.field(x, "__iter__"));
  184. }
  185. function testMakeVarArgs () {
  186. var f = function (a:Array<Dynamic>) {
  187. return a[0] + a[1];
  188. }
  189. var g = Reflect.makeVarArgs(f);
  190. var res = g(1,2);
  191. eq(3, res);
  192. }
  193. function testKwArgsAfterVarArgs () {
  194. function test (va:VarArgs, kw:KwArgs) {
  195. var a = va.toArray();
  196. eq(1,a[0]);
  197. eq(2,a[1]);
  198. eq(1,kw.get("a", null));
  199. }
  200. var a = python.Lib.anonToDict({ "a" : 1});
  201. var x = [1,2];
  202. test(x,a);
  203. }
  204. function testOptionalVarArgs () {
  205. function test (?va:VarArgs, ?kw:KwArgs) {
  206. var a = va.toArray();
  207. eq(0,a.length);
  208. }
  209. test();
  210. }
  211. function testOptionalKwArgs () {
  212. function test (?kw:KwArgs) eq(0,kw.toDict().length());
  213. test();
  214. }
  215. function testOptionalKwArgsAfterOptionalVarArgs () {
  216. function test (?va:VarArgs, ?kw:KwArgs) {
  217. var a = va.toArray();
  218. eq(1,a[0]);
  219. eq(2,a[1]);
  220. eq(0, kw.toDict().length());
  221. }
  222. var x = [1,2];
  223. test(x);
  224. function test (?va:VarArgs, ?kw:KwArgs) {
  225. var a = va.toArray();
  226. eq(0,a.length);
  227. eq(1, kw.get("a",null));
  228. }
  229. var a = python.Lib.anonToDict({ "a" : 1});
  230. test(a);
  231. }
  232. function testKwArgs () {
  233. function x (args:KwArgs) {
  234. var a = args.get("a", 0);
  235. var b = args.get("b", 0);
  236. return a + b;
  237. }
  238. var a = python.Lib.anonToDict({ "a" : 1, "b" : 2});
  239. var res = x( a );
  240. eq(3, res);
  241. var res2 = python.Syntax.callNamedUntyped(x, { a : 3, b : 5});
  242. eq(8, res2);
  243. }
  244. function testNonLocal() {
  245. try { }
  246. catch (e:Dynamic) {
  247. e = 1;
  248. }
  249. }
  250. var _s:String;
  251. var s(get, null):String;
  252. var s2(null, set_s2):String;
  253. var s3(get, set):String;
  254. function get_s() return s;
  255. function set_s2(s) return s2 = s;
  256. function get_s3() return _s;
  257. function set_s3(s) return _s = s;
  258. function testPropertyInit() {
  259. s += "a";
  260. s2 += "b";
  261. s3 += "c";
  262. eq("nulla", s);
  263. eq("nullb", s2);
  264. eq("nullc", s3);
  265. }
  266. function testIsViaParentInterface() {
  267. t(Std.is(new B(), IA));
  268. }
  269. // Syntax Tests
  270. function testPythonCodeStringInterpolation () {
  271. var z = 1;
  272. var a = (Syntax.pythonCode('[{0}, {1}]', z, 2):Array<Int>);
  273. eq(a[0], z);
  274. eq(a[1], 2);
  275. function test2 (x:Int) {
  276. x += 1;
  277. return (Syntax.pythonCode("{0}", x):Int);
  278. }
  279. function test3 (x:Int) {
  280. return (Syntax.pythonCode('[{0}]', x):Array<Int>);
  281. }
  282. var x = 1;
  283. eq(2, test2(x));
  284. eq(1, x);
  285. eq(1, test3(1)[0]);
  286. eq("foo1bar", Syntax.pythonCode("'foo' + str({0}) + 'bar'", x));
  287. function test4a (x:Int) {
  288. return (Syntax.pythonCode("[{0}][0]", x+x):Int);
  289. }
  290. function test4b (x:Int):String {
  291. return Syntax.pythonCode('[{0}][0]', (function () return Std.string(x+x))() );
  292. }
  293. eq(2, test4a(1));
  294. eq("2", test4b(1));
  295. }
  296. function testTupleCreation() {
  297. var t = Tup2.create(1, 2);
  298. eq(t._1, 1);
  299. eq(t._2, 2);
  300. eq(t.length, 2);
  301. }
  302. function testExtern()
  303. {
  304. eq(new ExternClass().f(1), 2);
  305. eq(new ExternNestedClass().f(1), 3);
  306. eq(ExternModule.f(1), 4);
  307. }
  308. }