aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/api/bun.zig9
-rw-r--r--src/bun.js/bindings/exports.zig46
-rw-r--r--src/bun.js/test/jest.zig72
3 files changed, 70 insertions, 57 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index f979472dc..4dcbad374 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -243,10 +243,11 @@ pub fn inspect(
Writer,
Writer,
writer,
- false,
- false,
- false,
- false,
+ .{
+ .enable_colors = false,
+ .add_newline = false,
+ .flush = false,
+ },
);
buffered_writer.flush() catch {
return JSC.C.JSValueMakeUndefined(ctx);
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig
index 1d4c01778..9e1f03592 100644
--- a/src/bun.js/bindings/exports.zig
+++ b/src/bun.js/bindings/exports.zig
@@ -964,10 +964,11 @@ pub const ZigConsoleClient = struct {
@TypeOf(buffered_writer.unbuffered_writer.context),
Writer,
writer,
- enable_colors,
- true,
- true,
- false,
+ .{
+ .enable_colors = enable_colors,
+ .add_newline = true,
+ .flush = true,
+ },
)
else if (message_type == .Log) {
_ = console.writer.write("\n") catch 0;
@@ -1005,6 +1006,14 @@ pub const ZigConsoleClient = struct {
) catch unreachable;
}
+ pub const FormatOptions = struct {
+ enable_colors: bool,
+ add_newline: bool,
+ flush: bool,
+ ordered_properties: bool = false,
+ quote_strings: bool = false,
+ };
+
pub fn format(
level: MessageLevel,
global: *JSGlobalObject,
@@ -1013,10 +1022,7 @@ pub const ZigConsoleClient = struct {
comptime RawWriter: type,
comptime Writer: type,
writer: Writer,
- enable_colors: bool,
- add_newline: bool,
- flush: bool,
- order_properties: bool,
+ options: FormatOptions,
) void {
var fmt: ZigConsoleClient.Formatter = undefined;
defer {
@@ -1031,7 +1037,8 @@ pub const ZigConsoleClient = struct {
fmt = ZigConsoleClient.Formatter{
.remaining_values = &[_]JSValue{},
.globalThis = global,
- .ordered_properties = order_properties,
+ .ordered_properties = options.ordered_properties,
+ .quote_strings = options.quote_strings,
};
const tag = ZigConsoleClient.Formatter.Tag.get(vals[0], global);
@@ -1041,7 +1048,7 @@ pub const ZigConsoleClient = struct {
writer;
if (tag.tag == .String) {
- if (enable_colors) {
+ if (options.enable_colors) {
if (level == .Error) {
unbuffered_writer.writeAll(comptime Output.prettyFmt("<r><red>", true)) catch unreachable;
}
@@ -1066,14 +1073,14 @@ pub const ZigConsoleClient = struct {
false,
);
}
- if (add_newline) _ = unbuffered_writer.write("\n") catch 0;
+ if (options.add_newline) _ = unbuffered_writer.write("\n") catch 0;
} else {
defer {
if (comptime Writer != RawWriter) {
- if (flush) writer.context.flush() catch {};
+ if (options.flush) writer.context.flush() catch {};
}
}
- if (enable_colors) {
+ if (options.enable_colors) {
fmt.format(
tag,
Writer,
@@ -1092,7 +1099,7 @@ pub const ZigConsoleClient = struct {
false,
);
}
- if (add_newline) _ = writer.write("\n") catch 0;
+ if (options.add_newline) _ = writer.write("\n") catch 0;
}
return;
@@ -1100,7 +1107,7 @@ pub const ZigConsoleClient = struct {
defer {
if (comptime Writer != RawWriter) {
- if (flush) writer.context.flush() catch {};
+ if (options.flush) writer.context.flush() catch {};
}
}
@@ -1108,12 +1115,13 @@ pub const ZigConsoleClient = struct {
fmt = ZigConsoleClient.Formatter{
.remaining_values = vals[0..len][1..],
.globalThis = global,
- .ordered_properties = order_properties,
+ .ordered_properties = options.ordered_properties,
+ .quote_strings = options.quote_strings,
};
var tag: ZigConsoleClient.Formatter.Tag.Result = undefined;
var any = false;
- if (enable_colors) {
+ if (options.enable_colors) {
if (level == .Error) {
writer.writeAll(comptime Output.prettyFmt("<r><red>", true)) catch unreachable;
}
@@ -1159,7 +1167,7 @@ pub const ZigConsoleClient = struct {
}
}
- if (add_newline) _ = writer.write("\n") catch 0;
+ if (options.add_newline) _ = writer.write("\n") catch 0;
}
pub const Formatter = struct {
@@ -1889,7 +1897,6 @@ pub const ZigConsoleClient = struct {
writer.print(comptime Output.prettyFmt("<r><red>", enable_ansi_colors), .{});
}
- if (jsType != .RegExpObject) writer.writeAll("\"");
if (str.is16Bit()) {
// streaming print
writer.print("{s}", .{str});
@@ -1904,7 +1911,6 @@ pub const ZigConsoleClient = struct {
writer.writeAll(buf);
}
}
- if (jsType != .RegExpObject) writer.writeAll("\"");
if (jsType == .RegExpObject and enable_ansi_colors) {
writer.print(comptime Output.prettyFmt("<r>", enable_ansi_colors), .{});
diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig
index 41d7d41f7..2a8ccf3f4 100644
--- a/src/bun.js/test/jest.zig
+++ b/src/bun.js/test/jest.zig
@@ -92,10 +92,13 @@ pub const DiffFormatter = struct {
Writer,
Writer,
buf_writer,
- false,
- false,
- false,
- true,
+ .{
+ .enable_colors = false,
+ .add_newline = false,
+ .flush = false,
+ .ordered_properties = true,
+ .quote_strings = true,
+ },
);
buffered_writer.flush() catch unreachable;
@@ -109,10 +112,13 @@ pub const DiffFormatter = struct {
Writer,
Writer,
buf_writer,
- false,
- false,
- false,
- true,
+ .{
+ .enable_colors = false,
+ .add_newline = false,
+ .flush = false,
+ .ordered_properties = true,
+ .quote_strings = true,
+ },
);
buffered_writer.flush() catch unreachable;
}
@@ -133,7 +139,7 @@ pub const DiffFormatter = struct {
switch (this.received.determineDiffMethod(this.expected, this.globalObject)) {
.none => {
const fmt = "Expected: <green>{any}<r>\nReceived: <red>{any}<r>";
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = this.globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = this.globalObject, .quote_strings = true };
if (Output.enable_ansi_colors) {
try writer.print(Output.prettyFmt(fmt, true), .{
this.expected.toFmt(this.globalObject, &formatter),
@@ -564,7 +570,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
if (not) {
const signature = comptime getSignature("toBe", "<green>expected<r>", true);
const fmt = signature ++ "\n\nExpected: not <green>{any}<r>\n";
@@ -659,20 +665,20 @@ pub const Expect = struct {
value.ensureStillAlive();
if (!value.isObject() and !value.isString()) {
- var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
globalObject.throw("Received value does not have a length property: {any}", .{value.toFmt(globalObject, &fmt)});
return .zero;
}
if (!expected.isNumber()) {
- var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
globalObject.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalObject, &fmt)});
return .zero;
}
const expected_length: f64 = expected.asNumber();
if (@round(expected_length) != expected_length or std.math.isInf(expected_length) or std.math.isNan(expected_length) or expected_length < 0) {
- var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
globalObject.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalObject, &fmt)});
return .zero;
}
@@ -688,11 +694,11 @@ pub const Expect = struct {
const length_value: JSValue = value.getIfPropertyExistsImpl(globalObject, "length", "length".len);
if (length_value.isEmpty()) {
- var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
globalObject.throw("Received value does not have a length property: {any}", .{value.toFmt(globalObject, &fmt)});
return .zero;
} else if (!length_value.isNumber()) {
- var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
globalObject.throw("Received value has non-number length property: {any}", .{length_value.toFmt(globalObject, &fmt)});
return .zero;
}
@@ -788,7 +794,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
const expected_fmt = expected.toFmt(globalObject, &formatter);
if (not) {
@@ -841,7 +847,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
if (not) {
const received_line = "Received: <red>{any}<r>\n";
@@ -885,7 +891,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
if (not) {
const received_line = "Received: <red>{any}<r>\n";
@@ -933,7 +939,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
if (not) {
const received_line = "Received: <red>{any}<r>\n";
@@ -976,7 +982,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
if (not) {
const received_line = "Received: <red>{any}<r>\n";
@@ -1019,7 +1025,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
if (not) {
const received_line = "Received: <red>{any}<r>\n";
@@ -1065,7 +1071,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
if (not) {
const received_line = "Received: <red>{any}<r>\n";
@@ -1253,7 +1259,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
if (not) {
if (expected_property != null) {
const signature = comptime getSignature("toHaveProperty", "<green>path<r><d>, <r><green>value<r>", true);
@@ -1389,7 +1395,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
const expected_fmt = other_value.toFmt(globalObject, &formatter);
if (not) {
@@ -1472,7 +1478,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
const expected_fmt = other_value.toFmt(globalObject, &formatter);
if (not) {
@@ -1552,7 +1558,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
const expected_fmt = other_value.toFmt(globalObject, &formatter);
if (not) {
@@ -1632,7 +1638,7 @@ pub const Expect = struct {
if (pass) return thisValue;
// handle failure
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const value_fmt = value.toFmt(globalObject, &formatter);
const expected_fmt = other_value.toFmt(globalObject, &formatter);
if (not) {
@@ -1675,7 +1681,7 @@ pub const Expect = struct {
const expected_value: JSValue = if (arguments.len > 0) brk: {
const value = arguments[0];
if (value.isEmptyOrUndefinedOrNull() or !value.isObject() and !value.isString()) {
- var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
globalObject.throw("Expected value must be string or Error: {any}", .{value.toFmt(globalObject, &fmt)});
return .zero;
}
@@ -1731,7 +1737,7 @@ pub const Expect = struct {
if (!did_throw) return thisValue;
const result: JSValue = result_.?;
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
if (expected_value.isEmpty()) {
const signature_no_args = comptime getSignature("toThrow", "", true);
@@ -1848,7 +1854,7 @@ pub const Expect = struct {
}
// error: message from received error does not match expected string
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
if (_received_message) |received_message| {
const expected_value_fmt = expected_value.toFmt(globalObject, &formatter);
@@ -1881,7 +1887,7 @@ pub const Expect = struct {
}
// error: message from received error does not match expected error message.
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
if (_received_message) |received_message| {
const expected_fmt = expected_message.toFmt(globalObject, &formatter);
@@ -1911,7 +1917,7 @@ pub const Expect = struct {
if (result.isInstanceOf(globalObject, expected_value)) return thisValue;
// error: received error not instance of received error constructor
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
var expected_class = ZigString.Empty;
var received_class = ZigString.Empty;
expected_value.getClassName(globalObject, &expected_class);
@@ -1958,7 +1964,7 @@ pub const Expect = struct {
}
// did not throw
- var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject };
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const received_line = "Received function did not throw\n";
if (expected_value.isEmpty()) {