Skip to content

Commit

Permalink
Toggleable JsValue internals displaying
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Feb 23, 2022
1 parent 2c19c6a commit e6cda69
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
16 changes: 15 additions & 1 deletion boa_engine/src/value/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ use super::{fmt, Display, HashSet, JsValue, PropertyKey};
#[derive(Debug, Clone, Copy)]
pub struct ValueDisplay<'value> {
pub(super) value: &'value JsValue,
pub(super) internals: bool,
}

impl ValueDisplay<'_> {
/// Display internal information about value.
///
/// By default this is `false`.
#[inline]
pub fn internals(mut self, yes: bool) -> Self {
self.internals = yes;
self
}
}

/// A helper macro for printing objects
Expand Down Expand Up @@ -275,7 +287,9 @@ impl Display for ValueDisplay<'_> {
},
JsValue::String(ref v) => write!(f, "\"{v}\""),
JsValue::Rational(v) => format_rational(*v, f),
JsValue::Object(_) => write!(f, "{}", log_string_from(self.value, true, true)),
JsValue::Object(_) => {
write!(f, "{}", log_string_from(self.value, self.internals, true))
}
JsValue::Integer(v) => write!(f, "{v}"),
JsValue::BigInt(ref num) => write!(f, "{num}n"),
}
Expand Down
8 changes: 7 additions & 1 deletion boa_engine/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ impl JsValue {

/// Returns an object that implements `Display`.
///
/// By default the internals are not shown, but they can be toggled
/// with [`ValueDisplay::internals`] method.
///
/// # Examples
///
/// ```
Expand All @@ -505,7 +508,10 @@ impl JsValue {
/// ```
#[inline]
pub fn display(&self) -> ValueDisplay<'_> {
ValueDisplay { value: self }
ValueDisplay {
value: self,
internals: false,
}
}

/// Converts the value to a string.
Expand Down
7 changes: 5 additions & 2 deletions boa_tester/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ impl Test {
match context.eval(&self.content.as_ref()) {
Ok(res) => (false, res.display().to_string()),
Err(e) => {
let passed =
e.display().to_string().contains(error_type.as_ref());
let passed = e
.display()
.internals(true)
.to_string()
.contains(error_type.as_ref());

(passed, format!("Uncaught {}", e.display()))
}
Expand Down

0 comments on commit e6cda69

Please sign in to comment.