This commit is contained in:
1 parent
e3510a96fb
commit
50278821ca
7 files changed
+185
-44
No files matched your search
+30
-12
@@ -576,25 +576,43 @@ enabled = true
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_xml_flatten_unflatten() {
|
||||
let xml_str = "<config><server><port>8080</port><enabled>true</enabled></server></config>";
|
||||
|
||||
let json_val = xml_to_json(xml_str).unwrap();
|
||||
|
||||
fn test_group_rename_write() {
|
||||
let mut vars = Vec::new();
|
||||
flatten(&json_val, Vec::new(), Some("".to_string()), 0, &mut vars);
|
||||
let json = serde_json::json!({
|
||||
"old_group": {
|
||||
"key": "val"
|
||||
}
|
||||
});
|
||||
|
||||
flatten(&json, Vec::new(), Some("".to_string()), 0, &mut vars);
|
||||
assert_eq!(vars.len(), 2);
|
||||
assert_eq!(vars[0].key, "old_group");
|
||||
assert_eq!(vars[0].is_group, true);
|
||||
assert_eq!(vars[1].key, "key");
|
||||
assert_eq!(vars[1].path_string(), "old_group.key");
|
||||
|
||||
// Manually simulate a rename of "old_group" to "new_group"
|
||||
let old_path = vars[0].path.clone();
|
||||
let new_key = "new_group".to_string();
|
||||
let mut new_path = vec![PathSegment::Key(new_key.clone())];
|
||||
|
||||
vars[0].key = new_key;
|
||||
vars[0].path = new_path.clone();
|
||||
|
||||
// Update child path
|
||||
vars[1].path = vec![PathSegment::Key("new_group".to_string()), PathSegment::Key("key".to_string())];
|
||||
|
||||
let handler = HierarchicalHandler::new(FormatType::Json);
|
||||
let mut root = Value::Object(Map::new());
|
||||
for var in vars {
|
||||
for var in &vars {
|
||||
if !var.is_group {
|
||||
insert_into_value(&mut root, &var.path, var.value.as_deref().unwrap_or(""), var.value_type);
|
||||
}
|
||||
}
|
||||
|
||||
let unflattened_xml = json_to_xml(&root);
|
||||
|
||||
assert!(unflattened_xml.contains("<port>8080</port>"));
|
||||
assert!(unflattened_xml.contains("<enabled>true</enabled>"));
|
||||
assert!(unflattened_xml.contains("<config>") && unflattened_xml.contains("</config>"));
|
||||
let out = serde_json::to_string(&root).unwrap();
|
||||
assert!(out.contains("\"new_group\""));
|
||||
assert!(out.contains("\"key\":\"val\""));
|
||||
assert!(!out.contains("\"old_group\""));
|
||||
}
|
||||
}
|
||||
+39
-8
@@ -80,15 +80,46 @@ mod tests {
|
||||
use std::io::Write;
|
||||
|
||||
#[test]
|
||||
fn test_parse_ini() {
|
||||
let mut file = NamedTempFile::new().unwrap();
|
||||
writeln!(file, "[server]\nport=8080\n[database]\nhost=localhost").unwrap();
|
||||
|
||||
fn test_section_rename_write() {
|
||||
let handler = IniHandler;
|
||||
let vars = handler.parse(file.path()).unwrap();
|
||||
let mut vars = vec![
|
||||
ConfigItem {
|
||||
key: "server".to_string(),
|
||||
path: vec![PathSegment::Key("server".to_string())],
|
||||
value: None,
|
||||
template_value: None,
|
||||
default_value: None,
|
||||
depth: 0,
|
||||
is_group: true,
|
||||
status: ItemStatus::Present,
|
||||
value_type: ValueType::Null,
|
||||
},
|
||||
ConfigItem {
|
||||
key: "port".to_string(),
|
||||
path: vec![PathSegment::Key("server".to_string()), PathSegment::Key("port".to_string())],
|
||||
value: Some("8080".to_string()),
|
||||
template_value: Some("8080".to_string()),
|
||||
default_value: Some("8080".to_string()),
|
||||
depth: 1,
|
||||
is_group: false,
|
||||
status: ItemStatus::Present,
|
||||
value_type: ValueType::String,
|
||||
}
|
||||
];
|
||||
|
||||
// Rename "server" to "srv"
|
||||
vars[0].key = "srv".to_string();
|
||||
vars[0].path = vec![PathSegment::Key("srv".to_string())];
|
||||
|
||||
assert!(vars.iter().any(|v| v.path_string() == "server" && v.is_group));
|
||||
assert!(vars.iter().any(|v| v.path_string() == "server.port" && v.value.as_deref() == Some("8080")));
|
||||
assert!(vars.iter().any(|v| v.path_string() == "database.host" && v.value.as_deref() == Some("localhost")));
|
||||
// Update child path
|
||||
vars[1].path = vec![PathSegment::Key("srv".to_string()), PathSegment::Key("port".to_string())];
|
||||
|
||||
let file = NamedTempFile::new().unwrap();
|
||||
handler.write(file.path(), &vars).unwrap();
|
||||
|
||||
let content = std::fs::read_to_string(file.path()).unwrap();
|
||||
assert!(content.contains("[srv]"));
|
||||
assert!(content.contains("port=8080"));
|
||||
assert!(!content.contains("[server]"));
|
||||
}
|
||||
}
|
||||
@@ -89,14 +89,45 @@ mod tests {
|
||||
use std::io::Write;
|
||||
|
||||
#[test]
|
||||
fn test_parse_properties() {
|
||||
let mut file = NamedTempFile::new().unwrap();
|
||||
writeln!(file, "server.port=8080\ndatabase.host=localhost").unwrap();
|
||||
|
||||
fn test_group_rename_write() {
|
||||
let handler = PropertiesHandler;
|
||||
let vars = handler.parse(file.path()).unwrap();
|
||||
let mut vars = vec![
|
||||
ConfigItem {
|
||||
key: "server".to_string(),
|
||||
path: vec![PathSegment::Key("server".to_string())],
|
||||
value: None,
|
||||
template_value: None,
|
||||
default_value: None,
|
||||
depth: 0,
|
||||
is_group: true,
|
||||
status: ItemStatus::Present,
|
||||
value_type: ValueType::Null,
|
||||
},
|
||||
ConfigItem {
|
||||
key: "port".to_string(),
|
||||
path: vec![PathSegment::Key("server".to_string()), PathSegment::Key("port".to_string())],
|
||||
value: Some("8080".to_string()),
|
||||
template_value: Some("8080".to_string()),
|
||||
default_value: Some("8080".to_string()),
|
||||
depth: 1,
|
||||
is_group: false,
|
||||
status: ItemStatus::Present,
|
||||
value_type: ValueType::String,
|
||||
}
|
||||
];
|
||||
|
||||
// Rename "server" to "srv"
|
||||
vars[0].key = "srv".to_string();
|
||||
vars[0].path = vec![PathSegment::Key("srv".to_string())];
|
||||
|
||||
assert!(vars.iter().any(|v| v.path_string() == "server" && v.is_group));
|
||||
assert!(vars.iter().any(|v| v.path_string() == "server.port" && v.value.as_deref() == Some("8080")));
|
||||
// Update child path
|
||||
vars[1].path = vec![PathSegment::Key("srv".to_string()), PathSegment::Key("port".to_string())];
|
||||
|
||||
let file = NamedTempFile::new().unwrap();
|
||||
handler.write(file.path(), &vars).unwrap();
|
||||
|
||||
let content = std::fs::read_to_string(file.path()).unwrap();
|
||||
assert!(content.contains("srv.port=8080"));
|
||||
assert!(!content.contains("server.port=8080"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user