Skip to content

Commit

Permalink
ported nodeunit tests to mocha+chai, fixes jsoverson#69
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender committed Jun 22, 2015
1 parent 93baace commit 8c5c13d
Show file tree
Hide file tree
Showing 34 changed files with 1,585 additions and 1,194 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/
/test/tmp
.idea
*.iml
node_modules
4 changes: 3 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"undef": true,
"boss": true,
"eqnull": true,
"node": true
"strict": true,
"node": true,
"mocha": true
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/
/test/tmp
.idea
*.iml
17 changes: 9 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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']);

};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 40 additions & 0 deletions test/context.spec.js
Original file line number Diff line number Diff line change
@@ -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 = "a<!-- @echo FINGERPRINT -->c";
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");
});
});
});
97 changes: 97 additions & 0 deletions test/echo.spec.js
Original file line number Diff line number Diff line change
@@ -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 = "a<!-- @echo FINGERPRINT -->c";
pp.preprocess(input, {FINGERPRINT: '0xDEADBEEF'}).should.equal("a0xDEADBEEFc");
});

it('and echo strings', function () {
input = "a<!-- @echo '-FOO*' -->c";
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 = "a<!-- @echo FOO -->b<!-- @echo BAR -->c";
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 = "a<!-- @echo '-*' -->b<!-- @echo '*-' -->c";
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");
});
});
});
56 changes: 56 additions & 0 deletions test/exclude.spec.js
Original file line number Diff line number Diff line change
@@ -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 = "a<!-- @exclude -->b<!-- @endexclude -->c";
pp.preprocess(input, {}).should.equal("ac");
});

it('multiple excludes in one line', function () {
input = "a<!-- @exclude -->b<!-- @endexclude -->c<!-- @exclude -->d<!-- @endexclude -->e";
pp.preprocess(input, {}).should.equal("ace");
});

it('with newlines', function () {
input = "a\n<!-- @exclude -->\nb\n<!-- @endexclude -->\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");
});
});
});
121 changes: 121 additions & 0 deletions test/exec.spec.js
Original file line number Diff line number Diff line change
@@ -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 = "a<!-- @exec hello('Chuck Norris') -->c";
pp.preprocess(input, {hello: hello.bind(null, 1)}).should.equal("aHello Chuck Norris!c");
});

it('and execute function with two parameters', function () {
input = "a<!-- @exec hello(\"Chuck Norris\", 'Gandhi') -->c";
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 = "a<!-- @exec hello(\"Chuck Norris\", buddy) -->c";
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");
});
});
});
Loading

0 comments on commit 8c5c13d

Please sign in to comment.