GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — node/lib/assert.js at master · rbs392/node Expand file tree/ Copy pathassert.jsMore file actions 459 lines (415 loc) · 13.4 KB / Copy pathassert.jsFile metadata and controls459 lines (415 loc) · 13.4 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 // Originally from narwhal.js (http://narwhaljs.org) // Copyright (c) 2009 Thomas Robinson <280north.com> // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the 'Software'), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; const { Buffer } = require('buffer'); const { isDeepEqual, isDeepStrictEqual } = require('internal/util/comparisons'); const errors = require('internal/errors'); const { openSync, closeSync, readSync } = require('fs'); const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn'); const { inspect } = require('util'); const { EOL } = require('os'); const codeCache = new Map(); // Escape control characters but not \n and \t to keep the line breaks and // indentation intact. // eslint-disable-next-line no-control-regex const escapeSequencesRegExp = /[\x00-\x08\x0b\x0c\x0e-\x1f]/g; const meta = [ '\\u0000', '\\u0001', '\\u0002', '\\u0003', '\\u0004', '\\u0005', '\\u0006', '\\u0007', '\\b', '', '', '\\u000b', '\\f', '', '\\u000e', '\\u000f', '\\u0010', '\\u0011', '\\u0012', '\\u0013', '\\u0014', '\\u0015', '\\u0016', '\\u0017', '\\u0018', '\\u0019', '\\u001a', '\\u001b', '\\u001c', '\\u001d', '\\u001e', '\\u001f' ]; const escapeFn = (str) => meta[str.charCodeAt(0)]; // The assert module provides functions that throw // AssertionError's when particular conditions are not met. The // assert module must conform to the following interface. const assert = module.exports = ok; const NO_EXCEPTION_SENTINEL = {}; // All of the following functions must throw an AssertionError // when a corresponding condition is not met, with a message that // may be undefined if not provided. All assertion methods provide // both the actual and expected values to the assertion error for // display purposes. function innerFail(obj) { if (obj.message instanceof Error) throw obj.message; throw new errors.AssertionError(obj); } function fail(actual, expected, message, operator, stackStartFn) { const argsLen = arguments.length; if (argsLen === 0) { message = 'Failed'; } else if (argsLen === 1) { message = actual; actual = undefined; } else if (argsLen === 2) { operator = '!='; } innerFail({ actual, expected, message, operator, stackStartFn: stackStartFn || fail }); } assert.fail = fail; // The AssertionError is defined in internal/error. // new assert.AssertionError({ message: message, // actual: actual, // expected: expected }); assert.AssertionError = errors.AssertionError; function getBuffer(fd, assertLine) { var lines = 0; // Prevent blocking the event loop by limiting the maximum amount of // data that may be read. var maxReads = 64; // bytesPerRead * maxReads = 512 kb var bytesRead = 0; var startBuffer = 0; // Start reading from that char on const bytesPerRead = 8192; const buffers = []; do { const buffer = Buffer.allocUnsafe(bytesPerRead); bytesRead = readSync(fd, buffer, 0, bytesPerRead); for (var i = 0; i < bytesRead; i++) { if (buffer[i] === 10) { lines++; if (lines === assertLine) { startBuffer = i + 1; // Read up to 15 more lines to make sure all code gets matched } else if (lines === assertLine + 16) { buffers.push(buffer.slice(startBuffer, i)); return buffers; } } } if (lines >= assertLine) { buffers.push(buffer.slice(startBuffer, bytesRead)); // Reset the startBuffer in case we need more than one chunk startBuffer = 0; } } while (--maxReads !== 0 && bytesRead !== 0); return buffers; } function innerOk(args, fn) { var [value, message] = args; if (args.length === 0) throw new errors.TypeError('ERR_MISSING_ARGS', 'value'); if (!value) { if (message == null) { // Use the call as error message if possible. // This does not work with e.g. the repl. const err = new Error(); // Make sure the limit is set to 1. Otherwise it could fail (<= 0) or it // does to much work. const tmpLimit = Error.stackTraceLimit; Error.stackTraceLimit = 1; Error.captureStackTrace(err, fn); Error.stackTraceLimit = tmpLimit; const tmpPrepare = Error.prepareStackTrace; Error.prepareStackTrace = (_, stack) => stack; const call = err.stack[0]; Error.prepareStackTrace = tmpPrepare; const filename = call.getFileName(); const line = call.getLineNumber() - 1; const column = call.getColumnNumber() - 1; const identifier = `${filename}${line}${column}`; if (codeCache.has(identifier)) { message = codeCache.get(identifier); } else { var fd; try { fd = openSync(filename, 'r', 0o666); const buffers = getBuffer(fd, line); const code = Buffer.concat(buffers).toString('utf8'); const nodes = parseExpressionAt(code, column); // Node type should be "CallExpression" and some times // "SequenceExpression". const node = nodes.type === 'CallExpression' ? nodes : nodes.expressions[0]; // TODO: fix the "generatedMessage property" // Since this is actually a generated message, it has to be // determined differently from now on. const name = node.callee.name; // Calling `ok` with .apply or .call is uncommon but we use a simple // safeguard nevertheless. if (name !== 'apply' && name !== 'call') { // Only use `assert` and `assert.ok` to reference the "real API" and // not user defined function names. const ok = name === 'ok' ? '.ok' : ''; const args = node.arguments; message = code .slice(args[0].start, args[args.length - 1].end) .replace(escapeSequencesRegExp, escapeFn); message = 'The expression evaluated to a falsy value:' + `${EOL}${EOL} assert${ok}(${message})${EOL}`; } // Make sure to always set the cache! No matter if the message is // undefined or not codeCache.set(identifier, message); } catch (e) { // Invalidate cache to prevent trying to read this part again. codeCache.set(identifier, undefined); } finally { if (fd !== undefined) closeSync(fd); } } } innerFail({ actual: value, expected: true, message, operator: '==', stackStartFn: fn }); } } // Pure assertion tests whether a value is truthy, as determined // by !!value. function ok(...args) { innerOk(args, ok); } assert.ok = ok; // The equality assertion tests shallow, coercive equality with ==. /* eslint-disable no-restricted-properties */ assert.equal = function equal(actual, expected, message) { // eslint-disable-next-line eqeqeq if (actual != expected) { innerFail({ actual, expected, message, operator: '==', stackStartFn: equal }); } }; // The non-equality assertion tests for whether two objects are not // equal with !=. assert.notEqual = function notEqual(actual, expected, message) { // eslint-disable-next-line eqeqeq if (actual == expected) { innerFail({ actual, expected, message, operator: '!=', stackStartFn: notEqual }); } }; // The equivalence assertion tests a deep equality relation. assert.deepEqual = function deepEqual(actual, expected, message) { if (!isDeepEqual(actual, expected)) { innerFail({ actual, expected, message, operator: 'deepEqual', stackStartFn: deepEqual }); } }; // The non-equivalence assertion tests for any deep inequality. assert.notDeepEqual = function notDeepEqual(actual, expected, message) { if (isDeepEqual(actual, expected)) { innerFail({ actual, expected, message, operator: 'notDeepEqual', stackStartFn: notDeepEqual }); } }; /* eslint-enable */ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { if (!isDeepStrictEqual(actual, expected)) { innerFail({ actual, expected, message, operator: 'deepStrictEqual', stackStartFn: deepStrictEqual }); } }; assert.notDeepStrictEqual = notDeepStrictEqual; function notDeepStrictEqual(actual, expected, message) { if (isDeepStrictEqual(actual, expected)) { innerFail({ actual, expected, message, operator: 'notDeepStrictEqual', stackStartFn: notDeepStrictEqual }); } } assert.strictEqual = function strictEqual(actual, expected, message) { if (!Object.is(actual, expected)) { innerFail({ actual, expected, message, operator: 'strictEqual', stackStartFn: strictEqual }); } }; assert.notStrictEqual = function notStrictEqual(actual, expected, message) { if (Object.is(actual, expected)) { innerFail({ actual, expected, message, operator: 'notStrictEqual', stackStartFn: notStrictEqual }); } }; function createMsg(msg, key, actual, expected) { if (msg) return msg; return `${key}: expected ${inspect(expected[key])}, ` + `not ${inspect(actual[key])}`; } function expectedException(actual, expected, msg) { if (typeof expected !== 'function') { if (expected instanceof RegExp) return expected.test(actual); // assert.doesNotThrow does not accept objects. if (arguments.length === 2) { throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'expected', ['Function', 'RegExp'], expected); } // The name and message could be non enumerable. Therefore test them // explicitly. if ('name' in expected) { assert.strictEqual( actual.name, expected.name, createMsg(msg, 'name', actual, expected)); } if ('message' in expected) { assert.strictEqual( actual.message, expected.message, createMsg(msg, 'message', actual, expected)); } const keys = Object.keys(expected); for (const key of keys) { assert.deepStrictEqual( actual[key], expected[key], createMsg(msg, key, actual, expected)); } return true; } // Guard instanceof against arrow functions as they don't have a prototype. if (expected.prototype !== undefined && actual instanceof expected) { return true; } if (Error.isPrototypeOf(expected)) { return false; } return expected.call({}, actual) === true; } function getActual(block) { if (typeof block !== 'function') { throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function', block); } try { block(); } catch (e) { return e; } return NO_EXCEPTION_SENTINEL; } // Expected to throw an error. assert.throws = function throws(block, error, message) { const actual = getActual(block); if (typeof error === 'string') { if (arguments.length === 3) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'error', ['Function', 'RegExp'], error); message = error; error = null; } if (actual === NO_EXCEPTION_SENTINEL) { let details = ''; if (error && error.name) { details += ` (${error.name})`; } details += message ? `: ${message}` : '.'; innerFail({ actual, expected: error, operator: 'throws', message: `Missing expected exception${details}`, stackStartFn: throws }); } if (error && expectedException(actual, error, message) === false) { throw actual; } }; assert.doesNotThrow = function doesNotThrow(block, error, message) { const actual = getActual(block); if (actual === NO_EXCEPTION_SENTINEL) return; if (typeof error === 'string') { message = error; error = null; } if (!error || expectedException(actual, error)) { const details = message ? `: ${message}` : '.'; innerFail({ actual, expected: error, operator: 'doesNotThrow', message: `Got unwanted exception${details}\n${actual && actual.message}`, stackStartFn: doesNotThrow }); } throw actual; }; assert.ifError = function ifError(err) { if (err) throw err; }; // Expose a strict only variant of assert function strict(...args) { innerOk(args, strict); } assert.strict = Object.assign(strict, assert, { equal: assert.strictEqual, deepEqual: assert.deepStrictEqual, notEqual: assert.notStrictEqual, notDeepEqual: assert.notDeepStrictEqual }); assert.strict.strict = assert.strict; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |