Skip to content

Commit

Permalink
+/-
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Feb 27, 2019
1 parent cc8e401 commit ddf80cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
17 changes: 7 additions & 10 deletions lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ module.exports = [
items: [
{
url: 'http://localhost:10200/byte-efficiency/script.js',
totalBytes: 53181,
wastedBytes: 46481,
wastedPercent: 87.40117365133875,
wastedBytes: '46481 +/- 100',
wastedPercent: '87 +/- 5',
},
{
url: 'http://localhost:10200/byte-efficiency/tester.html',
totalBytes: 6607,
wastedBytes: 6581,
wastedPercent: 99.60353766392193,
url: 'inline: \n function unusedFunction() {\n // Un...',
wastedBytes: '6581 +/- 100',
wastedPercent: '99.6 +/- 0.1',
},
{
url: 'http://localhost:10200/byte-efficiency/tester.html',
totalBytes: 6559,
wastedBytes: 6559,
url: 'inline: \n // Used block #1\n // FILLER DATA JUS...',
wastedBytes: '6559 +/- 100',
wastedPercent: 100,
},
],
Expand Down
19 changes: 12 additions & 7 deletions lighthouse-cli/test/smokehouse/smokehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ const PROTOCOL_TIMEOUT_EXIT_CODE = 67;
const PAGE_HUNG_EXIT_CODE = 68;
const INSECURE_DOCUMENT_REQUEST_EXIT_CODE = 69;
const RETRIES = 3;
const NUMERICAL_EXPECTATION_REGEXP = /^(<=?|>=?)((\d|\.)+)$/;
const NUMBER_REGEXP = /(?:\d|\.)+/.source;
const OPS_REGEXP = /<=?|>=?|\+\/-/.source;
const NUMERICAL_EXPECTATION_REGEXP =
new RegExp(`^(${NUMBER_REGEXP})?\\s?(${OPS_REGEXP})\\s?(${NUMBER_REGEXP})$`);

/**
* Attempt to resolve a path locally. If this fails, attempts to locate the path
Expand Down Expand Up @@ -148,17 +151,19 @@ function runLighthouse(url, configPath, isDebug) {
function matchesExpectation(actual, expected) {
if (typeof actual === 'number' && NUMERICAL_EXPECTATION_REGEXP.test(expected)) {
const parts = expected.match(NUMERICAL_EXPECTATION_REGEXP);
const operator = parts[1];
const number = parseFloat(parts[2]);
const operator = parts[2];
const numbers = [parts[1], parts[3]].filter(p => typeof p !== 'undefined').map(parseFloat);
switch (operator) {
case '>':
return actual > number;
return actual > numbers[0];
case '>=':
return actual >= number;
return actual >= numbers[0];
case '<':
return actual < number;
return actual < numbers[0];
case '<=':
return actual <= number;
return actual <= numbers[0];
case '+/-':
return Math.abs(actual - numbers[0]) <= numbers[1];
default:
throw new Error(`unexpected operator ${operator}`);
}
Expand Down

0 comments on commit ddf80cb

Please sign in to comment.