add cc tests
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 22 Mar 2025 18:29:12 +0000 (11:29 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 22 Mar 2025 18:29:12 +0000 (11:29 -0700)
rust/pspp/src/format/mod.rs

index 9f85588eedca828ea7dde555b4c0379874511bcf..342891a658d1fea7d1e30a3492efcf2d688f5011 100644 (file)
@@ -857,6 +857,10 @@ impl StyleSet {
 }
 
 impl Settings {
+    pub fn with_cc(mut self, cc: CC, style: NumberStyle) -> Self {
+        self.ccs[cc] = Some(Box::new(style));
+        self
+    }
     fn number_style(&self, type_: Type) -> &NumberStyle {
         static DEFAULT: LazyLock<NumberStyle> =
             LazyLock::new(|| NumberStyle::new("", "", Decimal::Dot, None, false));
@@ -1040,12 +1044,11 @@ impl FromStr for NumberStyle {
             let mut s = String::new();
             let mut quote = false;
             for c in iter {
-                if c == '\'' {
+                if c == '\'' && !quote {
                     quote = true;
+                } else if c == grouping && !quote {
+                    break;
                 } else {
-                    if c == grouping && !quote {
-                        break;
-                    }
                     s.push(c);
                     quote = false;
                 }
@@ -1969,7 +1972,7 @@ mod test {
 
     use crate::{
         dictionary::Value,
-        format::{AbstractFormat, Format, Settings, UncheckedFormat},
+        format::{AbstractFormat, Format, NumberStyle, Settings, UncheckedFormat, CC},
         lex::{
             scan::StringScanner,
             segment::Syntax,
@@ -1982,7 +1985,12 @@ mod test {
             .join("src/format/testdata")
             .join(name);
         let input = BufReader::new(File::open(&filename).unwrap());
-        let settings = Settings::default();
+        let settings = Settings::default()
+            .with_cc(CC::A, ",,,".parse().unwrap())
+            .with_cc(CC::B, "-,[[[,]]],-".parse().unwrap())
+            .with_cc(CC::C, "((,[,],))".parse().unwrap())
+            .with_cc(CC::D, ",XXX,,-".parse().unwrap())
+            .with_cc(CC::E, ",,YYY,-".parse().unwrap());
         let mut value = 0.0;
         let mut value_name = String::new();
         for (line_number, line) in input.lines().map(|r| r.unwrap()).enumerate() {
@@ -2015,6 +2023,7 @@ mod test {
                     let expected = tokens[2].as_string().unwrap();
                     let actual = Value::Number(Some(value))
                         .display(format, UTF_8)
+                        .with_settings(&settings)
                         .to_string();
                     assert_eq!(
                         expected,
@@ -2067,4 +2076,29 @@ mod test {
     fn z() {
         test("z.txt");
     }
+
+    #[test]
+    fn cca() {
+        test("cca.txt");
+    }
+
+    #[test]
+    fn ccb() {
+        test("ccb.txt");
+    }
+
+    #[test]
+    fn ccc() {
+        test("ccc.txt");
+    }
+
+    #[test]
+    fn ccd() {
+        test("ccd.txt");
+    }
+
+    #[test]
+    fn cce() {
+        test("cce.txt");
+    }
 }