Skip to content

Commit

Permalink
Fixed several issues in screw.js
Browse files Browse the repository at this point in the history
• Correct default "—run-as" method in help message
• Correct time formatting in diagnostic report
• Fixed expansion factor NaN bug
  • Loading branch information
fasttime committed Mar 14, 2017
1 parent 82be497 commit e8ffcf4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 17 deletions.
24 changes: 14 additions & 10 deletions screw.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ function getBasename()
function printErrorMessage(errorMessage)
{
var basename = getBasename();
var message =
basename + ': ' + errorMessage + '.\nTry "' + basename + ' --help" for more information.';
console.error(message);
console.error(
'%s: %s.\nTry "%s --help" for more information.',
basename,
errorMessage,
basename
);
}

function printHelpMessage()
{
var message =
'Usage: ' + getBasename() + ' [OPTION]... [SOURCE [DESTINATION]]\n' +
'Usage: %s [OPTION]... [SOURCE [DESTINATION]]\n' +
'Encodes JavaScript with JScrewIt.\n' +
'\n' +
' -d, --diagnostic print diagnostic report\n' +
' -f, --features FEATURES use a list of comma separated features\n' +
' -t, --trim-code strip leading and trailing blanks and comments\n' +
' -r, --run-as METHOD make output runnable with the specified method\n' +
' -r, --run-as METHOD control generated code type\n' +
' --help display this help and exit\n' +
' --version print version information and exit\n' +
'\n' +
Expand All @@ -46,12 +49,13 @@ function printHelpMessage()
' call -c, -w\n' +
' eval -e\n' +
' express -x\n' +
' express-call (default) -xc, -xw\n' +
' express-eval -xe\n' +
' express-call -xc, -xw\n' +
' express-eval (default) -xe\n' +
' none (none available)\n' +
'\n' +
'See the JScrewIt feature documentation for a list of all supported features.\n';
console.log(message);
var basename = getBasename();
console.log(message, basename);
}

function printVersion()
Expand Down Expand Up @@ -108,7 +112,7 @@ function prompt()
}
catch (error)
{
console.error(error.message);
console.error('%s', error.message);
}
return output;
};
Expand Down Expand Up @@ -156,7 +160,7 @@ function prompt()
}
catch (error)
{
console.error(error.message);
console.error('%s', error.message);
return;
}
if (outputFileName)
Expand Down
82 changes: 78 additions & 4 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,67 @@
var assert = require('assert');
var cli = require('../tools/cli');

describe(
'screw.js',
function ()
{
var exec = require('child_process').exec;

function doAssert(actual, expected)
{
if (expected instanceof RegExp)
assert(expected.test(actual), 'expected "' + actual + '" to match ' + expected);
else
assert.strictEqual(actual, expected);
}

function test(description, command, expectedStdout, expectedStderr)
{
it(
description,
function (done)
{
exec(
command,
null,
function (error, stdout, stderr)
{
doAssert(stdout, expectedStdout);
doAssert(stderr, expectedStderr);
done(error);
}
);
}
);
}

test(
'shows the help message with option "--help"',
'node ./screw.js --help',
/^Usage: screw.js [^]*\n$/,
''
);
test(
'shows an error message with an invalid option',
'node ./screw.js --foo',
'',
'screw.js: unrecognized option "--foo".\nTry "screw.js --help" for more information.\n'
);
test(
'shows an error message when an invalid feature is specified',
'node ./screw.js -f FOO',
'',
'Unknown feature "FOO"\n'
);
test(
'shows an error message when the input file does not exist',
'node ./screw.js ""',
'',
/^ENOENT\b. no such file or directory\b.* ''\n$/
);
}
);

describe(
'parseCommandLine returns expected results with params',
function ()
Expand Down Expand Up @@ -234,6 +295,19 @@ describe(
assert.strictEqual(actual, expected);
}
);
it(
'when original size is 0',
function ()
{
var actual = cli.createReport(0, 0, 0);
var expected =
'Original size: 0 bytes\n' +
'Screwed size: 0 bytes\n' +
'Expansion factor: -\n' +
'Encoding time: < 0.01 s';
assert.strictEqual(actual, expected);
}
);
}
);

Expand Down Expand Up @@ -261,7 +335,7 @@ describe(
coderName: 'coderA',
status: 'used',
outputLength: 100,
time: 0.123,
time: 123,
codingLog:
[
makePerfInfoList(
Expand All @@ -270,7 +344,7 @@ describe(
coderName: 'coderA1',
status: 'used',
outputLength: 50,
time: 0.045
time: 45
}
),
makePerfInfoList(
Expand All @@ -279,7 +353,7 @@ describe(
coderName: 'coderA2',
status: 'used',
outputLength: 25,
time: 0.067,
time: 67,
codingLog:
[
makePerfInfoList(
Expand All @@ -288,7 +362,7 @@ describe(
coderName: 'coderA2_extra',
status: 'used',
outputLength: 22,
time: 0.066
time: 66
}
)
]
Expand Down
7 changes: 4 additions & 3 deletions tools/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ function createDiagnosticReport(codingLog)
function createReport(originalSize, screwedSize, encodingTime)
{
var width = Math.max(widthOf(originalSize), widthOf(screwedSize));
var expansionFactorStr = originalSize ? (screwedSize / originalSize).toFixed(2) : '-';
var encodingTimeStr = timeUtils.formatDuration(encodingTime);
var report =
'Original size: ' + byteCount(originalSize, width) +
'\nScrewed size: ' + byteCount(screwedSize, width) +
'\nExpansion factor: ' + (screwedSize / originalSize).toFixed(2) +
'\nExpansion factor: ' + expansionFactorStr +
'\nEncoding time: ' + encodingTimeStr;
return report;
}
Expand All @@ -58,7 +59,7 @@ function formatCodingLog(codingLog, padding, nextCodingLog)

function formatInt(int)
{
var str = isNaN(int) ? '-' : int;
var str = int === undefined ? '-' : int;
return str;
}

Expand All @@ -78,7 +79,7 @@ function formatPerfInfoList(perfInfoList, padding, paddingChars)
padRight(perfInfo.coderName, 27 - paddingLength) +
padRight(perfInfo.status, 10) +
padLeft(formatInt(perfInfo.outputLength), 11) +
padLeft(formatInt(Math.round(1000 * perfInfo.time)), 11) +
padLeft(formatInt(perfInfo.time), 11) +
'\n';
codingLog = perfInfo.codingLog;
if (codingLog)
Expand Down

0 comments on commit e8ffcf4

Please sign in to comment.