From a7350cf88f570a2c0869582efdb310ccc6b9a1dd Mon Sep 17 00:00:00 2001 From: Stanislav Lesnikov Date: Tue, 2 Feb 2016 14:14:28 +0100 Subject: [PATCH] Correct tests --- Preprocessor.js | 24 +++++++++++++++++------- package.json | 2 +- tests/samples/subdir/glob1.js | 0 tests/samples/subdir/glob2.js | 0 tests/samples/subdir/sub/glob3.js | 0 tests/test_01.js | 4 ++-- tests/test_02.js | 2 +- tests/test_03.js | 2 +- tests/test_04.js | 2 +- tests/test_05.js | 6 ++++-- tests/test_06_01.js | 2 +- tests/test_06_02.js | 2 +- tests/test_06_03.js | 2 +- tests/test_07_01.js | 3 +-- tests/test_07_02.js | 3 +-- tests/test_08.js | 2 +- tests/test_09_01.js | 2 +- tests/test_09_02.js | 2 +- 18 files changed, 35 insertions(+), 25 deletions(-) mode change 100644 => 100755 tests/samples/subdir/glob1.js mode change 100644 => 100755 tests/samples/subdir/glob2.js mode change 100644 => 100755 tests/samples/subdir/sub/glob3.js diff --git a/Preprocessor.js b/Preprocessor.js index 4ed2360..b668966 100644 --- a/Preprocessor.js +++ b/Preprocessor.js @@ -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. @@ -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') { @@ -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) + '...')); @@ -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); @@ -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, ''); diff --git a/package.json b/package.json index b4f7873..e14ecee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "preprocessor.js", - "version": "1.4.1", + "version": "1.4.2", "author": "Daniel Wirtz ", "contributors": [ "Stanislav Lesnikov " diff --git a/tests/samples/subdir/glob1.js b/tests/samples/subdir/glob1.js old mode 100644 new mode 100755 diff --git a/tests/samples/subdir/glob2.js b/tests/samples/subdir/glob2.js old mode 100644 new mode 100755 diff --git a/tests/samples/subdir/sub/glob3.js b/tests/samples/subdir/sub/glob3.js old mode 100644 new mode 100755 diff --git a/tests/test_01.js b/tests/test_01.js index 3f5d831..81af093 100644 --- a/tests/test_01.js +++ b/tests/test_01.js @@ -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; }); diff --git a/tests/test_02.js b/tests/test_02.js index c14ca3b..8f3db5b 100644 --- a/tests/test_02.js +++ b/tests/test_02.js @@ -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!\\";";')); diff --git a/tests/test_03.js b/tests/test_03.js index a6b5cc4..742156d 100644 --- a/tests/test_03.js +++ b/tests/test_03.js @@ -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'); diff --git a/tests/test_04.js b/tests/test_04.js index 18ba645..bf44598 100644 --- a/tests/test_04.js +++ b/tests/test_04.js @@ -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'); diff --git a/tests/test_05.js b/tests/test_05.js index 853d25a..9483967 100644 --- a/tests/test_05.js +++ b/tests/test_05.js @@ -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'); diff --git a/tests/test_06_01.js b/tests/test_06_01.js index 2c0a0b5..2467281 100644 --- a/tests/test_06_01.js +++ b/tests/test_06_01.js @@ -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"); diff --git a/tests/test_06_02.js b/tests/test_06_02.js index 507fef6..29c2027 100644 --- a/tests/test_06_02.js +++ b/tests/test_06_02.js @@ -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"); diff --git a/tests/test_06_03.js b/tests/test_06_03.js index c9dc8a1..3bc95ca 100644 --- a/tests/test_06_03.js +++ b/tests/test_06_03.js @@ -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"); diff --git a/tests/test_07_01.js b/tests/test_07_01.js index 0f367b3..2e11fc7 100644 --- a/tests/test_07_01.js +++ b/tests/test_07_01.js @@ -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'); diff --git a/tests/test_07_02.js b/tests/test_07_02.js index f57dff2..1d91fb9 100644 --- a/tests/test_07_02.js +++ b/tests/test_07_02.js @@ -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'); diff --git a/tests/test_08.js b/tests/test_08.js index fbefb88..aae1c13 100644 --- a/tests/test_08.js +++ b/tests/test_08.js @@ -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'); diff --git a/tests/test_09_01.js b/tests/test_09_01.js index 2348c5b..ad0bda7 100644 --- a/tests/test_09_01.js +++ b/tests/test_09_01.js @@ -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';"); diff --git a/tests/test_09_02.js b/tests/test_09_02.js index 70f251f..f47ad6c 100644 --- a/tests/test_09_02.js +++ b/tests/test_09_02.js @@ -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';");