separate sack integration test binary
[pspp] / rust / src / sack.rs
index 41012c467e4eb40a880333cfbeb28a9d2c141b8e..1b711544441df9b7e180d0adc81d441d10241485 100644 (file)
@@ -12,6 +12,12 @@ use std::{
 use crate::endian::{Endian, ToBytes};
 
 pub fn sack(input: &str, endian: Endian) -> Result<Vec<u8>> {
+    let mut lexer = Lexer::new(input, endian)?;
+    while let Some(ref token) = lexer.token {
+        println!("{token:?}");
+        lexer.get()?;
+    }
+
     let mut symbol_table = HashMap::new();
     let output = _sack(input, endian, &mut symbol_table)?;
     let output = if !symbol_table.is_empty() {
@@ -231,7 +237,7 @@ where
     Ok(())
 }
 
-#[derive(PartialEq, Eq, Clone)]
+#[derive(PartialEq, Eq, Clone, Debug)]
 enum Token {
     Integer(i64),
     Float(OrderedFloat<f64>),
@@ -269,7 +275,7 @@ impl<'a> Lexer<'a> {
             line_number: 1,
             endian,
         };
-        lexer.next()?;
+        lexer.token = lexer.next()?;
         Ok(lexer)
     }
     fn take(&mut self) -> Result<Token> {
@@ -354,7 +360,7 @@ impl<'a> Lexer<'a> {
                 }
             }
             '"' => {
-                let mut s = String::from(c);
+                let mut s = String::new();
                 loop {
                     match self.iter.next() {
                         None => return Err(anyhow!("end-of-file inside string")),
@@ -410,3 +416,30 @@ impl<'a> Lexer<'a> {
         Ok(Some(token))
     }
 }
+
+#[cfg(test)]
+mod test {
+    use crate::endian::Endian;
+    use crate::sack::sack;
+    use anyhow::Result;
+    use hexplay::HexView;
+
+    #[test]
+    fn basic_sack() -> Result<()> {
+        let input = r#"
+"$FL2"; s60 "$(#) SPSS DATA FILE PSPP synthetic test file";
+2; # Layout code
+28; # Nominal case size
+0; # Not compressed
+0; # Not weighted
+1; # 1 case.
+100.0; # Bias.
+"01 Jan 11"; "20:53:52";
+"PSPP synthetic test file: "; i8 244; i8 245; i8 246; i8 248; s34 "";
+i8 0 *3;
+"#;
+        let output = sack(input, Endian::Big)?;
+        HexView::new(&output).print()?;
+        Ok(())
+    }
+}