Skip to content

Commit

Permalink
Refactor CheckArrayExpectation to ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
sendilkumarn authored Apr 6, 2017
1 parent 75efda9 commit 364d422
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions test/checkArrayExpectation.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
var fs = require("fs");
var path = require("path");
"use strict";
const fs = require("fs");
const path = require("path");

module.exports = function checkArrayExpectation(testDirectory, object, kind, filename, upperCaseKind, done) {
if(!done) {
done = upperCaseKind;
upperCaseKind = filename;
filename = kind + "s";
filename = `${kind}s`;
}
var array = object[kind + "s"].slice().sort();
if(kind === "warning") array = array.filter(function(item) {
return !/from UglifyJs/.test(item);
});
if(fs.existsSync(path.join(testDirectory, filename + ".js"))) {
var expectedFilename = path.join(testDirectory, filename + ".js");
var expected = require(expectedFilename);
let array = object[`${kind}s`].slice().sort();
if(kind === "warning") array = array.filter(item => !/from UglifyJs/.test(item));
if(fs.existsSync(path.join(testDirectory, `${filename}.js`))) {
const expectedFilename = path.join(testDirectory, `${filename}.js`);
const expected = require(expectedFilename);
if(expected.length < array.length)
return done(new Error("More " + kind + "s while compiling than expected:\n\n" + array.join("\n\n") + ". Check expected warnings: " + filename)), true;
return done(new Error(`More ${kind}s while compiling than expected:\n\n${array.join("\n\n")}. Check expected warnings: ${filename}`)), true;
else if(expected.length > array.length)
return done(new Error("Less " + kind + "s while compiling than expected:\n\n" + array.join("\n\n") + ". Check expected warnings: " + filename)), true;
for(var i = 0; i < array.length; i++) {
return done(new Error(`Less ${kind}s while compiling than expected:\n\n${array.join("\n\n")}. Check expected warnings: ${filename}`)), true;
for(let i = 0; i < array.length; i++) {
if(Array.isArray(expected[i])) {
for(var j = 0; j < expected[i].length; j++) {
for(let j = 0; j < expected[i].length; j++) {
if(!expected[i][j].test(array[i]))
return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i][j].toString())), true;
return done(new Error(`${upperCaseKind} ${i}: ${array[i]} doesn't match ${expected[i][j].toString()}`)), true;
}
} else if(!expected[i].test(array[i]))
return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i].toString())), true;
return done(new Error(`${upperCaseKind} ${i}: ${array[i]} doesn't match ${expected[i].toString()}`)), true;
}
} else if(array.length > 0) {
return done(new Error(upperCaseKind + "s while compiling:\n\n" + array.join("\n\n"))), true;
return done(new Error(`${upperCaseKind}s while compiling:\n\n${array.join("\n\n")}`)), true;
}
}

0 comments on commit 364d422

Please sign in to comment.