From d56125668862062365f905d7469b84005761551c Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Thu, 27 Jul 2023 11:04:08 +0100 Subject: [PATCH] cxx-qt-gen: move some mappings tests to be in mappings file --- crates/cxx-qt-gen/src/parser/cxxqtdata.rs | 167 +--------------------- crates/cxx-qt-gen/src/parser/mappings.rs | 124 ++++++++++++++++ 2 files changed, 125 insertions(+), 166 deletions(-) diff --git a/crates/cxx-qt-gen/src/parser/cxxqtdata.rs b/crates/cxx-qt-gen/src/parser/cxxqtdata.rs index 4f026e6b7..5882aa879 100644 --- a/crates/cxx-qt-gen/src/parser/cxxqtdata.rs +++ b/crates/cxx-qt-gen/src/parser/cxxqtdata.rs @@ -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! { @@ -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(); @@ -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(); diff --git a/crates/cxx-qt-gen/src/parser/mappings.rs b/crates/cxx-qt-gen/src/parser/mappings.rs index ea1726d5e..dbe857822 100644 --- a/crates/cxx-qt-gen/src/parser/mappings.rs +++ b/crates/cxx-qt-gen/src/parser/mappings.rs @@ -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 } + ); + } +}