|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const util = require('util'); |
| 6 | +const processUtil = process.binding('util'); |
| 7 | +const opts = {showProxy: true}; |
| 8 | + |
| 9 | +const target = {}; |
| 10 | +const handler = { |
| 11 | + get: function() { throw new Error('Getter should not be called'); } |
| 12 | +}; |
| 13 | +const proxyObj = new Proxy(target, handler); |
| 14 | + |
| 15 | +// Inspecting the proxy should not actually walk it's properties |
| 16 | +assert.doesNotThrow(() => util.inspect(proxyObj, opts)); |
| 17 | + |
| 18 | +// getProxyDetails is an internal method, not intended for public use. |
| 19 | +// This is here to test that the internals are working correctly. |
| 20 | +const details = processUtil.getProxyDetails(proxyObj); |
| 21 | +assert.strictEqual(target, details[0]); |
| 22 | +assert.strictEqual(handler, details[1]); |
| 23 | + |
| 24 | +assert.strictEqual(util.inspect(proxyObj, opts), |
| 25 | + 'Proxy [ {}, { get: [Function] } ]'); |
| 26 | + |
| 27 | +// Using getProxyDetails with non-proxy returns undefined |
| 28 | +assert.strictEqual(processUtil.getProxyDetails({}), undefined); |
| 29 | + |
| 30 | +// This will throw because the showProxy option is not used |
| 31 | +// and the get function on the handler object defined above |
| 32 | +// is actually invoked. |
| 33 | +assert.throws( |
| 34 | + () => util.inspect(proxyObj) |
| 35 | +); |
| 36 | + |
| 37 | +// Yo dawg, I heard you liked Proxy so I put a Proxy |
| 38 | +// inside your Proxy that proxies your Proxy's Proxy. |
| 39 | +const proxy1 = new Proxy({}, {}); |
| 40 | +const proxy2 = new Proxy(proxy1, {}); |
| 41 | +const proxy3 = new Proxy(proxy2, proxy1); |
| 42 | +const proxy4 = new Proxy(proxy1, proxy2); |
| 43 | +const proxy5 = new Proxy(proxy3, proxy4); |
| 44 | +const proxy6 = new Proxy(proxy5, proxy5); |
| 45 | +const expected0 = '{}'; |
| 46 | +const expected1 = 'Proxy [ {}, {} ]'; |
| 47 | +const expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]'; |
| 48 | +const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]'; |
| 49 | +const expected4 = 'Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]'; |
| 50 | +const expected5 = 'Proxy [ Proxy [ Proxy [ Proxy [Object], {} ],' + |
| 51 | + ' Proxy [ {}, {} ] ],\n Proxy [ Proxy [ {}, {} ]' + |
| 52 | + ', Proxy [ Proxy [Object], {} ] ] ]'; |
| 53 | +const expected6 = 'Proxy [ Proxy [ Proxy [ Proxy [Object], Proxy [Object]' + |
| 54 | + ' ],\n Proxy [ Proxy [Object], Proxy [Object] ] ],\n' + |
| 55 | + ' Proxy [ Proxy [ Proxy [Object], Proxy [Object] ],\n' + |
| 56 | + ' Proxy [ Proxy [Object], Proxy [Object] ] ] ]'; |
| 57 | +assert.strictEqual(util.inspect(proxy1, opts), expected1); |
| 58 | +assert.strictEqual(util.inspect(proxy2, opts), expected2); |
| 59 | +assert.strictEqual(util.inspect(proxy3, opts), expected3); |
| 60 | +assert.strictEqual(util.inspect(proxy4, opts), expected4); |
| 61 | +assert.strictEqual(util.inspect(proxy5, opts), expected5); |
| 62 | +assert.strictEqual(util.inspect(proxy6, opts), expected6); |
| 63 | +assert.strictEqual(util.inspect(proxy1), expected0); |
| 64 | +assert.strictEqual(util.inspect(proxy2), expected0); |
| 65 | +assert.strictEqual(util.inspect(proxy3), expected0); |
| 66 | +assert.strictEqual(util.inspect(proxy4), expected0); |
| 67 | +assert.strictEqual(util.inspect(proxy5), expected0); |
| 68 | +assert.strictEqual(util.inspect(proxy6), expected0); |
| 69 | + |
| 70 | +// Just for fun, let's create a Proxy using Arrays. |
| 71 | +const proxy7 = new Proxy([], []); |
| 72 | +const expected7 = 'Proxy [ [], [] ]'; |
| 73 | +assert.strictEqual(util.inspect(proxy7, opts), expected7); |
| 74 | +assert.strictEqual(util.inspect(proxy7), '[]'); |
| 75 | + |
| 76 | +// Now we're just getting silly, right? |
| 77 | +const proxy8 = new Proxy(Date, []); |
| 78 | +const proxy9 = new Proxy(Date, String); |
| 79 | +const expected8 = 'Proxy [ [Function: Date], [] ]'; |
| 80 | +const expected9 = 'Proxy [ [Function: Date], [Function: String] ]'; |
| 81 | +assert.strictEqual(util.inspect(proxy8, opts), expected8); |
| 82 | +assert.strictEqual(util.inspect(proxy9, opts), expected9); |
| 83 | +assert.strictEqual(util.inspect(proxy8), '[Function: Date]'); |
| 84 | +assert.strictEqual(util.inspect(proxy9), '[Function: Date]'); |
0 commit comments