From 2bbc1bb1f915ecb16d30425847f8804e215cfda9 Mon Sep 17 00:00:00 2001 From: fcrisci Date: Tue, 5 Mar 2013 11:22:56 +0100 Subject: [PATCH] Fix indentation in clean utility utils.clean extracts a test method content re-indenting the function for whitespaces. This commit fixes to issues with indentation * windows line feed `\r\n` * tab indentation `\t` --- lib/utils.js | 4 ++- test/acceptance/utils.js | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/acceptance/utils.js diff --git a/lib/utils.js b/lib/utils.js index 5142e06c5a..e9c974c1ff 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -193,11 +193,13 @@ exports.slug = function(str){ exports.clean = function(str) { str = str + .replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '') .replace(/^function *\(.*\) *{/, '') .replace(/\s+\}$/, ''); var spaces = str.match(/^\n?( *)/)[1].length - , re = new RegExp('^ {' + spaces + '}', 'gm'); + , tabs = str.match(/^\n?(\t*)/)[1].length + , re = new RegExp('^\n?' + (tabs ? '\t' : ' ') + '{' + (tabs ? tabs : spaces) + '}', 'gm'); str = str.replace(re, ''); diff --git a/test/acceptance/utils.js b/test/acceptance/utils.js new file mode 100644 index 0000000000..97373687a6 --- /dev/null +++ b/test/acceptance/utils.js @@ -0,0 +1,59 @@ +var utils = require('../../lib/utils'); + +describe('lib/utils', function () { + describe('clean', function () { + it("should format a single line test function", function () { + var fn = [ + "function () {" + , " var a = 1;" + , "}" + ].join("\n"); + utils.clean(fn).should.equal("var a = 1;"); + }); + + it("should format a multi line test indented with spaces", function () { + // and no new lines after curly braces, shouldn't matter + var fn = [ + "function(){ var a = 1;" + , " var b = 2;" // this one has more spaces + , " var c = 3; }" + ].join("\n"); + utils.clean(fn).should.equal("var a = 1;\n var b = 2;\nvar c = 3;"); + }); + + it("should format a multi line test indented with tabs", function () { + var fn = [ + "function (arg1, arg2) {" + , "\tif (true) {" + , "\t\tvar a = 1;" + , "\t}" + , "}" + ].join("\n"); + utils.clean(fn).should.equal("if (true) {\n\tvar a = 1;\n}"); + }); + + it("should format functions saved in windows style - spaces", function () { + var fn = [ + "function (one) {" + , " do {", + , ' "nothing";', + , " } while (false);" + , ' }' + ].join("\r\n"); + utils.clean(fn).should.equal('do {\n "nothing";\n} while (false);'); + }); + + it("should format functions saved in windows style - tabs", function () { + var fn = [ + "function ( ) {" + , "\tif (false) {" + , "\t\tvar json = {" + , '\t\t\tone : 1' + , '\t\t};' + , "\t}" + , "}" + ].join("\r\n"); + utils.clean(fn).should.equal("if (false) {\n\tvar json = {\n\t\tone : 1\n\t};\n}"); + }); + }); +}); \ No newline at end of file