#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "codegen_tests",
))]
#[path = "common/test_utils.rs"]
mod test_utils;
#[test]
fn test_vec_string_index_in_struct_needs_clone() {
let source = r#"
pub struct Info {
pub name: string,
}
pub fn make_info(names: Vec<string>, i: i32) -> Info {
return Info { name: names[i] }
}
fn main() {
let names = vec!["a".to_string(), "b".to_string()]
let info = make_info(names, 0)
println(info.name)
}
"#;
let rust = test_utils::compile_single(source);
assert!(
rust.contains("names[i as usize].clone()") || rust.contains("names[(i as usize)].clone()"),
"Vec<String> index in struct should use .clone()\nGenerated:\n{}",
rust
);
}
#[test]
fn test_string_to_str_auto_borrow_extern_fn() {
let source = r#"
extern fn hash(data: String) -> String
pub fn compute_hash(data: string) -> string {
return hash(data)
}
fn main() {
let h = compute_hash("test".to_string())
println(h)
}
"#;
let rust = test_utils::compile_single(source);
assert!(
rust.contains("hash(") && (rust.contains("string_to_ffi") || rust.contains("hash(&data)")),
"Extern fn with String arg should use FFI conversion or auto-borrow\nGenerated:\n{}",
rust
);
}
#[test]
fn test_string_concat_with_vec_element() {
let source = r#"
pub fn join(parts: Vec<string>) -> string {
let mut result = "".to_string()
let mut j = 0
while j < parts.len() {
if j > 0 {
result = result + "|"
}
result = result + parts[j]
j = j + 1
}
return result
}
fn main() {
let p = vec!["a".to_string(), "b".to_string()]
println(join(p))
}
"#;
let rust = test_utils::compile_single(source);
let has_valid_concat = rust.contains("result + &parts")
|| rust.contains("result += &parts")
|| rust.contains("+ &parts[");
assert!(
has_valid_concat,
"String concat with Vec element should use & for &str\nGenerated:\n{}",
rust
);
}