work
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 9 Dec 2025 23:50:47 +0000 (15:50 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 9 Dec 2025 23:50:47 +0000 (15:50 -0800)
rust/pspp/src/output/pivot.rs

index 4bbcf2dab9e7f1de26db8bbd05582a9288a269fb..3a60b2d6d99ab26d3ef9548e93be8e3ee8d0ac83 100644 (file)
@@ -3142,7 +3142,7 @@ impl TemplateValue {
                     if let Some(index) = index.checked_sub(1)
                         && let Some(arg) = args.get(index)
                     {
-                        args_consumed = args_consumed.max(index);
+                        args_consumed = args_consumed.max(index + 1);
                         write!(f, "{}", arg.display(display.options))?;
                     }
                 }
@@ -3381,7 +3381,8 @@ mod test {
     use std::str::FromStr;
 
     use crate::output::pivot::{
-        Color, Display26Adic, MetadataEntry, MetadataValue, Value, tests::assert_rendering,
+        Color, Display26Adic, MetadataEntry, MetadataValue, TemplateValue, Value, ValueInner,
+        tests::assert_rendering,
     };
 
     #[test]
@@ -3413,6 +3414,41 @@ mod test {
         }
     }
 
+    #[test]
+    fn template() {
+        for (template, expected) in [
+            (
+                "1: [:^1,:]1; [:^1,:]2",
+                "1: First,1.00,Second,2,; Third,3.00,Fourth,4,",
+            ),
+            (r#"2: [:^1\n:]1"#, "2: First\n1.00\nSecond\n2\n"),
+            (r#"3: [:^1 = ^2\n:]1"#, "3: First = 1.00\nSecond = 2\n"),
+            ("4: [%1:, ^1:]1", "4: First, 1.00, Second, 2"),
+            ("5: [%1 = %2:, ^1 = ^2:]1", "5: First = 1.00, Second = 2"),
+            ("6: [%1:, ^1:]1", "6: First, 1.00, Second, 2"),
+        ] {
+            let value = Value::new(ValueInner::Template(TemplateValue {
+                args: vec![
+                    vec![
+                        Value::new_user_text("First"),
+                        Value::new_number(Some(1.0)),
+                        Value::new_user_text("Second"),
+                        Value::new_integer(Some(2.0)),
+                    ],
+                    vec![
+                        Value::new_user_text("Third"),
+                        Value::new_number(Some(3.0)),
+                        Value::new_user_text("Fourth"),
+                        Value::new_integer(Some(4.0)),
+                    ],
+                ],
+                localized: String::from(template),
+                id: None,
+            }));
+            assert_eq!(value.display(()).to_string(), expected);
+        }
+    }
+
     #[test]
     fn metadata_entry() {
         let tree = MetadataEntry {