Skip to content

Commit

Permalink
Remove some duplication and improve helpers for mocking ParsedMethod (K…
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFordTytherington authored Sep 3, 2024
1 parent f945217 commit 34d25db
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 742 deletions.
66 changes: 12 additions & 54 deletions crates/cxx-qt-gen/src/generator/cpp/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,8 @@ mod tests {

use crate::generator::cpp::property::tests::{require_header, require_pair};
use crate::generator::naming::qobject::tests::create_qobjectname;
use crate::naming::Name;
use crate::parser::parameter::ParsedFunctionParameter;
use indoc::indoc;
use pretty_assertions::assert_str_eq;
use quote::format_ident;
use std::collections::HashSet;
use syn::{parse_quote, ForeignItemFn};

Expand All @@ -150,40 +147,19 @@ mod tests {
parse_quote! { fn specifiers_invokable(self: &MyObject, param: i32) -> i32; };
let method5: ForeignItemFn = parse_quote! { fn cpp_method(self: &MyObject); };
let invokables = vec![
ParsedMethod::from_method_and_params(&method1, vec![]),
ParsedMethod::from_method_and_params(
&method2,
vec![ParsedFunctionParameter {
ident: format_ident!("param"),
ty: parse_quote! { i32 },
}],
),
ParsedMethod::mut_from_method_and_params(
&method3,
vec![ParsedFunctionParameter {
ident: format_ident!("param"),
ty: parse_quote! { &QColor },
}],
),
ParsedMethod {
specifiers: {
let mut specifiers = HashSet::new();
specifiers.insert(ParsedQInvokableSpecifiers::Final);
specifiers.insert(ParsedQInvokableSpecifiers::Override);
specifiers.insert(ParsedQInvokableSpecifiers::Virtual);
specifiers
},
..ParsedMethod::from_method_and_params(
&method4,
vec![ParsedFunctionParameter {
ident: format_ident!("param"),
ty: parse_quote! { i32 },
}],
)
},
ParsedMethod::mock_qinvokable(&method1),
ParsedMethod::mock_qinvokable(&method2),
ParsedMethod::mock_qinvokable(&method3).make_mutable(),
ParsedMethod::mock_qinvokable(&method4).with_specifiers({
let mut specifiers = HashSet::new();
specifiers.insert(ParsedQInvokableSpecifiers::Final);
specifiers.insert(ParsedQInvokableSpecifiers::Override);
specifiers.insert(ParsedQInvokableSpecifiers::Virtual);
specifiers
}),
ParsedMethod {
is_qinvokable: false,
..ParsedMethod::from_method_and_params(&method5, vec![])
..ParsedMethod::mock_qinvokable(&method5)
},
];
let qobject_idents = create_qobjectname();
Expand Down Expand Up @@ -310,25 +286,7 @@ mod tests {
let method_declaration: ForeignItemFn =
parse_quote! { fn trivial_invokable(self: &MyObject, param: A) -> B; };

let method = ParsedMethod {
method: method_declaration.clone(),
qobject_ident: format_ident!("MyObject"),
mutable: false,
safe: true,
parameters: vec![ParsedFunctionParameter {
ident: format_ident!("param"),
ty: parse_quote! { i32 },
}],
specifiers: HashSet::new(),
is_qinvokable: true,
name: Name::from_rust_ident_and_attrs(
&method_declaration.sig.ident,
&method_declaration.attrs,
None,
None,
)
.unwrap(),
};
let method = ParsedMethod::mock_qinvokable(&method_declaration);
let invokables = vec![&method];
let qobject_idents = create_qobjectname();

Expand Down
13 changes: 2 additions & 11 deletions crates/cxx-qt-gen/src/generator/cpp/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl GeneratedCppQObject {
mod tests {
use super::*;

use crate::generator::mock_qml_singleton;
use crate::{generator::structuring::Structures, parser::Parser};
use syn::{parse_quote, ItemMod};

Expand Down Expand Up @@ -295,17 +296,7 @@ mod tests {

#[test]
fn test_generated_cpp_qobject_singleton() {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
extern "RustQt" {
#[qobject]
#[qml_element]
#[qml_singleton]
type MyObject = super::MyObjectRust;
}
}
};
let module = mock_qml_singleton();
let parser = Parser::from(module).unwrap();
let structures = Structures::new(&parser.cxx_qt_data).unwrap();

Expand Down
135 changes: 42 additions & 93 deletions crates/cxx-qt-gen/src/generator/cpp/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,36 +215,16 @@ mod tests {

use crate::generator::cpp::property::tests::{require_header, require_pair};
use crate::generator::naming::qobject::tests::create_qobjectname;
use crate::parser::parameter::ParsedFunctionParameter;
use indoc::indoc;
use pretty_assertions::assert_str_eq;
use quote::format_ident;
use syn::parse_quote;
use syn::{parse_quote, ForeignItemFn};

#[test]
fn test_generate_cpp_signals() {
let signal = ParsedSignal {
method: parse_quote! {
fn data_changed(self: Pin<&mut MyObject>, trivial: i32, opaque: UniquePtr<QColor>);
},
qobject_ident: format_ident!("MyObject"),
mutable: true,
parameters: vec![
ParsedFunctionParameter {
ident: format_ident!("trivial"),
ty: parse_quote! { i32 },
},
ParsedFunctionParameter {
ident: format_ident!("opaque"),
ty: parse_quote! { UniquePtr<QColor> },
},
],
name: Name::new(format_ident!("data_changed")).with_cxx_name("dataChanged".to_owned()),
safe: true,
inherit: false,
private: false,
docs: vec![],
let method: ForeignItemFn = parse_quote! {
fn data_changed(self: Pin<&mut MyObject>, trivial: i32, opaque: UniquePtr<QColor>);
};
let signal = ParsedSignal::mock_with_method(&method);
let signals = vec![&signal];
let qobject_idents = create_qobjectname();

Expand Down Expand Up @@ -321,22 +301,10 @@ mod tests {

#[test]
fn test_generate_cpp_signals_mapped_cxx_name() {
let signal = ParsedSignal {
method: parse_quote! {
fn data_changed(self: Pin<&mut MyObject>, mapped: A);
},
qobject_ident: format_ident!("MyObject"),
mutable: true,
parameters: vec![ParsedFunctionParameter {
ident: format_ident!("mapped"),
ty: parse_quote! { A },
}],
name: Name::new(format_ident!("data_changed")).with_cxx_name("dataChanged".to_owned()),
safe: true,
inherit: false,
private: false,
docs: vec![],
let method: ForeignItemFn = parse_quote! {
fn data_changed(self: Pin<&mut MyObject>, mapped: A);
};
let signal = ParsedSignal::mock_with_method(&method);
let signals = vec![&signal];
let qobject_idents = create_qobjectname();

Expand Down Expand Up @@ -410,20 +378,15 @@ mod tests {

#[test]
fn test_generate_cpp_signals_existing_cxx_name() {
let signal = ParsedSignal {
method: parse_quote! {
let method: ForeignItemFn = parse_quote! {
#[cxx_name = "baseName"]
fn existing_signal(self: Pin<&mut MyObject>);
},
qobject_ident: format_ident!("MyObject"),
mutable: true,
parameters: vec![],
name: Name::new(format_ident!("existing_signal")).with_cxx_name("baseName".to_owned()),
safe: true,
};
let signal = ParsedSignal {
inherit: true,
private: false,
docs: vec![],
..ParsedSignal::mock_with_method(&method)
};

let signals = vec![&signal];
let qobject_idents = create_qobjectname();
let generated =
Expand Down Expand Up @@ -491,23 +454,16 @@ mod tests {

#[test]
fn test_generate_cpp_signal_free() {
let method: ForeignItemFn = parse_quote! {
fn signal_rust_name(self: Pin<&mut MyObject>);
};
let signal = ParsedSignal {
method: parse_quote! {
fn signal_rust_name(self: Pin<&mut ObjRust>);
},
qobject_ident: format_ident!("ObjRust"),
mutable: true,
parameters: vec![],
name: Name::new(format_ident!("signal_rust_name"))
.with_cxx_name("signalRustName".to_owned()),
safe: true,
inherit: true,
private: false,
docs: vec![],
..ParsedSignal::mock_with_method(&method)
};

let mut type_names = TypeNames::default();
type_names.mock_insert("ObjRust", None, None, None);
type_names.mock_insert("MyObject", None, None, None);
let qobject_name = type_names.lookup(&signal.qobject_ident).unwrap();
let generated = generate_cpp_signal(&signal, qobject_name, &type_names).unwrap();

Expand All @@ -522,7 +478,7 @@ mod tests {
r#"
namespace rust::cxxqtgen1 {
::QMetaObject::Connection
ObjRust_signalRustNameConnect(ObjRust& self, ::rust::cxxqtgen1::ObjRustCxxQtSignalHandlersignalRustName closure, ::Qt::ConnectionType type);
MyObject_signalRustNameConnect(MyObject& self, ::rust::cxxqtgen1::MyObjectCxxQtSignalHandlersignalRustName closure, ::Qt::ConnectionType type);
} // namespace rust::cxxqtgen1
"#}
);
Expand All @@ -533,38 +489,38 @@ mod tests {
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
namespace rust::cxxqt1 {
template <>
SignalHandler<::rust::cxxqtgen1::ObjRustCxxQtSignalParamssignalRustName *>::~SignalHandler() noexcept
SignalHandler<::rust::cxxqtgen1::MyObjectCxxQtSignalParamssignalRustName *>::~SignalHandler() noexcept
{
if (data[0] == nullptr && data[1] == nullptr)
{
return;
}
drop_ObjRust_signal_handler_signalRustName(::std::move(*this));
drop_MyObject_signal_handler_signalRustName(::std::move(*this));
}
template <>
template <>
void SignalHandler<::rust::cxxqtgen1::ObjRustCxxQtSignalParamssignalRustName *>::operator()<ObjRust&>(ObjRust& self)
void SignalHandler<::rust::cxxqtgen1::MyObjectCxxQtSignalParamssignalRustName *>::operator()<MyObject&>(MyObject& self)
{
call_ObjRust_signal_handler_signalRustName(*this, self);
call_MyObject_signal_handler_signalRustName(*this, self);
}
static_assert(alignof(SignalHandler<::rust::cxxqtgen1::ObjRustCxxQtSignalParamssignalRustName *>) <= alignof(::std::size_t), "unexpected aligment");
static_assert(sizeof(SignalHandler<::rust::cxxqtgen1::ObjRustCxxQtSignalParamssignalRustName *>) == sizeof(::std::size_t[2]), "unexpected size");
static_assert(alignof(SignalHandler<::rust::cxxqtgen1::MyObjectCxxQtSignalParamssignalRustName *>) <= alignof(::std::size_t), "unexpected aligment");
static_assert(sizeof(SignalHandler<::rust::cxxqtgen1::MyObjectCxxQtSignalParamssignalRustName *>) == sizeof(::std::size_t[2]), "unexpected size");
} // namespace rust::cxxqt1
namespace rust::cxxqtgen1 {
::QMetaObject::Connection
ObjRust_signalRustNameConnect(ObjRust& self, ::rust::cxxqtgen1::ObjRustCxxQtSignalHandlersignalRustName closure, ::Qt::ConnectionType type)
MyObject_signalRustNameConnect(MyObject& self, ::rust::cxxqtgen1::MyObjectCxxQtSignalHandlersignalRustName closure, ::Qt::ConnectionType type)
{
return ::QObject::connect(
&self,
&ObjRust::signalRustName,
&MyObject::signalRustName,
&self,
[&, closure = ::std::move(closure)]() mutable {
const ::rust::cxxqt1::MaybeLockGuard<ObjRust> guard(self);
closure.template operator()<ObjRust&>(self);
const ::rust::cxxqt1::MaybeLockGuard<MyObject> guard(self);
closure.template operator()<MyObject&>(self);
},
type);
}
Expand All @@ -575,24 +531,17 @@ mod tests {

#[test]
fn test_generate_cpp_signal_free_mapped() {
let method = parse_quote! {
#[cxx_name = "signalCxxName"]
fn signal_rust_name(self: Pin<&mut MyObject>);
};
let signal = ParsedSignal {
method: parse_quote! {
#[cxx_name = "signalCxxName"]
fn signal_rust_name(self: Pin<&mut ObjRust>);
},
qobject_ident: format_ident!("ObjRust"),
mutable: true,
parameters: vec![],
name: Name::new(format_ident!("signal_rust_name"))
.with_cxx_name("signalCxxName".to_owned()),
safe: true,
inherit: true,
private: false,
docs: vec![],
..ParsedSignal::mock_with_method(&method)
};

let mut type_names = TypeNames::default();
type_names.mock_insert("ObjRust", None, Some("ObjCpp"), Some("mynamespace"));
type_names.mock_insert("MyObject", None, Some("ObjCpp"), Some("mynamespace"));
let qobject_name = type_names.lookup(&signal.qobject_ident).unwrap();
let generated = generate_cpp_signal(&signal, qobject_name, &type_names).unwrap();

Expand All @@ -607,7 +556,7 @@ mod tests {
r#"
namespace mynamespace::rust::cxxqtgen1 {
::QMetaObject::Connection
ObjCpp_signalCxxNameConnect(mynamespace::ObjCpp& self, ::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalHandlersignalCxxName closure, ::Qt::ConnectionType type);
ObjCpp_signalCxxNameConnect(mynamespace::ObjCpp& self, ::mynamespace::rust::cxxqtgen1::MyObjectCxxQtSignalHandlersignalCxxName closure, ::Qt::ConnectionType type);
} // namespace mynamespace::rust::cxxqtgen1
"#}
);
Expand All @@ -618,30 +567,30 @@ mod tests {
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
namespace rust::cxxqt1 {
template <>
SignalHandler<::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalParamssignalCxxName *>::~SignalHandler() noexcept
SignalHandler<::mynamespace::rust::cxxqtgen1::MyObjectCxxQtSignalParamssignalCxxName *>::~SignalHandler() noexcept
{
if (data[0] == nullptr && data[1] == nullptr)
{
return;
}
drop_ObjRust_signal_handler_signalCxxName(::std::move(*this));
drop_MyObject_signal_handler_signalCxxName(::std::move(*this));
}
template <>
template <>
void SignalHandler<::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalParamssignalCxxName *>::operator()<mynamespace::ObjCpp&>(mynamespace::ObjCpp& self)
void SignalHandler<::mynamespace::rust::cxxqtgen1::MyObjectCxxQtSignalParamssignalCxxName *>::operator()<mynamespace::ObjCpp&>(mynamespace::ObjCpp& self)
{
call_ObjRust_signal_handler_signalCxxName(*this, self);
call_MyObject_signal_handler_signalCxxName(*this, self);
}
static_assert(alignof(SignalHandler<::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalParamssignalCxxName *>) <= alignof(::std::size_t), "unexpected aligment");
static_assert(sizeof(SignalHandler<::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalParamssignalCxxName *>) == sizeof(::std::size_t[2]), "unexpected size");
static_assert(alignof(SignalHandler<::mynamespace::rust::cxxqtgen1::MyObjectCxxQtSignalParamssignalCxxName *>) <= alignof(::std::size_t), "unexpected aligment");
static_assert(sizeof(SignalHandler<::mynamespace::rust::cxxqtgen1::MyObjectCxxQtSignalParamssignalCxxName *>) == sizeof(::std::size_t[2]), "unexpected size");
} // namespace rust::cxxqt1
namespace mynamespace::rust::cxxqtgen1 {
::QMetaObject::Connection
ObjCpp_signalCxxNameConnect(mynamespace::ObjCpp& self, ::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalHandlersignalCxxName closure, ::Qt::ConnectionType type)
ObjCpp_signalCxxNameConnect(mynamespace::ObjCpp& self, ::mynamespace::rust::cxxqtgen1::MyObjectCxxQtSignalHandlersignalCxxName closure, ::Qt::ConnectionType type)
{
return ::QObject::connect(
&self,
Expand Down
Loading

0 comments on commit 34d25db

Please sign in to comment.