use super::PhpBackend;
use crate::core::backend::Backend;
use crate::core::config::{NewAlefConfig, ResolvedCrateConfig};
use crate::core::ir::{ApiSurface, FieldDef, TypeDef, TypeRef};
fn php_config_with_feature(configured_feature: Option<&str>) -> ResolvedCrateConfig {
let features_line = configured_feature
.map(|f| format!("features = [\"{f}\"]\n"))
.unwrap_or_default();
let toml_src = format!(
"[workspace]\nlanguages = [\"php\"]\n[[crates]]\nname = \"test-lib\"\nsources = [\"src/lib.rs\"]\n\
[crates.php]\n{features_line}"
);
let cfg: NewAlefConfig = toml::from_str(&toml_src).unwrap();
cfg.resolve().unwrap().remove(0)
}
fn cfg_gated_field_api() -> ApiSurface {
ApiSurface {
crate_name: "test-lib".to_string(),
version: "0.1.0".to_string(),
types: vec![TypeDef {
name: "ExtractionConfig".to_string(),
rust_path: "test_lib::ExtractionConfig".to_string(),
fields: vec![
FieldDef {
name: "use_cache".to_string(),
ty: TypeRef::Primitive(crate::core::ir::PrimitiveType::Bool),
..Default::default()
},
FieldDef {
name: "pdf_options".to_string(),
ty: TypeRef::String,
cfg: Some(r#"feature = "pdf""#.to_string()),
..Default::default()
},
],
..Default::default()
}],
..Default::default()
}
}
fn lib_rs_content(files: &[crate::core::backend::GeneratedFile]) -> &str {
&files
.iter()
.find(|f| f.path.to_string_lossy().ends_with("lib.rs"))
.expect("generate_bindings must emit lib.rs")
.content
}
#[test]
fn generate_bindings_keeps_host_cfg_gated_field_when_feature_is_configured_end_to_end() {
let api = cfg_gated_field_api();
let config = php_config_with_feature(Some("pdf"));
let files = PhpBackend.generate_bindings(&api, &config).unwrap();
let lib_rs = lib_rs_content(&files);
assert!(
lib_rs.contains("pdf_options"),
"a cfg-gated field whose feature this PHP binding configures must still be emitted on the \
generated mirror struct, got:\n{lib_rs}"
);
}
#[test]
fn generate_bindings_drops_host_cfg_gated_field_when_feature_is_not_configured_end_to_end() {
let api = cfg_gated_field_api();
let config = php_config_with_feature(None);
let files = PhpBackend.generate_bindings(&api, &config).unwrap();
let lib_rs = lib_rs_content(&files);
assert!(
!lib_rs.contains("pdf_options"),
"a cfg-gated field whose feature this PHP binding does NOT configure must not appear on \
the generated mirror struct (the core field does not exist to read/write), got:\n{lib_rs}"
);
}