diff options
Diffstat (limited to 'src/bun.js/test/pretty_format.zig')
-rw-r--r-- | src/bun.js/test/pretty_format.zig | 67 |
1 files changed, 36 insertions, 31 deletions
diff --git a/src/bun.js/test/pretty_format.zig b/src/bun.js/test/pretty_format.zig index 15ab88799..a6c6aa631 100644 --- a/src/bun.js/test/pretty_format.zig +++ b/src/bun.js/test/pretty_format.zig @@ -15,6 +15,7 @@ const JSPrinter = bun.js_printer; const JSPrivateDataPtr = JSC.JSPrivateDataPtr; const JS = @import("../javascript.zig"); const JSPromise = JSC.JSPromise; +const expect = @import("./expect.zig"); pub const EventType = enum(u8) { Event, @@ -362,7 +363,7 @@ pub const JestPrettyFormat = struct { }; pub fn get(value: JSValue, globalThis: *JSGlobalObject) Result { - switch (@enumToInt(value)) { + switch (@intFromEnum(value)) { 0, 0xa => return Result{ .tag = .Undefined, }, @@ -422,23 +423,20 @@ pub const JestPrettyFormat = struct { // If we check an Object has a method table and it does not // it will crash - const callable = js_type != .Object and value.isCallable(globalThis.vm()); + if (js_type != .Object and value.isCallable(globalThis.vm())) { + if (value.isClass(globalThis)) { + return .{ + .tag = .Class, + .cell = js_type, + }; + } - if (value.isClass(globalThis) and !callable) { return .{ - .tag = .Object, - .cell = js_type, - }; - } - - if (callable and js_type == .JSFunction) { - return .{ - .tag = .Function, - .cell = js_type, - }; - } else if (callable and js_type == .InternalFunction) { - return .{ - .tag = .Object, + // TODO: we print InternalFunction as Object because we have a lot of + // callable namespaces and printing the contents of it is better than [Function: namespace] + // ideally, we would print [Function: namespace] { ... } on all functions, internal and js. + // what we'll do later is rid of .Function and .Class and handle the prefix in the .Object formatter + .tag = if (js_type == .InternalFunction) .Object else .Function, .cell = js_type, }; } @@ -749,7 +747,7 @@ pub const JestPrettyFormat = struct { parent: JSValue, const enable_ansi_colors = enable_ansi_colors_; pub fn handleFirstProperty(this: *@This(), globalThis: *JSC.JSGlobalObject, value: JSValue) void { - if (!value.jsType().isFunction() and !value.isClass(globalThis)) { + if (!value.jsType().isFunction()) { var writer = WrappedWriter(Writer){ .ctx = this.writer, .failed = false, @@ -921,7 +919,7 @@ pub const JestPrettyFormat = struct { this.map = this.map_node.?.data; } - var entry = this.map.getOrPut(@enumToInt(value)) catch unreachable; + var entry = this.map.getOrPut(@intFromEnum(value)) catch unreachable; if (entry.found_existing) { writer.writeAll(comptime Output.prettyFmt("<r><cyan>[Circular]<r>", enable_ansi_colors)); return; @@ -930,7 +928,7 @@ pub const JestPrettyFormat = struct { defer { if (comptime Format.canHaveCircularReferences()) { - _ = this.map.remove(@enumToInt(value)); + _ = this.map.remove(@intFromEnum(value)); } } @@ -1049,7 +1047,7 @@ pub const JestPrettyFormat = struct { i = -i; } const digits = if (i != 0) - bun.fmt.fastDigitCount(@intCast(usize, i)) + @as(usize, @boolToInt(is_negative)) + bun.fmt.fastDigitCount(@intCast(usize, i)) + @as(usize, @intFromBool(is_negative)) else 1; this.addForNewLine(digits); @@ -1125,13 +1123,20 @@ pub const JestPrettyFormat = struct { this.addForNewLine(printable.len); if (printable.len == 0) { - writer.print(comptime Output.prettyFmt("[class]", enable_ansi_colors), .{}); + writer.print(comptime Output.prettyFmt("<cyan>[class]<r>", enable_ansi_colors), .{}); } else { - writer.print(comptime Output.prettyFmt("[class <cyan>{}<r>]", enable_ansi_colors), .{printable}); + writer.print(comptime Output.prettyFmt("<cyan>[class {}]<r>", enable_ansi_colors), .{printable}); } }, .Function => { - writer.writeAll("[Function]"); + var printable = ZigString.init(&name_buf); + value.getNameProperty(this.globalThis, &printable); + + if (printable.len == 0) { + writer.print(comptime Output.prettyFmt("<cyan>[Function]<r>", enable_ansi_colors), .{}); + } else { + writer.print(comptime Output.prettyFmt("<cyan>[Function: {}]<r>", enable_ansi_colors), .{printable}); + } }, .Array => { const len = @truncate(u32, value.getLength(this.globalThis)); @@ -1264,12 +1269,12 @@ pub const JestPrettyFormat = struct { } else if (value.as(JSC.ResolveMessage)) |resolve_log| { resolve_log.msg.writeFormat(writer_, enable_ansi_colors) catch {}; return; - } else if (value.as(JSC.Jest.ExpectAnything) != null) { + } else if (value.as(expect.ExpectAnything) != null) { this.addForNewLine("Anything".len); writer.writeAll("Anything"); return; - } else if (value.as(JSC.Jest.ExpectAny) != null) { - const constructor_value = JSC.Jest.ExpectAny.constructorValueGetCached(value) orelse return; + } else if (value.as(expect.ExpectAny) != null) { + const constructor_value = expect.ExpectAny.constructorValueGetCached(value) orelse return; this.addForNewLine("Any<".len); writer.writeAll("Any<"); @@ -1281,16 +1286,16 @@ pub const JestPrettyFormat = struct { writer.writeAll(">"); return; - } else if (value.as(JSC.Jest.ExpectStringContaining) != null) { - const substring_value = JSC.Jest.ExpectStringContaining.stringValueGetCached(value) orelse return; + } else if (value.as(expect.ExpectStringContaining) != null) { + const substring_value = expect.ExpectStringContaining.stringValueGetCached(value) orelse return; this.addForNewLine("StringContaining ".len); writer.writeAll("StringContaining "); this.printAs(.String, Writer, writer_, substring_value, .String, enable_ansi_colors); return; - } else if (value.as(JSC.Jest.ExpectStringMatching) != null) { - const test_value = JSC.Jest.ExpectStringMatching.testValueGetCached(value) orelse return; + } else if (value.as(expect.ExpectStringMatching) != null) { + const test_value = expect.ExpectStringMatching.testValueGetCached(value) orelse return; this.addForNewLine("StringMatching ".len); writer.writeAll("StringMatching "); @@ -1553,7 +1558,7 @@ pub const JestPrettyFormat = struct { { this.indent += 1; defer this.indent -|= 1; - const count_without_children = props_iter.len - @as(usize, @boolToInt(children_prop != null)); + const count_without_children = props_iter.len - @as(usize, @intFromBool(children_prop != null)); while (props_iter.next()) |prop| { if (prop.eqlComptime("children")) |