use super::EventType;
use super::OfdAction;
#[derive(Debug, Clone)]
pub struct CTAction {
pub event_type: EventType,
pub actions: Vec<Box<dyn OfdAction>>,
}
impl CTAction {
#[must_use]
pub fn new(event_type: EventType) -> Self {
Self {
event_type,
actions: Vec::new(),
}
}
pub fn add_action(&mut self, action: Box<dyn OfdAction>) {
self.actions.push(action);
}
#[must_use]
pub fn to_xml_string(&self) -> String {
let mut xml = format!("<ofd:CT_Action EventType=\"{}\">", self.event_type);
for action in &self.actions {
xml.push_str(&action.to_xml_string());
}
xml.push_str("</ofd:CT_Action>");
xml
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Clone)]
struct TestAction {
name: String,
}
impl OfdAction for TestAction {
fn to_xml_string(&self) -> String {
format!("<ofd:TestAction Name=\"{}\"/>", self.name)
}
fn clone_box(&self) -> Box<dyn OfdAction> {
Box::new(self.clone())
}
}
#[test]
fn test_ct_action_new() {
let action = CTAction::new(EventType::PO_DocumentOpen);
assert_eq!(action.event_type, EventType::PO_DocumentOpen);
assert!(action.actions.is_empty());
}
#[test]
fn test_ct_action_to_xml_empty() {
let action = CTAction::new(EventType::PO_ButtonClick);
let xml = action.to_xml_string();
assert!(xml.contains("EventType=\"PO_ButtonClick\""));
assert!(xml.contains("<ofd:CT_Action"));
assert!(xml.contains("</ofd:CT_Action>"));
}
#[test]
fn test_ct_action_with_children() {
let mut action = CTAction::new(EventType::PO_DocumentOpen);
action.add_action(Box::new(TestAction {
name: "test".to_string(),
}));
let xml = action.to_xml_string();
assert!(xml.contains("Name=\"test\""));
assert!(xml.contains("<ofd:TestAction"));
}
#[test]
fn test_ct_action_clone() {
let action = CTAction::new(EventType::PO_PageVisible);
let action2 = action.clone();
assert_eq!(action2.event_type, EventType::PO_PageVisible);
}
}