use crate::DepylerPipeline;
fn transpile(code: &str) -> String {
let pipeline = DepylerPipeline::new();
pipeline.transpile(code).expect("transpilation should succeed")
}
fn transpile_ok(code: &str) -> bool {
let pipeline = DepylerPipeline::new();
pipeline.transpile(code).is_ok()
}
#[test]
fn test_class_simple() {
let code = transpile("class Point:\n def __init__(self, x, y):\n self.x = x\n self.y = y");
assert!(code.contains("struct Point") || code.contains("impl Point") || code.contains("pub"));
}
#[test]
fn test_class_with_methods() {
let code = transpile("class Counter:\n def __init__(self):\n self.count = 0\n def increment(self):\n self.count += 1");
assert!(code.contains("struct Counter") || code.contains("impl"));
}
#[test]
fn test_class_with_static_method() {
let code = transpile("class Math:\n @staticmethod\n def add(a, b):\n return a + b");
assert!(code.contains("struct Math") || code.contains("fn add"));
}
#[test]
fn test_class_with_class_method() {
let code = transpile("class Factory:\n @classmethod\n def create(cls):\n return cls()");
assert!(code.contains("struct Factory") || code.contains("impl"));
}
#[test]
fn test_class_with_property() {
assert!(transpile_ok("class Person:\n def __init__(self, name):\n self._name = name\n @property\n def name(self):\n return self._name"));
}
#[test]
fn test_class_inheritance() {
assert!(transpile_ok("class Animal:\n pass\n\nclass Dog(Animal):\n pass"));
}
#[test]
fn test_class_multiple_methods() {
let code = transpile("class Calculator:\n def add(self, a, b):\n return a + b\n def sub(self, a, b):\n return a - b\n def mul(self, a, b):\n return a * b");
assert!(code.contains("fn add") || code.contains("fn sub") || code.contains("fn mul"));
}
#[test]
fn test_dataclass_simple() {
let code = transpile("from dataclasses import dataclass\n\n@dataclass\nclass Point:\n x: int\n y: int");
assert!(code.contains("struct Point") || code.contains("x:") || code.contains("y:"));
}
#[test]
fn test_dataclass_with_defaults() {
let code = transpile("from dataclasses import dataclass\n\n@dataclass\nclass Config:\n name: str\n value: int = 0");
assert!(code.contains("struct Config") || code.contains("name"));
}
#[test]
fn test_dataclass_with_complex_types() {
let code = transpile("from dataclasses import dataclass\nfrom typing import List, Optional\n\n@dataclass\nclass Container:\n items: List[int]\n label: Optional[str] = None");
assert!(code.contains("struct Container") || code.contains("Vec"));
}
#[test]
fn test_dataclass_frozen() {
let code = transpile("from dataclasses import dataclass\n\n@dataclass(frozen=True)\nclass Frozen:\n value: int");
assert!(code.contains("struct Frozen") || code.contains("value"));
}
#[test]
fn test_class_str_method() {
let code = transpile("class Foo:\n def __str__(self):\n return 'Foo'");
assert!(code.contains("Display") || code.contains("fmt") || code.contains("struct Foo"));
}
#[test]
fn test_class_repr_method() {
let code = transpile("class Foo:\n def __repr__(self):\n return 'Foo()'");
assert!(code.contains("Debug") || code.contains("fmt") || code.contains("struct Foo"));
}
#[test]
fn test_class_eq_method() {
let code = transpile("class Point:\n def __init__(self, x):\n self.x = x\n def __eq__(self, other):\n return self.x == other.x");
assert!(code.contains("PartialEq") || code.contains("==") || code.contains("struct Point"));
}
#[test]
fn test_class_hash_method() {
let code = transpile("class Key:\n def __init__(self, value):\n self.value = value\n def __hash__(self):\n return hash(self.value)");
assert!(code.contains("Hash") || code.contains("struct Key"));
}
#[test]
fn test_class_len_method() {
let code = transpile("class Container:\n def __init__(self):\n self.items = []\n def __len__(self):\n return len(self.items)");
assert!(code.contains("len") || code.contains("struct Container"));
}
#[test]
fn test_class_iter_method() {
let code = transpile("class Range:\n def __init__(self, n):\n self.n = n\n def __iter__(self):\n return iter(range(self.n))");
assert!(code.contains("iter") || code.contains("Iterator") || code.contains("struct Range"));
}
#[test]
fn test_class_getitem_method() {
let code = transpile("class List:\n def __init__(self):\n self.items = []\n def __getitem__(self, index):\n return self.items[index]");
assert!(code.contains("Index") || code.contains("[") || code.contains("struct List"));
}
#[test]
fn test_class_setitem_method() {
let code = transpile("class Dict:\n def __init__(self):\n self.data = {}\n def __setitem__(self, key, value):\n self.data[key] = value");
assert!(code.contains("[") || code.contains("struct Dict"));
}
#[test]
fn test_class_contains_method() {
let code = transpile("class Set:\n def __init__(self):\n self.items = set()\n def __contains__(self, item):\n return item in self.items");
assert!(code.contains("contains") || code.contains("struct Set"));
}
#[test]
fn test_class_add_method() {
let code = transpile("class Vector:\n def __init__(self, x):\n self.x = x\n def __add__(self, other):\n return Vector(self.x + other.x)");
assert!(code.contains("Add") || code.contains("+") || code.contains("struct Vector"));
}
#[test]
fn test_class_sub_method() {
let code = transpile("class Vector:\n def __init__(self, x):\n self.x = x\n def __sub__(self, other):\n return Vector(self.x - other.x)");
assert!(code.contains("Sub") || code.contains("-") || code.contains("struct Vector"));
}
#[test]
fn test_class_mul_method() {
let code = transpile("class Number:\n def __init__(self, value):\n self.value = value\n def __mul__(self, other):\n return Number(self.value * other)");
assert!(code.contains("Mul") || code.contains("*") || code.contains("struct Number"));
}
#[test]
fn test_class_lt_method() {
let code = transpile("class Score:\n def __init__(self, value):\n self.value = value\n def __lt__(self, other):\n return self.value < other.value");
assert!(code.contains("Ord") || code.contains("<") || code.contains("struct Score"));
}
#[test]
fn test_class_named_string() {
let code = transpile("class String:\n def __init__(self):\n pass");
assert!(code.contains("struct") || code.contains("String_"));
}
#[test]
fn test_class_named_vec() {
assert!(transpile_ok("class Vec:\n def __init__(self):\n pass"));
}
#[test]
fn test_class_named_option() {
assert!(transpile_ok("class Option:\n def __init__(self):\n pass"));
}
#[test]
fn test_method_immutable() {
let code = transpile("class Foo:\n def __init__(self):\n self.x = 0\n def get_x(self):\n return self.x");
assert!(code.contains("&self") || code.contains("fn get_x") || code.contains("struct Foo"));
}
#[test]
fn test_method_mutable() {
let code = transpile("class Foo:\n def __init__(self):\n self.x = 0\n def set_x(self, value):\n self.x = value");
assert!(code.contains("&mut self") || code.contains("fn set_x") || code.contains("struct Foo"));
}
#[test]
fn test_method_with_augmented_assign() {
let code = transpile("class Counter:\n def __init__(self):\n self.count = 0\n def increment(self):\n self.count += 1");
assert!(code.contains("&mut self") || code.contains("fn increment") || code.contains("struct Counter"));
}
#[test]
fn test_typed_field_int() {
let code = transpile("from dataclasses import dataclass\n\n@dataclass\nclass Data:\n value: int");
assert!(code.contains("i64") || code.contains("i32") || code.contains("value"));
}
#[test]
fn test_typed_field_float() {
let code = transpile("from dataclasses import dataclass\n\n@dataclass\nclass Data:\n value: float");
assert!(code.contains("f64") || code.contains("f32") || code.contains("value"));
}
#[test]
fn test_typed_field_str() {
let code = transpile("from dataclasses import dataclass\n\n@dataclass\nclass Data:\n name: str");
assert!(code.contains("String") || code.contains("str") || code.contains("name"));
}
#[test]
fn test_typed_field_bool() {
let code = transpile("from dataclasses import dataclass\n\n@dataclass\nclass Data:\n flag: bool");
assert!(code.contains("bool") || code.contains("flag"));
}
#[test]
fn test_typed_field_list() {
let code = transpile("from dataclasses import dataclass\nfrom typing import List\n\n@dataclass\nclass Data:\n items: List[int]");
assert!(code.contains("Vec") || code.contains("items"));
}
#[test]
fn test_typed_field_dict() {
let code = transpile("from dataclasses import dataclass\nfrom typing import Dict\n\n@dataclass\nclass Data:\n mapping: Dict[str, int]");
assert!(code.contains("HashMap") || code.contains("mapping"));
}
#[test]
fn test_typed_field_optional() {
let code = transpile("from dataclasses import dataclass\nfrom typing import Optional\n\n@dataclass\nclass Data:\n value: Optional[int]");
assert!(code.contains("Option") || code.contains("value"));
}
#[test]
fn test_typed_field_tuple() {
let code = transpile("from dataclasses import dataclass\nfrom typing import Tuple\n\n@dataclass\nclass Data:\n point: Tuple[int, int]");
assert!(code.contains("(") || code.contains("point"));
}
#[test]
fn test_custom_exception() {
let code = transpile("class CustomError(Exception):\n pass");
assert!(code.contains("struct CustomError") || code.contains("Error"));
}
#[test]
fn test_exception_with_message() {
let code = transpile("class ValidationError(Exception):\n def __init__(self, message):\n self.message = message\n super().__init__(message)");
assert!(code.contains("struct ValidationError") || code.contains("message"));
}
#[test]
fn test_enum_class() {
let code = transpile("from enum import Enum\n\nclass Color(Enum):\n RED = 1\n GREEN = 2\n BLUE = 3");
assert!(code.contains("enum Color") || code.contains("RED") || code.contains("GREEN"));
}
#[test]
fn test_enum_auto() {
assert!(transpile_ok("from enum import Enum, auto\n\nclass Status(Enum):\n PENDING = auto()\n ACTIVE = auto()\n DONE = auto()"));
}
#[test]
fn test_class_with_type_annotations() {
let code = transpile("class TypedClass:\n x: int\n y: str\n \n def __init__(self, x: int, y: str):\n self.x = x\n self.y = y");
assert!(code.contains("struct TypedClass") || code.contains("x:") || code.contains("y:"));
}
#[test]
fn test_class_with_default_factory() {
let code = transpile("from dataclasses import dataclass, field\nfrom typing import List\n\n@dataclass\nclass Container:\n items: List[int] = field(default_factory=list)");
assert!(code.contains("struct Container") || code.contains("Vec"));
}
#[test]
fn test_nested_class() {
assert!(transpile_ok("class Outer:\n class Inner:\n def __init__(self):\n pass\n def __init__(self):\n self.inner = self.Inner()"));
}
#[test]
fn test_class_with_slots() {
assert!(transpile_ok("class Efficient:\n __slots__ = ['x', 'y']\n def __init__(self, x, y):\n self.x = x\n self.y = y"));
}
#[test]
fn test_field_named_type() {
let code = transpile("class Node:\n def __init__(self):\n self.type = 'leaf'");
assert!(code.contains("r#type") || code.contains("type_") || code.contains("struct Node"));
}
#[test]
fn test_method_named_match() {
let code = transpile("class Matcher:\n def match(self, pattern):\n return True");
assert!(code.contains("r#match") || code.contains("fn match") || code.contains("struct Matcher"));
}
#[test]
fn test_field_named_loop() {
assert!(transpile_ok("class Cycler:\n def __init__(self):\n self.loop = 0"));
}
#[test]
fn test_private_method() {
let code = transpile("class Foo:\n def _private(self):\n return 1\n def public(self):\n return self._private()");
assert!(code.contains("fn _private") || code.contains("fn public") || code.contains("struct Foo"));
}
#[test]
fn test_dunder_method() {
let code = transpile("class Foo:\n def __custom__(self):\n return 1");
assert!(code.contains("fn") || code.contains("struct Foo"));
}
#[test]
fn test_abstract_class() {
assert!(transpile_ok("from abc import ABC, abstractmethod\n\nclass Shape(ABC):\n @abstractmethod\n def area(self):\n pass"));
}
#[test]
fn test_abstract_with_implementation() {
assert!(transpile_ok("from abc import ABC, abstractmethod\n\nclass Shape(ABC):\n @abstractmethod\n def area(self):\n pass\n\nclass Circle(Shape):\n def __init__(self, r):\n self.r = r\n def area(self):\n return 3.14 * self.r ** 2"));
}
#[test]
fn test_generic_class() {
assert!(transpile_ok("from typing import Generic, TypeVar\n\nT = TypeVar('T')\n\nclass Box(Generic[T]):\n def __init__(self, value: T):\n self.value = value"));
}
#[test]
fn test_multiple_classes() {
let code = transpile("class Foo:\n pass\n\nclass Bar:\n pass\n\nclass Baz:\n pass");
assert!(code.contains("struct Foo") || code.contains("struct Bar") || code.contains("struct Baz"));
}
#[test]
fn test_class_referencing_another() {
let code = transpile("class Node:\n def __init__(self, value):\n self.value = value\n self.next = None\n\nclass LinkedList:\n def __init__(self):\n self.head = None");
assert!(code.contains("struct Node") || code.contains("struct LinkedList"));
}
#[test]
fn test_class_with_keyword_field() {
assert!(transpile_ok("class Foo:\n def __init__(self):\n self.type = 'int'"));
}
#[test]
fn test_class_with_keyword_method() {
assert!(transpile_ok("class Foo:\n def match(self, pattern):\n return pattern"));
}
#[test]
fn test_class_shadowing_stdlib() {
assert!(transpile_ok("class Vec:\n def __init__(self):\n self.data = []"));
}
#[test]
fn test_class_shadowing_string() {
assert!(transpile_ok("class String:\n def __init__(self, s):\n self.value = s"));
}
#[test]
fn test_class_shadowing_option() {
assert!(transpile_ok("class Option:\n def __init__(self, value):\n self.value = value"));
}
#[test]
fn test_class_field_typed_int() {
assert!(transpile_ok(r#"class Counter:
def __init__(self):
self.count: int = 0"#));
}
#[test]
fn test_class_field_typed_str() {
assert!(transpile_ok(r#"class Person:
def __init__(self, name: str):
self.name = name"#));
}
#[test]
fn test_class_field_typed_list() {
assert!(transpile_ok(r#"from typing import List
class Container:
def __init__(self):
self.items: List[int] = []"#));
}
#[test]
fn test_class_field_typed_dict() {
assert!(transpile_ok(r#"from typing import Dict
class Cache:
def __init__(self):
self.data: Dict[str, int] = {}"#));
}
#[test]
fn test_class_field_optional() {
assert!(transpile_ok(r#"from typing import Optional
class Node:
def __init__(self):
self.next: Optional[Node] = None"#));
}
#[test]
fn test_class_str_method_coverage() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"({self.x}, {self.y})""#));
}
#[test]
fn test_class_repr_method_coverage() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Point({self.x}, {self.y})""#));
}
#[test]
fn test_class_eq_method_coverage() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
return self.x == other.x and self.y == other.y"#));
}
#[test]
fn test_class_hash_method_coverage() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __hash__(self):
return hash((self.x, self.y))"#));
}
#[test]
fn test_class_len_method_coverage() {
assert!(transpile_ok(r#"class Container:
def __init__(self):
self.items = []
def __len__(self):
return len(self.items)"#));
}
#[test]
fn test_class_getitem_method_coverage() {
assert!(transpile_ok(r#"class Container:
def __init__(self):
self.items = []
def __getitem__(self, index):
return self.items[index]"#));
}
#[test]
fn test_class_setitem_method_coverage() {
assert!(transpile_ok(r#"class Container:
def __init__(self):
self.items = []
def __setitem__(self, index, value):
self.items[index] = value"#));
}
#[test]
fn test_class_iter_method_coverage() {
assert!(transpile_ok(r#"class Container:
def __init__(self):
self.items = []
def __iter__(self):
return iter(self.items)"#));
}
#[test]
fn test_class_contains_method_coverage() {
assert!(transpile_ok(r#"class Container:
def __init__(self):
self.items = []
def __contains__(self, item):
return item in self.items"#));
}
#[test]
fn test_class_add_method_coverage() {
assert!(transpile_ok(r#"class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)"#));
}
#[test]
fn test_class_sub_method_coverage() {
assert!(transpile_ok(r#"class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)"#));
}
#[test]
fn test_class_mul_method_coverage() {
assert!(transpile_ok(r#"class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)"#));
}
#[test]
fn test_class_truediv_method() {
assert!(transpile_ok(r#"class Fraction:
def __init__(self, num, den):
self.num = num
self.den = den
def __truediv__(self, other):
return Fraction(self.num * other.den, self.den * other.num)"#));
}
#[test]
fn test_class_floordiv_method() {
assert!(transpile_ok(r#"class MyInt:
def __init__(self, val):
self.val = val
def __floordiv__(self, other):
return MyInt(self.val // other.val)"#));
}
#[test]
fn test_class_mod_method() {
assert!(transpile_ok(r#"class MyInt:
def __init__(self, val):
self.val = val
def __mod__(self, other):
return MyInt(self.val % other.val)"#));
}
#[test]
fn test_class_neg_method() {
assert!(transpile_ok(r#"class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __neg__(self):
return Vector(-self.x, -self.y)"#));
}
#[test]
fn test_class_lt_method_coverage() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x):
self.x = x
def __lt__(self, other):
return self.x < other.x"#));
}
#[test]
fn test_class_le_method() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x):
self.x = x
def __le__(self, other):
return self.x <= other.x"#));
}
#[test]
fn test_class_gt_method() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x):
self.x = x
def __gt__(self, other):
return self.x > other.x"#));
}
#[test]
fn test_class_ge_method() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x):
self.x = x
def __ge__(self, other):
return self.x >= other.x"#));
}
#[test]
fn test_class_ne_method() {
assert!(transpile_ok(r#"class Point:
def __init__(self, x):
self.x = x
def __ne__(self, other):
return self.x != other.x"#));
}
#[test]
fn test_class_enter_exit_methods() {
assert!(transpile_ok(r#"class FileHandler:
def __init__(self, path):
self.path = path
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass"#));
}
#[test]
fn test_class_single_inheritance() {
assert!(transpile_ok(r#"class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "bark""#));
}
#[test]
fn test_class_multiple_inheritance() {
assert!(transpile_ok(r#"class A:
def a(self):
return 1
class B:
def b(self):
return 2
class C(A, B):
def c(self):
return self.a() + self.b()"#));
}
#[test]
fn test_class_call_super() {
assert!(transpile_ok(r#"class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed"#));
}
#[test]
fn test_dataclass_simple_coverage() {
assert!(transpile_ok(r#"from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int"#));
}
#[test]
fn test_dataclass_with_default() {
assert!(transpile_ok(r#"from dataclasses import dataclass
@dataclass
class Config:
name: str
value: int = 0"#));
}
#[test]
fn test_dataclass_with_field() {
assert!(transpile_ok(r#"from dataclasses import dataclass, field
from typing import List
@dataclass
class Container:
items: List[int] = field(default_factory=list)"#));
}
#[test]
fn test_dataclass_frozen_coverage() {
assert!(transpile_ok(r#"from dataclasses import dataclass
@dataclass(frozen=True)
class Point:
x: int
y: int"#));
}
#[test]
fn test_method_access_field() {
assert!(transpile_ok(r#"class Counter:
def __init__(self):
self.count = 0
def get(self):
return self.count"#));
}
#[test]
fn test_method_modify_field() {
assert!(transpile_ok(r#"class Counter:
def __init__(self):
self.count = 0
def set(self, value):
self.count = value"#));
}
#[test]
fn test_method_call_other_method() {
assert!(transpile_ok(r#"class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
def double_increment(self):
self.increment()
self.increment()"#));
}
#[test]
fn test_class_constant() {
assert!(transpile_ok(r#"class Math:
PI = 3.14159
E = 2.71828"#));
}
#[test]
fn test_class_constant_access() {
assert!(transpile_ok(r#"class Circle:
PI = 3.14159
def __init__(self, r):
self.r = r
def area(self):
return Circle.PI * self.r ** 2"#));
}
#[test]
fn test_enum_simple() {
assert!(transpile_ok(r#"from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3"#));
}
#[test]
fn test_enum_string() {
assert!(transpile_ok(r#"from enum import Enum
class Status(Enum):
PENDING = "pending"
ACTIVE = "active"
DONE = "done""#));
}
#[test]
fn test_enum_auto_coverage() {
assert!(transpile_ok(r#"from enum import Enum, auto
class Color(Enum):
RED = auto()
GREEN = auto()
BLUE = auto()"#));
}
#[test]
fn test_property_getter() {
assert!(transpile_ok(r#"class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name"#));
}
#[test]
fn test_property_setter() {
assert!(transpile_ok(r#"class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value"#));
}
#[test]
fn test_property_deleter() {
assert!(transpile_ok(r#"class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.deleter
def name(self):
self._name = None"#));
}
#[test]
fn test_class_with_slots_coverage() {
assert!(transpile_ok(r#"class Point:
__slots__ = ['x', 'y']
def __init__(self, x, y):
self.x = x
self.y = y"#));
}
#[test]
fn test_class_nested() {
assert!(transpile_ok(r#"class Outer:
class Inner:
def __init__(self):
self.value = 1
def __init__(self):
self.inner = Outer.Inner()"#));
}
#[test]
fn test_class_method_default_mutable() {
assert!(transpile_ok(r#"class Container:
def add(self, items=None):
if items is None:
items = []
return items"#));
}
#[test]
fn test_class_annotation_classvar() {
assert!(transpile_ok(r#"from typing import ClassVar
class Counter:
count: ClassVar[int] = 0"#));
}
#[test]
fn test_class_annotation_generic_field() {
assert!(transpile_ok(r#"from typing import List, Optional
class Node:
children: List['Node']
parent: Optional['Node']
def __init__(self):
self.children = []
self.parent = None"#));
}
#[test]
fn test_keyword_as_param() {
let code = transpile(r#"def process_type(type: str) -> str:
return type"#);
assert!(code.contains("fn") || code.contains("type") || code.contains("r#type"));
}
#[test]
fn test_keyword_match_as_var() {
let code = transpile(r#"def find_match(items: list) -> str:
match = items[0]
return match"#);
assert!(code.contains("fn") || code.contains("r#match") || code.contains("match_"));
}
#[test]
fn test_self_param_suffix() {
let code = transpile(r#"def use_self_name():
self_ = "me"
return self_"#);
assert!(code.contains("fn") || code.contains("self_"));
}
#[test]
fn test_crate_param_handling() {
let code = transpile(r#"def check_crate(crate_name: str) -> bool:
return len(crate_name) > 0"#);
assert!(code.contains("fn") || code.contains("crate"));
}
#[test]
fn test_sanitize_invalid_identifier() {
let code = transpile(r#"def assign():
x = 1
return x"#);
assert!(code.contains("fn"));
}
#[test]
fn test_empty_identifier_handling() {
let code = transpile(r#"def empty_test():
pass"#);
assert!(code.contains("fn empty_test"));
}
#[test]
fn test_type_alias_simple() {
let code = transpile(r#"from typing import List
IntList = List[int]
def process(items: IntList) -> int:
return len(items)"#);
assert!(code.contains("fn") || code.contains("Vec"));
}
#[test]
fn test_newtype_pattern() {
let code = transpile(r#"from typing import NewType
UserId = NewType('UserId', int)
def get_user(id: UserId) -> str:
return str(id)"#);
assert!(code.contains("fn") || code.contains("UserId") || code.contains("i64"));
}
#[test]
fn test_type_alias_dict() {
let code = transpile(r#"from typing import Dict
StringMap = Dict[str, str]
def create() -> StringMap:
return {}"#);
assert!(code.contains("fn") || code.contains("HashMap"));
}
#[test]
fn test_type_alias_optional() {
let code = transpile(r#"from typing import Optional
MaybeInt = Optional[int]
def get() -> MaybeInt:
return None"#);
assert!(code.contains("fn") || code.contains("Option"));
}
#[test]
fn test_protocol_with_type_param() {
let code = transpile(r#"from typing import Protocol, TypeVar
T = TypeVar('T')
class Container(Protocol[T]):
def get(self) -> T:
pass"#);
assert!(code.contains("trait") || code.contains("fn get") || code.contains("struct"));
}
#[test]
fn test_exception_class_value_error() {
let code = transpile(r#"class ValidationError(ValueError):
def __init__(self, message):
self.message = message"#);
assert!(code.contains("struct ValidationError") || code.contains("message"));
}
#[test]
fn test_exception_class_base_exception() {
let code = transpile(r#"class CustomError(BaseException):
def __init__(self, code, msg):
self.code = code
self.msg = msg"#);
assert!(code.contains("struct") || code.contains("code") || code.contains("msg"));
}
#[test]
fn test_exception_class_runtime_error() {
let code = transpile(r#"class OperationFailed(RuntimeError):
def __init__(self, operation, reason):
self.operation = operation
self.reason = reason"#);
assert!(code.contains("struct") || code.contains("operation"));
}
#[test]
fn test_generic_class_simple() {
let code = transpile(r#"from typing import Generic, TypeVar
T = TypeVar('T')
class Box(Generic[T]):
def __init__(self, value: T):
self.value = value"#);
assert!(code.contains("struct Box") || code.contains("<T>") || code.contains("value"));
}
#[test]
fn test_generic_class_multiple_params() {
let code = transpile(r#"from typing import Generic, TypeVar
K = TypeVar('K')
V = TypeVar('V')
class Pair(Generic[K, V]):
def __init__(self, key: K, value: V):
self.key = key
self.value = value"#);
assert!(code.contains("struct Pair") || code.contains("key") || code.contains("value"));
}
#[test]
fn test_generic_class_with_phantom() {
let code = transpile(r#"from typing import Generic, TypeVar
T = TypeVar('T')
class Factory(Generic[T]):
def __init__(self):
self.count = 0"#);
assert!(code.contains("struct Factory") || code.contains("PhantomData") || code.contains("count"));
}
#[test]
fn test_class_with_mutex_field() {
let code = transpile(r#"import threading
class Counter:
def __init__(self):
self.lock = threading.Lock()
self.value = 0"#);
assert!(code.contains("struct Counter") || code.contains("value"));
}
#[test]
fn test_class_with_list_field() {
let code = transpile(r#"from typing import List
class Container:
def __init__(self):
self.items: List[int] = []"#);
assert!(code.contains("struct Container") || code.contains("Vec"));
}
#[test]
fn test_class_with_dict_field() {
let code = transpile(r#"from typing import Dict
class Cache:
def __init__(self):
self.data: Dict[str, int] = {}"#);
assert!(code.contains("struct Cache") || code.contains("HashMap"));
}
#[test]
fn test_class_with_set_field() {
let code = transpile(r#"from typing import Set
class UniqueContainer:
def __init__(self):
self.items: Set[int] = set()"#);
assert!(code.contains("struct") || code.contains("HashSet"));
}
#[test]
fn test_class_variable_constant() {
let code = transpile(r#"class Config:
MAX_SIZE: int = 100
def __init__(self):
self.size = 0"#);
assert!(code.contains("struct Config") || code.contains("100"));
}
#[test]
fn test_class_variable_classvar() {
let code = transpile(r#"from typing import ClassVar
class Counter:
count: ClassVar[int] = 0
def __init__(self):
self.id = 1"#);
assert!(code.contains("struct Counter"));
}
#[test]
fn test_method_mutates_self_assign() {
let code = transpile(r#"class Counter:
def __init__(self):
self.value = 0
def increment(self):
self.value += 1"#);
assert!(code.contains("fn increment") || code.contains("&mut self") || code.contains("impl"));
}
#[test]
fn test_method_no_mutation() {
let code = transpile(r#"class Counter:
def __init__(self):
self.value = 0
def get(self) -> int:
return self.value"#);
assert!(code.contains("fn get") || code.contains("&self") || code.contains("impl"));
}
#[test]
fn test_method_conditional_mutation() {
let code = transpile(r#"class Toggle:
def __init__(self):
self.on = False
def toggle(self):
if self.on:
self.on = False
else:
self.on = True"#);
assert!(code.contains("fn toggle") || code.contains("impl"));
}
#[test]
fn test_method_loop_mutation() {
let code = transpile(r#"class Adder:
def __init__(self):
self.sum = 0
def add_all(self, items: list):
for item in items:
self.sum += item"#);
assert!(code.contains("fn add_all") || code.contains("impl"));
}
#[test]
fn test_dataclass_new_params() {
let code = transpile(r#"from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int"#);
assert!(code.contains("fn new") || code.contains("struct Point"));
}
#[test]
fn test_dataclass_with_default_new() {
let code = transpile(r#"from dataclasses import dataclass
@dataclass
class Config:
name: str
value: int = 0
active: bool = True"#);
assert!(code.contains("struct Config") || code.contains("fn new"));
}
#[test]
fn test_init_unused_params() {
let code = transpile(r#"class Foo:
def __init__(self, x, y, unused):
self.x = x
self.y = y"#);
assert!(code.contains("struct Foo") || code.contains("fn new"));
}
#[test]
fn test_init_all_used_params() {
let code = transpile(r#"class Pair:
def __init__(self, first, second):
self.first = first
self.second = second"#);
assert!(code.contains("struct Pair") || code.contains("fn new"));
}
#[test]
fn test_init_with_default_values() {
let code = transpile(r#"class Config:
def __init__(self, name):
self.name = name
self.count = 0
self.active = True"#);
assert!(code.contains("struct Config") || code.contains("fn new"));
}
#[test]
fn test_infer_return_type_literal_int() {
let code = transpile(r#"class Counter:
def __init__(self):
self.value = 0
def get(self):
return self.value"#);
assert!(code.contains("fn get") || code.contains("i64") || code.contains("impl"));
}
#[test]
fn test_infer_return_type_literal_string() {
let code = transpile(r#"class Named:
def __init__(self):
self.name = ""
def get_name(self):
return self.name"#);
assert!(code.contains("fn get_name") || code.contains("String") || code.contains("impl"));
}
#[test]
fn test_infer_return_type_literal_bool() {
let code = transpile(r#"class Flag:
def __init__(self):
self.active = False
def is_active(self):
return self.active"#);
assert!(code.contains("fn is_active") || code.contains("bool") || code.contains("impl"));
}
#[test]
fn test_infer_return_type_comparison() {
let code = transpile(r#"class Checker:
def __init__(self):
self.value = 0
def is_positive(self):
return self.value > 0"#);
assert!(code.contains("fn is_positive") || code.contains("bool") || code.contains("impl"));
}
#[test]
fn test_type_var_in_list() {
let code = transpile(r#"from typing import TypeVar, List
T = TypeVar('T')
def identity(items: List[T]) -> List[T]:
return items"#);
assert!(code.contains("fn identity") || code.contains("Vec"));
}
#[test]
fn test_type_var_in_dict() {
let code = transpile(r#"from typing import TypeVar, Dict
K = TypeVar('K')
V = TypeVar('V')
def swap(d: Dict[K, V]) -> Dict[V, K]:
return {v: k for k, v in d.items()}"#);
assert!(code.contains("fn swap") || code.contains("HashMap"));
}
#[test]
fn test_type_var_in_optional() {
let code = transpile(r#"from typing import TypeVar, Optional
T = TypeVar('T')
def unwrap_or(opt: Optional[T], default: T) -> T:
if opt is None:
return default
return opt"#);
assert!(code.contains("fn unwrap_or") || code.contains("Option"));
}
#[test]
fn test_type_var_in_tuple() {
let code = transpile(r#"from typing import TypeVar, Tuple
T = TypeVar('T')
def first(pair: Tuple[T, T]) -> T:
return pair[0]"#);
assert!(code.contains("fn first") || code.contains("("));
}
#[test]
fn test_type_var_in_callable() {
let code = transpile(r#"from typing import TypeVar, Callable
T = TypeVar('T')
R = TypeVar('R')
def apply(f: Callable[[T], R], x: T) -> R:
return f(x)"#);
assert!(code.contains("fn apply") || code.contains("Fn"));
}
#[test]
fn test_dr_class_named_vec() {
let code = transpile(r#"class Vec:
def __init__(self, x, y):
self.x = x
self.y = y"#);
assert!(code.contains("struct") || code.contains("Vec") || code.contains("PyVec"));
}
#[test]
fn test_dr_class_named_option() {
let code = transpile(r#"class Option:
def __init__(self, value):
self.value = value"#);
assert!(code.contains("struct") || code.contains("Option") || code.contains("PyOption"));
}
#[test]
fn test_dr_class_named_result() {
let code = transpile(r#"class Result:
def __init__(self, value, error):
self.value = value
self.error = error"#);
assert!(code.contains("struct") || code.contains("Result") || code.contains("PyResult"));
}
#[test]
fn test_dr_class_named_string() {
let code = transpile(r#"class String:
def __init__(self, data):
self.data = data"#);
assert!(code.contains("struct") || code.contains("String") || code.contains("PyString"));
}
#[test]
fn test_tuple_pattern_simple() {
let code = transpile(r#"def process():
(a, b) = (1, 2)
return a + b"#);
assert!(code.contains("fn process") || code.contains("let"));
}
#[test]
fn test_tuple_pattern_in_for() {
let code = transpile(r#"def process(items: list):
for (k, v) in items:
print(k, v)"#);
assert!(code.contains("fn process") || code.contains("for"));
}
#[test]
fn test_function_with_varargs() {
let code = transpile(r#"def sum_all(*args):
total = 0
for x in args:
total += x
return total"#);
assert!(code.contains("fn sum_all") || code.contains("args"));
}
#[test]
fn test_function_with_kwargs() {
let code = transpile(r#"def configure(**kwargs):
return len(kwargs)"#);
assert!(code.contains("fn configure") || code.contains("kwargs"));
}
#[test]
fn test_function_with_type_hints() {
let code = transpile(r#"def process(x: int, y: str) -> bool:
return len(y) == x"#);
assert!(code.contains("fn process") || code.contains("i64") || code.contains("String"));
}
#[test]
fn test_function_with_defaults() {
let code = transpile(r#"def greet(name: str = "World"):
return "Hello, " + name"#);
assert!(code.contains("fn greet") || code.contains("World"));
}
#[test]
fn test_empty_class() {
let code = transpile(r#"class Empty:
pass"#);
assert!(code.contains("struct Empty"));
}
#[test]
fn test_class_with_docstring() {
let code = transpile(r#"class Documented:
"""This is a documented class."""
def __init__(self):
self.value = 0"#);
assert!(code.contains("struct Documented") || code.contains("///"));
}
#[test]
fn test_method_returning_self() {
let code = transpile(r#"class Builder:
def __init__(self):
self.value = 0
def with_value(self, v: int):
self.value = v
return self"#);
assert!(code.contains("fn with_value") || code.contains("Self") || code.contains("impl"));
}
#[test]
fn test_dr_nested_class() {
let code = transpile(r#"class Outer:
class Inner:
def __init__(self):
self.x = 0"#);
assert!(code.contains("struct") || code.contains("Outer") || code.contains("Inner"));
}
#[test]
fn test_protocol_simple_ok() {
assert!(transpile_ok(r#"from typing import Protocol
class Drawable(Protocol):
def draw(self) -> None:
pass"#));
}
#[test]
fn test_protocol_multiple_methods_ok() {
assert!(transpile_ok(r#"from typing import Protocol
class Iterator(Protocol):
def next(self) -> int:
pass
def has_next(self) -> bool:
pass"#));
}
#[test]
fn test_protocol_runtime_checkable_ok() {
assert!(transpile_ok(r#"from typing import Protocol, runtime_checkable
@runtime_checkable
class Sized(Protocol):
def __len__(self) -> int:
pass"#));
}
#[test]
fn test_tuple_pattern_nested_skip() {
}
#[test]
fn test_keyword_async_as_field_ok() {
assert!(transpile_ok(r#"class Task:
def __init__(self):
self.is_async = False"#));
}
#[test]
fn test_cov95_stdlib_shadowing_list() {
let code = transpile(r#"def process(list: list) -> int:
return len(list)"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_stdlib_shadowing_dict() {
let code = transpile(r#"def process(dict: dict):
return dict.keys()"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_stdlib_shadowing_str() {
let code = transpile(r#"def process(str: str) -> str:
return str.upper()"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_safe_class_name_keywords() {
let code = transpile(r#"class Type:
x: int"#);
assert!(code.contains("struct"));
}
#[test]
fn test_cov95_safe_class_name_underscore() {
let code = transpile(r#"class _Private:
x: int"#);
assert!(code.contains("struct"));
}
#[test]
fn test_cov95_sanitize_identifier_rust_keyword() {
let code = transpile(r#"def process(type: str) -> str:
return type"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_sanitize_identifier_self() {
let code = transpile(r#"class Foo:
def get_self(self):
return self"#);
assert!(code.contains("struct") || code.contains("impl"));
}
#[test]
fn test_cov95_type_alias_simple() {
let code = transpile(r#"from typing import List
IntList = List[int]
def process(items: IntList) -> int:
return sum(items)"#);
assert!(code.contains("fn") || code.contains("type"));
}
#[test]
fn test_cov95_type_alias_dict() {
let code = transpile(r#"from typing import Dict
StrMap = Dict[str, str]
def get(d: StrMap, key: str) -> str:
return d.get(key, "")"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_protocol_with_return() {
assert!(transpile_ok(r#"from typing import Protocol
class Comparable(Protocol):
def compare(self, other) -> int:
pass"#));
}
#[test]
fn test_cov95_protocol_with_args() {
assert!(transpile_ok(r#"from typing import Protocol
class Handler(Protocol):
def handle(self, data: bytes, offset: int) -> int:
pass"#));
}
#[test]
fn test_cov95_class_with_multiple_fields() {
let code = transpile(r#"class Person:
name: str
age: int
email: str
active: bool"#);
assert!(code.contains("struct") && code.contains("Person"));
}
#[test]
fn test_cov95_class_with_optional_field() {
let code = transpile(r#"from typing import Optional
class Config:
name: str
timeout: Optional[int]"#);
assert!(code.contains("struct") || code.contains("Option"));
}
#[test]
fn test_cov95_class_with_list_field() {
let code = transpile(r#"from typing import List
class Container:
items: List[int]"#);
assert!(code.contains("struct") || code.contains("Vec"));
}
#[test]
fn test_cov95_class_with_dict_field() {
let code = transpile(r#"from typing import Dict
class Cache:
data: Dict[str, int]"#);
assert!(code.contains("struct") || code.contains("HashMap"));
}
#[test]
fn test_cov95_dataclass_simple() {
let code = transpile(r#"from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int"#);
assert!(code.contains("struct") || code.contains("Point"));
}
#[test]
fn test_cov95_dataclass_with_default() {
let code = transpile(r#"from dataclasses import dataclass
@dataclass
class Config:
name: str
timeout: int = 30"#);
assert!(code.contains("struct") || code.contains("Config"));
}
#[test]
fn test_cov95_dataclass_frozen() {
let code = transpile(r#"from dataclasses import dataclass
@dataclass(frozen=True)
class Frozen:
value: int"#);
assert!(code.contains("struct"));
}
#[test]
fn test_cov95_init_with_default_args() {
let code = transpile(r#"class Builder:
def __init__(self, name: str, count: int = 0):
self.name = name
self.count = count"#);
assert!(code.contains("struct") || code.contains("impl"));
}
#[test]
fn test_cov95_init_with_validation() {
let code = transpile(r#"class Positive:
def __init__(self, value: int):
if value < 0:
value = 0
self.value = value"#);
assert!(code.contains("struct") || code.contains("impl"));
}
#[test]
fn test_cov95_method_mutates_field() {
let code = transpile(r#"class Counter:
count: int
def increment(self):
self.count += 1"#);
assert!(code.contains("&mut self") || code.contains("struct"));
}
#[test]
fn test_cov95_method_reads_only() {
let code = transpile(r#"class Reader:
value: int
def get_value(self) -> int:
return self.value"#);
assert!(code.contains("&self") || code.contains("struct"));
}
#[test]
fn test_cov95_method_infers_return_field() {
let code = transpile(r#"class Container:
items: list
def get_items(self):
return self.items"#);
assert!(code.contains("impl") || code.contains("fn"));
}
#[test]
fn test_cov95_method_infers_return_computed() {
let code = transpile(r#"class Calculator:
a: int
b: int
def sum(self):
return self.a + self.b"#);
assert!(code.contains("impl") || code.contains("fn"));
}
#[test]
fn test_cov95_method_with_generics() {
let code = transpile(r#"from typing import TypeVar, Generic
T = TypeVar('T')
class Box(Generic[T]):
value: T
def get(self) -> T:
return self.value"#);
assert!(code.contains("impl") || code.contains("struct"));
}
#[test]
fn test_cov95_method_static() {
let code = transpile(r#"class Factory:
@staticmethod
def create() -> int:
return 42"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_method_classmethod() {
let code = transpile(r#"class Counter:
count: int
@classmethod
def zero(cls):
return cls(count=0)"#);
assert!(code.contains("fn") || code.contains("struct"));
}
#[test]
fn test_cov95_function_with_varargs() {
let code = transpile(r#"def sum_all(*args) -> int:
total = 0
for x in args:
total += x
return total"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_function_with_kwargs() {
let code = transpile(r#"def configure(**kwargs):
for key, value in kwargs.items():
print(key, value)"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_function_async() {
let code = transpile(r#"async def fetch(url: str) -> str:
return url"#);
assert!(code.contains("async") || code.contains("fn"));
}
#[test]
fn test_cov95_function_generator() {
let code = transpile(r#"def count_up(n: int):
i = 0
while i < n:
yield i
i += 1"#);
assert!(code.contains("fn") || code.contains("Iterator"));
}
#[test]
fn test_cov95_nested_loops() {
let code = transpile(r#"def matrix_sum(matrix: list) -> int:
total = 0
for row in matrix:
for val in row:
total += val
return total"#);
assert!(code.contains("fn") || code.contains("for"));
}
#[test]
fn test_cov95_nested_conditions() {
let code = transpile(r#"def classify(x: int) -> str:
if x > 0:
if x > 100:
return "large"
return "small"
return "zero or negative""#);
assert!(code.contains("fn") || code.contains("if"));
}
#[test]
fn test_cov95_assign_tuple_unpack() {
let code = transpile(r#"def swap(a: int, b: int) -> tuple:
a, b = b, a
return (a, b)"#);
assert!(code.contains("fn") || code.contains("let"));
}
#[test]
fn test_cov95_assign_augmented_add() {
let code = transpile(r#"def increment(x: int) -> int:
x += 1
return x"#);
assert!(code.contains("fn") || code.contains("+="));
}
#[test]
fn test_cov95_assign_augmented_mul() {
let code = transpile(r#"def double(x: int) -> int:
x *= 2
return x"#);
assert!(code.contains("fn") || code.contains("*="));
}
#[test]
fn test_cov95_assign_augmented_div() {
let code = transpile(r#"def halve(x: float) -> float:
x /= 2.0
return x"#);
assert!(code.contains("fn") || code.contains("/="));
}
#[test]
fn test_cov95_index_assign_list() {
let code = transpile(r#"def set_first(items: list, val: int):
items[0] = val"#);
assert!(code.contains("fn") || code.contains("[0]"));
}
#[test]
fn test_cov95_index_assign_dict() {
let code = transpile(r#"def set_key(d: dict, key: str, val: int):
d[key] = val"#);
assert!(code.contains("fn") || code.contains("insert"));
}
#[test]
fn test_cov95_attr_assign_self() {
let code = transpile(r#"class Setter:
value: int
def set_value(self, v: int):
self.value = v"#);
assert!(code.contains("struct") || code.contains("impl"));
}
#[test]
fn test_cov95_type_mapping_primitive() {
let code = transpile(r#"def process(a: int, b: float, c: str, d: bool):
pass"#);
assert!(code.contains("i64") || code.contains("f64") || code.contains("String") || code.contains("bool"));
}
#[test]
fn test_cov95_type_mapping_optional() {
let code = transpile(r#"from typing import Optional
def process(x: Optional[int]) -> int:
return x if x else 0"#);
assert!(code.contains("Option") || code.contains("fn"));
}
#[test]
fn test_cov95_type_mapping_list() {
let code = transpile(r#"from typing import List
def process(items: List[str]) -> int:
return len(items)"#);
assert!(code.contains("Vec") || code.contains("fn"));
}
#[test]
fn test_cov95_type_mapping_dict() {
let code = transpile(r#"from typing import Dict
def process(d: Dict[str, int]) -> list:
return list(d.keys())"#);
assert!(code.contains("HashMap") || code.contains("fn"));
}
#[test]
fn test_cov95_type_mapping_tuple() {
let code = transpile(r#"from typing import Tuple
def process(t: Tuple[int, str]) -> int:
return t[0]"#);
assert!(code.contains("(") || code.contains("fn"));
}
#[test]
fn test_cov95_type_mapping_set() {
let code = transpile(r#"from typing import Set
def process(s: Set[int]) -> int:
return len(s)"#);
assert!(code.contains("HashSet") || code.contains("fn"));
}
#[test]
fn test_cov95_mutable_vars_reassign() {
let code = transpile(r#"def counter() -> int:
x = 0
x = x + 1
x = x + 1
return x"#);
assert!(code.contains("mut") || code.contains("fn"));
}
#[test]
fn test_cov95_mutable_vars_loop() {
let code = transpile(r#"def sum_range(n: int) -> int:
total = 0
for i in range(n):
total += i
return total"#);
assert!(code.contains("mut") || code.contains("fn"));
}
#[test]
fn test_cov95_generic_class() {
let code = transpile(r#"from typing import TypeVar, Generic
T = TypeVar('T')
class Stack(Generic[T]):
items: list
def push(self, item: T):
self.items.append(item)
def pop(self) -> T:
return self.items.pop()"#);
assert!(code.contains("struct") || code.contains("impl"));
}
#[test]
fn test_cov95_generic_function() {
let code = transpile(r#"from typing import TypeVar
T = TypeVar('T')
def identity(x: T) -> T:
return x"#);
assert!(code.contains("fn") || code.contains("<T>"));
}
#[test]
fn test_cov95_try_except_simple() {
let code = transpile(r#"def safe_div(a: int, b: int) -> int:
try:
return a // b
except:
return 0"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_try_except_finally() {
let code = transpile(r#"def read_file(path: str) -> str:
try:
with open(path) as f:
return f.read()
except:
return ""
finally:
print("done")"#);
assert!(code.contains("fn"));
}
#[test]
fn test_cov95_dunder_len() {
let code = transpile(r#"class Sized:
items: list
def __len__(self) -> int:
return len(self.items)"#);
assert!(code.contains("struct") || code.contains("len"));
}
#[test]
fn test_cov95_dunder_iter() {
let code = transpile(r#"class Iterable:
items: list
def __iter__(self):
return iter(self.items)"#);
assert!(code.contains("struct") || code.contains("iter"));
}
#[test]
fn test_cov95_dunder_str() {
let code = transpile(r#"class Printable:
name: str
def __str__(self) -> str:
return self.name"#);
assert!(code.contains("struct") || code.contains("Display"));
}
#[test]
fn test_cov95_dunder_repr() {
let code = transpile(r#"class Debuggable:
value: int
def __repr__(self) -> str:
return f"Value({self.value})""#);
assert!(code.contains("struct") || code.contains("Debug"));
}
#[test]
fn test_cov95_class_inheritance() {
let code = transpile(r#"class Animal:
name: str
class Dog(Animal):
breed: str"#);
assert!(code.contains("struct"));
}
#[test]
fn test_cov95_abstract_base() {
let code = transpile(r#"from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self) -> float:
pass"#);
assert!(code.contains("trait") || code.contains("struct"));
}