use depyler_core::DepylerPipeline;
#[test]
fn test_nested_scope_management() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def outer():
x = 1
if True:
y = 2
z = x + y
return x
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn outer"));
assert!(rust_code.contains("let") || rust_code.contains("_cse_temp"));
}
#[test]
fn test_variable_shadowing() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def shadow_test():
x = 1
if True:
x = 2 # Shadow outer x
y = x + 1
return x
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn shadow_test"));
}
#[test]
fn test_multiple_scope_levels() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def deep_scopes():
a = 1
if True:
b = 2
if True:
c = 3
if True:
d = 4
result = a + b + c + d
return result
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn deep_scopes"));
}
#[test]
fn test_loop_scoping() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def loop_scope():
total = 0
for i in [1, 2, 3]:
temp = i * 2
total = total + temp
return total
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn loop_scope"));
assert!(rust_code.contains("for ") || rust_code.contains("while"));
}
#[test]
fn test_parameter_scoping() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def with_params(x: int, y: int) -> int:
result = x + y
return result
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn with_params"));
assert!(rust_code.contains("i32") || rust_code.contains("int"));
}
#[test]
fn test_union_type_processing() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
from typing import Union
def union_func(value: Union[int, str]) -> Union[int, str]:
return value
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn union_func"));
}
#[test]
fn test_union_multiple_types() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
from typing import Union
def multi_union(value: Union[int, str, bool]) -> int:
return 42
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn multi_union"));
}
#[test]
fn test_nested_union_types() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
from typing import Union
def nested_unions(
a: Union[int, str],
b: Union[float, bool]
) -> Union[int, str]:
return a
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn nested_unions"));
}
#[test]
fn test_mutation_scope_invariants() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def scope_invariant():
x = 1
y = x + 1
if True:
z = y + 1
w = z + 1
return x + y
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn scope_invariant"));
}
#[test]
fn test_union_enum_reuse() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
from typing import Union
def func1(value: Union[int, str]) -> Union[int, str]:
return value
def func2(value: Union[int, str]) -> Union[int, str]:
return value
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn func1"));
assert!(rust_code.contains("fn func2"));
}
#[test]
fn test_empty_scope_stack() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def simple():
return 42
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn simple"));
assert!(rust_code.contains("42"));
}
#[test]
fn test_undeclared_variable_check() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def forward_ref():
x = y if False else 1
y = 2
return x
"#;
let result = pipeline.transpile(python_code);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_complex_context_scenario() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
from typing import Union
def complex_context(value: Union[int, str]) -> int:
result = 0
if isinstance(value, int):
temp = value * 2
result = temp + 1
else:
temp = 42
result = temp
return result
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn complex_context"));
}
#[test]
fn test_mutable_variable_tracking() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def mutable_tracking():
x = 0
x = x + 1
x = x + 2
return x
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
assert!(rust_code.contains("fn mutable_tracking"));
assert!(rust_code.contains("mut") || rust_code.contains("_cse_temp"));
}
#[test]
fn test_generator_state() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def generator_func():
for i in [1, 2, 3]:
yield i
"#;
let result = pipeline.transpile(python_code);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_classmethod_context() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
class MyClass:
@classmethod
def class_method(cls):
return 42
"#;
let result = pipeline.transpile(python_code);
assert!(result.is_ok() || result.is_err());
}