Skip to content

Commit

Permalink
feat: support code for @json.inspect (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunchen authored Oct 9, 2024
1 parent b1ddeb8 commit b061a30
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
49 changes: 47 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/moonbuild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ semver.workspace = true
chrono.workspace = true
zip.workspace = true
thiserror.workspace = true
json-structural-diff = { version = "0.1.0", features = ["colorize"] }

[dev-dependencies]
expect-test.workspace = true
57 changes: 45 additions & 12 deletions crates/moonbuild/src/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct BufferExpect {
expect: String,
actual: String,
kind: TargetKind,
mode: Option<String>,
}

// something like array out of bounds, moonbit panic & abort catch by js
Expand Down Expand Up @@ -81,6 +82,7 @@ pub struct Target {
kind: TargetKind,
expect: String,
actual: String,
mode: Option<String>,
}

#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
Expand All @@ -90,6 +92,7 @@ pub struct ExpectFailedRaw {
pub expect: String,
pub actual: String,
pub snapshot: Option<bool>,
pub mode: Option<String>,
}

pub fn expect_failed_to_snapshot_result(efr: ExpectFailedRaw) -> SnapshotResult {
Expand Down Expand Up @@ -167,6 +170,8 @@ struct Replace {

pub expect: String,
pub expect_loc: Option<Location>,

pub mode: Option<String>,
}

impl Replace {
Expand All @@ -183,6 +188,7 @@ impl Replace {
kind: TargetKind::Trivial,
expect: self.expect.clone(),
actual: self.actual.clone(),
mode: self.mode.clone(),
})
} else {
let is_pipe = self.actual_loc.ahead(&self.loc);
Expand All @@ -197,6 +203,7 @@ impl Replace {
kind: TargetKind::Pipe,
expect: self.expect.clone(),
actual: self.actual.clone(),
mode: self.mode.clone(),
})
} else {
// TODO: find comma
Expand All @@ -211,6 +218,7 @@ impl Replace {
kind: TargetKind::Call,
expect: self.expect.clone(),
actual: self.actual.clone(),
mode: self.mode.clone(),
})
}
}
Expand Down Expand Up @@ -246,6 +254,7 @@ fn parse_expect_failed_message(msg: &str) -> anyhow::Result<Replace> {
expect_loc,
actual: j.actual,
actual_loc,
mode: j.mode,
})
}

Expand Down Expand Up @@ -431,6 +440,7 @@ fn gen_patch(targets: HashMap<String, BTreeSet<Target>>) -> anyhow::Result<Packa
expect: t.expect,
actual: t.actual,
kind: t.kind,
mode: t.mode.clone(),
});
}

Expand Down Expand Up @@ -535,18 +545,28 @@ fn apply_patch(pp: &PackagePatch) -> anyhow::Result<()> {
}
}

if !patch.actual.contains('\n') && !patch.actual.contains('"') {
output.push_str(&format!("{:?}", &patch.actual));
} else {
let next_char = content_chars[usize::from(end)..].first();
let prev_char = content_chars[..usize::from(start)].last();
push_multi_line_string(
&mut output,
spaces + 2,
&patch.actual,
prev_char,
next_char,
);
match patch.mode.as_deref() {
None => {
if !patch.actual.contains('\n') && !patch.actual.contains('"') {
output.push_str(&format!("{:?}", &patch.actual));
} else {
let next_char = content_chars[usize::from(end)..].first();
let prev_char = content_chars[..usize::from(start)].last();
push_multi_line_string(
&mut output,
spaces + 2,
&patch.actual,
prev_char,
next_char,
);
}
}
Some("json") => {
output.push_str(&patch.actual.to_string());
}
Some(mode) => {
anyhow::bail!("unsupported mode: {:?} in expect testing", mode);
}
}

if let Some(padding) = patch.right_padding {
Expand Down Expand Up @@ -639,6 +659,19 @@ pub fn render_expect_fail(msg: &str) -> anyhow::Result<()> {
let json_str = &msg[EXPECT_FAILED.len()..];
let rep = parse_expect_failed_message(json_str)?;

if let Some("json") = rep.mode.as_deref() {
let j_expect = serde_json_lenient::from_str(&rep.expect)?;
let j_actual = serde_json_lenient::from_str(&rep.actual)?;
let diffs = json_structural_diff::JsonDiff::diff(&j_expect, &j_actual, false);
if let Some(diff) = diffs.diff {
let diffs = json_structural_diff::colorize(&diff, true);
println!("inspect failed at {}", rep.loc.raw);
println!("{}", "Diff:".bold());
println!("{}", diffs);
}
return Ok(());
}

let d = dissimilar::diff(&rep.expect, &rep.actual);
println!(
r#"expect test failed at {}
Expand Down

0 comments on commit b061a30

Please sign in to comment.