|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// The input battery for test-validate-parity.js. Both sides share |
| 4 | +// this module, so the compared inputs can never drift apart. |
| 5 | + |
| 6 | +// A JSON-safe, human-readable label for a call. |
| 7 | +function label(fn, args) { |
| 8 | + const rendered = args.map((a) => |
| 9 | + typeof a === 'string' ? `'${a}'` : String(a), |
| 10 | + ); |
| 11 | + return `${fn}(${rendered.join(', ')})`; |
| 12 | +} |
| 13 | + |
| 14 | +// Calls fn with args and describes the outcome: nothing thrown, or the |
| 15 | +// thrown error's name, code, and message. |
| 16 | +function outcome(fn, args) { |
| 17 | + try { |
| 18 | + fn(...args); |
| 19 | + return { thrown: false }; |
| 20 | + } catch (err) { |
| 21 | + return { |
| 22 | + thrown: true, |
| 23 | + name: err.name, |
| 24 | + code: err.code, |
| 25 | + message: err.message, |
| 26 | + }; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +const kValidatorCases = [ |
| 31 | + ['validateString', [123, 'x']], |
| 32 | + ['validateString', [undefined, 'x']], |
| 33 | + ['validateString', [null, 'x']], |
| 34 | + ['validateString', [true, 'x']], |
| 35 | + ['validateString', [Symbol('s'), 'x']], |
| 36 | + ['validateString', [{ a: 1 }, 'x']], |
| 37 | + ['validateString', [['a'], 'x']], |
| 38 | + ['validateString', [() => {}, 'x']], |
| 39 | + ['validateString', ['ok', 'x']], |
| 40 | + ['validateObject', [null, 'x']], |
| 41 | + ['validateObject', [[1, 2], 'x']], |
| 42 | + ['validateObject', ['str', 'x']], |
| 43 | + ['validateObject', [123, 'x']], |
| 44 | + ['validateObject', [() => {}, 'x']], |
| 45 | + ['validateObject', [{}, 'x']], |
| 46 | + ['validateInteger', [0, 'x', 1]], |
| 47 | + ['validateInteger', ['abc', 'x', 1]], |
| 48 | + ['validateInteger', [1.5, 'x', 1]], |
| 49 | + ['validateInteger', [NaN, 'x', 1]], |
| 50 | + ['validateInteger', [5, 'x', 1]], |
| 51 | + ['validateInteger', [9007199254740999, 'x', 1]], |
| 52 | +]; |
| 53 | + |
| 54 | +// Error-constructor cases: impl.fn(...args) must return the error object. |
| 55 | +const kErrorCases = [ |
| 56 | + [ |
| 57 | + 'invalidArgTypeError', |
| 58 | + ['options.filter', ['string', 'null', 'undefined'], 42], |
| 59 | + ], |
| 60 | + ['invalidArgTypeError', ['options.endpoint', ['string'], 123]], |
| 61 | + ['invalidArgTypeError', ['options.maxBufferSize', ['integer'], 'abc']], |
| 62 | + ['invalidArgTypeError', ['options.foo', ['Object'], []]], |
| 63 | + [ |
| 64 | + 'invalidArgTypeError', |
| 65 | + ['first argument', ['string', 'null', 'undefined'], 42], |
| 66 | + ], |
| 67 | + [ |
| 68 | + 'invalidArgValueError', |
| 69 | + ['options.endpoint', 'not-a-valid-url', 'must be a valid URL'], |
| 70 | + ], |
| 71 | + [ |
| 72 | + 'invalidArgValueError', |
| 73 | + ['options.serviceName', '', 'must be a non-empty string'], |
| 74 | + ], |
| 75 | + ['invalidArgValueError', ['options.maxBufferSize', 0]], |
| 76 | + ['outOfRangeError', ['x', '>= 1 && <= 100', 0]], |
| 77 | + ['outOfRangeError', ['n', 'an integer', 10000000000]], |
| 78 | +]; |
| 79 | + |
| 80 | +// Runs the battery through impl: either this package's lib/validate.js |
| 81 | +// exports or wrappers around core's internal error constructors. |
| 82 | +function runBattery(impl) { |
| 83 | + const results = []; |
| 84 | + |
| 85 | + for (const [name, args] of kValidatorCases) { |
| 86 | + results.push({ |
| 87 | + call: label(name, args), |
| 88 | + outcome: outcome(impl[name], args), |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + for (const [name, args] of kErrorCases) { |
| 93 | + const err = impl[name](...args); |
| 94 | + results.push({ |
| 95 | + call: label(name, args), |
| 96 | + outcome: { |
| 97 | + thrown: true, |
| 98 | + name: err.name, |
| 99 | + code: err.code, |
| 100 | + message: err.message, |
| 101 | + }, |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + return results; |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = { |
| 109 | + runBattery, |
| 110 | +}; |
0 commit comments