Permalink
Browse files

Added test mocha test cases

  • Loading branch information...
1 parent 871c791 commit c0a47d0e902e755e26b0911b9a0e36b1a21a778e @vkadam committed Oct 20, 2014
View
@@ -27,6 +27,14 @@
"define",
"exports",
"module",
- "__dirname"
+ "__dirname",
+ "describe",
+ "xdescribe",
+ "before",
+ "beforeEach",
+ "after",
+ "afterEach",
+ "it",
+ "xit"
]
}
View
@@ -80,7 +80,7 @@ module.exports = function(grunt) {
}
},
dest: {
- src: ["tmp/not-been-beautified.js"],
+ src: ["tmp/common/not-been-beautified.js"],
options: {
dest: "dest"
}
@@ -96,7 +96,15 @@ module.exports = function(grunt) {
},
clean: ["tmp", "dest"],
nodeunit: {
- all: ["test/**/*.js"]
+ all: ["test/**/*_test.js"]
+ },
+ mochaTest: {
+ test: {
+ options: {
+ reporter: 'spec'
+ },
+ src: ['test/**/*_spec.js']
+ }
},
devUpdate: {
main: {
@@ -116,6 +124,6 @@ module.exports = function(grunt) {
grunt.loadTasks("tasks");
// By default, beautify, lint and run all tests.
- grunt.registerTask("test", ["jshint", "copy", "nodeunit", "clean"]);
+ grunt.registerTask("test", ["jshint", "copy", "nodeunit", "clean", "mochaTest"]);
grunt.registerTask("default", ["devUpdate", "test", "jsbeautifier:default"]);
};
View
@@ -158,14 +158,14 @@ JsBeautifierTask.prototype.run = function() {
return config;
}
- if (this.task.filesSrc && this.task.filesSrc.length > 0) {
+ var sourceFiles = this.task.files;
+ if (sourceFiles && sourceFiles.length > 0) {
if (!_.isEmpty(options.dest)) {
grunt.verbose.writeln("All beautified files will be stored under \"" + options.dest + "\" folder");
if (!stringUtils.endsWith(options.dest, "/")) {
options.dest += "/";
}
}
- grunt.verbose.writeln("Beautifying using filesSrc with " + this.task.filesSrc.length.toString().cyan + " files...");
grunt.verbose.writeln("Using mode=\"" + options.mode + "\"...");
var actionHandler = "VERIFY_ONLY" === options.mode ? verifyActionHandler : verifyAndWriteActionHandler,
@@ -191,7 +191,9 @@ JsBeautifierTask.prototype.run = function() {
done();
};
- q.push(this.task.filesSrc);
+ sourceFiles.forEach(function(fileset) {
+ q.push(fileset.src);
+ });
}
};
View
@@ -28,12 +28,15 @@
"underscore.string": ">=2.3.3"
},
"devDependencies": {
+ "chai": "^1.9.2",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-copy": "^0.7.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-dev-update": "^0.9.0",
- "load-grunt-tasks": "^0.6.0"
+ "grunt-mocha-test": "^0.12.1",
+ "load-grunt-tasks": "^0.6.0",
+ "ncp": "^1.0.1"
},
"scripts": {
"test": "grunt test -v"
@@ -0,0 +1,99 @@
+"use strict";
+/*jshint -W079*/
+var chai = require("chai"),
+ expect = chai.expect,
+ ncp = require('ncp').ncp,
+ grunt = require("grunt"),
+ _ = grunt.util._,
+ JsBeautifierTask = require("../lib/jsbeautifier");
+
+/*jshint -W030*/
+describe("JsBeautifier: Config file test", function() {
+ var createMockTask;
+ var mockTask;
+
+ createMockTask = function(taskOptions, files, done) {
+ return {
+ _taskOptions: taskOptions,
+ files: [{
+ src: grunt.file.expand(files)
+ }],
+ options: function(defs) {
+ return _.defaults(this._taskOptions, defs);
+ },
+ async: function() {
+ return done || function() {};
+ }
+ };
+ };
+
+ beforeEach(function(done) {
+ grunt.file.mkdir("tmp/configFile");
+ ncp("test/fixtures/configFile", "tmp/configFile", done);
+ });
+
+ afterEach(function() {
+ mockTask = null;
+ grunt.file.delete("tmp");
+ });
+
+ function assertBeautifiedFile(actualFile, expectedFile) {
+ var actual = grunt.file.read("tmp/configFile/" + actualFile),
+ expected = grunt.file.read("tmp/configFile/" + expectedFile);
+ expect(actual, expected, "should beautify js " + actualFile + " using config file");
+ }
+
+ it("beautification of js, css & html file using settings from config file", function(done) {
+ var task;
+ mockTask = createMockTask({
+ config: "tmp/configFile/jsbeautifyrc.json"
+ }, ["tmp/configFile/test.js", "tmp/configFile/test.css", "tmp/configFile/test.html"], function() {
+ assertBeautifiedFile("test.js", "expected/test_expected.js");
+ assertBeautifiedFile("test.css", "expected/test_expected.css");
+ assertBeautifiedFile("test.html", "expected/test_expected.html");
+ done();
+ });
+
+ task = new JsBeautifierTask(mockTask);
+ task.run();
+ });
+
+ it("beautification of js, css & html file using settings from flat config file", function(done) {
+ var task;
+ mockTask = createMockTask({
+ config: "tmp/configFile/jsbeautifyrc_flat.json"
+ }, ["tmp/configFile/test.js", "tmp/configFile/test.css", "tmp/configFile/test.html"], function() {
+ assertBeautifiedFile("test.js", "expected/test_expected.js");
+ assertBeautifiedFile("test.css", "expected/test_expected.css");
+ assertBeautifiedFile("test.html", "expected/test_expected.html");
+ done();
+ });
+
+ task = new JsBeautifierTask(mockTask);
+ task.run();
+ });
+
+ it("beautification of js, css & html file using settings from config file and gruntfile", function(done) {
+ var task;
+ mockTask = createMockTask({
+ config: "tmp/configFile/jsbeautifyrc_flat.json",
+ js: {
+ indentSize: 3
+ },
+ css: {
+ indentSize: 5
+ },
+ html: {
+ indentSize: 7
+ }
+ }, ["tmp/configFile/test.js", "tmp/configFile/test.css", "tmp/configFile/test.html"], function() {
+ assertBeautifiedFile("test.js", "expected/withGruntFileOptions/test_expected.js");
+ assertBeautifiedFile("test.css", "expected/withGruntFileOptions/test_expected.css");
+ assertBeautifiedFile("test.html", "expected/withGruntFileOptions/test_expected.html");
+ done();
+ });
+
+ task = new JsBeautifierTask(mockTask);
+ task.run();
+ });
+});
View
@@ -0,0 +1,59 @@
+"use strict";
+/*jshint -W079*/
+var chai = require("chai"),
+ expect = chai.expect,
+ ncp = require('ncp').ncp,
+ grunt = require("grunt"),
+ _ = grunt.util._,
+ JsBeautifierTask = require("../lib/jsbeautifier");
+
+/*jshint -W030*/
+describe("JsBeautifier: Destination test", function() {
+ var createMockTask;
+ var mockTask;
+
+ createMockTask = function(taskOptions, files, done) {
+ return {
+ _taskOptions: taskOptions,
+ files: [{
+ src: grunt.file.expand(files)
+ }],
+ options: function(defs) {
+ return _.defaults(this._taskOptions, defs);
+ },
+ async: function() {
+ return done || function() {};
+ }
+ };
+ };
+
+ beforeEach(function(done) {
+ grunt.file.mkdir("tmp/common");
+ ncp("test/fixtures/common", "tmp/common", done);
+ });
+
+ afterEach(function() {
+ mockTask = null;
+ grunt.file.delete("tmp");
+ grunt.file.delete("dest");
+ });
+
+ function assertBeautifiedFile() {
+ var actual = grunt.file.read("dest/tmp/common/not-been-beautified.js"),
+ expected = grunt.file.read("tmp/common/been-beautified.js");
+ expect(actual, expected, "should beautify js and put into dest folder");
+ }
+
+ it("Verify beautification with dest folder", function(done) {
+ var task;
+ mockTask = createMockTask({
+ dest: "dest"
+ }, ["tmp/common/not-been-beautified.js"], function() {
+ assertBeautifiedFile();
+ done();
+ });
+
+ task = new JsBeautifierTask(mockTask);
+ task.run();
+ });
+});
View
@@ -7,8 +7,8 @@ exports.dest_test = {
"Verify beautification with dest folder": function(test) {
test.expect(1);
exec("grunt jsbeautifier:dest", function() {
- var actual = grunt.file.read("dest/tmp/not-been-beautified.js"),
- expected = grunt.file.read("test/fixtures/been-beautified.js");
+ var actual = grunt.file.read("dest/tmp/common/not-been-beautified.js"),
+ expected = grunt.file.read("test/fixtures/common/been-beautified.js");
test.equal(actual, expected, "should beautify js using config file");
test.done();
});
@@ -0,0 +1,71 @@
+"use strict";
+/*jshint -W079*/
+var chai = require("chai"),
+ expect = chai.expect,
+ ncp = require('ncp').ncp,
+ grunt = require("grunt"),
+ _ = grunt.util._,
+ JsBeautifierTask = require("../lib/jsbeautifier");
+
+/*jshint -W030*/
+describe("JsBeautifier: FileMapping test", function() {
+ var createMockTask;
+ var mockTask;
+
+ createMockTask = function(taskOptions, files, done) {
+ return {
+ _taskOptions: taskOptions,
+ files: [{
+ src: grunt.file.expand(files)
+ }],
+ options: function(defs) {
+ return _.defaults(this._taskOptions, defs);
+ },
+ async: function() {
+ return done || function() {};
+ }
+ };
+ };
+
+ beforeEach(function(done) {
+ grunt.file.mkdir("tmp/fileMapping");
+ ncp("test/fixtures/fileMapping", "tmp/fileMapping", done);
+ });
+
+ afterEach(function() {
+ mockTask = null;
+ grunt.file.delete("tmp");
+ });
+
+ function assertBeautifiedFile(actualFile, expectedFile) {
+ var actual = grunt.file.read("tmp/fileMapping/" + actualFile),
+ expected = grunt.file.read("tmp/fileMapping/" + expectedFile);
+ expect(actual, expected, "should beautify js " + actualFile + " using config file");
+ }
+
+ it("beautification of js, css & html file using file mapping", function(done) {
+ var task;
+ mockTask = createMockTask({
+ js: {
+ fileTypes: [".js.erb"],
+ maxPreserveNewlines: 2
+ },
+ css: {
+ fileTypes: [".css.erb"]
+ },
+ html: {
+ fileTypes: [".html.erb"],
+ preserveNewLines: true,
+ maxPreserveNewlines: 1
+ }
+ }, ["tmp/fileMapping/not-beautified.js.erb", "tmp/fileMapping/not-beautified.css.erb", "tmp/fileMapping/not-beautified.html.erb"], function() {
+ assertBeautifiedFile("not-beautified.js.erb", "expected/beautified.js.erb");
+ assertBeautifiedFile("not-beautified.css.erb", "expected/beautified.css.erb");
+ assertBeautifiedFile("not-beautified.html.erb", "expected/beautified.html.erb");
+ done();
+ });
+
+ task = new JsBeautifierTask(mockTask);
+ task.run();
+ });
+});
Oops, something went wrong.

0 comments on commit c0a47d0

Please sign in to comment.