Skip to content

Add xml matching test #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions atspi-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
tokio-stream = { version = "0.1", default-features = false, features = [ "time",
] }
tokio-test = "0.4.2"
zbus_xml = "5.0.2"
zbus = { workspace = true }
41 changes: 40 additions & 1 deletion atspi-common/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,44 @@
};
}

// We decorate the macro with a `#[cfg(test)]` attribute.
// This prevents Clippy from complaining about the macro not being used.
// It is being used, but only in test mode.
//
/// Tests conversion to and from the `Event` enum.
///
/// Obtains a default for the given event struct.
/// Converts the struct into the `Event` enum, wrapping the struct.
/// Converts the `Event` enum into the given event struct.
/// Asserts that the original struct and the converted struct are equal.
#[cfg(test)]
macro_rules! event_has_matching_xml_definition {
($type:ty) => {
#[test]
fn event_has_matching_xml_definition() {
use zbus_xml;
use crate::events::{DBusInterface, DBusMember};

let fname = match <$type>::DBUS_INTERFACE.split(".").last().expect("Has last section") {
"Cache" => "xml/Cache.xml",
"Socket" => "xml/Socket.xml",
"Registry" => "xml/Registry.xml",
_ => "xml/Event.xml",
};
let reader = std::fs::File::open(fname).expect("Valid file path!");
let xml = zbus_xml::Node::from_reader(reader).expect("Valid DBus XML file!");
let Some(interface) = xml.interfaces().iter().find(|int| int.name() == <$type>::DBUS_INTERFACE) else {
let possible_names: Vec<String> = xml.interfaces().iter().map(|int| int.name().as_str().to_string()).collect();
panic!("{} has interface name {}, but it was not found in the list of interfaces defined in the XML: {:?}", std::any::type_name::<$type>(), <$type>::DBUS_INTERFACE, possible_names);

Check warning on line 405 in atspi-common/src/macros.rs

View check run for this annotation

Codecov / codecov/patch

atspi-common/src/macros.rs#L404-L405

Added lines #L404 - L405 were not covered by tests
};
let Some(_member) = interface.signals().iter().find(|mem| mem.name() == <$type>::DBUS_MEMBER) else {
let possible_names: Vec<String> = interface.signals().iter().map(|mem| mem.name().as_str().to_string()).collect();
panic!("{} has interface name {} and member name {}, but it was not found in the list of members defined in the corresponding interface in the XML: {:?}", std::any::type_name::<$type>(), <$type>::DBUS_INTERFACE, <$type>::DBUS_MEMBER, possible_names);

Check warning on line 409 in atspi-common/src/macros.rs

View check run for this annotation

Codecov / codecov/patch

atspi-common/src/macros.rs#L408-L409

Added lines #L408 - L409 were not covered by tests
};
}
};
}

#[cfg(test)]
macro_rules! zbus_message_qtspi_test_case {
($type:ty, Auto) => {
Expand Down Expand Up @@ -816,8 +854,9 @@
assert_eq!(specific_event.sender(), generic_event.sender(), "Senders do not match. hint: {hint}");
}

generic_event_test_case!($ufet);
zbus_message_test_case!($ufet, $qt);
event_has_matching_xml_definition!($ufet);
generic_event_test_case!($ufet);
}
assert_impl_all!(
$ufet: Clone,
Expand Down