#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "analyzer_tests",
))]
#[path = "common/test_utils.rs"]
mod test_utils;
#[test]
fn test_no_double_clone_on_explicit_clone() {
let source = r#"
pub struct Item {
pub id: string,
pub name: string,
}
pub struct ItemStack {
pub item: Item,
pub quantity: i32,
}
pub struct Inventory {
pub items: Vec<Item>,
}
impl Inventory {
pub fn add_item(self, item: Item) {
self.items.push(item)
}
}
pub struct Trade {
pub offer: Vec<ItemStack>,
}
impl Trade {
pub fn execute(self, inv: Inventory) {
for stack in self.offer {
inv.add_item(stack.item)
}
}
}
"#;
let generated = test_utils::compile_single(source);
println!("Generated:\n{}", generated);
assert!(
!generated.contains(".clone().clone()"),
"Should not have double .clone().clone().\nGenerated:\n{}",
generated
);
assert!(
generated.contains("stack.item.clone()"),
"Should have single .clone() for non-Copy field.\nGenerated:\n{}",
generated
);
}
#[test]
fn test_no_double_clone_field_used_multiple_times() {
let source = r#"
pub struct Item {
pub id: string,
pub name: string,
}
pub struct Container {
pub items: Vec<Item>,
}
impl Container {
pub fn process(self) {
for item in self.items {
let id = item.id
let name = item.name
}
}
}
"#;
let generated = test_utils::compile_single(source);
println!("Generated:\n{}", generated);
assert!(
!generated.contains(".clone().clone()"),
"Should not have double .clone().clone().\nGenerated:\n{}",
generated
);
}
#[test]
fn test_no_double_clone_multi_use_in_loop() {
let source = r#"
pub struct Item {
pub id: string,
pub name: string,
}
pub struct ItemStack {
pub item: Item,
pub quantity: i32,
}
pub struct Inventory {
pub items: Vec<Item>,
}
impl Inventory {
pub fn add_item(self, item: Item) {
self.items.push(item)
}
pub fn remove_item_by_id(self, id: string, qty: i32) -> bool {
true
}
}
pub struct Trade {
pub offer: Vec<ItemStack>,
}
impl Trade {
pub fn execute(self, inv: Inventory) {
for stack in self.offer {
inv.remove_item_by_id(stack.item.id, stack.quantity)
inv.add_item(stack.item)
}
}
}
"#;
let generated = test_utils::compile_single(source);
println!("Generated:\n{}", generated);
assert!(
!generated.contains(".clone().clone()"),
"Should not have double .clone().clone().\nGenerated:\n{}",
generated
);
assert!(
generated.contains("stack.item.clone()"),
"Should have single .clone() for stack.item.\nGenerated:\n{}",
generated
);
}