pdf.js/external/builder/test.js
Rob Wu f8af4d6567 Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:

- Add word boundary after regex for preprocessor token matching.
  Previously, when you mistakenly used "#ifdef" instead of "#if", the
  line would be parsed as a preprocessor directive (because "#ifdef"
  starts with "#if"), but without condition (because "def" does not
  start with a space). Consequently, the condition would always be false
  and anything between "#ifdef" and "#endif" would not be included.

- Add validation and error reporting everywhere, to aid debugging.

- Support nested comments (by accounting for the whole stack of
  conditions, instead of only the current one).

- Add #elif preprocessor command. Could be used as follows:
  //#if !FEATURE_ENABLED
  //#error FEATURE_ENABLED must be set
  //#endif

- Add #error preprocessor command.

- Add end-of-line word boundary after "-->" in the comment trimmer.
  Otherwise the pattern would also match "-->" in the middle of a line,
  and incorrectly convert something like "while(i-->0)" to "while(i0)".

Code health:

- Add unit tests for the preprocessor (run external/builder/test.js).

- Fix broken link to MDN (resolved to DXR).

- Refactor to use STATE_* names instead of magic numbers (the original
  meaning of the numbers is preserved, with one exception).

- State 3 has been split in two states, to distinguish between being in
  an #if and #else. This is needed to ensure that #else cannot be
  started without an #if.
2015-07-19 14:47:28 +02:00

55 lines
1.4 KiB
JavaScript

/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* jshint node:true */
/* globals cat, cd, echo, ls */
'use strict';
require('shelljs/make');
var builder = require('./builder');
var fs = require('fs');
var path = require('path');
var errors = 0;
cd(__dirname);
cd('fixtures');
ls('*-expected.*').forEach(function(expectationFilename) {
var inFilename = expectationFilename.replace('-expected', '');
var expectation = cat(expectationFilename).trim()
.replace(/__filename/g, fs.realpathSync(inFilename));
var outLines = [];
var outFilename = function(line) {
outLines.push(line);
};
var defines = {
TRUE: true,
FALSE: false,
};
var out;
try {
builder.preprocess(inFilename, outFilename, defines);
out = outLines.join('\n').trim();
} catch (e) {
out = ('Error: ' + e.message).replace(/^/gm, '//');
}
if (out !== expectation) {
echo('Assertion failed for ' + inFilename);
echo('--------------------------------------------------');
echo('EXPECTED:');
echo(expectation);
echo('--------------------------------------------------');
echo('ACTUAL');
echo(out);
echo('--------------------------------------------------');
echo();
}
});
if (errors) {
echo('Found ' + errors + ' expectation failures.');
} else {
echo('All tests completed without errors.');
}