Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(smokehouse): +/- operator #7343

Merged
merged 3 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ module.exports = [
details: {
overallSavingsBytes: '>45000',
overallSavingsMs: '>500',
items: {
length: 1,
},
items: [
{
url: 'http://localhost:10200/byte-efficiency/script.js',
wastedBytes: '46481 +/- 100',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so nice!

wastedPercent: '87 +/- 5',
},
],
},
},
'unused-css-rules': {
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,8 +34,12 @@ 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 VERBOSE = Boolean(process.env.LH_SMOKE_VERBOSE);
const NUMBER_REGEXP = /(?:\d|\.)+/.source;
const OPS_REGEXP = /<=?|>=?|\+\/-/.source;
// An optional number, optional whitespace, an operator, optional whitespace, a number.
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 @@ -149,17 +153,18 @@ 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 [, prefixNumber, operator, postfixNumber] = parts;
switch (operator) {
case '>':
return actual > number;
return actual > postfixNumber;
case '>=':
return actual >= number;
return actual >= postfixNumber;
case '<':
return actual < number;
return actual < postfixNumber;
case '<=':
return actual <= number;
return actual <= postfixNumber;
case '+/-':
return Math.abs(actual - prefixNumber) <= postfixNumber;
default:
throw new Error(`unexpected operator ${operator}`);
}
Expand Down