Skip to content

Commit 8b510e5

Browse files
committed
feat: add body to snapshots
1 parent 9c48c01 commit 8b510e5

22 files changed

Lines changed: 436 additions & 8 deletions

libs/@local/hashql/eval/src/postgres/filter/tests.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use hash_graph_postgres_store::store::postgres::query::{Expression, Transpile as
1515
use hashql_core::{
1616
heap::{Heap, Scratch},
1717
id::Id as _,
18+
pretty::Formatter,
1819
symbol::sym,
19-
r#type::{TypeBuilder, TypeId, environment::Environment},
20+
r#type::{TypeBuilder, TypeFormatter, TypeFormatterOptions, TypeId, environment::Environment},
2021
};
2122
use hashql_diagnostics::DiagnosticIssues;
2223
use hashql_hir::node::operation::InputOp;
@@ -31,6 +32,7 @@ use hashql_mir::{
3132
analysis::SizeEstimationAnalysis,
3233
execution::{ExecutionAnalysis, ExecutionAnalysisResidual, IslandKind, TargetId},
3334
},
35+
pretty::TextFormatOptions,
3436
};
3537
use insta::{Settings, assert_snapshot};
3638

@@ -101,27 +103,48 @@ struct FilterIslandReport {
101103
}
102104

103105
struct FilterReport {
106+
body: String,
104107
islands: Vec<FilterIslandReport>,
105108
}
106109

107110
impl core::fmt::Display for FilterReport {
108111
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
109-
for (i, island) in self.islands.iter().enumerate() {
110-
if i > 0 {
111-
writeln!(f)?;
112-
}
112+
writeln!(f, "{:=^80}\n", " MIR ")?;
113+
write!(f, "{}", self.body)?;
114+
115+
for island in &self.islands {
113116
let label = format!(
114117
" Island (entry: bb{}, target: {}) ",
115118
island.entry_block.as_u32(),
116119
island.target,
117120
);
118-
writeln!(f, "{label:=^80}\n")?;
121+
writeln!(f, "\n{label:=^80}\n")?;
119122
write!(f, "{}", island.sql)?;
120123
}
121124
Ok(())
122125
}
123126
}
124127

128+
fn format_body<'heap>(fixture: &Fixture<'heap>, heap: &'heap Heap) -> String {
129+
let formatter = Formatter::new(heap);
130+
let mut type_formatter =
131+
TypeFormatter::new(&formatter, &fixture.env, TypeFormatterOptions::terse());
132+
133+
let mut text_format = TextFormatOptions {
134+
writer: Vec::<u8>::new(),
135+
indent: 4,
136+
sources: (),
137+
types: &mut type_formatter,
138+
annotations: (),
139+
}
140+
.build();
141+
142+
let body = &fixture.bodies[fixture.def()];
143+
text_format.format_body(body).expect("formatting failed");
144+
145+
String::from_utf8(text_format.writer).expect("valid UTF-8")
146+
}
147+
125148
fn compile_filter_islands<'heap>(fixture: &Fixture<'heap>, heap: &'heap Heap) -> FilterReport {
126149
let mut scratch = Scratch::new();
127150
let def = fixture.def();
@@ -177,6 +200,7 @@ fn compile_filter_islands<'heap>(fixture: &Fixture<'heap>, heap: &'heap Heap) ->
177200
}
178201

179202
FilterReport {
203+
body: format_body(fixture, heap),
180204
islands: island_reports,
181205
}
182206
}
@@ -199,13 +223,17 @@ fn find_entry_block(
199223
unreachable!("The postgres island always has an entry block (BasicBlockId::START)")
200224
}
201225
struct QueryReport {
226+
body: String,
202227
sql: String,
203228
parameters: String,
204229
}
205230

206231
impl core::fmt::Display for QueryReport {
207232
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
208-
writeln!(f, "{:=^80}\n", " SQL ")?;
233+
writeln!(f, "{:=^80}\n", " MIR ")?;
234+
write!(f, "{}", self.body)?;
235+
236+
writeln!(f, "\n{:=^80}\n", " SQL ")?;
209237
write!(f, "{}", self.sql)?;
210238

211239
if !self.parameters.is_empty() {
@@ -264,7 +292,11 @@ fn compile_full_query_with_mask<'heap>(
264292
let sql = prepared_query.transpile().to_string();
265293
let parameters = format!("{}", prepared_query.parameters);
266294

267-
QueryReport { sql, parameters }
295+
QueryReport {
296+
body: format_body(fixture, heap),
297+
sql,
298+
parameters,
299+
}
268300
}
269301

270302
fn snapshot_settings() -> Settings {

libs/@local/hashql/eval/tests/ui/postgres/filter/binary_bitand_bigint_cast.snap

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/eval/tests/ui/postgres/filter/binary_sub_numeric_cast.snap

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/eval/tests/ui/postgres/filter/data_island_provides_without_lateral.snap

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/eval/tests/ui/postgres/filter/diamond_cfg_merge.snap

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/eval/tests/ui/postgres/filter/dynamic_index_projection.snap

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/eval/tests/ui/postgres/filter/field_by_name_projection.snap

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/eval/tests/ui/postgres/filter/field_index_projection.snap

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/eval/tests/ui/postgres/filter/island_exit_empty_arrays.snap

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/eval/tests/ui/postgres/filter/island_exit_goto.snap

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)