2017-04-06 16:17:43 +08:00
|
|
|
"use strict";
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2014-06-18 04:34:30 +08:00
|
|
|
|
2018-12-19 01:29:12 +08:00
|
|
|
const check = (expected, actual) => {
|
2019-02-19 15:58:46 +08:00
|
|
|
if (expected instanceof RegExp) {
|
2018-12-19 01:29:12 +08:00
|
|
|
expected = { message: expected };
|
|
|
|
}
|
|
|
|
return Object.keys(expected).every(key => {
|
|
|
|
let value = actual[key];
|
2019-02-19 15:58:46 +08:00
|
|
|
if (typeof value === "object") {
|
2018-12-19 01:29:12 +08:00
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
return expected[key].test(value);
|
2019-02-19 15:58:46 +08:00
|
|
|
});
|
|
|
|
};
|
2018-12-19 01:29:12 +08:00
|
|
|
|
|
|
|
const explain = object => {
|
2019-02-19 15:58:46 +08:00
|
|
|
if (object instanceof RegExp) {
|
2018-12-19 01:29:12 +08:00
|
|
|
object = { message: object };
|
|
|
|
}
|
2019-02-19 15:58:46 +08:00
|
|
|
return Object.keys(object)
|
|
|
|
.map(key => {
|
|
|
|
let value = object[key];
|
|
|
|
if (typeof value === "object" && !(value instanceof RegExp)) {
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
let msg = `${key} = ${value}`;
|
|
|
|
if (msg.length > 100) msg = msg.slice(0, 97) + "...";
|
|
|
|
return msg;
|
|
|
|
})
|
|
|
|
.join("; ");
|
|
|
|
};
|
2018-12-19 01:29:12 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
module.exports = function checkArrayExpectation(
|
|
|
|
testDirectory,
|
|
|
|
object,
|
|
|
|
kind,
|
|
|
|
filename,
|
|
|
|
upperCaseKind,
|
|
|
|
done
|
|
|
|
) {
|
|
|
|
if (!done) {
|
2016-07-03 19:13:01 +08:00
|
|
|
done = upperCaseKind;
|
|
|
|
upperCaseKind = filename;
|
2017-04-06 16:17:43 +08:00
|
|
|
filename = `${kind}s`;
|
2016-07-03 19:13:01 +08:00
|
|
|
}
|
2018-09-07 20:11:48 +08:00
|
|
|
let array = object[`${kind}s`];
|
2018-12-30 18:50:04 +08:00
|
|
|
if (Array.isArray(array)) {
|
2019-02-19 15:58:46 +08:00
|
|
|
if (kind === "warning") {
|
2018-12-30 18:50:04 +08:00
|
|
|
array = array.filter(item => !/from Terser/.test(item));
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
if (fs.existsSync(path.join(testDirectory, `${filename}.js`))) {
|
2017-04-06 16:17:43 +08:00
|
|
|
const expectedFilename = path.join(testDirectory, `${filename}.js`);
|
|
|
|
const expected = require(expectedFilename);
|
2018-12-30 18:50:04 +08:00
|
|
|
if (expected.length < array.length) {
|
2018-02-25 09:00:20 +08:00
|
|
|
return (
|
|
|
|
done(
|
|
|
|
new Error(
|
2019-02-19 15:58:46 +08:00
|
|
|
`More ${kind}s while compiling than expected:\n\n${array
|
|
|
|
.map(explain)
|
|
|
|
.join("\n\n")}. Check expected ${kind}s: ${expectedFilename}`
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-12-30 18:50:04 +08:00
|
|
|
} else if (expected.length > array.length) {
|
2018-02-25 09:00:20 +08:00
|
|
|
return (
|
|
|
|
done(
|
|
|
|
new Error(
|
2019-02-19 15:58:46 +08:00
|
|
|
`Less ${kind}s while compiling than expected:\n\n${array
|
|
|
|
.map(explain)
|
|
|
|
.join("\n\n")}. Check expected ${kind}s: ${expectedFilename}`
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-12-30 18:50:04 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
if (Array.isArray(expected[i])) {
|
|
|
|
for (let j = 0; j < expected[i].length; j++) {
|
2018-12-30 18:50:04 +08:00
|
|
|
if (!check(expected[i][j], array[i])) {
|
2018-02-25 09:00:20 +08:00
|
|
|
return (
|
|
|
|
done(
|
|
|
|
new Error(
|
2019-02-19 15:58:46 +08:00
|
|
|
`${upperCaseKind} ${i}: ${explain(
|
|
|
|
array[i]
|
|
|
|
)} doesn't match ${explain(expected[i][j])}`
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-12-30 18:50:04 +08:00
|
|
|
}
|
2014-06-18 04:34:30 +08:00
|
|
|
}
|
2018-12-19 01:29:12 +08:00
|
|
|
} else if (!check(expected[i], array[i]))
|
2018-02-25 09:00:20 +08:00
|
|
|
return (
|
|
|
|
done(
|
|
|
|
new Error(
|
2019-02-19 15:58:46 +08:00
|
|
|
`${upperCaseKind} ${i}: ${explain(
|
|
|
|
array[i]
|
|
|
|
)} doesn't match ${explain(expected[i])}`
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2014-06-18 04:34:30 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (array.length > 0) {
|
|
|
|
return (
|
|
|
|
done(
|
2019-02-19 15:58:46 +08:00
|
|
|
new Error(
|
|
|
|
`${upperCaseKind}s while compiling:\n\n${array
|
|
|
|
.map(explain)
|
|
|
|
.join("\n\n")}`
|
|
|
|
)
|
2018-02-25 09:00:20 +08:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2014-06-18 04:34:30 +08:00
|
|
|
}
|
2017-11-15 21:08:11 +08:00
|
|
|
};
|