#![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_borrowed_string_param_generates_ampersand_string() {
let code = r#"
pub struct Inventory {
pub items: Vec<string>,
}
impl Inventory {
pub fn has_item(self, item_id: string) -> bool {
self.items.contains(item_id)
}
}
pub fn main() {
let inv = Inventory { items: Vec::new() }
let result = inv.has_item("sword")
}
"#;
let result = test_utils::compile_single_result(code);
match &result {
Ok(rust_code) => {
assert!(
rust_code.contains("item_id: &String") || rust_code.contains("item_id: &str"),
"Parameter should be either &String or &str (checking presence)"
);
}
Err(e) => {
panic!("Should compile successfully:\n{}", e);
}
}
assert!(
result.is_ok(),
"Borrowed string parameter must work with Vec<String>::contains:\n{:?}",
result.err()
);
}
#[test]
fn test_owned_string_param_stays_string() {
let code = r#"
pub struct Item {
pub name: string,
}
impl Item {
pub fn new(name: string) -> Item {
Item { name: name }
}
}
pub fn main() {
let item = Item::new("Sword")
}
"#;
let result = test_utils::compile_single_result(code);
assert!(
result.is_ok(),
"Owned string parameter should work:\n{:?}",
result.err()
);
let rust_code = result.unwrap();
assert!(
rust_code.contains("name: String"),
"Owned parameter should be String:\n{}",
rust_code
);
}