From: Ben Pfaff Date: Mon, 4 Aug 2025 20:10:31 +0000 (-0700) Subject: test writing attributes X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b96f93af1b02a05487b65d6ac5175b73c30dd8a;p=pspp test writing attributes --- diff --git a/rust/pspp/src/dictionary.rs b/rust/pspp/src/dictionary.rs index 5a1d8fa772..6f30e29972 100644 --- a/rust/pspp/src/dictionary.rs +++ b/rust/pspp/src/dictionary.rs @@ -1268,6 +1268,11 @@ impl Attributes { self.0.insert(name, values); } + pub fn with(mut self, name: Identifier, values: Vec) -> Self { + self.insert(name, values); + self + } + pub fn append(&mut self, other: &mut Self) { self.0.append(&mut other.0) } diff --git a/rust/pspp/src/sys/write.rs b/rust/pspp/src/sys/write.rs index 2297a828eb..02e3ff5e2e 100644 --- a/rust/pspp/src/sys/write.rs +++ b/rust/pspp/src/sys/write.rs @@ -671,7 +671,7 @@ where } fn write_data_file_attributes(&mut self) -> Result<(), BinError> { - if self.options.sysfile_version != SysfileVersion::V2 { + if self.options.sysfile_version != SysfileVersion::V3 { return Ok(()); } let mut s = String::new(); @@ -680,7 +680,7 @@ where } fn write_variable_attributes(&mut self) -> Result<(), BinError> { - if self.options.sysfile_version != SysfileVersion::V2 { + if self.options.sysfile_version != SysfileVersion::V3 { return Ok(()); } let mut s = String::new(); @@ -694,6 +694,7 @@ where if index > 0 { s.push('/'); } + write!(&mut s, "{}:", &variable.name).unwrap(); put_attributes(&attributes, &mut s); } self.write_string_record(18, &s) @@ -1213,8 +1214,9 @@ mod tests { use crate::{ data::{ByteString, Datum, RawString}, dictionary::{ - Alignment, CategoryLabels, DictIndexMultipleResponseSet, DictIndexVariableSet, - Dictionary, Measure, MissingValueRange, MultipleResponseType, VarWidth, Variable, + Alignment, Attributes, CategoryLabels, DictIndexMultipleResponseSet, + DictIndexVariableSet, Dictionary, Measure, MissingValueRange, MultipleResponseType, + VarWidth, Variable, }, identifier::{ByIdentifier, Identifier}, sys::{ @@ -1877,4 +1879,55 @@ $e=E 11 6 choice 0 n o p } assert!(names(&expected).eq(names(&actual))); } + + /// This tests the example from the documentation for the system file + /// format. + #[test] + fn attributes() { + let mut dictionary = Dictionary::new(UTF_8); + let attributes = Attributes::new() + .with( + Identifier::new("fred").unwrap(), + vec![String::from("23"), String::from("34")], + ) + .with(Identifier::new("bert").unwrap(), vec![String::from("123")]); + dictionary.attributes = attributes.clone(); + let mut variable = + Variable::new(Identifier::new("dummy").unwrap(), VarWidth::Numeric, UTF_8); + variable.attributes = attributes; + dictionary.add_var(variable).unwrap(); + + fn get_attributes(dictionary: &Dictionary, vars: bool) -> String { + let mut raw = Vec::new(); + let options = WriteOptions::reproducible(None); + let mut cursor = Cursor::new(&mut raw); + let mut writer = DictionaryWriter::new(&options, &mut cursor, dictionary); + if vars { + writer.write_variable_attributes().unwrap(); + } else { + writer.write_data_file_attributes().unwrap(); + } + if raw.is_empty() { + String::new() + } else { + str::from_utf8(&raw[16..]).unwrap().into() + } + } + + assert_eq!( + &get_attributes(&dictionary, false), + "bert('123' +)fred('23' +'34' +)" + ); + assert_eq!( + &get_attributes(&dictionary, true), + "dummy:$@Role('0' +)bert('123' +)fred('23' +'34' +)" + ); + } }