Skip to content

Commit

Permalink
cxx-qt-gen: move some mappings tests to be in mappings file
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Jul 31, 2023
1 parent 6d8f8a5 commit 86b7275
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 166 deletions.
167 changes: 1 addition & 166 deletions crates/cxx-qt-gen/src/parser/cxxqtdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,55 +523,7 @@ mod tests {
}

#[test]
fn test_cxx_mappings_cxx_name_empty() {
let mut cxx_qt_data = create_parsed_cxx_qt_data();

let item: Item = parse_quote! {
extern "C++" {
type A;
}
};
assert!(cxx_qt_data.populate_mappings_from_item(&item).is_ok());
assert!(cxx_qt_data.cxx_mappings.cxx_names.is_empty());

assert_eq!(cxx_qt_data.cxx_mappings.qualified.len(), 1);
assert_eq!(
cxx_qt_data
.cxx_mappings
.qualified
.get(&format_ident!("A"))
.unwrap(),
&parse_quote! { ffi::A }
);
}

#[test]
fn test_cxx_mappings_cxx_name_normal() {
let mut cxx_qt_data = create_parsed_cxx_qt_data();

let item: Item = parse_quote! {
extern "C++" {
#[cxx_name = "B"]
type A;
}
};
assert!(cxx_qt_data.populate_mappings_from_item(&item).is_ok());
assert_eq!(cxx_qt_data.cxx_mappings.cxx_names.len(), 1);
assert_eq!(cxx_qt_data.cxx_mappings.cxx_names.get("A").unwrap(), "B");

assert_eq!(cxx_qt_data.cxx_mappings.qualified.len(), 1);
assert_eq!(
cxx_qt_data
.cxx_mappings
.qualified
.get(&format_ident!("A"))
.unwrap(),
&parse_quote! { ffi::A }
);
}

#[test]
fn test_cxx_mappings_cxx_name_unsafe() {
fn test_cxx_mappings_cxx_name() {
let mut cxx_qt_data = create_parsed_cxx_qt_data();

let item: Item = parse_quote! {
Expand All @@ -595,99 +547,6 @@ mod tests {
);
}

#[test]
fn test_cxx_mappings_cxx_name_namespace_bridge() {
let mut cxx_qt_data = create_parsed_cxx_qt_data();
cxx_qt_data.namespace = "bridge_namespace".to_owned();

let item: Item = parse_quote! {
extern "C++" {
type A;

#[cxx_name = "C"]
type B;
}
};
assert!(cxx_qt_data.populate_mappings_from_item(&item).is_ok());
assert_eq!(cxx_qt_data.cxx_mappings.cxx_names.len(), 1);
assert_eq!(cxx_qt_data.cxx_mappings.cxx_names.get("B").unwrap(), "C");

assert_eq!(cxx_qt_data.cxx_mappings.namespaces.len(), 2);
assert_eq!(
cxx_qt_data.cxx_mappings.namespaces.get("A").unwrap(),
"bridge_namespace"
);
assert_eq!(
cxx_qt_data.cxx_mappings.namespaces.get("B").unwrap(),
"bridge_namespace"
);

assert_eq!(cxx_qt_data.cxx_mappings.qualified.len(), 2);
assert_eq!(
cxx_qt_data
.cxx_mappings
.qualified
.get(&format_ident!("A"))
.unwrap(),
&parse_quote! { ffi::A }
);
assert_eq!(
cxx_qt_data
.cxx_mappings
.qualified
.get(&format_ident!("B"))
.unwrap(),
&parse_quote! { ffi::B }
);
}

#[test]
fn test_cxx_mappings_cxx_name_namespace_items() {
let mut cxx_qt_data = create_parsed_cxx_qt_data();
cxx_qt_data.namespace = "namespace".to_owned();

let item: Item = parse_quote! {
#[namespace = "extern_namespace"]
extern "C++" {
type A;

#[namespace = "type_namespace"]
type B;
}
};
// Also ensure item namespace is chosen instead of bridge namespace
assert!(cxx_qt_data.populate_mappings_from_item(&item).is_ok());
assert_eq!(cxx_qt_data.cxx_mappings.cxx_names.len(), 0);

assert_eq!(cxx_qt_data.cxx_mappings.namespaces.len(), 2);
assert_eq!(
cxx_qt_data.cxx_mappings.namespaces.get("A").unwrap(),
"extern_namespace"
);
assert_eq!(
cxx_qt_data.cxx_mappings.namespaces.get("B").unwrap(),
"type_namespace"
);

assert_eq!(cxx_qt_data.cxx_mappings.qualified.len(), 2);
assert_eq!(
cxx_qt_data
.cxx_mappings
.qualified
.get(&format_ident!("A"))
.unwrap(),
&parse_quote! { ffi::A }
);
assert_eq!(
cxx_qt_data
.cxx_mappings
.qualified
.get(&format_ident!("B"))
.unwrap(),
&parse_quote! { ffi::B }
);
}

#[test]
fn test_cxx_mappings_cxx_name_normal_namespace_cxx_name() {
let mut cxx_qt_data = create_parsed_cxx_qt_data();
Expand Down Expand Up @@ -809,30 +668,6 @@ mod tests {
);
}

#[test]
fn test_cxx_mappings_rust_name_normal() {
let mut cxx_qt_data = create_parsed_cxx_qt_data();

let item: Item = parse_quote! {
extern "C++" {
#[rust_name = "B"]
type A;
}
};
assert!(cxx_qt_data.populate_mappings_from_item(&item).is_ok());
assert!(cxx_qt_data.cxx_mappings.cxx_names.is_empty());

assert_eq!(cxx_qt_data.cxx_mappings.qualified.len(), 1);
assert_eq!(
cxx_qt_data
.cxx_mappings
.qualified
.get(&format_ident!("A"))
.unwrap(),
&parse_quote! { ffi::B }
);
}

#[test]
fn test_parse_inherited_methods() {
let mut cxxqtdata = create_parsed_cxx_qt_data();
Expand Down
124 changes: 124 additions & 0 deletions crates/cxx-qt-gen/src/parser/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,127 @@ impl ParsedCxxMappings {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

use quote::format_ident;
use syn::parse_quote;

#[test]
fn test_attribute_none() {
let mut mappings = ParsedCxxMappings::default();
assert!(mappings
.populate(&format_ident!("A"), &[], "", &format_ident!("ffi"))
.is_ok());

assert!(mappings.cxx_names.is_empty());
assert!(mappings.namespaces.is_empty());
assert_eq!(mappings.qualified.len(), 1);
assert_eq!(
mappings.qualified.get(&format_ident!("A")).unwrap(),
&parse_quote! { ffi::A }
);
}

#[test]
fn test_attribute_cxx_name() {
let mut mappings = ParsedCxxMappings::default();
assert!(mappings
.populate(
&format_ident!("A"),
&[parse_quote! { #[cxx_name = "B"] }],
"",
&format_ident!("ffi")
)
.is_ok());

assert_eq!(mappings.cxx_names.len(), 1);
assert_eq!(mappings.cxx_names.get("A").unwrap(), "B");
assert!(mappings.namespaces.is_empty());
assert_eq!(
mappings.qualified.get(&format_ident!("A")).unwrap(),
&parse_quote! { ffi::A }
);
}

#[test]
fn test_attribute_namespace() {
let mut mappings = ParsedCxxMappings::default();
assert!(mappings
.populate(
&format_ident!("A"),
&[parse_quote! { #[namespace = "type_namespace"] }],
"bridge_namespace",
&format_ident!("ffi")
)
.is_ok());

assert!(mappings.cxx_names.is_empty());
assert_eq!(mappings.namespaces.get("A").unwrap(), "type_namespace");
assert_eq!(mappings.qualified.len(), 1);
assert_eq!(
mappings.qualified.get(&format_ident!("A")).unwrap(),
&parse_quote! { ffi::A }
);
}

#[test]
fn test_attribute_rust_name() {
let mut mappings = ParsedCxxMappings::default();
assert!(mappings
.populate(
&format_ident!("A"),
&[parse_quote! { #[rust_name = "B"] }],
"",
&format_ident!("ffi")
)
.is_ok());

assert!(mappings.cxx_names.is_empty());
assert!(mappings.namespaces.is_empty());
assert_eq!(mappings.qualified.len(), 1);
assert_eq!(
mappings.qualified.get(&format_ident!("A")).unwrap(),
&parse_quote! { ffi::B }
);
}

#[test]
fn test_parent_namespace() {
let mut mappings = ParsedCxxMappings::default();
assert!(mappings
.populate(
&format_ident!("A"),
&[],
"bridge_namespace",
&format_ident!("ffi")
)
.is_ok());

assert!(mappings.cxx_names.is_empty());
assert_eq!(mappings.namespaces.get("A").unwrap(), "bridge_namespace");
assert_eq!(mappings.qualified.len(), 1);
assert_eq!(
mappings.qualified.get(&format_ident!("A")).unwrap(),
&parse_quote! { ffi::A }
);
}

#[test]
fn test_qualified() {
let mut mappings = ParsedCxxMappings::default();
assert!(mappings
.populate(&format_ident!("A"), &[], "", &format_ident!("my_module"))
.is_ok());

assert!(mappings.cxx_names.is_empty());
assert!(mappings.namespaces.is_empty());
assert_eq!(mappings.qualified.len(), 1);
assert_eq!(
mappings.qualified.get(&format_ident!("A")).unwrap(),
&parse_quote! { my_module::A }
);
}
}

0 comments on commit 86b7275

Please sign in to comment.