From 11b9a9021e23b3a68ebb2c529dc03d10d10cd6d7 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 10 Apr 2025 10:34:27 -0700 Subject: [PATCH] more tests --- rust/pspp/src/output/pivot/mod.rs | 8 ++- rust/pspp/src/output/pivot/output.rs | 22 +++----- rust/pspp/src/output/pivot/test.rs | 84 +++++++++++++++++++++++----- 3 files changed, 82 insertions(+), 32 deletions(-) diff --git a/rust/pspp/src/output/pivot/mod.rs b/rust/pspp/src/output/pivot/mod.rs index e0cb9be00a..8e315100b8 100644 --- a/rust/pspp/src/output/pivot/mod.rs +++ b/rust/pspp/src/output/pivot/mod.rs @@ -444,11 +444,13 @@ impl GroupBuilder { self } pub fn with_label_hidden(mut self) -> Self { - self.show_label = Some(false); - self + self.with_show_label(false) } pub fn with_label_shown(mut self) -> Self { - self.show_label = Some(true); + self.with_show_label(true) + } + pub fn with_show_label(mut self, show_label: bool) -> Self { + self.show_label = Some(show_label); self } fn len(&self) -> usize { diff --git a/rust/pspp/src/output/pivot/output.rs b/rust/pspp/src/output/pivot/output.rs index ce2d32c372..8e9db75836 100644 --- a/rust/pspp/src/output/pivot/output.rs +++ b/rust/pspp/src/output/pivot/output.rs @@ -533,8 +533,12 @@ impl<'a> Headings<'a> { let row_label_position = if h == Axis2::Y && pt.look.row_label_position == LabelPosition::Corner - && !headings.is_empty() - && headings[0].move_dimension_labels_to_corner() + && headings + .iter_mut() + .map(|heading| heading.move_dimension_labels_to_corner()) + .filter(|x| *x) + .count() + > 0 { LabelPosition::Corner } else { @@ -575,18 +579,8 @@ impl<'a> Headings<'a> { vrules[0] = true; vrules[n_columns] = true; - // The first heading takes the axis's row label position, the remainder - // are all nested. - let dimension_label_positions = std::iter::once(self.row_label_position) - .chain(std::iter::repeat(LabelPosition::Nested)); - let mut v_ofs = 0; - for ((index, heading), dimension_label_position) in self - .headings - .iter() - .enumerate() - .zip(dimension_label_positions) - { + for (index, heading) in self.headings.iter().enumerate() { let inner = index == self.headings.len() - 1; heading.render( table, @@ -598,7 +592,7 @@ impl<'a> Headings<'a> { rotate_inner_labels, rotate_outer_labels, inner, - dimension_label_position, + self.row_label_position, ); v_ofs += heading.height; if !inner { diff --git a/rust/pspp/src/output/pivot/test.rs b/rust/pspp/src/output/pivot/test.rs index 1e61180102..4a131c408f 100644 --- a/rust/pspp/src/output/pivot/test.rs +++ b/rust/pspp/src/output/pivot/test.rs @@ -71,14 +71,14 @@ fn test_look() -> Arc { Arc::new(look) } -fn d2(title: &str, axes: [Axis3; 2]) -> PivotTable { - let mut a = GroupBuilder::new(Value::new_text("a")); +fn d2(title: &str, axes: [Axis3; 2], dimension_labels: bool) -> PivotTable { + let mut a = GroupBuilder::new(Value::new_text("a")).with_show_label(dimension_labels); for name in ["a1", "a2", "a3"] { a.push(Value::new_text(name)); } let d1 = DimensionBuilder::new(axes[0], a); - let mut b = GroupBuilder::new(Value::new_text("b")); + let mut b = GroupBuilder::new(Value::new_text("b")).with_show_label(dimension_labels); for name in ["b1", "b2", "b3"] { b.push(Value::new_text(name)); } @@ -95,10 +95,18 @@ fn d2(title: &str, axes: [Axis3; 2]) -> PivotTable { pt.with_look(test_look()).build() } +#[track_caller] +fn assert_rendering(pivot_table: PivotTable, expected: &str) { + let actual = pivot_table.to_string(); + if actual != expected { + panic!("Unexpected pivot table rendering.\nActual:\n{actual}\nExpected:\n{expected}"); + } +} + #[test] fn d2_cc() { - assert_eq!( - d2("Columns", [Axis3::X, Axis3::X]).to_string(), + assert_rendering( + d2("Columns", [Axis3::X, Axis3::X], false), "\ Columns ╭────────┬────────┬────────╮ @@ -108,14 +116,35 @@ Columns ├──┼──┼──┼──┼──┼──┼──┼──┼──┤ │ 0│ 1│ 2│ 3│ 4│ 5│ 6│ 7│ 8│ ╰──┴──┴──┴──┴──┴──┴──┴──┴──╯ -" +", + ); +} + +#[test] +fn d2_cc_with_dim_labels() { + assert_rendering( + d2("Columns", [Axis3::X, Axis3::X], true), + "\ +Columns +╭──────────────────────────╮ +│ b │ +├────────┬────────┬────────┤ +│ b1 │ b2 │ b3 │ +├────────┼────────┼────────┤ +│ a │ a │ a │ +├──┬──┬──┼──┬──┬──┼──┬──┬──┤ +│a1│a2│a3│a1│a2│a3│a1│a2│a3│ +├──┼──┼──┼──┼──┼──┼──┼──┼──┤ +│ 0│ 1│ 2│ 3│ 4│ 5│ 6│ 7│ 8│ +╰──┴──┴──┴──┴──┴──┴──┴──┴──╯ +", ); } #[test] fn d2_rr() { - assert_eq!( - d2("Rows", [Axis3::Y, Axis3::Y]).to_string(), + assert_rendering( + d2("Rows", [Axis3::Y, Axis3::Y], false), "\ Rows ╭─────┬─╮ @@ -131,14 +160,39 @@ Rows │ a2│7│ │ a3│8│ ╰─────┴─╯ -" +", + ); +} + +#[test] +fn d2_rr_with_dim_labels() { + assert_rendering( + d2("Rows - Corner", [Axis3::Y, Axis3::Y], true), + "\ +Rows - Corner +╭─────┬─╮ +│b a │ │ +├─────┼─┤ +│b1 a1│0│ +│ a2│1│ +│ a3│2│ +├─────┼─┤ +│b2 a1│3│ +│ a2│4│ +│ a3│5│ +├─────┼─┤ +│b3 a1│6│ +│ a2│7│ +│ a3│8│ +╰─────┴─╯ +", ); } #[test] fn d2_cr() { - assert_eq!( - d2("Column x Row", [Axis3::X, Axis3::Y]).to_string(), + assert_rendering( + d2("Column x Row", [Axis3::X, Axis3::Y], false), "\ Column x Row ╭──┬──┬──┬──╮ @@ -148,14 +202,14 @@ Column x Row │b2│ 3│ 4│ 5│ │b3│ 6│ 7│ 8│ ╰──┴──┴──┴──╯ -" +", ); } #[test] fn d2_rc() { - assert_eq!( - d2("Row x Column", [Axis3::Y, Axis3::X]).to_string(), + assert_rendering( + d2("Row x Column", [Axis3::Y, Axis3::X], false), "\ Row x Column ╭──┬──┬──┬──╮ @@ -165,6 +219,6 @@ Row x Column │a2│ 1│ 4│ 7│ │a3│ 2│ 5│ 8│ ╰──┴──┴──┴──╯ -" +", ); } -- 2.30.2