use super::test_method::render_test_method;
use crate::core::config::ResolvedCrateConfig;
use crate::core::ir::{EnumDef, EnumVariant, FieldDef, FunctionDef, TypeDef, TypeRef};
use crate::e2e::config::{CallConfig, E2eConfig};
use crate::e2e::fixture::{Assertion, Fixture};
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) -> FieldDef {
FieldDef {
name: "kind".to_string(),
ty,
..FieldDef::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()))],
..TypeDef::default()
},
TypeDef {
name: "OtherResult".to_string(),
fields: vec![kind_field(TypeRef::String)],
..TypeDef::default()
},
];
let enums = vec![data_node_kind_enum()];
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()
},
];
(type_defs, enums, functions)
}
fn kind_fixture(id: &str) -> Fixture {
Fixture {
docs: None,
requirements: Vec::new(),
id: id.to_string(),
category: None,
description: "test".to_string(),
tags: vec![],
skip: None,
env: None,
setup: Vec::new(),
call: None,
input: serde_json::Value::Null,
mock_response: None,
source: String::new(),
http: None,
asyncapi: None,
websocket: None,
preserve_input_urls: false,
assertions: vec![Assertion {
assertion_type: "equals".to_string(),
field: Some("kind".to_string()),
value: Some(serde_json::Value::String("KeyValue".to_string())),
..Assertion::default()
}],
visitor: None,
args: vec![],
assertion_recipes: vec![],
}
}
fn render(function: &str, type_defs: &[TypeDef], enums: &[EnumDef], functions: &[FunctionDef]) -> String {
let fixture = kind_fixture("kind_smoke");
let call = CallConfig {
function: function.to_string(),
result_var: "result".to_string(),
..CallConfig::default()
};
let e2e_config = E2eConfig {
call,
..Default::default()
};
let config = ResolvedCrateConfig::default();
let mut out = String::new();
render_test_method(
&mut out,
&fixture,
"SampleClass",
"",
"",
&[],
None,
false,
&e2e_config,
&std::collections::HashMap::new(),
false,
&[],
&config,
type_defs,
enums,
functions,
&[],
);
out
}
#[test]
fn an_enum_typed_field_with_no_enum_fields_config_is_classified_as_enum_via_the_ir() {
let (type_defs, enums, functions) = table_ir();
let out = render("process", &type_defs, &enums, &functions);
assert!(
out.contains(".getValue()"),
"expected the enum field to render through .getValue(), got:\n{out}"
);
assert!(
out.contains("assertEquals(\"KeyValue\", "),
"expected the equals assertion to compare against the wire value, got:\n{out}"
);
}
#[test]
fn a_same_named_non_enum_field_on_an_unrelated_type_is_not_misclassified_as_enum() {
let (type_defs, enums, functions) = table_ir();
let out = render("other", &type_defs, &enums, &functions);
assert!(
!out.contains(".getValue()"),
"a non-enum String field must not be routed through .getValue(), got:\n{out}"
);
}