#![allow(clippy::used_underscore_binding, clippy::all, warnings, missing_docs)]
use super::*;
#[ derive( Debug, PartialEq, Clone, Default ) ]
pub struct SimpleProperty {
name: String,
code: isize,
}
impl SimpleProperty {
#[ inline ]
pub fn new<N, C>(name: N, code: C) -> Self
where
N: Into<String>,
C: Into<isize>,
{
Self {
name: name.into(),
code: code.into(),
}
}
}
#[ derive( Debug, PartialEq, Clone, Default ) ]
pub struct ParametrizedProperty<T>
where
T: Clone + Default + PartialEq + core::fmt::Debug,
{
name: T,
code: isize,
}
impl<T> ParametrizedProperty<T>
where
T: Clone + Default + PartialEq + core::fmt::Debug,
{
#[ inline ]
pub fn new<N, C>(name: N, code: C) -> Self
where
N: Into<T>,
C: Into<isize>,
{
Self {
name: name.into(),
code: code.into(),
}
}
}
#[ derive( Debug, PartialEq, former::Former ) ]
pub struct ParametrizedChild<T>
where
T: Clone + Default + PartialEq + core::fmt::Debug,
{
pub name: String,
pub properties: Vec<ParametrizedProperty<T>>,
pub active: bool,
}
impl<T> Default for ParametrizedChild<T>
where
T: Clone + Default + PartialEq + core::fmt::Debug,
{
fn default() -> Self {
Self {
name: "default_child".to_string(),
properties: Vec::new(),
active: true,
}
}
}
#[ derive( Debug, PartialEq, former::Former ) ]
pub struct StringParametrizedChild {
pub name: String,
pub properties: Vec<ParametrizedProperty<String>>,
pub active: bool,
}
impl Default for StringParametrizedChild {
fn default() -> Self {
Self {
name: "default_string_child".to_string(),
properties: Vec::new(),
active: true,
}
}
}
#[ derive( Debug, PartialEq, former::Former ) ]
pub struct IntParametrizedChild {
pub name: String,
pub properties: Vec<ParametrizedProperty<i32>>,
pub active: bool,
}
impl Default for IntParametrizedChild {
fn default() -> Self {
Self {
name: "default_int_child".to_string(),
properties: Vec::new(),
active: true,
}
}
}
#[ test ]
fn parametrized_struct_where_simple_property_test() {
let prop = SimpleProperty::new("test_prop", 42isize);
assert_eq!(prop.name, "test_prop");
assert_eq!(prop.code, 42isize);
let prop2 = SimpleProperty::new("another_prop".to_string(), -1_isize);
assert_eq!(prop2.name, "another_prop");
assert_eq!(prop2.code, -1);
}
#[ test ]
fn parametrized_struct_where_string_property_test() {
let string_prop = ParametrizedProperty::<String>::new("string_prop".to_string(), 100isize);
assert_eq!(string_prop.name, "string_prop");
assert_eq!(string_prop.code, 100isize);
let got = StringParametrizedChild::former()
.name("string_child".to_string())
.properties(vec![string_prop.clone()])
.active(true)
.form();
let expected = StringParametrizedChild {
name: "string_child".to_string(),
properties: vec![string_prop],
active: true,
};
assert_eq!(got, expected);
}
#[ test ]
fn parametrized_struct_where_int_property_test() {
let int_prop = ParametrizedProperty::<i32>::new(123, 200isize);
assert_eq!(int_prop.name, 123);
assert_eq!(int_prop.code, 200isize);
let got = IntParametrizedChild::former()
.name("int_child".to_string())
.properties(vec![int_prop.clone()])
.active(false)
.form();
let expected = IntParametrizedChild {
name: "int_child".to_string(),
properties: vec![int_prop],
active: false,
};
assert_eq!(got, expected);
}
#[ test ]
fn parametrized_struct_where_generic_child_test() {
let string_prop = ParametrizedProperty::<String>::new("generic_prop".to_string(), 300isize);
let got = ParametrizedChild::former()
.name("generic_child".to_string())
.properties(vec![string_prop.clone()])
.active(true)
.form();
let expected = ParametrizedChild {
name: "generic_child".to_string(),
properties: vec![string_prop],
active: true,
};
assert_eq!(got, expected);
}
#[ test ]
fn parametrized_struct_where_complex_generics_test() {
let bool_prop = ParametrizedProperty::<bool>::new(true, 400isize);
let bool_child = ParametrizedChild::former()
.name("bool_child".to_string())
.properties(vec![bool_prop.clone()])
.active(false)
.form();
assert!(bool_child.properties[0].name);
assert_eq!(bool_child.properties[0].code, 400isize);
let option_prop = ParametrizedProperty::<Option<String>>::new(Some("optional".to_string()), 500isize);
let option_child = ParametrizedChild::former()
.name("option_child".to_string())
.properties(vec![option_prop.clone()])
.active(true)
.form();
assert_eq!(option_child.properties[0].name, Some("optional".to_string()));
assert_eq!(option_child.properties[0].code, 500isize);
}
#[ test ]
#[allow(clippy::cast_possible_wrap)]
fn parametrized_struct_where_multiple_properties_test() {
let props = vec![
ParametrizedProperty::<String>::new("prop1".to_string(), 1isize),
ParametrizedProperty::<String>::new("prop2".to_string(), 2isize),
ParametrizedProperty::<String>::new("prop3".to_string(), 3isize),
];
let got = StringParametrizedChild::former()
.name("multi_prop_child".to_string())
.properties(props.clone())
.active(true)
.form();
assert_eq!(got.name, "multi_prop_child");
assert_eq!(got.properties.len(), 3);
assert!(got.active);
for (i, prop) in got.properties.iter().enumerate() {
assert_eq!(prop.name, format!("prop{}", i + 1));
assert_eq!(prop.code, (i + 1) as isize);
}
}
#[ test ]
fn parametrized_struct_where_comprehensive_validation_test() {
let simple_prop = SimpleProperty::new("simple", 1000isize);
let string_prop = ParametrizedProperty::<String>::new("string".to_string(), 2000isize);
let int_prop = ParametrizedProperty::<i32>::new(42, 3000isize);
let string_child = StringParametrizedChild::former()
.name("comprehensive_string".to_string())
.properties(vec![ParametrizedProperty::<String>::new("comp_str".to_string(), 4000isize)])
.active(true)
.form();
let int_child = IntParametrizedChild::former()
.name("comprehensive_int".to_string())
.properties(vec![ParametrizedProperty::<i32>::new(999, 5000isize)])
.active(false)
.form();
assert_eq!(simple_prop.name, "simple");
assert_eq!(simple_prop.code, 1000isize);
assert_eq!(string_prop.name, "string");
assert_eq!(string_prop.code, 2000isize);
assert_eq!(int_prop.name, 42);
assert_eq!(int_prop.code, 3000isize);
assert_eq!(string_child.name, "comprehensive_string");
assert_eq!(string_child.properties[0].name, "comp_str");
assert_eq!(string_child.properties[0].code, 4000isize);
assert_eq!(int_child.name, "comprehensive_int");
assert_eq!(int_child.properties[0].name, 999);
assert_eq!(int_child.properties[0].code, 5000isize);
}