pub trait OfdAction {
fn to_xml_string(&self) -> String;
fn clone_box(&self) -> Box<dyn OfdAction>;
}
impl Clone for Box<dyn OfdAction> {
fn clone(&self) -> Self {
self.clone_box()
}
}
impl std::fmt::Debug for Box<dyn OfdAction> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "OfdAction(\"{}\")", self.to_xml_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Clone)]
struct DummyAction {
tag_name: String,
}
impl OfdAction for DummyAction {
fn to_xml_string(&self) -> String {
format!("<{}/>", self.tag_name)
}
fn clone_box(&self) -> Box<dyn OfdAction> {
Box::new(self.clone())
}
}
#[test]
fn test_ofd_action_trait_to_xml() {
let action = DummyAction {
tag_name: "TestAction".to_string(),
};
assert_eq!(action.to_xml_string(), "<TestAction/>");
}
#[test]
fn test_ofd_action_trait_object() {
let action: Box<dyn OfdAction> = Box::new(DummyAction {
tag_name: "Boxed".to_string(),
});
assert_eq!(action.to_xml_string(), "<Boxed/>");
}
#[test]
fn test_ofd_action_clone_box() {
let action: Box<dyn OfdAction> = Box::new(DummyAction {
tag_name: "Clone".to_string(),
});
let cloned = action.clone_box();
assert_eq!(cloned.to_xml_string(), "<Clone/>");
}
#[test]
fn test_ofd_action_debug_box() {
let action: Box<dyn OfdAction> = Box::new(DummyAction {
tag_name: "Dbg".to_string(),
});
let dbg = format!("{action:?}");
assert!(dbg.contains("OfdAction"));
assert!(dbg.contains("<Dbg/>"));
}
}