test-fixtures_esprima.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. const p2 = require("./preprocessor2.js");
  3. const fs = require("fs");
  4. const path = require("path");
  5. let errors = 0;
  6. const baseDir = path.join(__dirname, "fixtures_esprima");
  7. const files = fs
  8. .readdirSync(baseDir)
  9. .filter(function (name) {
  10. return /-expected\./.test(name);
  11. })
  12. .map(function (name) {
  13. return path.join(baseDir, name);
  14. });
  15. files.forEach(function (expectationFilename) {
  16. const inFilename = expectationFilename.replace("-expected", "");
  17. const expectation = fs
  18. .readFileSync(expectationFilename)
  19. .toString()
  20. .trim()
  21. .replace(/__filename/g, fs.realpathSync(inFilename));
  22. const input = fs.readFileSync(inFilename).toString();
  23. const defines = {
  24. TRUE: true,
  25. FALSE: false,
  26. OBJ: { obj: { i: 1 }, j: 2 },
  27. TEXT: "text",
  28. };
  29. const map = {
  30. "import-alias": "import-name",
  31. };
  32. const ctx = {
  33. defines,
  34. map,
  35. rootPath: __dirname + "/../..",
  36. };
  37. let out;
  38. try {
  39. out = p2.preprocessPDFJSCode(ctx, input);
  40. } catch (e) {
  41. out = ("Error: " + e.message).replace(/^/gm, "//");
  42. }
  43. if (out !== expectation) {
  44. errors++;
  45. console.log("Assertion failed for " + inFilename);
  46. console.log("--------------------------------------------------");
  47. console.log("EXPECTED:");
  48. console.log(expectation);
  49. console.log("--------------------------------------------------");
  50. console.log("ACTUAL");
  51. console.log(out);
  52. console.log("--------------------------------------------------");
  53. console.log();
  54. }
  55. });
  56. if (errors) {
  57. console.error("Found " + errors + " expectation failures.");
  58. process.exit(1);
  59. } else {
  60. console.log("All tests completed without errors.");
  61. process.exit(0);
  62. }