Skip to content

Commit

Permalink
Correct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
STAH committed Feb 2, 2016
1 parent 628e14a commit a7350cf
Show file tree
Hide file tree
Showing 18 changed files with 35 additions and 25 deletions.
24 changes: 17 additions & 7 deletions Preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@
* #define EXPRESSION
* @type {!RegExp}
*/
Preprocessor.VAR = /var[ ]+([a-zA-Z_][a-zA-Z0-9_]*)[ ]*=[ ]*(.+)/g;
Preprocessor.VAR = /define[ ]+var[ ]+([a-zA-Z_][a-zA-Z0-9_]*)[ ]*=[ ]*(.+)/g;

/**
* #define EXPRESSION
* @type {!RegExp}
*/
Preprocessor.BOOLVAR = /([a-zA-Z_][a-zA-Z0-9_]*)[ ]*/g;
Preprocessor.BOOLVAR = /define[ ]+([a-zA-Z_][a-zA-Z0-9_]*)[ ]*/g;

/**
* #define EXPRESSION
* @type {!RegExp}
*/
Preprocessor.FUNCTION = /function[ ]+([a-zA-Z_][a-zA-Z0-9_]*)[ ]*(.+)/g;
Preprocessor.FUNCTION = /define[ ]+function[ ]+([a-zA-Z_][a-zA-Z0-9_]*)[ ]*(.+)/g;

/**
* Strips slashes from an escaped string.
Expand Down Expand Up @@ -316,6 +316,7 @@
this.source.substring(match.index, match.index + this.errorSourceAhead) + '...'));
}
verbose(' test: ' + match2[2]);
verbose(' defines ' + JSON.stringify(defines));
if (match2[1] === 'ifdef') {
include = defines[match2[2]] !== undefined;
} else if (match2[1] === 'ifndef') {
Expand Down Expand Up @@ -392,8 +393,11 @@
}
break;
case 'define':
// https://github.com/dcodeIO/Preprocessor.js/issues/5
Preprocessor.DEFINE.lastIndex = match.index;
Preprocessor.VAR.lastIndex = match.index;
Preprocessor.FUNCTION.lastIndex = match.index;
Preprocessor.BOOLVAR.lastIndex = match.index;

if ((match2 = Preprocessor.DEFINE.exec(this.source)) === null) {
throw (new Error('Illegal #' + match[2] + ': ' +
this.source.substring(match.index, match.index + this.errorSourceAhead) + '...'));
Expand All @@ -402,23 +406,27 @@
verbose(' def: "' + define + '"');

var identifier, value, type;
if ((match3 = Preprocessor.VAR.exec(define)) !== null) {
if ((match3 = Preprocessor.VAR.exec(this.source)) !== null) {
type = 'var';
identifier = match3[1];
value = match3[2];
} else if ((match3 = Preprocessor.FUNCTION.exec(define)) !== null) {
verbose(' match3(var): ' + JSON.stringify(match3));
} else if ((match3 = Preprocessor.FUNCTION.exec(this.source)) !== null) {
type = 'function';
identifier = match3[1];
value = match3[2];
} else if ((match3 = Preprocessor.BOOLVAR.exec(define)) !== null) {
verbose(' match3(function): ' + JSON.stringify(match3));
} else if ((match3 = Preprocessor.BOOLVAR.exec(this.source)) !== null) {
type = 'var';
identifier = match3[1];
value = true;
verbose(' match3(boolvar): ' + JSON.stringify(match3));
} else {
throw (new Error('Illegal #' + match[2] + ': ' +
this.source.substring(match.index, match.index + this.errorSourceAhead) + '...'));
}

verbose(' type: ' + type);
verbose(' identifier: ' + identifier);
verbose(' value: ' + value);

Expand All @@ -427,6 +435,8 @@
'value': value
};

verbose(' defines ' + JSON.stringify(defines));

var lineEnding = '';
if (this.preserveLineNumbers) {
lineEnding = this.source.substring(match.index, Preprocessor.DEFINE.lastIndex).replace(NOT_LINE_ENDING, '');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "preprocessor.js",
"version": "1.4.1",
"version": "1.4.2",
"author": "Daniel Wirtz <[email protected]>",
"contributors": [
"Stanislav Lesnikov <[email protected]>"
Expand Down
Empty file modified tests/samples/subdir/glob1.js
100644 → 100755
Empty file.
Empty file modified tests/samples/subdir/glob2.js
100644 → 100755
Empty file.
Empty file modified tests/samples/subdir/sub/glob3.js
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions tests/test_01.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ var test = require('tape');

var Preprocessor = require(__dirname + '/../Preprocessor.js');

test('init', function(t) {
test('test_01:init', function(t) {
t.ok(typeof Preprocessor === 'function', 'Preprocessor is a function');

t.end();
});

test('verbose', function(t) {
test('test_01:verbose', function(t) {
var pp = new Preprocessor('// #ifdef UNDEFINED\ntest();\n// #endif\n');
var msgs = '';
pp.process({}, function(msg) { msgs += msg; });
Expand Down
2 changes: 1 addition & 1 deletion tests/test_02.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var test = require('tape');

var Preprocessor = require(__dirname + '/../Preprocessor.js');

test('evaluate', function(t) {
test('test_02:evaluate', function(t) {
var defines = { 'VERSION': { 'type': 'var', 'value': '"1.0"' } };
t.equal(defines['VERSION'].value + ';', Preprocessor.evaluate(defines, "'\"'+VERSION+'\";'"));
t.equal('"' + 'Hello world!' + '";', Preprocessor.evaluate(defines, '"\\"Hello world!\\";";'));
Expand Down
2 changes: 1 addition & 1 deletion tests/test_03.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var test = require('tape');
var Preprocessor = require(__dirname + '/../Preprocessor.js');
var fs = require('fs');

test('test1', function(t) {
test('test_03:test1', function(t) {
var pp = new Preprocessor(fs.readFileSync(__dirname + '/samples/test1.js'), __dirname + '/samples');
var src = pp.process({ 'VERSION': { 'type': 'var', 'value': '"1.0"' } }, console.log).replace(/\r/g, '');
t.equal(src, '\nconsole.log("UNDEFINED is not defined");\n\nconsole.log("UNDEFINED is not defined (else)");\n\nvar version = "1.0";\n');
Expand Down
2 changes: 1 addition & 1 deletion tests/test_04.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var test = require('tape');
var Preprocessor = require(__dirname + '/../Preprocessor.js');
var fs = require('fs');

test('test2', function(t) {
test('test_04:test2', function(t) {
var pp = new Preprocessor(fs.readFileSync(__dirname + '/samples/test2.js'), __dirname + '/samples');
var src = pp.process({ 'VERSION': { 'type': 'var', 'value': '"1.0"' } }, console.log).replace(/\r/g, '');
t.equal(src, ' console.log("2==2")\n console.log("VERSION=="+"1.0");\n');
Expand Down
6 changes: 4 additions & 2 deletions tests/test_05.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ var test = require('tape');
var Preprocessor = require(__dirname + '/../Preprocessor.js');
var fs = require('fs');

test('define', function(t) {
var pp = new Preprocessor(fs.readFileSync(__dirname + '/samples/define.js'), __dirname + '/samples');
test('test_05:define', function(t) {
var content = fs.readFileSync(__dirname + '/samples/define.js', 'utf-8');
console.log(content);
var pp = new Preprocessor(content, __dirname + '/samples');
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src, 'var angle = 171.88733853924697;\n');

Expand Down
2 changes: 1 addition & 1 deletion tests/test_06_01.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var test = require('tape');
var Preprocessor = require(__dirname + '/../Preprocessor.js');
var fs = require('fs');

test('globalDefines_1', function(t) {
test('test_06_01:globalDefines_1', function(t) {
var pp = new Preprocessor(fs.readFileSync(__dirname + '/samples/issue11.js'), __dirname + '/samples');
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src, "console.log('hi');\n");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_06_02.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var test = require('tape');
var Preprocessor = require(__dirname + '/../Preprocessor.js');
var fs = require('fs');

test('globalDefines_2', function(t) {
test('test_06_02:globalDefines_2', function(t) {
var pp = new Preprocessor(fs.readFileSync(__dirname + '/samples/issue11_2.js'), __dirname + '/samples');
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src, "console.log('hi');\n");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_06_03.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var test = require('tape');
var Preprocessor = require(__dirname + '/../Preprocessor.js');
var fs = require('fs');

test('globalDefines_3', function(t) {
test('test_06_03:globalDefines_3', function(t) {
var pp = new Preprocessor(fs.readFileSync(__dirname + '/samples/issue11_3.js'), __dirname + '/samples');
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src, "console.log('hi');\n");
Expand Down
3 changes: 1 addition & 2 deletions tests/test_07_01.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ var test = require('tape');

var Preprocessor = require(__dirname + '/../Preprocessor.js');

test('include_1', function(t) {
t.plan(1);
test('test_07_01:include_1', function(t) {
var pp = new Preprocessor('// #include "number.js"\n// #include "number.js"\n', __dirname + '/samples');
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src.trim(), '42\n42');
Expand Down
3 changes: 1 addition & 2 deletions tests/test_07_02.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ var test = require('tape');

var Preprocessor = require(__dirname + '/../Preprocessor.js');

test('include_2', function(t) {
t.plan(1);
test('test_07_02:include_2', function(t) {
var pp = new Preprocessor('// #include_once "number.js"\n// #include_once "number.js"\n', __dirname + '/samples');
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src.trim(), '42');
Expand Down
2 changes: 1 addition & 1 deletion tests/test_08.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var test = require('tape');
var Preprocessor = require(__dirname + '/../Preprocessor.js');
var fs = require('fs');

test('preserveLineNumbers', function(t) {
test('test_08:preserveLineNumbers', function(t) {
var pp = new Preprocessor(fs.readFileSync(__dirname + '/samples/preserve_lines.js'), __dirname + '/samples', true);
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src, 'var i = 0;\n\n\n\ni = 2;\n\n\nconsole.log(i);\n');
Expand Down
2 changes: 1 addition & 1 deletion tests/test_09_01.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var test = require('tape');

var Preprocessor = require(__dirname + '/../Preprocessor.js');

test('includeGlob_1', function(t) {
test('test_09_01:includeGlob_1', function(t) {
var pp = new Preprocessor('// #include_once "subdir/*.js"\n', __dirname + '/samples');
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src.trim(), "'glob1.js';'glob2.js';");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_09_02.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var test = require('tape');

var Preprocessor = require(__dirname + '/../Preprocessor.js');

test('includeGlob', function(t) {
test('test_09_02:includeGlob', function(t) {
var pp = new Preprocessor('// #include_once "subdir/**/glob*.js"\n', __dirname + '/samples');
var src = pp.process({}, console.log).replace(/\r/g, '');
t.equal(src.trim(), "'glob1.js';'glob2.js';'glob3.js';");
Expand Down

0 comments on commit a7350cf

Please sign in to comment.