more tests
authorBen Pfaff <blp@cs.stanford.edu>
Thu, 10 Apr 2025 17:34:27 +0000 (10:34 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Thu, 10 Apr 2025 17:34:27 +0000 (10:34 -0700)
rust/pspp/src/output/pivot/mod.rs
rust/pspp/src/output/pivot/output.rs
rust/pspp/src/output/pivot/test.rs

index e0cb9be00a806b00261568a99bb54dfd03d7202d..8e315100b865ad9f4d86abf5cf44d91fb56384ea 100644 (file)
@@ -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 {
index ce2d32c37274d4a93f07fa51cac18dcbb550586f..8e9db75836dd0429689d66b3bbe9636d7bf2d943 100644 (file)
@@ -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 {
index 1e61180102341a74931b6c52e2dd4df029618394..4a131c408f18625dc3a9246c322a31f0cb336fec 100644 (file)
@@ -71,14 +71,14 @@ fn test_look() -> Arc<Look> {
     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│
 ╰──┴──┴──┴──╯
-"
+",
     );
 }