}
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();
}
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();
if index > 0 {
s.push('/');
}
+ write!(&mut s, "{}:", &variable.name).unwrap();
put_attributes(&attributes, &mut s);
}
self.write_string_record(18, &s)
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::{
}
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'
+)"
+ );
+ }
}