node/test/parallel/test-zlib-brotli.js
Ruben Bridgewater e038d6a1cd
test: refactor common.expectsError
This completely refactors the `expectsError` behavior: so far it's
almost identical to `assert.throws(fn, object)` in case it was used
with a function as first argument. It had a magical property check
that allowed to verify a functions `type` in case `type` was passed
used in the validation object. This pattern is now completely removed
and `assert.throws()` should be used instead.

The main intent for `common.expectsError()` is to verify error cases
for callback based APIs. This is now more flexible by accepting all
validation possibilites that `assert.throws()` accepts as well. No
magical properties exist anymore. This reduces surprising behavior
for developers who are not used to the Node.js core code base.

This has the side effect that `common` is used significantly less
frequent.

PR-URL: https://github.com/nodejs/node/pull/31092
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-12-31 15:54:20 +01:00

74 lines
1.9 KiB
JavaScript

'use strict';
require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const zlib = require('zlib');
// Test some brotli-specific properties of the brotli streams that can not
// be easily covered through expanding zlib-only tests.
const sampleBuffer = fixtures.readSync('/pss-vectors.json');
{
// Test setting the quality parameter at stream creation:
const sizes = [];
for (let quality = zlib.constants.BROTLI_MIN_QUALITY;
quality <= zlib.constants.BROTLI_MAX_QUALITY;
quality++) {
const encoded = zlib.brotliCompressSync(sampleBuffer, {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: quality
}
});
sizes.push(encoded.length);
}
// Increasing quality should roughly correspond to decreasing compressed size:
for (let i = 0; i < sizes.length - 1; i++) {
assert(sizes[i + 1] <= sizes[i] * 1.05, sizes); // 5 % margin of error.
}
assert(sizes[0] > sizes[sizes.length - 1], sizes);
}
{
// Test that setting out-of-bounds option values or keys fails.
assert.throws(() => {
zlib.createBrotliCompress({
params: {
10000: 0
}
});
}, {
code: 'ERR_BROTLI_INVALID_PARAM',
name: 'RangeError',
message: '10000 is not a valid Brotli parameter'
});
// Test that accidentally using duplicate keys fails.
assert.throws(() => {
zlib.createBrotliCompress({
params: {
'0': 0,
'00': 0
}
});
}, {
code: 'ERR_BROTLI_INVALID_PARAM',
name: 'RangeError',
message: '00 is not a valid Brotli parameter'
});
assert.throws(() => {
zlib.createBrotliCompress({
params: {
// This is a boolean flag
[zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING]: 42
}
});
}, {
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
name: 'Error',
message: 'Initialization failed'
});
}