test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. if (Sk.inBrowser)
  2. {
  3. goog.require('goog.dom');
  4. goog.require('goog.ui.ComboBox');
  5. }
  6. var tokenizefail = 0;
  7. var tokenizepass = 0;
  8. function dump_tokens(fn, input)
  9. {
  10. var uneval = function(t)
  11. {
  12. return new Sk.builtins['repr'](new Sk.builtins['str'](t)).v;
  13. };
  14. var ret = '',
  15. lines = input.split("\n"),
  16. curIndex = 0,
  17. printer = function (type, token, st, en, line)
  18. {
  19. var srow = st[0],
  20. scol = st[1],
  21. erow = en[0],
  22. ecol = en[1];
  23. var data = sprintf("%-12.12s %-13.13s (%d, %d) (%d, %d)", Sk.Tokenizer.tokenNames[type], uneval(token), srow, scol, erow, ecol);
  24. //print("DUMP:"+data);
  25. ret += data;
  26. ret += "\n";
  27. };
  28. var tokenizer = new Sk.Tokenizer(fn, false, printer);
  29. var done = false;
  30. for (var i = 0; i < lines.length && !done; ++i)
  31. {
  32. done = tokenizer.generateTokens(lines[i] + ((i === lines.length - 1) ? "" : "\n"));
  33. }
  34. if (!done) tokenizer.generateTokens();
  35. return ret;
  36. }
  37. function testTokenize(name)
  38. {
  39. try { var input = read(name + ".py"); }
  40. catch (e) { return; }
  41. if (input.charAt(input.length - 1) !== "\n")
  42. {
  43. throw "input wasn't nl term";
  44. }
  45. input = input.substring(0, input.length - 1);
  46. if (input.charAt(input.length - 1) === "\r")
  47. {
  48. input = input.substring(0, input.length - 1);
  49. }
  50. var expect = read(name + ".expect");
  51. var got = '';
  52. try
  53. {
  54. got = dump_tokens(name + ".py", input);
  55. }
  56. catch (e)
  57. {
  58. got += Sk.builtins['str'](e).v + "\n";
  59. }
  60. if (expect !== got)
  61. {
  62. print("FAILED: (" + name + ".py)\n-----");
  63. print(input);
  64. print("-----\nGOT:\n-----");
  65. print(got);
  66. print("-----\nWANTED:\n-----");
  67. print(expect);
  68. tokenizefail += 1;
  69. }
  70. else
  71. {
  72. tokenizepass += 1;
  73. }
  74. }
  75. var parsefail = 0;
  76. var parsepass = 0;
  77. function testParse(name)
  78. {
  79. try { var input = read(name + ".py"); }
  80. catch (e) { return; }
  81. var expect = read(name + ".expect");
  82. var got;
  83. try
  84. {
  85. got = Sk.parseTreeDump(Sk.parse(name + ".py", input));
  86. }
  87. catch (e)
  88. {
  89. got = "EXCEPTION\n";
  90. got += e.constructor.name + "\n";
  91. got += JSON.stringify(e) + "\n";
  92. }
  93. if (expect !== got)
  94. {
  95. print("FAILED: (" + name + ".py)\n-----");
  96. print(input);
  97. print("-----\nGOT:\n-----");
  98. print(got);
  99. print("-----\nWANTED:\n-----");
  100. print(expect);
  101. parsefail += 1;
  102. }
  103. else
  104. {
  105. parsepass += 1;
  106. }
  107. }
  108. var transformpass = 0;
  109. var transformfail = 0;
  110. var transformdisabled = 0;
  111. function testTransform(name)
  112. {
  113. try { var input = read(name + ".py"); }
  114. catch (e) { return; }
  115. var expect = 'NO_.TRANS_FILE';
  116. try { expect = read(name + ".trans"); }
  117. catch (e) {
  118. transformdisabled += 1;
  119. return;
  120. }
  121. var cst = Sk.parse(name + ".py", input);
  122. var got = Sk.astDump(Sk.astFromParse(cst)) + "\n";
  123. //print(got);
  124. //print(Sk.parseTreeDump(cst));
  125. if (expect !== got)
  126. {
  127. print("FAILED: (" + name + ".py)\n-----");
  128. print(input);
  129. print("-----\nGOT:\n-----");
  130. print(got);
  131. print("-----\nWANTED:\n-----");
  132. print(expect);
  133. //print("-----\nCST:\n-----");
  134. //print(Sk.parseTestDump(cst));
  135. transformfail += 1;
  136. }
  137. else
  138. {
  139. transformpass += 1;
  140. }
  141. }
  142. var symtabpass = 0;
  143. var symtabfail = 0;
  144. var symtabdisabled = 0;
  145. function testSymtab(name)
  146. {
  147. try { var input = read(name + ".py"); }
  148. catch (e) { return; }
  149. //print(name);
  150. var expect = 'NO_.SYMTAB_FILE';
  151. try { expect = read(name + ".py.symtab"); }
  152. catch (e) {
  153. symtabdisabled += 1;
  154. return;
  155. }
  156. var cst = Sk.parse(name + ".py", input);
  157. var ast = Sk.astFromParse(cst);
  158. var st = Sk.symboltable(ast, name + ".py");
  159. var got = Sk.dumpSymtab(st);
  160. if (expect !== got)
  161. {
  162. print("FAILED: (" + name + ".py)\n-----");
  163. print(input);
  164. print("-----\nGOT:\n-----");
  165. print(got);
  166. print("-----\nWANTED:\n-----");
  167. print(expect);
  168. symtabfail += 1;
  169. }
  170. else
  171. {
  172. symtabpass += 1;
  173. }
  174. }
  175. var AllRunTests = [];
  176. var runpass = 0;
  177. var runfail = 0;
  178. var rundisabled = 0;
  179. function testRun(name, nocatch, debugMode)
  180. {
  181. try { var input = read(name + ".py"); }
  182. catch (e) {
  183. try { read(name + ".py.disabled"); rundisabled += 1;}
  184. catch (e) {}
  185. return;
  186. }
  187. AllRunTests.unshift(name);
  188. var got = '';
  189. var justpath = name.substr(0, name.lastIndexOf('/'));
  190. Sk.configure({
  191. output: function(str) { got += str; },
  192. sysargv: [ name + '.py' ],
  193. read: read,
  194. debugging: debugMode,
  195. syspath: [ justpath ]
  196. });
  197. var expect = read(name + ".py.real");
  198. var expectalt;
  199. try { expectalt = read(name + ".py.real.alt"); }
  200. catch (e) {}
  201. var justname = name.substr(name.lastIndexOf('/') + 1);
  202. var promise = Sk.misceval.asyncToPromise(function() {
  203. return Sk.importMain(justname, false, true);
  204. });
  205. if (!nocatch)
  206. {
  207. promise = promise.then(null, function(e) {
  208. if (e instanceof Sk.builtin.SystemExit) {
  209. // SystemExit isn't a failing exception, so treat it specially
  210. got += e.toString() + "\n";
  211. }
  212. else if (e.name !== undefined)
  213. {
  214. // js exception, currently happens for del'd objects. shouldn't
  215. // really though.
  216. got = "EXCEPTION: " + e.name + "\n";
  217. }
  218. else
  219. {
  220. got = "EXCEPTION: " + Sk.builtins['str'](e).v + "\n";
  221. }
  222. });
  223. var origPromise = promise;
  224. promise = new Promise(function(resolve) {
  225. var compareResult = function(module) {
  226. if (expect !== got && (expectalt === undefined || expectalt !== got))
  227. {
  228. print("FAILED: (" + name + ".py)\n-----");
  229. print(input);
  230. print("-----\nGOT:\n-----");
  231. print(got);
  232. print("-----\nWANTED:\n-----");
  233. print(expect);
  234. print("-----\nDIFF:\n-----")
  235. print("len got: " + got.length + "\n")
  236. print("len wanted: " + expect.length + "\n")
  237. var longest = got.length > expect.length ? got : expect;
  238. for (var i in longest) {
  239. if (got[i] !== expect[i]){
  240. try{
  241. print("firstdiff at: " + i + " got: " + got[i].charCodeAt(0) + " (" + got.substr(i) + ") expect: " + expect[i].charCodeAt(0) + " (" + expect.substr(i) + ")");
  242. } catch (err){
  243. break;
  244. }
  245. break;
  246. }
  247. }
  248. if (module && module.$js)
  249. {
  250. print("-----\nJS:\n-----");
  251. var beaut = js_beautify(module.$js);
  252. print(beaut);
  253. }
  254. runfail += 1;
  255. //throw "dying on first run fail";
  256. }
  257. else
  258. {
  259. runpass += 1;
  260. }
  261. resolve();
  262. };
  263. // Whatever happens, we resolve our promise successfully
  264. origPromise.then(compareResult, compareResult);
  265. });
  266. }
  267. return promise;
  268. }
  269. var interactivepass = 0;
  270. var interactivefail = 0;
  271. var interactivedisabled = 0;
  272. function testInteractive(name)
  273. {
  274. try { var input = read(name + ".py"); }
  275. catch (e) {
  276. try { read(name + ".py.disabled"); interactivedisabled += 1;}
  277. catch (e) {}
  278. return;
  279. }
  280. var expect = read(name + ".py.real");
  281. var got = '';
  282. sk$output = function(str) { got += str; }
  283. var lines = input.split("\n");
  284. var ic = new Skulpt.InteractiveContext();
  285. for (var i = 0; i < lines.length; ++i)
  286. {
  287. //print("LINE:"+lines[i]);
  288. js = ic.evalLine(lines[i] + "\n");
  289. //print("JS now:'"+js+"'");
  290. if (js !== false)
  291. {
  292. try {
  293. var ret = eval(js);
  294. if (ret && ret.tp$repr !== undefined)
  295. got += ret.tp$repr().v + "\n";
  296. }
  297. catch (e) { got += "EXCEPTION: " + e.name + "\n" }
  298. //print("made new context");
  299. ic = new Skulpt.InteractiveContext();
  300. }
  301. }
  302. if (expect !== got)
  303. {
  304. print("FAILED: (" + name + ".py)\n-----");
  305. print(input);
  306. print("-----\nGOT:\n-----");
  307. print(got);
  308. print("-----\nWANTED:\n-----");
  309. print(expect);
  310. interactivefail += 1;
  311. }
  312. else
  313. {
  314. interactivepass += 1;
  315. }
  316. }
  317. var doTestToken = false
  318. var doTestParse = false
  319. var doTestTrans = false
  320. var doTestSymtab = false
  321. var doTestRun = true
  322. var testInDebugMode = arguments.indexOf("--debug-mode") != -1;
  323. function testsMain()
  324. {
  325. var i, promise = Promise.resolve();
  326. if (doTestToken) {
  327. for (i = 0; i <= 100; i += 1)
  328. {
  329. testTokenize(sprintf("test/tokenize/t%02d", i));
  330. }
  331. print(sprintf("tokenize: %d/%d", tokenizepass, tokenizepass + tokenizefail));
  332. }
  333. if (doTestParse) {
  334. for (i = 0; i <= 10; i += 1)
  335. {
  336. testParse(sprintf("test/parse/t%02d", i));
  337. }
  338. print(sprintf("parse: %d/%d", parsepass, parsepass + parsefail));
  339. }
  340. if (doTestTrans) {
  341. for (i = 0; i <= 1000; ++i)
  342. {
  343. testTransform(sprintf("test/run/t%02d", i));
  344. }
  345. print(sprintf("transform: %d/%d (+%d disabled)", transformpass, transformpass + transformfail, transformdisabled));
  346. }
  347. if (doTestSymtab) {
  348. for (i = 0; i <= 1000; ++i)
  349. {
  350. testSymtab(sprintf("test/run/t%02d", i));
  351. }
  352. print(sprintf("symtab: %d/%d (+%d disabled)", symtabpass, symtabpass + symtabfail, symtabdisabled));
  353. }
  354. if (doTestRun) {
  355. for (i = 0; i <= 1000; ++i)
  356. {
  357. (function(i) {
  358. promise = promise.then(function(p) {
  359. return testRun(sprintf("test/run/t%02d", i), undefined, testInDebugMode);
  360. });
  361. })(i);
  362. }
  363. for (i = 0; i < namedtfiles.length; i++ ) {
  364. (function(i) {
  365. promise = promise.then(function(p) {
  366. return testRun(namedtfiles[i],undefined,testInDebugMode);
  367. });
  368. })(i);
  369. }
  370. promise = promise.then(function() {
  371. print(sprintf("run: %d/%d (+%d disabled)", runpass, runpass + runfail, rundisabled));
  372. }, function(e) {
  373. print("Internal error: "+e);
  374. });
  375. }
  376. if (Sk.inBrowser)
  377. {
  378. var origrunfail = runfail;
  379. runpass = runfail = rundisabled = 0;
  380. for (i = 0; i <= 20; ++i)
  381. {
  382. (function(i) {
  383. promise = promise.then(function() {
  384. testRun(sprintf("test/closure/t%02d", i));
  385. });
  386. })(i);
  387. }
  388. promise = promise.then(function() {
  389. print(sprintf("closure: %d/%d", runpass, runpass + runfail));
  390. runfail += origrunfail; // for exit code
  391. // make a combobox of all tests so we can run just one
  392. var el = goog.dom.getElement('one-test');
  393. var cb = new goog.ui.ComboBox();
  394. cb.setUseDropdownArrow(true);
  395. cb.setDefaultText('Run one test...');
  396. for (var i = 0; i < AllRunTests.length; ++i)
  397. {
  398. cb.addItem(new goog.ui.ComboBoxItem(AllRunTests[i]));
  399. }
  400. cb.render(el);
  401. goog.events.listen(cb, 'change', function(e) {
  402. goog.dom.setTextContent(goog.dom.getElement('output'), "");
  403. print("running", e.target.getValue());
  404. testRun(e.target.getValue(), true);
  405. });
  406. });
  407. }
  408. else
  409. {
  410. print("closure: skipped");
  411. }
  412. //return;
  413. // for (i = 0; i <= 100; ++i)
  414. // {
  415. // testInteractive(sprintf("test/interactive/t%02d", i));
  416. // }
  417. // print(sprintf("interactive: %d/%d (+%d disabled)", interactivepass, interactivepass + interactivefail, interactivedisabled));
  418. //print('exiting with: ' + tokenizefail + parsefail + transformfail + symtabfail + runfail + interactivefail);
  419. if (!Sk.inBrowser) {
  420. promise.then(function(x) {
  421. print("Quitting");
  422. var exitCode = tokenizefail + parsefail + transformfail + symtabfail + runfail + interactivefail;
  423. if (exitCode > 0) {
  424. quit(exitCode);
  425. }
  426. // Do not quit if success; may prevent other scripts from running after this one
  427. });
  428. }
  429. }
  430. if (!Sk.inBrowser)
  431. {
  432. testsMain();
  433. }