#![allow(dead_code, unused_variables)]
use rust_pattern_macros::simple_factory;
use rust_patterns::{FactoryError, FactoryFallback};
#[simple_factory]
pub trait TestProduct {
fn get_id(&self) -> u32;
}
#[derive(Default)]
struct ProductImpl1;
impl TestProduct for ProductImpl1 {
fn get_id(&self) -> u32 {
1
}
}
#[derive(Default)]
struct ProductImpl2;
impl TestProduct for ProductImpl2 {
fn get_id(&self) -> u32 {
2
}
}
#[test]
fn test_simple_factory_macro_basic() {
let factory_type_name = std::any::type_name::<TestProductFactory>();
assert!(factory_type_name.contains("TestProductFactory"));
println!("Basic simple_factory macro test passed");
}
#[test]
fn test_simple_factory_create_method_exists() {
let result = TestProductFactory::create("test", FactoryFallback::NoFallback);
match result {
Err(FactoryError::FactoryNotFound(_)) => {
println!("Create method exists and returns correct error type");
}
_ => {
panic!("Expected FactoryNotFound error");
}
}
}
#[test]
fn test_simple_factory_method_signature() {
let _result = TestProductFactory::create("test", FactoryFallback::NoFallback);
fn assert_return_type<T: ?Sized>(_result: Result<Box<T>, FactoryError>) {}
match TestProductFactory::create("test", FactoryFallback::NoFallback) {
Err(FactoryError::FactoryNotFound(_)) => {
}
_ => {
}
}
println!("Create method signature test passed");
}
#[simple_factory(Send + Sync)]
pub trait ThreadSafeService {
fn process(&self) -> String;
}
#[derive(Default)]
struct SafeServiceImpl;
impl ThreadSafeService for SafeServiceImpl {
fn process(&self) -> String {
"processed".to_string()
}
}
#[test]
fn test_simple_factory_with_bounds() {
let factory_type_name = std::any::type_name::<ThreadSafeServiceFactory>();
assert!(factory_type_name.contains("ThreadSafeServiceFactory"));
let result = ThreadSafeServiceFactory::create("safe_service", FactoryFallback::NoFallback);
match result {
Err(FactoryError::FactoryNotFound(_)) => {
println!("Factory with bounds created successfully");
}
_ => {
panic!("Expected FactoryNotFound error for factory with bounds");
}
}
}
#[simple_factory(Send + Sync)]
pub trait ComplexService {
fn compute(&self) -> i32;
}
#[derive(Default)]
struct ComplexServiceImpl;
impl ComplexService for ComplexServiceImpl {
fn compute(&self) -> i32 {
42
}
}
#[test]
fn test_simple_factory_with_multiple_bounds() {
let _factory = ComplexServiceFactory;
let result = ComplexServiceFactory::create("complex", FactoryFallback::NoFallback);
match result {
Err(FactoryError::FactoryNotFound(_)) => {
println!("Factory with multiple bounds created successfully");
}
_ => {
panic!("Expected FactoryNotFound error for factory with multiple bounds");
}
}
}
#[test]
fn test_simple_factory_error_types() {
let result = TestProductFactory::create("nonexistent", FactoryFallback::NoFallback);
match result {
Err(FactoryError::FactoryNotFound(id)) => {
assert_eq!(id, "nonexistent");
println!("FactoryNotFound error test passed");
}
_ => panic!("Expected FactoryNotFound error"),
}
let result = TestProductFactory::create("", FactoryFallback::NoFallback);
match result {
Err(FactoryError::EmptyIdNoFallback) => {
println!("EmptyIdNoFallback error test passed");
}
_ => panic!("Expected EmptyIdNoFallback error"),
}
}
#[test]
fn test_factory_visibility() {
let _factory: TestProductFactory = TestProductFactory;
println!("Factory visibility test passed");
}
fn main() {
println!("Running simple_factory macro tests...");
test_simple_factory_macro_basic();
test_simple_factory_create_method_exists();
test_simple_factory_method_signature();
test_simple_factory_with_bounds();
test_simple_factory_with_multiple_bounds();
test_simple_factory_error_types();
test_factory_visibility();
println!("All tests passed!");
}