test-fixtures.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. const builder = require("./builder.js");
  3. const fs = require("fs");
  4. const path = require("path");
  5. let errors = 0;
  6. const baseDir = path.join(__dirname, "fixtures");
  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 outLines = [];
  23. const outFilename = function (line) {
  24. outLines.push(line);
  25. };
  26. const defines = {
  27. TRUE: true,
  28. FALSE: false,
  29. };
  30. let out;
  31. try {
  32. builder.preprocess(inFilename, outFilename, defines);
  33. out = outLines.join("\n").trim();
  34. } catch (e) {
  35. out = ("Error: " + e.message).replace(/^/gm, "//");
  36. }
  37. if (out !== expectation) {
  38. errors++;
  39. console.log("Assertion failed for " + inFilename);
  40. console.log("--------------------------------------------------");
  41. console.log("EXPECTED:");
  42. console.log(expectation);
  43. console.log("--------------------------------------------------");
  44. console.log("ACTUAL");
  45. console.log(out);
  46. console.log("--------------------------------------------------");
  47. console.log();
  48. }
  49. });
  50. if (errors) {
  51. console.error("Found " + errors + " expectation failures.");
  52. process.exit(1);
  53. } else {
  54. console.log("All tests completed without errors.");
  55. process.exit(0);
  56. }