use crate::core::config::ResolvedCrateConfig;
use crate::core::ir::{EnumDef, EnumVariant, FieldDef, FunctionDef, TypeDef, TypeRef};
use crate::e2e::config::{CallConfig, CallOverride, E2eConfig};
use crate::e2e::field_access::SwiftFirstClassMap;
use crate::e2e::fixture::{Assertion, Fixture};
use std::collections::{HashMap, HashSet};
fn data_node_kind_enum() -> EnumDef {
EnumDef {
name: "DataNodeKind".to_string(),
variants: vec![
EnumVariant {
name: "KeyValue".to_string(),
..EnumVariant::default()
},
EnumVariant {
name: "Sequence".to_string(),
..EnumVariant::default()
},
],
..EnumDef::default()
}
}
fn kind_field(ty: TypeRef, optional: bool) -> FieldDef {
FieldDef {
name: "kind".to_string(),
ty,
optional,
..FieldDef::default()
}
}
fn fixture_calling(call: &str) -> Fixture {
Fixture {
id: "kind_smoke".to_string(),
description: "Kind field smoke".to_string(),
call: Some(call.to_string()),
assertions: vec![Assertion {
assertion_type: "equals".to_string(),
field: Some("kind".to_string()),
value: Some(serde_json::Value::String("key_value".to_string())),
..Assertion::default()
}],
..Fixture::default()
}
}
fn stage_output_union() -> EnumDef {
EnumDef {
name: "StageOutput".to_string(),
variants: vec![EnumVariant {
name: "Text".to_string(),
fields: vec![FieldDef {
name: "_0".to_string(),
ty: TypeRef::String,
..FieldDef::default()
}],
is_tuple: true,
..EnumVariant::default()
}],
serde_untagged: true,
has_serde: true,
..EnumDef::default()
}
}
fn table_ir() -> (Vec<TypeDef>, Vec<EnumDef>, Vec<FunctionDef>) {
let type_defs = vec![
TypeDef {
name: "ProcessResult".to_string(),
fields: vec![kind_field(TypeRef::Named("DataNodeKind".to_string()), false)],
..TypeDef::default()
},
TypeDef {
name: "OtherResult".to_string(),
fields: vec![kind_field(TypeRef::String, false)],
..TypeDef::default()
},
TypeDef {
name: "OptionalResult".to_string(),
fields: vec![kind_field(
TypeRef::Optional(Box::new(TypeRef::Named("DataNodeKind".to_string()))),
true,
)],
..TypeDef::default()
},
TypeDef {
name: "UnionResult".to_string(),
fields: vec![kind_field(TypeRef::Named("StageOutput".to_string()), false)],
..TypeDef::default()
},
];
let enums = vec![data_node_kind_enum(), stage_output_union()];
let functions = vec![
FunctionDef {
name: "process".to_string(),
return_type: TypeRef::Named("ProcessResult".to_string()),
..FunctionDef::default()
},
FunctionDef {
name: "other".to_string(),
return_type: TypeRef::Named("OtherResult".to_string()),
..FunctionDef::default()
},
FunctionDef {
name: "process_optional".to_string(),
return_type: TypeRef::Named("OptionalResult".to_string()),
..FunctionDef::default()
},
FunctionDef {
name: "process_union".to_string(),
return_type: TypeRef::Named("UnionResult".to_string()),
..FunctionDef::default()
},
];
(type_defs, enums, functions)
}
fn first_class_map() -> SwiftFirstClassMap {
SwiftFirstClassMap {
first_class_types: ["ProcessResult", "OtherResult", "OptionalResult", "UnionResult"]
.into_iter()
.map(str::to_string)
.collect(),
field_types: HashMap::new(),
vec_field_names: HashSet::new(),
json_bridged_field_names: HashSet::new(),
json_bridged_by_type: HashMap::new(),
getter_optionality: HashMap::new(),
root_type: None,
stringy_fields_by_type: HashMap::new(),
}
}
fn e2e_config_for(call: &str, result_type: &str, extra: impl FnOnce(&mut CallConfig)) -> E2eConfig {
let mut call_config = CallConfig {
function: call.to_string(),
..CallConfig::default()
};
call_config.overrides.insert(
"csharp".to_string(),
CallOverride {
result_type: Some(result_type.to_string()),
..CallOverride::default()
},
);
extra(&mut call_config);
let mut e2e_config = E2eConfig::default();
e2e_config.calls.insert(call.to_string(), call_config);
e2e_config
}
fn render(
fixture: &Fixture,
e2e_config: &E2eConfig,
swift_first_class_map: &SwiftFirstClassMap,
type_defs: &[TypeDef],
enums: &[EnumDef],
functions: &[FunctionDef],
) -> String {
let config = ResolvedCrateConfig {
name: "sample".to_string(),
..ResolvedCrateConfig::default()
};
let mut out = String::new();
super::test_method::render_test_method(
&mut out,
fixture,
e2e_config,
"",
"",
&[],
false,
None,
swift_first_class_map,
"Sample",
&config,
type_defs,
enums,
functions,
&[],
);
out
}
struct Case {
name: &'static str,
call: &'static str,
result_type: &'static str,
expect_raw_value: bool,
}
const CASES: &[Case] = &[
Case {
name: "an enum-typed field with no fields_enum config gets .rawValue via the IR",
call: "process",
result_type: "ProcessResult",
expect_raw_value: true,
},
Case {
name: "a same-named non-enum field on an unrelated type is not misclassified as enum",
call: "other",
result_type: "OtherResult",
expect_raw_value: false,
},
Case {
name: "an Option<Enum> field is classified as enum via the IR",
call: "process_optional",
result_type: "OptionalResult",
expect_raw_value: true,
},
Case {
name: "a payload-carrying union field does not get .rawValue (associated values have none)",
call: "process_union",
result_type: "UnionResult",
expect_raw_value: false,
},
];
#[test]
fn enum_field_classification_table() {
let (type_defs, enums, functions) = table_ir();
let map = first_class_map();
for case in CASES {
let e2e_config = e2e_config_for(case.call, case.result_type, |_| {});
let fixture = fixture_calling(case.call);
let out = render(&fixture, &e2e_config, &map, &type_defs, &enums, &functions);
let has_raw_value = out.contains(".rawValue");
assert_eq!(
has_raw_value, case.expect_raw_value,
"{}: expected .rawValue = {}, got:\n{out}",
case.name, case.expect_raw_value
);
}
}
#[test]
fn an_explicit_enum_fields_config_entry_still_classifies_as_enum() {
let (type_defs, enums, functions) = table_ir();
let map = first_class_map();
let e2e_config = e2e_config_for("other", "OtherResult", |call| {
call.overrides.insert(
"swift".to_string(),
CallOverride {
enum_fields: [("kind".to_string(), "DataNodeKind".to_string())].into_iter().collect(),
..CallOverride::default()
},
);
});
let fixture = fixture_calling("other");
let out = render(&fixture, &e2e_config, &map, &type_defs, &enums, &functions);
assert!(
out.contains(".rawValue"),
"explicit enum_fields config must still classify the field as enum, got:\n{out}"
);
}
const UNIT_ENUM_ASSERTION: &str = " XCTAssertEqual(result.kind.rawValue, \"key_value\")";
const UNION_SKIP_LINE: &str = " // skipped: enum field 'kind' is a payload-carrying union \
with no scalar wire accessor in this binding";
#[test]
fn a_unit_only_enum_property_still_lowers_to_its_exact_raw_value_comparison() {
let (type_defs, enums, functions) = table_ir();
let map = first_class_map();
let out = render(
&fixture_calling("process"),
&e2e_config_for("process", "ProcessResult", |_| {}),
&map,
&type_defs,
&enums,
&functions,
);
assert!(
out.contains(UNIT_ENUM_ASSERTION),
"expected exactly `{UNIT_ENUM_ASSERTION}`, got:\n{out}"
);
}
#[test]
fn a_payload_carrying_union_field_renders_a_registered_refusal_not_a_type_mismatch() {
let (type_defs, enums, functions) = table_ir();
let map = first_class_map();
let union_out = render(
&fixture_calling("process_union"),
&e2e_config_for("process_union", "UnionResult", |_| {}),
&map,
&type_defs,
&enums,
&functions,
);
assert!(
union_out.contains(UNION_SKIP_LINE),
"expected exactly `{UNION_SKIP_LINE}`, got:\n{union_out}"
);
assert!(
!union_out.contains(".rawValue"),
"a payload-carrying union is a Swift enum with associated values and no rawValue, so the \
assertion must not reach for one, got:\n{union_out}"
);
assert!(
!union_out.contains("\"key_value\""),
"the fixture literal must not be compared against anything: every string arm would lower \
the union leaf into a Swift type mismatch, got:\n{union_out}"
);
}
#[test]
fn the_union_refusal_is_recognised_by_the_field_skip_funnel() {
use crate::e2e::codegen::field_skip::FieldSkip;
assert_eq!(
FieldSkip::extract_classified(UNION_SKIP_LINE),
Some(("kind", FieldSkip::PayloadUnionHasNoScalarWireAccessor))
);
}