diff --git a/.gitignore b/.gitignore index ff7dfd2..c135aae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/node_modules/ +/test/tmp .idea *.iml -node_modules diff --git a/.jshintrc b/.jshintrc index aad30f7..2e238a6 100644 --- a/.jshintrc +++ b/.jshintrc @@ -9,5 +9,7 @@ "undef": true, "boss": true, "eqnull": true, - "node": true + "strict": true, + "node": true, + "mocha": true } \ No newline at end of file diff --git a/.npmignore b/.npmignore index 2bc09f4..8f4bb69 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,4 @@ /node_modules/ +/test/tmp .idea *.iml \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js index 6f0de40..c2b1405 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -4,18 +4,21 @@ module.exports = function(grunt) { require('load-grunt-tasks')(grunt); - // Project configuration. grunt.initConfig({ - nodeunit: { - preprocess : { - src: ['test/preprocess_test.js'] + mochaTest: { + preprocess: { + options: { + reporter: 'spec' + }, + src: ['test/**/*.spec.js'] } }, jshint: { options: { jshintrc : '.jshintrc' }, - lib : ['lib/**/*.js'] + lib : ['lib/**/*.js'], + test : ['test/**/*.spec.js'] }, watch: { src: { @@ -32,9 +35,7 @@ module.exports = function(grunt) { } }); - // Default task. - grunt.registerTask('test', ['jshint', 'nodeunit']); + grunt.registerTask('test', ['jshint', 'mochaTest']); grunt.registerTask('dev', ['deps-ok', 'watch']); grunt.registerTask('default', ['test']); - }; diff --git a/package.json b/package.json index 514917b..cfd2a58 100644 --- a/package.json +++ b/package.json @@ -32,12 +32,14 @@ "xregexp": "^2.0.0" }, "devDependencies": { + "chai": "^3.0.0", "grunt": "~0.4.1", "grunt-contrib-jshint": "~0.11.0", - "grunt-contrib-nodeunit": "~0.4.0", "grunt-contrib-watch": "~0.6.1", "grunt-deps-ok": "^0.8.0", - "load-grunt-tasks": "~3.2.0" + "grunt-mocha-test": "^0.12.7", + "load-grunt-tasks": "~3.2.0", + "mocha": "^2.2.5" }, "keywords": [ "directive", diff --git a/test/context.spec.js b/test/context.spec.js new file mode 100644 index 0000000..4c23f24 --- /dev/null +++ b/test/context.spec.js @@ -0,0 +1,40 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('preprocess context', function () { + var input; + + it('default to env if not provided', function () { + input = "ac"; + process.env.FINGERPRINT = '0xDEADBEEF'; + + pp.preprocess(input).should.equal("a0xDEADBEEFc"); + }); + + describe('in nested cases', function () { + var context = {'FOO': {'BAR': 'test'}}; + + it('and resolve path-s and echo content to nested attrs', function () { + input = "// @echo FOO.BAR"; + pp.preprocess(input, context, 'js').should.equal("test"); + }); + + it('and maintain backwards compatibility', function () { + input = "// @echo FOO"; + pp.preprocess(input, context, 'js').should.equal("[object Object]"); + }); + + it('and be able to compare nested context attrs', function () { + input = "a\n" + + "// @if FOO.BAR=='test' \n" + + "b\n" + + "// @endif \n" + + "c"; + pp.preprocess(input, context, 'js').should.equal("a\nb\nc"); + }); + }); +}); \ No newline at end of file diff --git a/test/echo.spec.js b/test/echo.spec.js new file mode 100644 index 0000000..8d9242d --- /dev/null +++ b/test/echo.spec.js @@ -0,0 +1,97 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@extend directive shall be preprocessed', function () { + var input; + + describe('in html', function () { + it('and resolve and echo variables', function () { + input = "ac"; + pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}).should.equal("a0xDEADBEEFc"); + }); + + it('and echo strings', function () { + input = "ac"; + pp.preprocess(input).should.equal("a-FOO*c"); + }); + }); + + describe('in javascript', function () { + it('and resolve and echo variables (block)', function () { + input = "a/* @echo FINGERPRINT */c"; + pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'js').should.equal("a0xDEADBEEFc"); + }); + + it('and echo strings (block)', function () { + input = "a/* @echo '-FOO*' */c"; + pp.preprocess(input, {}, 'js').should.equal("a-FOO*c"); + }); + + it('and resolve and echo variables (line)', function () { + input = "a\n// @echo FINGERPRINT\nc"; + pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'js').should.equal("a\n0xDEADBEEF\nc"); + }); + + it('and echo strings (line)', function () { + input = "a\n// @echo '-FOO*'\nc"; + pp.preprocess(input, {}, 'js').should.equal("a\n-FOO*\nc"); + }); + }); + + describe('in plain text files', function () { + it('and resolve and echo variables', function () { + input = "a\n@echo FINGERPRINT\nc"; + pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'simple').should.equal("a\n0xDEADBEEF\nc"); + }); + + it('and echo strings', function () { + input = "a\n@echo '-FOO*'\nc"; + pp.preprocess(input, {}, 'simple').should.equal("a\n-FOO*\nc"); + }); + }); + + describe('in coffeescript', function () { + it('and resolve and echo variables', function () { + input = "a\n# @echo FINGERPRINT\nc"; + pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'coffee').should.equal("a\n0xDEADBEEF\nc"); + }); + + it('and resolve and echo variables (multiple hashes)', function () { + input = "a\n## @echo FINGERPRINT\nc"; + pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'coffee').should.equal("a\n0xDEADBEEF\nc"); + }); + + it('and echo strings', function () { + input = "a\n# @echo '-FOO*'\nc"; + pp.preprocess(input, {}, 'coffee').should.equal("a\n-FOO*\nc"); + }); + }); + + describe('with multiple @echo directives inline in html/js', function () { + var input; + + it('without overreaching', function () { + input = "abc"; + pp.preprocess(input, {FOO: 1, BAR: 2}).should.equal("a1b2c"); + }); + + it('without overreaching (js)', function () { + input = "a/* @echo FOO */b/* @echo BAR */c"; + pp.preprocess(input, {FOO: 1, BAR: 2}, 'js').should.equal("a1b2c"); + }); + + it('without overreaching when string param contains `-` and `*` chars ', function () { + input = "abc"; + pp.preprocess(input, {FOO: 1, BAR: 2}).should.equal("a-*b*-c"); + }); + + it('without overreaching when string param contains `-` and `*` chars (js)', function () { + input = "a/* @echo '-*' */b/* @echo '*-' */c"; + pp.preprocess(input, {FOO: 1, BAR: 2}, 'js').should.equal("a-*b*-c"); + }); + }); +}); diff --git a/test/exclude.spec.js b/test/exclude.spec.js new file mode 100644 index 0000000..1a1e465 --- /dev/null +++ b/test/exclude.spec.js @@ -0,0 +1,56 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@exclude directive shall be preprocessed', function () { + var input; + + describe('in html', function () { + it('with default notation', function () { + input = "abc"; + pp.preprocess(input, {}).should.equal("ac"); + }); + + it('multiple excludes in one line', function () { + input = "abcde"; + pp.preprocess(input, {}).should.equal("ace"); + }); + + it('with newlines', function () { + input = "a\n\nb\n\nc"; + pp.preprocess(input, {}).should.equal("a\nc"); + }); + }); + + describe('in javascript', function () { + it('with line comments', function () { + input = "a\n// @exclude\nb\n// @endexclude\nc"; + pp.preprocess(input, {}, 'js').should.equal("a\nc"); + }); + + it('with block comments', function () { + input = "a/* @exclude */b/* @endexclude */c"; + pp.preprocess(input, {}, 'js').should.equal("ac"); + }); + + it('multiple excludes in one line', function () { + input = "a\n/* @exclude */\nb\n/* @endexclude */\nc"; + pp.preprocess(input, {}, 'js').should.equal("a\nc"); + }); + }); + + describe('in coffeescript', function () { + it('with default notation', function () { + input = "a\n# @exclude\nb\n# @endexclude\nc"; + pp.preprocess(input, {}, 'coffee').should.equal("a\nc"); + }); + + it('with multiple hashes', function () { + input = "a\n## @exclude\nb\n## @endexclude\nc"; + pp.preprocess(input, {}, 'coffee').should.equal("a\nc"); + }); + }); +}); \ No newline at end of file diff --git a/test/exec.spec.js b/test/exec.spec.js new file mode 100644 index 0000000..73b0698 --- /dev/null +++ b/test/exec.spec.js @@ -0,0 +1,121 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@exec directive shall be preprocessed', function () { + var input; + + function hello(expectedParamNumber) { + (arguments.length - 1).should.equal(expectedParamNumber); + + var names = Array.prototype.slice.call(arguments, 1); + + return 'Hello ' + names.join() + '!'; + } + + describe('in html', function () { + it('and execute function with one parameter', function () { + input = "ac"; + pp.preprocess(input, {hello: hello.bind(null, 1)}).should.equal("aHello Chuck Norris!c"); + }); + + it('and execute function with two parameters', function () { + input = "ac"; + pp.preprocess(input, {hello: hello.bind(null, 2)}).should.equal("aHello Chuck Norris,Gandhi!c"); + }); + + it('and execute function with two parameters: one string and one variable', function () { + input = "ac"; + pp.preprocess(input, { + hello: hello.bind(null, 2), + buddy: 'Michael Jackson' + }).should.equal("aHello Chuck Norris,Michael Jackson!c"); + }); + }); + + describe('in javascript', function () { + it('and execute function with one parameter (block)', function () { + input = "a/* @exec hello('Chuck Norris') */c"; + pp.preprocess(input, {hello: hello.bind(null, 1)}, 'js').should.equal("aHello Chuck Norris!c"); + }); + + it('and execute function with two parameters (block)', function () { + input = "a/* @exec hello(\"Chuck Norris\", 'Gandhi') */c"; + pp.preprocess(input, {hello: hello.bind(null, 2)}, 'js').should.equal("aHello Chuck Norris,Gandhi!c"); + }); + + it('and execute function with two parameters: one string and one variable (block)', function () { + input = "a/* @exec hello(\"Chuck Norris\", buddy) */c"; + pp.preprocess(input, { + hello: hello.bind(null, 2), + buddy: 'Michael Jackson' + }, 'js').should.equal("aHello Chuck Norris,Michael Jackson!c"); + }); + + it('and execute function with one parameter (line)', function () { + input = "a\n// @exec hello('Chuck Norris')\nc"; + pp.preprocess(input, {hello: hello.bind(null, 1)}, 'js').should.equal("a\nHello Chuck Norris!\nc"); + }); + + it('and execute function with two parameters (line)', function () { + input = "a\n// @exec hello(\"Chuck Norris\", 'Gandhi')\nc"; + pp.preprocess(input, {hello: hello.bind(null, 2)}, 'js').should.equal("a\nHello Chuck Norris,Gandhi!\nc"); + }); + + it('and execute function with two parameters: one string and one variable (line)', function () { + input = "a\n// @exec hello(\"Chuck Norris\", buddy)\nc"; + pp.preprocess(input, { + hello: hello.bind(null, 2), + buddy: 'Michael Jackson' + }, 'js').should.equal("a\nHello Chuck Norris,Michael Jackson!\nc"); + }); + }); + + describe('in plain text files', function () { + it('and execute function with one parameter', function () { + input = "a\n@exec hello('Chuck Norris')\nc"; + pp.preprocess(input, {hello: hello.bind(null, 1)}, 'simple').should.equal("a\nHello Chuck Norris!\nc"); + }); + + it('and execute function with two parameters', function () { + input = "a\n@exec hello(\"Chuck Norris\", 'Gandhi')\nc"; + pp.preprocess(input, {hello: hello.bind(null, 2)}, 'simple').should.equal("a\nHello Chuck Norris,Gandhi!\nc"); + }); + + it('and execute function with two parameters: one string and one variable', function () { + input = "a\n@exec hello(\"Chuck Norris\", buddy)\nc"; + pp.preprocess(input, { + hello: hello.bind(null, 2), + buddy: 'Michael Jackson' + }, 'simple').should.equal("a\nHello Chuck Norris,Michael Jackson!\nc"); + }); + }); + + describe('in coffeescript', function () { + it('and execute function with one parameter', function () { + input = "a\n# @exec hello('Chuck Norris')\nc"; + pp.preprocess(input, {hello: hello.bind(null, 1)}, 'coffee').should.equal("a\nHello Chuck Norris!\nc"); + }); + + it('and execute function with two parameters', function () { + input = "a\n# @exec hello(\"Chuck Norris\", 'Gandhi')\nc"; + pp.preprocess(input, {hello: hello.bind(null, 2)}, 'coffee').should.equal("a\nHello Chuck Norris,Gandhi!\nc"); + }); + + it('and execute function with two parameters (multiple hashes)', function () { + input = "a\n## @exec hello(\"Chuck Norris\", 'Gandhi')\nc"; + pp.preprocess(input, {hello: hello.bind(null, 2)}, 'coffee').should.equal("a\nHello Chuck Norris,Gandhi!\nc"); + }); + + it('and execute function with two parameters: one string and one variable', function () { + input = "a\n# @exec hello(\"Chuck Norris\", buddy)\nc"; + pp.preprocess(input, { + hello: hello.bind(null, 2), + buddy: 'Michael Jackson' + }, 'coffee').should.equal("a\nHello Chuck Norris,Michael Jackson!\nc"); + }); + }); +}); \ No newline at end of file diff --git a/test/extend.spec.js b/test/extend.spec.js new file mode 100644 index 0000000..2d86443 --- /dev/null +++ b/test/extend.spec.js @@ -0,0 +1,71 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@extend directive shall be preprocessed', function () { + var input; + + describe('in html', function () { + it('and extend files', function () { + input = "qr"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}).should.equal("aqrb"); + }); + + it('and support nested @extend', function () { + input = "qxr"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}).should.equal("aqaxbrb"); + }); + + it('and extend files with multiple @extend-s in one line', function () { + input = "xqryhiz"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}).should.equal("xaqrbyahibz"); + }); + + it('and should strip newlines from inserted content', function () { + input = "\nqa\n"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}).should.equal("aqab"); + }); + }); + + describe('in javascript', function () { + it('and extend files (block)', function () { + input = "/* @extend extend.js */qr/* @endextend */"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}, 'js').should.equal("aqrb"); + }); + + it('and extend files with multiple @extend-s in one line (block)', function () { + input = "x/* @extend extend.js */qr/* @endextend */y/* @extend extend.js */hi/* @endextend */z"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}, 'js').should.equal("xaqrbyahibz"); + }); + + it('and should strip newlines from inserted content (block)', function () { + input = "/* @extend extend.js */\nqa\n/* @endextend */"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}, 'js').should.equal("aqab"); + }); + + it('and should strip newlines from inserted content (line)', function () { + input = "// @extend extend.js\nqr\n// @endextend"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}, 'js').should.equal("aqrb"); + }); + }); + + describe('in coffeescript', function () { + it('and extend files', function () { + input = "# @extend extend.coffee\nqr\n# @endextend"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}, 'coffee').should.equal("a\nqr\nb"); + }); + + it('and should strip newlines from inserted content', function () { + input = "# @extend extend.coffee\nqr\n# @endextend"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}, 'coffee').should.equal("a\nqr\nb"); + }); + + it('and should strip newlines from inserted content (multiple hashes)', function () { + input = "## @extend extend.coffee\nqr\n## @endextend"; + pp.preprocess(input, {srcDir: 'test/fixtures/extend'}, 'coffee').should.equal("a\nqr\nb"); + }); + }); +}); \ No newline at end of file diff --git a/test/extend.coffee b/test/fixtures/extend/extend.coffee similarity index 100% rename from test/extend.coffee rename to test/fixtures/extend/extend.coffee diff --git a/test/extend.html b/test/fixtures/extend/extend.html similarity index 100% rename from test/extend.html rename to test/fixtures/extend/extend.html diff --git a/test/extend.js b/test/fixtures/extend/extend.js similarity index 100% rename from test/extend.js rename to test/fixtures/extend/extend.js diff --git a/test/extendadv.html b/test/fixtures/extend/extendadv.html similarity index 100% rename from test/extendadv.html rename to test/fixtures/extend/extendadv.html diff --git a/test/include.block.js b/test/fixtures/include/include.block.js similarity index 100% rename from test/include.block.js rename to test/fixtures/include/include.block.js diff --git a/test/include.coffee b/test/fixtures/include/include.coffee similarity index 100% rename from test/include.coffee rename to test/fixtures/include/include.coffee diff --git a/test/include.html b/test/fixtures/include/include.html similarity index 100% rename from test/include.html rename to test/fixtures/include/include.html diff --git a/test/include.js b/test/fixtures/include/include.js similarity index 100% rename from test/include.js rename to test/fixtures/include/include.js diff --git a/test/include.txt b/test/fixtures/include/include.txt similarity index 100% rename from test/include.txt rename to test/fixtures/include/include.txt diff --git a/test/includenewline.txt b/test/fixtures/include/includenewline.txt similarity index 100% rename from test/includenewline.txt rename to test/fixtures/include/includenewline.txt diff --git a/test/static.txt b/test/fixtures/include/static.txt similarity index 100% rename from test/static.txt rename to test/fixtures/include/static.txt diff --git a/test/fixtures/processFileSyncTest.html b/test/fixtures/processFile/processFileSyncTest.html similarity index 100% rename from test/fixtures/processFileSyncTest.html rename to test/fixtures/processFile/processFileSyncTest.html diff --git a/test/fixtures/processFileTest.html b/test/fixtures/processFile/processFileTest.html similarity index 100% rename from test/fixtures/processFileTest.html rename to test/fixtures/processFile/processFileTest.html diff --git a/test/foreach.spec.js b/test/foreach.spec.js new file mode 100644 index 0000000..1eb777d --- /dev/null +++ b/test/foreach.spec.js @@ -0,0 +1,192 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@foreach directive shall be preprocessed', function () { + var input; + + describe('with array as input', function () { + describe('in html', function () { + it('and run basic loop with one item (array toString() form)', function () { + input = "$ITEM"; + pp.preprocess(input, {LIST: ['a'].toString()}).should.equal("a"); + }); + + it('and run basic loop with multiple items (array toString() form)', function () { + input = "$ITEM"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}).should.equal("ab"); + }); + + it('and run basic loop (JSON-like array form)', function () { + input = "$ITEM"; + pp.preprocess(input, {LIST: "['a','b']"}).should.equal("ab"); + }); + }); + + describe('in javascript', function () { + it('and run basic loop with multiple items (array toString() form, block)', function () { + input = "/* @foreach $ITEM in LIST */$ITEM/* @endfor */"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'js').should.equal("ab"); + }); + + it('and run basic loop with multiple items (array toString() form, line)', function () { + input = "// @foreach $ITEM in LIST\n$ITEM\n// @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'js').should.equal("a\nb\n"); + }); + }); + + describe('in coffeescript', function () { + it('and run basic loop with multiple items (array toString() form)', function () { + input = "# @foreach $ITEM in LIST\n$ITEM\n# @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'coffee').should.equal("a\nb\n"); + }); + + it('and run basic loop with multiple items (array toString() form, multiple hashes)', function () { + input = "## @foreach $ITEM in LIST\n$ITEM\n## @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'coffee').should.equal("a\nb\n"); + }); + }); + + it('and support nested @foreach', function () { + input = "/* @foreach $ITEMA in LIST *//* @foreach $ITEMB in LIST */$ITEMA$ITEMB/* @endfor *//* @endfor */"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'js').should.equal("aaabbabb"); + }); + }); + + describe('with object as input', function () { + describe('in html', function () { + it('and run basic loop just repeating content', function () { + input = "ab"; + pp.preprocess(input, {LIST: '{"itemOne": "a", "itemTwo": "b"}'}).should.equal("abab"); + }); + + it('and run basic loop with multiple items', function () { + input = "$ITEM"; + pp.preprocess(input, {LIST: '{"itemOne": "a", "itemTwo": "b"}'}).should.equal("ab"); + }); + }); + + describe('in javascript', function () { + it('and run basic loop with multiple items (block)', function () { + input = "/* @foreach $ITEM in LIST */$ITEM/* @endfor */"; + pp.preprocess(input, {LIST: '{"itemOne": "a", "itemTwo": "b"}'}, 'js').should.equal("ab"); + }); + + it('and run basic loop with multiple items (line)', function () { + input = "// @foreach $ITEM in LIST\n$ITEM\n// @endfor"; + pp.preprocess(input, {LIST: '{"itemOne": "a", "itemTwo": "b"}'}, 'js').should.equal("a\nb\n"); + }); + }); + + describe('in coffeescript', function () { + it('and run basic loop with multiple items', function () { + input = "# @foreach $ITEM in LIST\n$ITEM\n# @endfor"; + pp.preprocess(input, {LIST: '{"itemOne": "a", "itemTwo": "b"}'}, 'coffee').should.equal("a\nb\n"); + }); + + it('and run basic loop with multiple items (multiple hashes)', function () { + input = "## @foreach $ITEM in LIST\n$ITEM\n## @endfor"; + pp.preprocess(input, {LIST: '{"itemOne": "a", "itemTwo": "b"}'}, 'coffee').should.equal("a\nb\n"); + }); + }); + }); + + describe('with mixed input', function () { + describe('in html', function () { + it('duplicate loop with @echo variable included in each', function () { + input = "
$ITEM
"; + pp.preprocess(input, { + LIST: ['a', 'b'].toString(), + LIST_CLASS: 'list' + }).should.equal("
a
b
"); + }); + + it('loop with @ifdef', function () { + input = "a$ITEM"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}).should.equal("aa"); + }); + + it('loop with @ifndef', function () { + input = "a$ITEM"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}).should.equal("aaab"); + }); + }); + + describe('in javascript', function () { + it('duplicate loop with @echo variable included in each (block)', function () { + input = "/* @foreach $ITEM in LIST *//* @echo LIST_CLASS */$ITEM/* @endfor */"; + pp.preprocess(input, {LIST: ['a', 'b'].toString(), LIST_CLASS: 'list'}, 'js').should.equal("listalistb"); + }); + + it('loop with @ifdef (block)', function () { + input = "/* @foreach $ITEM in LIST */a/* @ifdef NOVAR */$ITEM/* @endif *//* @endfor */"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'js').should.equal("aa"); + }); + + it('loop with @ifndef (block)', function () { + input = "/* @foreach $ITEM in LIST */a/* @ifndef NOVAR */$ITEM/* @endif *//* @endfor */"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'js').should.equal("aaab"); + }); + + it('duplicate loop with @echo variable included in each (line)', function () { + input = "// @foreach $ITEM in LIST\n// @echo LIST_CLASS\n$ITEM\n// @endfor"; + pp.preprocess(input, { + LIST: ['a', 'b'].toString(), + LIST_CLASS: 'list' + }, 'js').should.equal("list\na\nlist\nb\n"); + }); + + it('loop with @ifdef (line)', function () { + input = "// @foreach $ITEM in LIST\na\n// @ifdef NOVAR\n$ITEM\n// @endif\n// @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'js').should.equal("a\na\n"); + }); + + it('loop with @ifndef (line)', function () { + input = "// @foreach $ITEM in LIST\na\n// @ifndef NOVAR\n$ITEM\n// @endif\n// @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'js').should.equal("a\na\na\nb\n"); + }); + }); + + describe('in coffeescript', function () { + it('duplicate loop with @echo variable included in each', function () { + input = "# @foreach $ITEM in LIST\n# @echo LIST_CLASS\n$ITEM\n# @endfor"; + pp.preprocess(input, { + LIST: ['a', 'b'].toString(), + LIST_CLASS: 'list' + }, 'coffee').should.equal("list\na\nlist\nb\n"); + }); + + it('duplicate loop with @echo variable included in each (multiple hashes)', function () { + input = "## @foreach $ITEM in LIST\n## @echo LIST_CLASS\n$ITEM\n## @endfor"; + pp.preprocess(input, { + LIST: ['a', 'b'].toString(), + LIST_CLASS: 'list' + }, 'coffee').should.equal("list\na\nlist\nb\n"); + }); + + it('loop with @ifdef', function () { + input = "# @foreach $ITEM in LIST\na\n# @ifdef NOVAR\n$ITEM\n# @endif\n# @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'coffee').should.equal("a\na\n"); + }); + + it('loop with @ifdef (multiple hashes)', function () { + input = "## @foreach $ITEM in LIST\na\n## @ifdef NOVAR\n$ITEM\n## @endif\n## @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'coffee').should.equal("a\na\n"); + }); + + it('loop with @ifndef', function () { + input = "# @foreach $ITEM in LIST\na\n# @ifndef NOVAR\n$ITEM\n# @endif\n# @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'coffee').should.equal("a\na\na\nb\n"); + }); + + it('loop with @ifndef (multiple hashes)', function () { + input = "## @foreach $ITEM in LIST\na\n## @ifndef NOVAR\n$ITEM\n## @endif\n## @endfor"; + pp.preprocess(input, {LIST: ['a', 'b'].toString()}, 'coffee').should.equal("a\na\na\nb\n"); + }); + }); + }); +}); + diff --git a/test/hidden-by-default.spec.js b/test/hidden-by-default.spec.js new file mode 100644 index 0000000..83f2f7c --- /dev/null +++ b/test/hidden-by-default.spec.js @@ -0,0 +1,122 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('hidden by default comment blocks shall be preprocessed', function () { + var input; + + function hello(expectedParamNumber) { + (arguments.length - 1).should.equal(expectedParamNumber); + + var names = Array.prototype.slice.call(arguments, 1); + + return 'Hello ' + names.join() + '!'; + } + + describe('in html', function () { + it('and process @if and exec nested @echo', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'html').should.equal("a bc"); + }); + + it('and process @if and exec nested @exec', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev', hello: hello.bind(null, 1)}, 'html').should.equal("a Hello b!c"); + }); + + it('and process @if and exec nested @include', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/include'}, 'html').should.equal("a !bazqux!c"); + }); + + it('and process @if and exec nested @include-static', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/include'}, 'html').should.equal("a !bazqux!c"); + }); + + it('and process @if and exec nested @exclude', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'html').should.equal("ac"); + }); + + it('and process @if and exec nested @extend', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/extend'}, 'html').should.equal("aacbc"); + }); + + it('and process @extend and exec nested @if', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/extend'}, 'html').should.equal("aacbc"); + }); + + it('and process @extend and exec nested @ifdef', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/extend'}, 'html').should.equal("aacbc"); + }); + + it('and process @extend and exec nested @ifndef', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/extend'}, 'html').should.equal("aabc"); + }); + + it('and process @if and exec nested @extend', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev', ARR: "['b', 'c']"}, 'html').should.equal("abcc"); + }); + }); + + describe('in javascript', function () { + it('and process @if and exec nested @echo', function () { + input = "a/* @if NODE_ENV=='dev' ** /* @echo 'b' ** /* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("a bc"); + }); + + it('and process @if and exec nested @exec', function () { + input = "a/* @if NODE_ENV=='dev' ** /* @exec hello('b') ** /* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev', hello: hello.bind(null, 1)}, 'js').should.equal("a Hello b!c"); + }); + + it('and process @if and exec nested @include', function () { + input = "a/* @if NODE_ENV=='dev' ** /* @include static.txt ** /* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/include'}, 'js').should.equal("a !bazqux!c"); + }); + + it('and process @if and exec nested @include-static', function () { + input = "a/* @if NODE_ENV=='dev' ** /* @include-static static.txt ** /* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/include'}, 'js').should.equal("a !bazqux!c"); + }); + + it('and process @if and exec nested @exclude', function () { + input = "a/* @if NODE_ENV=='dev' ** /* @exclude ** b /* @endexclude ** /* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("ac"); + }); + + it('and process @if and exec nested @extend', function () { + input = "a/* @if NODE_ENV=='dev' ** /* @extend extend.js **c/* @endextend ** /* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/extend'}, 'js').should.equal("aacbc"); + }); + + it('and process @extend and exec nested @if', function () { + input = "a/* @extend extend.js ** /* @if NODE_ENV=='dev' **c/* @endif ** /* @endextend */c"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/extend'}, 'js').should.equal("aacbc"); + }); + + it('and process @extend and exec nested @ifdef', function () { + input = "a/* @extend extend.js ** /* @ifdef NODE_ENV **c/* @endif ** /* @endextend */c"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/extend'}, 'js').should.equal("aacbc"); + }); + + it('and process @extend and exec nested @ifndef', function () { + input = "a/* @extend extend.js ** /* @ifndef NODE_ENV **c/* @endif ** /* @endextend */c"; + pp.preprocess(input, {NODE_ENV: 'dev', srcDir: 'test/fixtures/extend'}, 'js').should.equal("aabc"); + }); + + it('and process @if and exec nested @extend', function () { + input = "a/* @if NODE_ENV=='dev' ** /* @foreach $var in ARR **$var/* @endfor ** /* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev', ARR: "['b', 'c']"}, 'js').should.equal("abcc"); + }); + }); +}); \ No newline at end of file diff --git a/test/if.spec.js b/test/if.spec.js new file mode 100644 index 0000000..c2da16f --- /dev/null +++ b/test/if.spec.js @@ -0,0 +1,332 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@if directive shall be preprocessed', function () { + describe('in html', function () { + + ['!=', '!=='].forEach(function (unEqualsOp) { + describe('and should work with unequality operator `' + unEqualsOp + '`', function () { + describe('with common comment syntax', function () { + var input = "a\n" + + "\n" + + "b\n" + + "\n" + + "c"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("a\nc"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("a\nb\nc"); + }); + }); + + + describe('with comment hidden by default syntax (end tag)', function () { + var input = "a\n" + + "\n" + + "c"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("a\nc"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("a\nb\nc"); + }); + }); + + describe('with comment hidden by default syntax (end tag and start tag)', function () { + var input = "a\n" + + "\n" + + "c"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("a\nc"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("a\nb\nc"); + }); + }); + }); + }); + + ['=', '==', '==='].forEach(function (equalsOp) { + describe('and should work with equality operator `' + equalsOp + '`', function () { + describe('with common comment syntax', function () { + var input = "a\n" + + "\n" + + "b\n" + + "\n" + + "c"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("a\nc"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("a\nb\nc"); + }); + }); + + describe('with comment hidden by default syntax (end tag)', function () { + var input = "a\n" + + "\n" + + "c"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("a\nc"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("a\nb\nc"); + }); + }); + + describe('with comment hidden by default syntax (end tag and start tag)', function () { + var input = "a\n" + + "\n" + + "c"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("a\nc"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("a\nb\nc"); + }); + }); + }); + }); + }); + + describe('in javascript', function () { + describe('and should work with unequality operator', function () { + var input = "a\n" + + "// @if NODE_ENV!='production' \n" + + "b\n" + + "// @endif \n" + + "c"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'production'}, 'js').should.equal("a\nc"); + }); + + it('and not exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("a\nb\nc"); + }); + }); + + describe('and should work with equality operator', function () { + var input = "a\n" + + "// @if NODE_ENV=='production'\n" + + "b\n" + + "// @endif\n" + + "c"; + + it('and not exclude when condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'production'}, 'js').should.equal("a\nb\nc"); + }); + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("a\nc"); + }); + }); + + it("should exclude if not match (block)", function () { + var input = "a/* @if NODE_ENV=='production' */b/* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("ac"); + }); + + it('should not exclude if match (hidden by default syntax)', function () { + var input = "a/* @if NODE_ENV=='production' **b/* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'production'}, 'js').should.equal("abc"); + }); + + it('should support nesting (block)', function () { + var input = "a/* @if true */b/* @if false */bad/* @endif */c/* @if true */d/* @endif */e/* @endif */f"; + pp.preprocess(input, {}, 'js').should.equal("abcdef"); + }); + + it('should support nesting (line)', function () { + var input = "a\n" + + "// @if true\n" + + "b\n" + + "// @if false\n" + + "bad\n" + + "// @endif\n" + + "c\n" + + "// @if true\n" + + "d\n" + + "// @endif\n" + + "e\n" + + "// @endif\n" + + "f"; + pp.preprocess(input, {}, 'js').should.equal("a\nb\nc\nd\ne\nf"); + }); + }); + + describe('in coffeescript', function () { + describe('and should work with unequality operator', function () { + var input = "a\n" + + "# @if NODE_ENV!='production'\n" + + "b\n" + + "# @endif \n" + + "c"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'production'}, 'coffee').should.equal("a\nc"); + }); + + it('and not exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}, 'coffee').should.equal("a\nb\nc"); + }); + }); + + describe('and should work with equality operator', function () { + var input = "a\n" + + "# @if NODE_ENV=='production'\n" + + "b\n" + + "# @endif\n" + + "c"; + + it('and not exclude when condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'production'}, 'coffee').should.equal("a\nb\nc"); + }); + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}, 'coffee').should.equal("a\nc"); + }); + }); + + it('and should not overreach upon matching', function () { + var input = + " ## @if NODE_ENV == 'development'\n" + + " host = 'localhost'\n" + + " protocol = 'http'\n" + + " port = 3001\n" + + " ## @endif\n" + + "\n" + + " console.log \"Socket info\", protocol, host, port"; + var expected = + " host = 'localhost'\n" + + " protocol = 'http'\n" + + " port = 3001\n" + + "\n" + + " console.log \"Socket info\", protocol, host, port"; + pp.preprocess(input, {NODE_ENV: 'development'}, 'coffee').should.equal(expected); + }); + }); + + describe('in same line in html', function () { + describe('and should work with unequality operator', function () { + describe('with common comment syntax', function () { + var input = "abc"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("ac"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("abc"); + }); + }); + + describe('with comment hidden by default syntax (end tag)', function () { + var input = "ac"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("ac"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("abc"); + }); + }); + + describe('with comment hidden by default syntax (end tag and start tag)', function () { + var input = "ac"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("ac"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("abc"); + }); + }); + }); + + describe('and should work with equality operator', function () { + describe('with common comment syntax', function () { + var input = "abc"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("ac"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("abc"); + }); + }); + + describe('with comment hidden by default syntax (end tag)', function () { + var input = "ac"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("ac"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("abc"); + }); + }); + + describe('with comment hidden by default syntax (end tag and start tag)', function () { + var input = "ac"; + + it('and exclude when condition evals to false', function () { + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("ac"); + }); + + it('and not exclude if condition evals to true', function () { + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("abc"); + }); + }); + }); + }); + + describe('sequentially following each other in html', function () { + it('2 in sequence in common comment syntax', function () { + var input = "abc" + + "def"; + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("abcdef"); + }); + + it('2 in sequence in hidden by default syntax (end tag)', function () { + var input = "ac" + + "df"; + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("abcdef"); + }); + + it('2 in sequence in hidden by default syntax (end tag and start tag)', function () { + var input = "ac" + + "df"; + pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("abcdef"); + }); + }); +}); \ No newline at end of file diff --git a/test/ifdef.spec.js b/test/ifdef.spec.js new file mode 100644 index 0000000..de1beee --- /dev/null +++ b/test/ifdef.spec.js @@ -0,0 +1,114 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@ifdef directive shall be preprocessed', function () { + var input; + + describe('in html', function () { + it('fail case, should not be included', function () { + input = "abc"; + pp.preprocess(input, {}).should.equal("ac"); + }); + + it('success case, should be included', function () { + input = "abc"; + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("abc"); + }); + + it('fail case, should not be included (hidden by default syntax, end tag)', function () { + input = "ac"; + pp.preprocess(input, {}).should.equal("ac"); + }); + + it('success case, should be included (hidden by default syntax, end tag)', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("abc"); + }); + + it('fail case, should not be included (hidden by default syntax, end tag and start tag)', function () { + input = "ac"; + pp.preprocess(input, {}).should.equal("ac"); + }); + + it('success case, should be included (hidden by default syntax, end tag and start tag)', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("abc"); + }); + }); + + describe('in javascript', function () { + it('fail case, should not be included (line)', function () { + input = "a\n" + + "// @ifdef NONEXISTANT\n" + + "b\n" + + "// @endif\n" + + "c"; + pp.preprocess(input, {}, 'js').should.equal("a\nc"); + }); + + it('success case, should be included (line)', function () { + input = "a\n" + + "// @ifdef NODE_ENV\n" + + "b\n" + + "// @endif\n" + + "c"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("a\nb\nc"); + }); + + it('fail case, should not be included (block)', function () { + input = "a/* @ifdef NONEXISTANT */b/* @endif */c"; + pp.preprocess(input, {}, 'js').should.equal("ac"); + }); + + it('success case, should be included (block)', function () { + input = "a/* @ifdef NODE_ENV */b/* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("abc"); + }); + }); + + describe('in coffeescript', function () { + it('fail case, should not be included', function () { + input = "a\n" + + "# @ifdef FLAG\n" + + "b\n" + + "# @endif\n" + + "c"; + pp.preprocess(input, {}, 'coffee').should.equal("a\nc"); + }); + + it('success case, should be included', function () { + input = "a\n" + + "# @ifdef FLAG\n" + + "b\n" + + "# @endif\n" + + "c"; + pp.preprocess(input, {FLAG: 1}, 'coffee').should.equal("a\nb\nc"); + }); + + it('fail case, should not be included (multiple hashes)', function () { + input = "a\n" + + "## @ifdef FLAG\n" + + "b\n" + + "## @endif\n" + + "c"; + pp.preprocess(input, {}, 'coffee').should.equal("a\nc"); + }); + + it('should support nesting', function () { + input = "a\n" + + "# @ifdef FLAG\n" + + "b\n" + + "# @ifdef FLAG2\n" + + "bad\n" + + "# @endif\n" + + "c\n" + + "# @endif\n" + + "d"; + pp.preprocess(input, {FLAG: 1}, 'coffee').should.equal("a\nb\nc\nd"); + }); + }); +}); \ No newline at end of file diff --git a/test/ifndef.spec.js b/test/ifndef.spec.js new file mode 100644 index 0000000..e2a3ceb --- /dev/null +++ b/test/ifndef.spec.js @@ -0,0 +1,106 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@ifndef directive shall be preprocessed', function () { + var input; + + describe('in html', function () { + it('success case, should be included', function () { + input = "abc"; + pp.preprocess(input, {}).should.equal("abc"); + }); + + it('fail case, should not be included', function () { + input = "abc"; + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("ac"); + }); + + it('success case, should be included (hidden by default syntax, end tag)', function () { + input = "ac"; + pp.preprocess(input, {}).should.equal("abc"); + }); + + it('fail case, should not be included (hidden by default syntax, end tag)', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("ac"); + }); + + it('success case, should be included (hidden by default syntax, end tag and start tag)', function () { + input = "ac"; + pp.preprocess(input, {}).should.equal("abc"); + }); + + it('fail case, should not be included (hidden by default syntax, end tag and start tag)', function () { + input = "ac"; + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("ac"); + }); + + it('should support nesting', function () { + input = "abbadcd"; + pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("abcd"); + }); + }); + + describe('in javascript', function () { + it('success case, should be included (line)', function () { + input = "a\n" + + "// @ifndef NONEXISTANT\n" + + "b\n" + + "// @endif\n" + + "c"; + pp.preprocess(input, {}, 'js').should.equal("a\nb\nc"); + }); + + it('fail case, should not be included (line)', function () { + input = "a\n" + + "// @ifndef NODE_ENV\n" + + "b\n" + + "// @endif\n" + + "c"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("a\nc"); + }); + + it('success case, should be included (block)', function () { + input = "a/* @ifndef NONEXISTANT */b/* @endif */c"; + pp.preprocess(input, {}, 'js').should.equal("abc"); + }); + + it('fail case, should not be included (block)', function () { + input = "a/* @ifndef NODE_ENV */b/* @endif */c"; + pp.preprocess(input, {NODE_ENV: 'dev'}, 'js').should.equal("ac"); + }); + }); + + describe('in coffeescript', function () { + it('success case, should be included', function () { + input = "a\n" + + "# @ifndef FLAG\n" + + "b\n" + + "# @endif\n" + + "c"; + pp.preprocess(input, {}, 'coffee').should.equal("a\nb\nc"); + }); + + it('success case, should be included (multiple hashes)', function () { + input = "a\n" + + "## @ifndef FLAG\n" + + "b\n" + + "## @endif\n" + + "c"; + pp.preprocess(input, {}, 'coffee').should.equal("a\nb\nc"); + }); + + it('fail case, should not be included', function () { + input = "a\n" + + "# @ifndef FLAG\n" + + "b\n" + + "# @endif\n" + + "c"; + pp.preprocess(input, {FLAG: 1}, 'coffee').should.equal("a\nc"); + }); + }); +}); \ No newline at end of file diff --git a/test/include-static.spec.js b/test/include-static.spec.js new file mode 100644 index 0000000..9bd62a0 --- /dev/null +++ b/test/include-static.spec.js @@ -0,0 +1,79 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@include-static directive shall be preprocessed', function () { + var input; + + describe('in html', function () { + it('and include files non-recursively', function () { + input = "ac"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}) + .should.equal("a!foobar!c"); + }); + + it('and include files and indent if ending with a newline', function () { + input = "a\n c"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}).should.equal("a\n !foobar!\n c"); + }); + }); + + describe('in javascript', function () { + it('and include files non-recursively (block)', function () { + input = "a\n /* @include-static include.block.js */c"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'js') + .should.equal("a\n !foobar!/* @exec hello('js') */\n /* @include static.txt */c"); + }); + + it('and include files and indent if ending with a newline (block)', function () { + input = "a\n /* @include-static includenewline.txt */c"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'js').should.equal("a\n !foobar!\n c"); + }); + + it('and include files non-recursively (line)', function () { + input = "a\n// @include-static include.js\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'js') + .should.equal("a\n!foobar!\n// @exec hello('js')\n// @include static.txt\nc"); + }); + + it('and include files and indent if ending with a newline (line)', function () { + input = "a\n // @include-static includenewline.txt\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'js').should.equal("a\n !foobar!\n \nc"); + }); + }); + + describe('in plain text files', function () { + it('and include files non-recursively', function () { + input = "a\n@include-static include.txt\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'simple') + .should.equal("a\n!foobar!\n@exec hello('simple')\n@include static.txt\nc"); + }); + + it('and include files and indent if ending with a newline', function () { + input = "a\n @include-static includenewline.txt\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'simple').should.equal("a\n !foobar!\n \nc"); + }); + }); + + describe('in coffeescript', function () { + it('and include files non-recursively', function () { + input = "a\n# @include-static include.coffee\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'coffee') + .should.equal("a\n!foobar!\n# @exec hello('coffee')\n# @include static.txt\nc"); + }); + + it('and include files non-recursively (multiple hashes)', function () { + input = "a\n## @include-static include.coffee\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'coffee') + .should.equal("a\n!foobar!\n# @exec hello('coffee')\n# @include static.txt\nc"); + }); + + it('and include files and indent if ending with a newline', function () { + input = "a\n # @include-static includenewline.txt\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'coffee').should.equal("a\n !foobar!\n \nc"); + }); + }); +}); \ No newline at end of file diff --git a/test/include.spec.js b/test/include.spec.js new file mode 100644 index 0000000..623c10f --- /dev/null +++ b/test/include.spec.js @@ -0,0 +1,99 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('@include directive shall be preprocessed', function () { + var input; + + function hello(expectedParamNumber) { + (arguments.length - 1).should.equal(expectedParamNumber); + + var names = Array.prototype.slice.call(arguments, 1); + + return 'Hello ' + names.join() + '!'; + } + + describe('in html', function () { + it('and include files', function () { + input = "ac"; + pp.preprocess(input, { + srcDir: 'test/fixtures/include', + hello: hello.bind(null, 1) + }).should.equal("a!foobar!Hello html!!bazqux!c"); + }); + + it('and include files and indent if ending with a newline', function () { + input = "a\n c"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}).should.equal("a\n !foobar!\n c"); + }); + }); + + describe('in javascript', function () { + it('and include files (block)', function () { + input = "a\n /* @include include.block.js */c"; + pp.preprocess(input, { + srcDir: 'test/fixtures/include', + hello: hello.bind(null, 1) + }, 'js').should.equal("a\n !foobar!Hello js!\n !bazqux!c"); + }); + + it('and include files and indent if ending with a newline (block)', function () { + input = "a\n /* @include includenewline.txt */c"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'js').should.equal("a\n !foobar!\n c"); + }); + + it('and include files (line)', function () { + input = "a\n// @include include.js\nc"; + pp.preprocess(input, { + srcDir: 'test/fixtures/include', + hello: hello.bind(null, 1) + }, 'js').should.equal("a\n!foobar!\nHello js!\n!bazqux!\nc"); + }); + + it('and include files and indent if ending with a newline (line)', function () { + input = "a\n // @include includenewline.txt\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'js').should.equal("a\n !foobar!\n \nc"); + }); + }); + + describe('in plain text files', function () { + it('and include files', function () { + input = "a\n@include include.txt\nc"; + pp.preprocess(input, { + srcDir: 'test/fixtures/include', + hello: hello.bind(null, 1) + }, 'simple').should.equal("a\n!foobar!\nHello simple!\n!bazqux!\nc"); + }); + + it('and include files and indent if ending with a newline', function () { + input = "a\n @include includenewline.txt\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'simple').should.equal("a\n !foobar!\n \nc"); + }); + }); + + describe('in coffeescript', function () { + it('and include files', function () { + input = "a\n# @include include.coffee\nc"; + pp.preprocess(input, { + srcDir: 'test/fixtures/include', + hello: hello.bind(null, 1) + }, 'coffee').should.equal("a\n!foobar!\nHello coffee!\n!bazqux!\nc"); + }); + + it('and include files (multiple hashes)', function () { + input = "a\n## @include include.coffee\nc"; + pp.preprocess(input, { + srcDir: 'test/fixtures/include', + hello: hello.bind(null, 1) + }, 'coffee').should.equal("a\n!foobar!\nHello coffee!\n!bazqux!\nc"); + }); + + it('and include files and indent if ending with a newline', function () { + input = "a\n # @include includenewline.txt\nc"; + pp.preprocess(input, {srcDir: 'test/fixtures/include'}, 'coffee').should.equal("a\n !foobar!\n \nc"); + }); + }); +}); \ No newline at end of file diff --git a/test/newlines.spec.js b/test/newlines.spec.js new file mode 100644 index 0000000..205cf1e --- /dev/null +++ b/test/newlines.spec.js @@ -0,0 +1,81 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'); + +chai.should(); + +describe('newlines shall be handled in all common formats', function () { + var input; + + function hello(expectedParamNumber) { + (arguments.length - 1).should.equal(expectedParamNumber); + + var names = Array.prototype.slice.call(arguments, 1); + + return 'Hello ' + names.join() + '!'; + } + + it('and handle \\n (Unix) style EOLs', function () { + input = "a\n" + + "\n" + + "b\n" + + "\n" + + "c"; + pp.preprocess(input, {TEST: ""}).should.equal("a\nb\nc"); + }); + + it('and handle \\r\\n (Windows) style EOLs', function () { + input = "a\r\n" + + "\r\n" + + "b\r\n" + + "\r\n" + + "c"; + pp.preprocess(input, {TEST: ""}).should.equal("a\r\nb\r\nc"); + }); + + it('and handle \\r (legacy Mac) style EOLs', function () { + input = "a\r" + + "\r" + + "b\r" + + "\r" + + "c"; + pp.preprocess(input, {TEST: ""}).should.equal("a\rb\rc"); + }); + + it('and replace EOL in sources with mixed style EOLs with the EOL from current OS', function () { + var osEol = require('os').EOL; + + input = "a\r" + + "\r\n" + + "b\n" + + "\r" + + "c"; + pp.preprocess(input, {TEST: ""}).should.equal("a" + osEol + "b" + osEol + "c"); + }); + + it('and convert EOLs from included files into EOLs from target file when using @include', function () { + // includenewline.txt has \n style EOLs + input = "a\r" + + "\r" + + "b\r"; + pp.preprocess(input, {srcDir: "test/fixtures/include"}).should.equal("a\r!foobar!\r\rb\r"); + }); + + it('and convert EOLs from included files into EOLs from target file when using @include-static', function () { + input = "a\r" + + "\r" + + "b\r"; + pp.preprocess(input, {srcDir: "test/fixtures/include"}).should.equal("a\r!foobar!\r\rb\r"); + }); + + it('and extend files and adapt all EOLs from file to be included to EOLs in target file when using @extend', function () { + // extendadv.html has \r style EOLs + input = "a\n\nqa\n\n\nc"; + pp.preprocess(input, { + srcDir: 'test/fixtures/extend', + BLUE: "red", + hello: hello.bind(null, 1) + }).should.equal("a\na\n b\n red\n Hello extend!\n qa\nc\nc"); + }); +}); \ No newline at end of file diff --git a/test/preprocess_test.js b/test/preprocess_test.js deleted file mode 100644 index 9ccab92..0000000 --- a/test/preprocess_test.js +++ /dev/null @@ -1,1182 +0,0 @@ -'use strict'; - -/* - ======== A Handy Little Nodeunit Reference ======== - https://github.com/caolan/nodeunit - - Test methods: - test.expect(numAssertions) - test.done() - Test assertions: - test.ok(value, [message]) - test.equal(actual, expected, [message]) - test.notEqual(actual, expected, [message]) - test.deepEqual(actual, expected, [message]) - test.notDeepEqual(actual, expected, [message]) - test.strictEqual(actual, expected, [message]) - test.notStrictEqual(actual, expected, [message]) - test.throws(block, [error], [message]) - test.doesNotThrow(block, [error], [message]) - test.ifError(value) -*/ - -var pp = require('../lib/preprocess'), - fs = require('fs'); - -function hello(test, expectedParamNumber) { - test.equal(arguments.length - 2, expectedParamNumber, "Correct number of params have to be passed to function."); - - var names = Array.prototype.slice.call(arguments, 2); - - return 'Hello '+ names.join() +'!'; -} - -exports['preprocess'] = { - setUp: function(done) { - // setup here - done(); - }, - 'preprocess @if in html': function(test) { - test.expect(12); - - // tests here - - var input,expected,settings; - - input = "a\n" + - "\n" + - "b\n" + - "\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should exclude if match'); - - input = "a\n" + - "\n" + - "b\n" + - "\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not exclude if not match'); - - input = "a\n" + - "\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should exclude if match (bang)'); - - input = "a\n" + - "\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not exclude if not match (bang)'); - - input = "a\n" + - "\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should exclude if match (bangbang)'); - - input = "a\n" + - "\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not exclude if not match (bangbang)'); - - input = "a\n" + - "\n" + - "b\n" + - "\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should include if match'); - - input = "a\n" + - "\n" + - "b\n" + - "\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not include if not match'); - - input = "a\n" + - "\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should include if match (bang)'); - - input = "a\n" + - "\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not include if not match (bang)'); - - input = "a\n" + - "\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should include if match (bangbang)'); - - input = "a\n" + - "\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not include if not match (bangbang)'); - - test.done(); - }, - 'preprocess multiple @echo directives inline in html/js': function(test) { - test.expect(4); - - var input,expected,settings; - - input = "abc"; - expected = "a1b2c"; - test.equal(pp.preprocess(input, { FOO: 1, BAR : 2}), expected, 'Should process without overreaching'); - - input = "a/* @echo FOO */b/* @echo BAR */c"; - expected = "a1b2c"; - test.equal(pp.preprocess(input, { FOO: 1, BAR : 2}, 'js'), expected, 'Should process without overreaching (js)'); - - input = "abc"; - expected = "a-*b*-c"; - test.equal(pp.preprocess(input), expected, 'Should process without overreaching when param contains - and * chars'); - - input = "a/* @echo '-*' */b/* @echo '*-' */c"; - expected = "a-*b*-c"; - test.equal(pp.preprocess(input, {}, 'js'), expected, 'Should process without overreaching when param contains - and * chars (js)'); - - test.done(); - }, - 'preprocess @if in javascript': function(test) { - test.expect(7); - - var input,expected; - - input = "a\n" + - "// @if NODE_ENV!='production'\n" + - "b\n" + - "// @endif \n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}, 'js'), expected, 'Should exclude if match'); - - - input = "a\n" + - "// @if NODE_ENV!='production' \n" + - "b\n" + - "// @endif \n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'js'), expected, 'Should not exclude if not match'); - - input = "a\n" + - "// @if NODE_ENV=='production'\n" + - "b\n" + - "// @endif\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}, 'js'), expected, 'Should include if match'); - - - input = "a\n" + - "// @if NODE_ENV=='production'\n" + - "b\n" + - "// @endif\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'js'), expected, 'Should not include if not match'); - - input = "a/* @if NODE_ENV=='production' */b/* @endif */c"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'js'), expected, 'Should not include if not match'); - - input = "a/* @if NODE_ENV=='production' **b/* @endif */c"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}, 'js'), expected, 'Should include if match (hidden by default syntax)'); - - input = "a/* @if true */b/* @if false */bad/* @endif */c/* @if true */d/* @endif */e/* @endif */f"; - expected = "abcdef"; - test.equal(pp.preprocess(input, {}, 'js'), expected, 'Should support nested if'); - - test.done(); - }, - 'preprocess @if in coffeescript': function(test) { - test.expect(5); - - var input, expected, settings; - - input = "a\n" + - "# @if NODE_ENV!='production'\n" + - "b\n" + - "# @endif \n"+ - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, {NODE_ENV: 'production'}, 'coffee'), expected, 'Should exclude if match'); - - input = "a\n" + - "# @if NODE_ENV!='production'\n" + - "b\n" + - "# @endif \n"+ - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'coffee'), expected, 'Should not exclude if not match'); - - input = "a\n" + - "# @if NODE_ENV=='production'\n" + - "b\n" + - "# @endif\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}, 'coffee'), expected, 'Should include if match'); - - - input = "a\n" + - "# @if NODE_ENV=='production'\n" + - "b\n" + - "# @endif\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'coffee'), expected, 'Should not include if not match'); - - input = - " ## @if NODE_ENV == 'development'\n" + - " host = 'localhost'\n" + - " protocol = 'http'\n" + - " port = 3001\n" + - " ## @endif\n" + - "\n" + - " console.log \"Socket info\", protocol, host, port"; - expected = - " host = 'localhost'\n" + - " protocol = 'http'\n" + - " port = 3001\n" + - "\n" + - " console.log \"Socket info\", protocol, host, port"; - test.equal(pp.preprocess(input, { NODE_ENV: 'development'}, 'coffee'), expected, 'Should include if match'); - - test.done(); - }, - 'preprocess @if in same line in html': function(test) { - test.expect(12); - - // tests here - - var input,expected,settings; - - input = "abc"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should exclude if match'); - - input = "abc"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not exclude if not match'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should exclude if match (bang)'); - - input = "ac"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not exclude if not match (bang)'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should exclude if match (bangbang)'); - - input = "ac"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not exclude if not match (bangbang)'); - - input = "abc"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should include if match'); - - input = "abc"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not include if not match'); - - input = "ac"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should include if match (bang)'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not include if not match (bang)'); - - input = "ac"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should include if match (bangbang)'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should not include if not match (bangbang)'); - - test.done(); - }, - 'preprocess sequential @ifs in html': function(test) { - test.expect(3); - - var input,expected,settings; - - input = "abc" + - "def"; - expected = "abcdef"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should process 2 sequential @ifs'); - - input = "ac" + - "df"; - expected = "abcdef"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should process 2 sequential @ifs (bang)'); - - input = "ac" + - "df"; - expected = "abcdef"; - test.equal(pp.preprocess(input, { NODE_ENV: 'production'}), expected, 'Should process 2 sequential @ifs (bangbang)'); - - test.done(); - }, - 'preprocess @exclude': function(test) { - test.expect(8); - - var input,expected; - - input = "abc"; - expected = "ac"; - test.equal(pp.preprocess(input, {}), expected, 'Should exclude'); - - input = "abcde"; - expected = "ace"; - test.equal(pp.preprocess(input, {}), expected, 'Should exclude multiple excludes in one line'); - - input = "a\n\nb\n\nc"; - expected = "a\nc"; - test.equal(pp.preprocess(input, {}), expected, 'Should exclude with newlines'); - - input = "a\n// @exclude\nb\n// @endexclude\nc"; - expected = "a\nc"; - test.equal(pp.preprocess(input, {}, 'js'), expected, 'Should exclude (js, line)'); - - input = "a/* @exclude */b/* @endexclude */c"; - expected = "ac"; - test.equal(pp.preprocess(input, {}, 'js'), expected, 'Should exclude (js, block)'); - - input = "a\n/* @exclude */\nb\n/* @endexclude */\nc"; - expected = "a\nc"; - test.equal(pp.preprocess(input, {}, 'js'), expected, 'Should exclude with newlines (js, block)'); - - input = "a\n# @exclude\nb\n# @endexclude\nc"; - expected = "a\nc"; - test.equal(pp.preprocess(input, {}, 'coffee'), expected, 'Should exclude (coffee)'); - - input = "a\n## @exclude\nb\n## @endexclude\nc"; - expected = "a\nc"; - test.equal(pp.preprocess(input, {}, 'coffee'), expected, 'Should exclude (coffee, multiple hashes)'); - - test.done(); - }, - 'force at least double equals for @if conditions': function(test) { - test.expect(3); - - var input,expected,settings; - - input = "abc"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Fail case, should not be included'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Fail case, should not be included (bang)'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Fail case, should not be included (bangbang)'); - - test.done(); - }, - '@ifdef': function(test) { - test.expect(12); - - var input,expected,settings; - - input = "abc"; - expected = "ac"; - test.equal(pp.preprocess(input, { }), expected, 'Fail case, should not be included'); - - input = "abc"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Success case, should be included'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { }), expected, 'Fail case, should not be included (bang)'); - - input = "ac"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Success case, should be included (bang)'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { }), expected, 'Fail case, should not be included (bangbang)'); - - input = "ac"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Success case, should be included (bangbang)'); - - input = "a/* @ifdef NONEXISTANT */b/* @endif */c"; - expected = "ac"; - test.equal(pp.preprocess(input, { },'js'), expected, 'Fail case, should not be included'); - - input = "a/* @ifdef NODE_ENV */b/* @endif */c"; - expected = "abc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'},'js'), expected, 'Success case, should be included'); - - input = - "a\n" + - "# @ifdef FLAG\n" + - "b\n" + - "# @endif\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { },'coffee'), expected, 'Fail case, should not be included (coffee)'); - - input = - "a\n" + - "## @ifdef FLAG\n" + - "b\n" + - "## @endif\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, { },'coffee'), expected, 'Fail case, should not be included (coffee, multiple hashes)'); - - input = - "a\n" + - "# @ifdef FLAG\n" + - "b\n" + - "# @endif\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, {FLAG: 1 },'coffee'), expected, 'Success case, should be included (coffee)'); - - input = - "a\n" + - "# @ifdef FLAG\n" + - "b\n" + - "# @ifdef FLAG2\n" + - "bad\n" + - "# @endif\n" + - "c\n" + - "# @endif\n" + - "d"; - expected = "a\nb\nc\nd"; - - test.equal(pp.preprocess(input, {FLAG: 1 },'coffee'), expected, 'Should support nested ifdef (coffee)'); - - test.done(); - }, - '@ifndef': function(test) { - test.expect(12); - - var input,expected,settings; - - input = "abc"; - expected = "abc"; - test.equal(pp.preprocess(input, { }), expected, 'Fail case, should not be included'); - - input = "abc"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Success case, should be included'); - - input = "ac"; - expected = "abc"; - test.equal(pp.preprocess(input, { }), expected, 'Fail case, should not be included (bang)'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Success case, should be included (bang)'); - - input = "ac"; - expected = "abc"; - test.equal(pp.preprocess(input, { }), expected, 'Fail case, should not be included (bangbang)'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Success case, should be included (bangbang)'); - - input = "a/* @ifndef NONEXISTANT */b/* @endif */c"; - expected = "abc"; - test.equal(pp.preprocess(input, { },'js'), expected, 'Fail case, should not be included'); - - input = "a/* @ifndef NODE_ENV */b/* @endif */c"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'},'js'), expected, 'Success case, should be included'); - - input = "abbadcd"; - expected = "abcd"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}), expected, 'Should support nested ifndef'); - - input = - "a\n" + - "# @ifndef FLAG\n" + - "b\n" + - "# @endif\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { },'coffee'), expected, 'Success case, should be included (coffee)'); - - input = - "a\n" + - "## @ifndef FLAG\n" + - "b\n" + - "## @endif\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, { },'coffee'), expected, 'Success case, should be included (coffee, multiple hashes)'); - - input = - "a\n" + - "# @ifndef FLAG\n" + - "b\n" + - "# @endif\n" + - "c"; - expected = "a\nc"; - test.equal(pp.preprocess(input, {FLAG: 1 },'coffee'), expected, 'Fail case, should not be included (coffee)'); - - test.done(); - }, - '@include files': function(test) { - test.expect(17); - - var input,expected; - input = "ac"; - expected = "a!foobar!Hello html!!bazqux!c"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)}), expected, 'Should include files'); - - input = "a\n c"; - expected = "a\n !foobar!\n c"; - test.equal(pp.preprocess(input, { srcDir : 'test'}), expected, 'Should include files and indent if ending with a newline'); - - input = "a\n /* @include include.block.js */c"; - expected = "a\n !foobar!Hello js!\n !bazqux!c"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'js'), expected, 'Should include files (js, block)'); - - input = "a\n /* @include includenewline.txt */c"; - expected = "a\n !foobar!\n c"; - test.equal(pp.preprocess(input, { srcDir : 'test'},'js'), expected, 'Should include files and indent if ending with a newline (js, block)'); - - input = "a\n// @include include.js\nc"; - expected = "a\n!foobar!\nHello js!\n!bazqux!\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)}, 'js'), expected, 'Should include files (js, line)'); - - input = "a\n // @include includenewline.txt\nc"; - expected = "a\n !foobar!\n \nc"; - test.equal(pp.preprocess(input, { srcDir : 'test'}, 'js'), expected, 'Should include files and indent if ending with a newline (js, line)'); - - input = "a\n@include include.txt\nc"; - expected = "a\n!foobar!\nHello simple!\n!bazqux!\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'simple'), expected, 'Should include files (simple)'); - - input = "a\n @include includenewline.txt\nc"; - expected = "a\n !foobar!\n \nc"; - test.equal(pp.preprocess(input, { srcDir : 'test'},'simple'), expected, 'Should include files and indent if ending with a newline (simple)'); - - input = "a\n# @include include.coffee\nc"; - expected = "a\n!foobar!\nHello coffee!\n!bazqux!\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'coffee'), expected, 'Should include files (coffee)'); - - input = "a\n## @include include.coffee\nc"; - expected = "a\n!foobar!\nHello coffee!\n!bazqux!\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'coffee'), expected, 'Should include files (coffee, multiple hashes)'); - - input = "a\n # @include includenewline.txt\nc"; - expected = "a\n !foobar!\n \nc"; - test.equal(pp.preprocess(input, { srcDir : 'test'},'coffee'), expected, 'Should include files and indent if ending with a newline (coffee)'); - - test.done(); - }, - '@include-static files': function(test) { - test.expect(11); - - var input,expected; - input = "ac"; - expected = "a!foobar!c"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)}), expected, 'Should include files, but not recursively'); - - input = "a\n c"; - expected = "a\n !foobar!\n c"; - test.equal(pp.preprocess(input, { srcDir : 'test'}), expected, 'Should include-static files and indent if ending with a newline, just like @include'); - - input = "a\n /* @include-static include.block.js */c"; - expected = "a\n !foobar!/* @exec hello('js') */\n /* @include static.txt */c"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'js'), expected, 'Should include files (js, block), but not recursively'); - - input = "a\n /* @include-static includenewline.txt */c"; - expected = "a\n !foobar!\n c"; - test.equal(pp.preprocess(input, { srcDir : 'test'},'js'), expected, 'Should include-static files and indent if ending with a newline (js, block), just like @include'); - - input = "a\n// @include-static include.js\nc"; - expected = "a\n!foobar!\n// @exec hello('js')\n// @include static.txt\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'js'), expected, 'Should include files (js, line), but not recursively'); - - input = "a\n // @include-static includenewline.txt\nc"; - expected = "a\n !foobar!\n \nc"; - test.equal(pp.preprocess(input, { srcDir : 'test'},'js'), expected, 'Should include-static files and indent if ending with a newline (js, line), just like @include'); - - input = "a\n@include-static include.txt\nc"; - expected = "a\n!foobar!\n@exec hello('simple')\n@include static.txt\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'simple'), expected, 'Should include files (simple), but not recursively'); - - input = "a\n @include-static includenewline.txt\nc"; - expected = "a\n !foobar!\n \nc"; - test.equal(pp.preprocess(input, { srcDir : 'test'},'simple'), expected, 'Should include files and indent if ending with a newline (simple), just like @include'); - - input = "a\n# @include-static include.coffee\nc"; - expected = "a\n!foobar!\n# @exec hello('coffee')\n# @include static.txt\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'coffee'), expected, 'Should include files (coffee), but not recursively'); - - input = "a\n## @include-static include.coffee\nc"; - expected = "a\n!foobar!\n# @exec hello('coffee')\n# @include static.txt\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', hello: hello.bind(null, test, 1)},'coffee'), expected, 'Should include files (coffee, multiple hashes), but not recursively'); - - input = "a\n # @include-static includenewline.txt\nc"; - expected = "a\n !foobar!\n \nc"; - test.equal(pp.preprocess(input, { srcDir : 'test'},'coffee'), expected, 'Should include files and indent if ending with a newline (coffee), just like @include'); - - test.done(); - }, - '@extend files': function(test) { - test.expect(10); - - var input,expected; - - input = "qr"; - expected = "aqrb"; - test.equal(pp.preprocess(input, { srcDir : 'test'}), expected, 'Should extend files'); - - input = "qxr"; - expected = "aqaxbrb"; - test.equal(pp.preprocess(input, { srcDir : 'test'}), expected, 'Should support nested extend'); - - input = "xqryhiz"; - expected = "xaqrbyahibz"; - test.equal(pp.preprocess(input, { srcDir : 'test'}), expected, 'Should extend files with multiple extends in one line'); - - input = "\nqa\n"; - expected = "aqab"; - test.equal(pp.preprocess(input, { srcDir : 'test'}), expected, 'Should extend files while stripping newlines from inserted content'); - - input = "/* @extend extend.js */qr/* @endextend */"; - expected = "aqrb"; - test.equal(pp.preprocess(input, { srcDir : 'test'}, 'js'), expected, 'Should extend files (js, block)'); - - input = "x/* @extend extend.js */qr/* @endextend */y/* @extend extend.js */hi/* @endextend */z"; - expected = "xaqrbyahibz"; - test.equal(pp.preprocess(input, { srcDir : 'test'}, 'js'), expected, 'Should extend files with multiple extends in one line (js, block)'); - - input = "/* @extend extend.js */\nqa\n/* @endextend */"; - expected = "aqab"; - test.equal(pp.preprocess(input, { srcDir : 'test'}, 'js'), expected, 'Should extend files while stripping newlines from inserted content (js, block)'); - - input = "// @extend extend.js\nqr\n// @endextend"; - expected = "aqrb"; - test.equal(pp.preprocess(input, { srcDir : 'test'}, 'js'), expected, 'Should extend files while stripping newlines from inserted content (js, line)'); - - input = "# @extend extend.coffee\nqr\n# @endextend"; - expected = "a\nqr\nb"; - test.equal(pp.preprocess(input, { srcDir : 'test'}, 'coffee'), expected, 'Should extend files while stripping newlines from inserted content (coffee)'); - - input = "## @extend extend.coffee\nqr\n## @endextend"; - expected = "a\nqr\nb"; - test.equal(pp.preprocess(input, { srcDir : 'test'}, 'coffee'), expected, 'Should extend files while stripping newlines from inserted content (coffee, multiple hashes)'); - - test.done(); - }, - '@echo': function(test) { - test.expect(11); - - var input,expected; - - input = "ac"; - expected = "a0xDEADBEEFc"; - test.equal(pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}), expected, 'Should resolve and echo variables'); - - input = "ac"; - expected = "a-FOO*c"; - test.equal(pp.preprocess(input), expected, 'Should echo strings'); - - input = "a/* @echo FINGERPRINT */c"; - expected = "a0xDEADBEEFc"; - test.equal(pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'js'), expected, 'Should resolve and echo variables (js, block)'); - - input = "a/* @echo '-FOO*' */c"; - expected = "a-FOO*c"; - test.equal(pp.preprocess(input, {}, 'js'), expected, 'Should echo strings (js, block)'); - - input = "a\n// @echo FINGERPRINT\nc"; - expected = "a\n0xDEADBEEF\nc"; - test.equal(pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'js'), expected, 'Should resolve and echo variables (js, line)'); - - input = "a\n// @echo '-FOO*'\nc"; - expected = "a\n-FOO*\nc"; - test.equal(pp.preprocess(input, {}, 'js'), expected, 'Should echo strings (js, line)'); - - input = "a\n@echo FINGERPRINT\nc"; - expected = "a\n0xDEADBEEF\nc"; - test.equal(pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'simple'), expected, 'Should resolve and echo variables (simple)'); - - input = "a\n@echo '-FOO*'\nc"; - expected = "a\n-FOO*\nc"; - test.equal(pp.preprocess(input,{},'simple'), expected, 'Should echo strings (simple)'); - - input = "a\n# @echo FINGERPRINT\nc"; - expected = "a\n0xDEADBEEF\nc"; - test.equal(pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'coffee'), expected, 'Should resolve and echo variables (coffee)'); - - input = "a\n## @echo FINGERPRINT\nc"; - expected = "a\n0xDEADBEEF\nc"; - test.equal(pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}, 'coffee'), expected, 'Should resolve and echo variables (coffee, multiple hashes)'); - - input = "a\n# @echo '-FOO*'\nc"; - expected = "a\n-FOO*\nc"; - test.equal(pp.preprocess(input,{},'coffee'), expected, 'Should echo strings (coffee)'); - - test.done(); - }, - '@foreach with array': function(test) { - test.expect(8); - - var input,expected; - - input = "$ITEM"; - expected = "a"; - test.equal(pp.preprocess(input, { LIST: ['a'].toString()}), expected, 'Should run basic loop from Array with one item'); - - input = "$ITEM"; - expected = "ab"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString()}), expected, 'Should run basic loop from stringified Array with two items'); - - input = "$ITEM"; - expected = "ab"; - test.equal(pp.preprocess(input, { LIST: "['a','b']"}), expected, 'Should run basic loop string presented formatted Array'); - - input = "/* @foreach $ITEM in LIST */$ITEM/* @endfor */"; - expected = "ab"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString()}, 'js'), expected, 'Should run basic loop string presented formatted Array (js, block)'); - - input = "// @foreach $ITEM in LIST\n$ITEM\n// @endfor"; - expected = "a\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString()}, 'js'), expected, 'Should run basic loop string presented formatted Array (js, line)'); - - input = "# @foreach $ITEM in LIST\n$ITEM\n# @endfor"; - expected = "a\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString()}, 'coffee'), expected, 'Should run basic loop string presented formatted Array (coffee)'); - - input = "## @foreach $ITEM in LIST\n$ITEM\n## @endfor"; - expected = "a\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString()}, 'coffee'), expected, 'Should run basic loop string presented formatted Array (coffee, multiple hashes)'); - - input = "/* @foreach $ITEMA in LIST *//* @foreach $ITEMB in LIST */$ITEMA$ITEMB/* @endfor *//* @endfor */"; - expected = "aaabbabb"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString()}, 'js'), expected, 'Should support nested foreach'); - - test.done(); - }, - '@foreach with object': function(test) { - test.expect(6); - - var input,expected; - - input = "ab"; - expected = "abab"; - test.equal(pp.preprocess(input, { LIST: '{"itemOne": "a", "itemTwo": "b"}'}), expected, 'Should run basic loop just repeating content'); - - input = "$ITEM"; - expected = "ab"; - test.equal(pp.preprocess(input, { LIST: '{"itemOne": "a", "itemTwo": "b"}'}), expected, 'Should run basic loop from Object with two items'); - - input = "/* @foreach $ITEM in LIST */$ITEM/* @endfor */"; - expected = "ab"; - test.equal(pp.preprocess(input, { LIST: '{"itemOne": "a", "itemTwo": "b"}'}, 'js'), expected, 'Should run basic loop from Object with two items (js, block)'); - - input = "// @foreach $ITEM in LIST\n$ITEM\n// @endfor"; - expected = "a\nb\n"; - test.equal(pp.preprocess(input, { LIST: '{"itemOne": "a", "itemTwo": "b"}'}, 'js'), expected, 'Should run basic loop from Object with two items (js, line)'); - - input = "# @foreach $ITEM in LIST\n$ITEM\n# @endfor"; - expected = "a\nb\n"; - test.equal(pp.preprocess(input, { LIST: '{"itemOne": "a", "itemTwo": "b"}'}, 'coffee'), expected, 'Should run basic loop from Object with two items (coffee)'); - - input = "## @foreach $ITEM in LIST\n$ITEM\n## @endfor"; - expected = "a\nb\n"; - test.equal(pp.preprocess(input, { LIST: '{"itemOne": "a", "itemTwo": "b"}'}, 'coffee'), expected, 'Should run basic loop from Object with two items (coffee, multiple hashes)'); - - test.done(); - }, - '@foreach mixing': function(test) { - test.expect(15); - - var input,expected; - - input = "
$ITEM
"; - expected = "
a
b
"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString(), LIST_CLASS: 'list' }), expected, 'Duplicate loop with echo variable included in each'); - - input = "a$ITEM"; - expected = "aa"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }), expected, 'Loop with ifdef'); - - input = "a$ITEM"; - expected = "aaab"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }), expected, 'Loop with ifndef'); - - input = "/* @foreach $ITEM in LIST *//* @echo LIST_CLASS */$ITEM/* @endfor */"; - expected = "listalistb"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString(), LIST_CLASS: 'list' }, 'js'), expected, 'Duplicate loop with echo variable included in each (js, block)'); - - input = "/* @foreach $ITEM in LIST */a/* @ifdef NOVAR */$ITEM/* @endif *//* @endfor */"; - expected = "aa"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }, 'js'), expected, 'Loop with ifdef (js, block)'); - - input = "/* @foreach $ITEM in LIST */a/* @ifndef NOVAR */$ITEM/* @endif *//* @endfor */"; - expected = "aaab"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }, 'js'), expected, 'Loop with ifndef (js, block)'); - - input = "// @foreach $ITEM in LIST\n// @echo LIST_CLASS\n$ITEM\n// @endfor"; - expected = "list\na\nlist\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString(), LIST_CLASS: 'list' }, 'js'), expected, 'Duplicate loop with echo variable included in each (js, line)'); - - input = "// @foreach $ITEM in LIST\na\n// @ifdef NOVAR\n$ITEM\n// @endif\n// @endfor"; - expected = "a\na\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }, 'js'), expected, 'Loop with ifdef (js, line)'); - - input = "// @foreach $ITEM in LIST\na\n// @ifndef NOVAR\n$ITEM\n// @endif\n// @endfor"; - expected = "a\na\na\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }, 'js'), expected, 'Loop with ifndef (js, line)'); - - input = "# @foreach $ITEM in LIST\n# @echo LIST_CLASS\n$ITEM\n# @endfor"; - expected = "list\na\nlist\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString(), LIST_CLASS: 'list' }, 'coffee'), expected, 'Duplicate loop with echo variable included in each (coffee)'); - - input = "## @foreach $ITEM in LIST\n## @echo LIST_CLASS\n$ITEM\n## @endfor"; - expected = "list\na\nlist\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString(), LIST_CLASS: 'list' }, 'coffee'), expected, 'Duplicate loop with echo variable included in each (coffee, multiple hashes)'); - - input = "# @foreach $ITEM in LIST\na\n# @ifdef NOVAR\n$ITEM\n# @endif\n# @endfor"; - expected = "a\na\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }, 'coffee'), expected, 'Loop with ifdef (coffee)'); - - input = "## @foreach $ITEM in LIST\na\n## @ifdef NOVAR\n$ITEM\n## @endif\n## @endfor"; - expected = "a\na\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }, 'coffee'), expected, 'Loop with ifdef (coffee, multiple hashes)'); - - input = "# @foreach $ITEM in LIST\na\n# @ifndef NOVAR\n$ITEM\n# @endif\n# @endfor"; - expected = "a\na\na\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }, 'coffee'), expected, 'Loop with ifndef (coffee)'); - - input = "## @foreach $ITEM in LIST\na\n## @ifndef NOVAR\n$ITEM\n## @endif\n## @endfor"; - expected = "a\na\na\nb\n"; - test.equal(pp.preprocess(input, { LIST: ['a','b'].toString() }, 'coffee'), expected, 'Loop with ifndef (coffee, multiple hashes)'); - - test.done(); - }, - '@exec': function(test) { - test.expect(32); - - var input,expected; - - input = "ac"; - expected = "aHello Chuck Norris!c"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 1)}), expected, 'Should execute exec statement with one parameter'); - - input = "ac"; - expected = "aHello Chuck Norris,Gandhi!c"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2)}), expected, 'Should execute exec statement with two parameters'); - - input = "ac"; - expected = "aHello Chuck Norris,Michael Jackson!c"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2), buddy: 'Michael Jackson'}), expected, 'Should execute exec statement with two parameters: one string and one variable'); - - input = "a/* @exec hello('Chuck Norris') */c"; - expected = "aHello Chuck Norris!c"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 1)}, 'js'), expected, 'Should execute exec statement with one parameter (js, block comment)'); - - input = "a/* @exec hello(\"Chuck Norris\", 'Gandhi') */c"; - expected = "aHello Chuck Norris,Gandhi!c"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2)}, 'js'), expected, 'Should execute exec statement with two parameters (js, block comment)'); - - input = "a/* @exec hello(\"Chuck Norris\", buddy) */c"; - expected = "aHello Chuck Norris,Michael Jackson!c"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2), buddy: 'Michael Jackson'}, 'js'), expected, 'Should execute exec statement with two parameters: one string and one variable (js, block comment)'); - - input = "a\n// @exec hello('Chuck Norris')\nc"; - expected = "a\nHello Chuck Norris!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 1)}, 'js'), expected, 'Should execute exec statement with one parameter (js, line comment)'); - - input = "a\n// @exec hello(\"Chuck Norris\", 'Gandhi')\nc"; - expected = "a\nHello Chuck Norris,Gandhi!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2)}, 'js'), expected, 'Should execute exec statement with two parameters (js, line comment)'); - - input = "a\n// @exec hello(\"Chuck Norris\", buddy)\nc"; - expected = "a\nHello Chuck Norris,Michael Jackson!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2), buddy: 'Michael Jackson'}, 'js'), expected, 'Should execute exec statement with two parameters: one string and one variable (js, line comment)'); - - input = "a\n@exec hello('Chuck Norris')\nc"; - expected = "a\nHello Chuck Norris!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 1)}, 'simple'), expected, 'Should execute exec statement with one parameter (simple)'); - - input = "a\n@exec hello(\"Chuck Norris\", 'Gandhi')\nc"; - expected = "a\nHello Chuck Norris,Gandhi!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2)}, 'simple'), expected, 'Should execute exec statement with two parameters (simple)'); - - input = "a\n@exec hello(\"Chuck Norris\", buddy)\nc"; - expected = "a\nHello Chuck Norris,Michael Jackson!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2), buddy: 'Michael Jackson'}, 'simple'), expected, 'Should execute exec statement with two parameters: one string and one variable (simple)'); - - input = "a\n# @exec hello('Chuck Norris')\nc"; - expected = "a\nHello Chuck Norris!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 1)}, 'coffee'), expected, 'Should execute exec statement with one parameter (coffee)'); - - input = "a\n# @exec hello(\"Chuck Norris\", 'Gandhi')\nc"; - expected = "a\nHello Chuck Norris,Gandhi!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2)}, 'coffee'), expected, 'Should execute exec statement with two parameters (coffee)'); - - input = "a\n## @exec hello(\"Chuck Norris\", 'Gandhi')\nc"; - expected = "a\nHello Chuck Norris,Gandhi!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2)}, 'coffee'), expected, 'Should execute exec statement with two parameters (coffee, multiple hashes)'); - - input = "a\n# @exec hello(\"Chuck Norris\", buddy)\nc"; - expected = "a\nHello Chuck Norris,Michael Jackson!\nc"; - test.equal(pp.preprocess(input, {hello: hello.bind(null, test, 2), buddy: 'Michael Jackson'}, 'coffee'), expected, 'Should execute exec statement with two parameters: one string and one variable (coffee)'); - - test.done(); - }, - 'preprocess html (hidden by default syntax)': function(test) { - test.expect(11); - - var input,expected; - - input = "ac"; - expected = "a bc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'html'), expected, 'Should process @if and exec nested @echo'); - - input = "ac"; - expected = "a Hello b!c"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', hello: hello.bind(null, test, 1)}, 'html'), expected, 'Should process @if and exec nested @exec'); - - input = "ac"; - expected = "a !bazqux!c"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'html'), expected, 'Should process @if and exec nested @include'); - - input = "ac"; - expected = "a !bazqux!c"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'html'), expected, 'Should process @if and exec nested @include-static'); - - input = "ac"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'html'), expected, 'Should process @if and exec nested @exclude'); - - input = "ac"; - expected = "aacbc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'html'), expected, 'Should process @if and exec nested @extend'); - - input = "ac"; - expected = "aacbc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'html'), expected, 'Should process @extend and exec nested @if'); - - input = "ac"; - expected = "aacbc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'html'), expected, 'Should process @extend and exec nested @ifdef'); - - input = "ac"; - expected = "aabc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'html'), expected, 'Should process @extend and exec nested @ifndef'); - - input = "ac"; - expected = "abcc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', ARR: "['b', 'c']"}, 'html'), expected, 'Should process @if and exec nested @extend'); - - test.done(); - }, - 'preprocess javascript (hidden by default syntax)': function(test) { - test.expect(11); - - var input,expected; - - input = "a/* @if NODE_ENV=='dev' ** /* @echo 'b' ** /* @endif */c"; - expected = "a bc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'js'), expected, 'Should process @if and exec nested @echo'); - - input = "a/* @if NODE_ENV=='dev' ** /* @exec hello('b') ** /* @endif */c"; - expected = "a Hello b!c"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', hello: hello.bind(null, test, 1)}, 'js'), expected, 'Should process @if and exec nested @exec'); - - input = "a/* @if NODE_ENV=='dev' ** /* @include static.txt ** /* @endif */c"; - expected = "a !bazqux!c"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'js'), expected, 'Should process @if and exec nested @include'); - - input = "a/* @if NODE_ENV=='dev' ** /* @include-static static.txt ** /* @endif */c"; - expected = "a !bazqux!c"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'js'), expected, 'Should process @if and exec nested @include-static'); - - input = "a/* @if NODE_ENV=='dev' ** /* @exclude ** b /* @endexclude ** /* @endif */c"; - expected = "ac"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev'}, 'js'), expected, 'Should process @if and exec nested @exclude'); - - input = "a/* @if NODE_ENV=='dev' ** /* @extend extend.js **c/* @endextend ** /* @endif */c"; - expected = "aacbc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'js'), expected, 'Should process @if and exec nested @extend'); - - input = "a/* @extend extend.js ** /* @if NODE_ENV=='dev' **c/* @endif ** /* @endextend */c"; - expected = "aacbc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'js'), expected, 'Should process @extend and exec nested @if'); - - input = "a/* @extend extend.js ** /* @ifdef NODE_ENV **c/* @endif ** /* @endextend */c"; - expected = "aacbc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'js'), expected, 'Should process @extend and exec nested @ifdef'); - - input = "a/* @extend extend.js ** /* @ifndef NODE_ENV **c/* @endif ** /* @endextend */c"; - expected = "aabc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', srcDir: 'test'}, 'js'), expected, 'Should process @extend and exec nested @ifndef'); - - input = "a/* @if NODE_ENV=='dev' ** /* @foreach $var in ARR **$var/* @endfor ** /* @endif */c"; - expected = "abcc"; - test.equal(pp.preprocess(input, { NODE_ENV: 'dev', ARR: "['b', 'c']"}, 'js'), expected, 'Should process @if and exec nested @extend'); - - test.done(); - }, - 'handle newlines in all possible formats': function(test) { - test.expect(8); - - var input,expected; - - input = "a\n" + - "\n" + - "b\n" + - "\n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, {TEST: ""}), expected, 'Should handle \\n (Unix) style EOLs'); - - input = "a\r\n" + - "\r\n" + - "b\r\n" + - "\r\n" + - "c"; - expected = "a\r\nb\r\nc"; - test.equal(pp.preprocess(input, {TEST: ""}), expected, 'Should handle \\r\\n (Windows) style EOLs'); - - input = "a\r" + - "\r" + - "b\r" + - "\r" + - "c"; - expected = "a\rb\rc"; - test.equal(pp.preprocess(input, {TEST: ""}), expected, 'Should handle \\r (legacy Mac) style EOLs'); - - var osEol = require('os').EOL; - input = "a\r" + - "\r\n" + - "b\n" + - "\r" + - "c"; - expected = "a" + osEol + "b" + osEol + "c"; - test.equal(pp.preprocess(input, {TEST: ""}), expected, 'Should replace EOL in sources with mixed style EOLs with the EOL from current OS'); - - // includenewline.txt has \n style EOLs - input = "a\r" + - "\r" + - "b\r"; - expected = "a\r!foobar!\r\rb\r"; - test.equal(pp.preprocess(input, {srcDir: "test"}), expected, 'Should convert EOLs from included files into EOLs from target file when using @include'); - - input = "a\r" + - "\r" + - "b\r"; - expected = "a\r!foobar!\r\rb\r"; - test.equal(pp.preprocess(input, {srcDir: "test"}), expected, 'Should convert EOLs from included files into EOLs from target file when using @include-static'); - - // extendadv.html has \r style EOLs - input = "a\n\nqa\n\n\nc"; - expected = "a\na\n b\n red\n Hello extend!\n qa\nc\nc"; - test.equal(pp.preprocess(input, { srcDir : 'test', BLUE: "red", hello: hello.bind(null, test, 1)}), expected, - 'Should extend files and adapt all EOLs from file to be included to EOLs in target file when using @extend'); - - test.done(); - }, - 'default to env': function(test) { - test.expect(1); - - var input,expected; - - input = "ac"; - expected = "a0xDEADBEEFc"; - process.env.FINGERPRINT = '0xDEADBEEF'; - - test.equal(pp.preprocess(input), expected, 'Should include echo statement'); - - test.done(); - }, - 'processFile': function(test) { - test.expect(1); - - var input,expected; - - expected = "a0xDEADBEEFb"; - pp.preprocessFile('test/fixtures/processFileTest.html', 'test/tmp/processFileTest.dest.html', { TEST : '0xDEADBEEF'}, function(){ - test.equal(fs.readFileSync('test/tmp/processFileTest.dest.html').toString(), expected, 'Should process a file to disk'); - - test.done(); - }) - }, - 'processFileSync': function(test) { - test.expect(1); - - var input,expected; - - expected = "aa0xDEADBEEFbb"; - pp.preprocessFileSync('test/fixtures/processFileSyncTest.html', 'test/tmp/processFileSyncTest.dest.html', { TEST : '0xDEADBEEF'}); - var actual = fs.readFileSync('test/tmp/processFileSyncTest.dest.html').toString(); - test.equal(actual, expected, 'Should process a file to disk'); - test.done(); - }, - 'multilevelContext': function(test) { - test.expect(3); - - var input,expected; - var context = {'FOO' :{'BAR':'test'}}; - - input = "// @echo FOO.BAR"; - expected = "test"; - test.equal(pp.preprocess(input, context, 'js'), expected, 'Should echo multi-level context'); - - input = "// @echo FOO"; - expected = "[object Object]"; - test.equal(pp.preprocess(input, context, 'js'), expected, 'Should maintain backwards compatibility'); - - input = "a\n" + - "// @if FOO.BAR=='test' \n" + - "b\n" + - "// @endif \n" + - "c"; - expected = "a\nb\nc"; - test.equal(pp.preprocess(input, context, 'js'), expected, 'Should compare multi-level context'); - - test.done(); - } -}; diff --git a/test/processFile.spec.js b/test/processFile.spec.js new file mode 100644 index 0000000..4b2b912 --- /dev/null +++ b/test/processFile.spec.js @@ -0,0 +1,30 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'), + fs = require('fs'); + +chai.should(); + +describe('processFile', function () { + beforeEach(function () { + if (!fs.existsSync('test/tmp')) { + fs.mkdirSync('test/tmp'); + } + }); + + it('shall preprocess files asynchronously', function (done) { + var expected = "a0xDEADBEEFb"; + + pp.preprocessFile( + 'test/fixtures/processFile/processFileTest.html', + 'test/tmp/processFileTest.dest.html', + {TEST: '0xDEADBEEF'}, + function () { + fs.readFileSync('test/tmp/processFileTest.dest.html').toString().should.equal(expected); + + done(); + } + ); + }); +}); \ No newline at end of file diff --git a/test/processFileSync.spec.js b/test/processFileSync.spec.js new file mode 100644 index 0000000..03967a0 --- /dev/null +++ b/test/processFileSync.spec.js @@ -0,0 +1,26 @@ +'use strict'; + +var chai = require('chai'), + pp = require('../lib/preprocess'), + fs = require('fs'); + +chai.should(); + +describe('processFileSync', function () { + beforeEach(function () { + if (!fs.existsSync('test/tmp')) { + fs.mkdirSync('test/tmp'); + } + }); + + it('shall preprocess files synchronously', function () { + var expected = "a0xDEADBEEFb"; + + pp.preprocessFileSync( + 'test/fixtures/processFile/processFileTest.html', + 'test/tmp/processFileTest.dest.html', + {TEST: '0xDEADBEEF'} + ); + fs.readFileSync('test/tmp/processFileTest.dest.html').toString().should.equal(expected); + }); +}); \ No newline at end of file