github.com/iosif02/goja_nodejs@v1.0.1/assert.js (about) 1 'use strict'; 2 3 const assert = { 4 _isSameValue(a, b) { 5 if (a === b) { 6 // Handle +/-0 vs. -/+0 7 return a !== 0 || 1 / a === 1 / b; 8 } 9 10 // Handle NaN vs. NaN 11 return a !== a && b !== b; 12 }, 13 14 _toString(value) { 15 try { 16 if (value === 0 && 1 / value === -Infinity) { 17 return '-0'; 18 } 19 20 return String(value); 21 } catch (err) { 22 if (err.name === 'TypeError') { 23 return Object.prototype.toString.call(value); 24 } 25 26 throw err; 27 } 28 }, 29 30 sameValue(actual, expected, message) { 31 if (assert._isSameValue(actual, expected)) { 32 return; 33 } 34 if (message === undefined) { 35 message = ''; 36 } else { 37 message += ' '; 38 } 39 40 message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(expected) + '») to be true'; 41 42 throw new Error(message); 43 }, 44 45 throws(f, ctor, message) { 46 if (message === undefined) { 47 message = ''; 48 } else { 49 message += ' '; 50 } 51 try { 52 f(); 53 } catch (e) { 54 if (e.constructor !== ctor) { 55 throw new Error(message + "Wrong exception type was thrown: " + e.constructor.name); 56 } 57 return; 58 } 59 throw new Error(message + "No exception was thrown"); 60 }, 61 62 throwsNodeError(f, ctor, code, message) { 63 if (message === undefined) { 64 message = ''; 65 } else { 66 message += ' '; 67 } 68 try { 69 f(); 70 } catch (e) { 71 if (e.constructor !== ctor) { 72 throw new Error(message + "Wrong exception type was thrown: " + e.constructor.name); 73 } 74 if (e.code !== code) { 75 throw new Error(message + "Wrong exception code was thrown: " + e.code); 76 } 77 return; 78 } 79 throw new Error(message + "No exception was thrown"); 80 } 81 } 82 83 module.exports = assert;