|
| 1 | +describe("inspect", function () { |
| 2 | + it("formats primitives and plain objects", function () { |
| 3 | + expect(__inspect(42)).toBe("42"); |
| 4 | + expect(__inspect("hi")).toBe('"hi"'); |
| 5 | + expect(__inspect({ a: 1, b: "x" })).toBe('{ a: 1, b: "x" }'); |
| 6 | + expect(__inspect([1, [2, 3]])).toBe("[ 1, [ 2, 3 ] ]"); |
| 7 | + }); |
| 8 | + |
| 9 | + it("limits depth", function () { |
| 10 | + expect(__inspect({ a: { b: { c: { d: 1 } } } })).toBe("{ a: { b: { c: [Object] } } }"); |
| 11 | + expect(__inspect({ a: { b: { c: { d: 1 } } } }, { depth: 3 })).toBe("{ a: { b: { c: { d: 1 } } } }"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("reports true cycles and only true cycles", function () { |
| 15 | + var cyc = {}; |
| 16 | + cyc.self = cyc; |
| 17 | + expect(__inspect(cyc)).toBe("{ self: [Circular] }"); |
| 18 | + |
| 19 | + var shared = { x: 1 }; |
| 20 | + expect(__inspect({ a: shared, b: shared })).toBe("{ a: { x: 1 }, b: { x: 1 } }"); |
| 21 | + }); |
| 22 | + |
| 23 | + it("caps arrays and total output", function () { |
| 24 | + var big = []; |
| 25 | + for (var i = 0; i < 250; i++) { |
| 26 | + big[i] = i; |
| 27 | + } |
| 28 | + expect(__inspect(big).indexOf("... 150 more items")).toBeGreaterThan(-1); |
| 29 | + |
| 30 | + var huge = {}; |
| 31 | + for (var k = 0; k < 100000; k++) { |
| 32 | + huge["key" + k] = k; |
| 33 | + } |
| 34 | + var start = Date.now(); |
| 35 | + var out = __inspect(huge); |
| 36 | + var elapsed = Date.now() - start; |
| 37 | + expect(out.length).toBeLessThan(20000); |
| 38 | + // The old JSON path would serialize all 100k keys; the budgeted |
| 39 | + // formatter must not take anywhere near a second. |
| 40 | + expect(elapsed).toBeLessThan(1000); |
| 41 | + }); |
| 42 | + |
| 43 | + it("caps long strings", function () { |
| 44 | + var long = new Array(12001).join("a"); |
| 45 | + var out = __inspect(long); |
| 46 | + expect(out.indexOf("... 2000 more characters")).toBeGreaterThan(-1); |
| 47 | + }); |
| 48 | + |
| 49 | + it("never invokes getters", function () { |
| 50 | + var invoked = false; |
| 51 | + var obj = {}; |
| 52 | + Object.defineProperty(obj, "x", { |
| 53 | + enumerable: true, |
| 54 | + get: function () { |
| 55 | + invoked = true; |
| 56 | + return 1; |
| 57 | + } |
| 58 | + }); |
| 59 | + expect(__inspect(obj)).toBe("{ x: [Getter] }"); |
| 60 | + expect(invoked).toBe(false); |
| 61 | + }); |
| 62 | + |
| 63 | + it("formats collections, dates, regexes, errors and functions", function () { |
| 64 | + expect(__inspect(new Map([["k", 1]]))).toBe('Map(1) { "k" => 1 }'); |
| 65 | + expect(__inspect(new Set([1, 2]))).toBe("Set(2) { 1, 2 }"); |
| 66 | + expect(__inspect(new Date(0))).toBe("1970-01-01T00:00:00.000Z"); |
| 67 | + expect(__inspect(/ab+c/gi)).toBe("/ab+c/gi"); |
| 68 | + expect(__inspect(function foo() {})).toBe("[Function: foo]"); |
| 69 | + expect(__inspect(class Foo {})).toBe("[class Foo]"); |
| 70 | + expect(__inspect(new Uint8Array(3))).toBe("Uint8Array(3)"); |
| 71 | + expect(__inspect(10n)).toBe("10n"); |
| 72 | + |
| 73 | + var errOut = __inspect(new Error("boom")); |
| 74 | + expect(errOut.indexOf("Error: boom")).toBe(0); |
| 75 | + }); |
| 76 | + |
| 77 | + it("identifies java objects without walking them", function () { |
| 78 | + var out = __inspect(new java.lang.Object()); |
| 79 | + expect(out.indexOf("[")).toBe(0); |
| 80 | + expect(out.indexOf("java.lang.Object")).toBeGreaterThan(-1); |
| 81 | + |
| 82 | + var listOut = __inspect(new java.util.ArrayList()); |
| 83 | + expect(listOut.indexOf("java.util.ArrayList")).toBeGreaterThan(-1); |
| 84 | + |
| 85 | + // The wrapper hint replaces the whole graph, so a java object nested in |
| 86 | + // a plain object stays a single token. |
| 87 | + var nested = __inspect({ v: new java.lang.Object() }); |
| 88 | + expect(nested.indexOf("{ v: [")).toBe(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it("does not materialize java packages", function () { |
| 92 | + // Package children are native data properties, so reading descriptors |
| 93 | + // off one would build every class it contains. |
| 94 | + var start = Date.now(); |
| 95 | + var out = __inspect(java); |
| 96 | + expect(out.indexOf("[package java")).toBe(0); |
| 97 | + expect(__inspect(java.lang).indexOf("[package java.lang")).toBe(0); |
| 98 | + expect(Date.now() - start).toBeLessThan(1000); |
| 99 | + }); |
| 100 | + |
| 101 | + it("renders java classes as callables, not as graphs", function () { |
| 102 | + // Class wrappers are constructor functions, so they take the callable |
| 103 | + // branch before the native-wrapper hint is consulted. |
| 104 | + var out = __inspect(java.lang.Object); |
| 105 | + expect(/^\[(Function|class)\b/.test(out)).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it("leaves plain javascript objects to structural rendering", function () { |
| 109 | + expect(__inspect({ a: 1 })).toBe("{ a: 1 }"); |
| 110 | + expect(__inspect([1])).toBe("[ 1 ]"); |
| 111 | + }); |
| 112 | + |
| 113 | + it("console.log of a huge cyclic object completes quickly", function () { |
| 114 | + var huge = { name: "root" }; |
| 115 | + var cursor = huge; |
| 116 | + for (var i = 0; i < 5000; i++) { |
| 117 | + cursor = cursor.child = { i: i, parent: huge }; |
| 118 | + } |
| 119 | + huge.self = huge; |
| 120 | + var start = Date.now(); |
| 121 | + console.log(huge); |
| 122 | + expect(Date.now() - start).toBeLessThan(1000); |
| 123 | + }); |
| 124 | + |
| 125 | + it("honors custom toString overrides (NativeScript core convention)", function () { |
| 126 | + function ViewLike() { this.id = 42; } |
| 127 | + ViewLike.prototype.toString = function () { return "Button(42)"; }; |
| 128 | + expect(__inspect(new ViewLike())).toBe("Button(42)"); |
| 129 | + expect(__inspect({ v: new ViewLike() })).toBe("{ v: Button(42) }"); |
| 130 | + expect(__inspect({ toString: function () { return "custom!"; } })).toBe("custom!"); |
| 131 | + // A broken override degrades to structural rendering instead of hiding data. |
| 132 | + var broken = { a: 1, toString: function () { throw new Error("x"); } }; |
| 133 | + expect(__inspect(broken).indexOf("a: 1")).toBeGreaterThan(-1); |
| 134 | + }); |
| 135 | + |
| 136 | + it("formats under tampered prototypes", function () { |
| 137 | + var savedSlice = Array.prototype.slice; |
| 138 | + var savedIndexOf = Array.prototype.indexOf; |
| 139 | + var savedKeys = Object.keys; |
| 140 | + var savedStringify = JSON.stringify; |
| 141 | + var boom = function () { throw new Error("tampered"); }; |
| 142 | + var out; |
| 143 | + try { |
| 144 | + Array.prototype.slice = boom; |
| 145 | + Array.prototype.indexOf = boom; |
| 146 | + Object.keys = boom; |
| 147 | + JSON.stringify = boom; |
| 148 | + out = __inspect({ a: [1, 2], m: new Map([[1, 2]]) }); |
| 149 | + } finally { |
| 150 | + Array.prototype.slice = savedSlice; |
| 151 | + Array.prototype.indexOf = savedIndexOf; |
| 152 | + Object.keys = savedKeys; |
| 153 | + JSON.stringify = savedStringify; |
| 154 | + } |
| 155 | + expect(out).toBe("{ a: [ 1, 2 ], m: Map(1) { 1 => 2 } }"); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
0 commit comments