#![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]
#[cfg_attr(tarpaulin, ignore)]
fn test_vec_push_string_literal() {
let code = r#"
pub fn create_list() -> Vec<string> {
let mut items = Vec::new()
items.push("first")
items.push("second")
items
}
"#;
let generated = test_utils::compile_single_result(code).expect("Compilation failed");
assert!(
generated.contains("push(\"first\".to_string())"),
"Should auto-convert first string literal. Generated:\n{}",
generated
);
assert!(
generated.contains("push(\"second\".to_string())"),
"Should auto-convert second string literal. Generated:\n{}",
generated
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_vec_push_string_variable_no_conversion() {
let code = r#"
pub fn add_item(item: string) -> Vec<string> {
let mut items = Vec::new()
items.push(item)
items
}
"#;
let generated = test_utils::compile_single_result(code).expect("Compilation failed");
assert!(
generated.contains("push(item)") && !generated.contains("push(item.to_string())"),
"Should NOT convert string variables. Generated:\n{}",
generated
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_custom_method_string_param() {
let code = r#"
pub struct Logger {
messages: Vec<string>
}
impl Logger {
pub fn log(self, message: string) {
self.messages.push(message)
}
}
pub fn test() {
let mut logger = Logger { messages: Vec::new() }
logger.log("Hello, World!")
}
"#;
let generated = test_utils::compile_single_result(code).expect("Compilation failed");
assert!(
generated.contains("log(\"Hello, World!\".to_string())"),
"Should auto-convert string literal in custom method. Generated:\n{}",
generated
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_str_param_no_conversion() {
let code = r#"
pub struct Text {
content: string
}
impl Text {
pub fn starts_with(self, prefix: string) -> bool {
return self.content.starts_with(prefix)
}
}
pub fn test(text: Text) -> bool {
return text.starts_with("Hello")
}
"#;
let generated = test_utils::compile_single_result(code).expect("Compilation failed");
assert!(
generated.contains("starts_with(\"Hello\")") && !generated.contains(".to_string()"),
"Should NOT convert for &str parameters. Generated:\n{}",
generated
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_multiple_string_params() {
let code = r#"
pub fn concatenate(a: string, b: string, c: string) -> string {
return a + &b + &c
}
pub fn test() -> string {
return concatenate("Hello", "World", "!")
}
"#;
let generated = test_utils::compile_single_result(code).expect("Compilation failed");
assert!(
generated.contains("concatenate(\"Hello\".to_string(), \"World\", \"!\")"),
"a needs .to_string(), b and c use literals directly (Phase 2). Generated:\n{}",
generated
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_mixed_str_and_string_params() {
let code = r#"
pub fn process(name: string, suffix: string) -> string {
return name + suffix
}
pub fn test() -> string {
return process("test", ".txt")
}
"#;
let generated = test_utils::compile_single_result(code).expect("Compilation failed");
assert!(
generated.contains("process(\"test\".to_string(), \".txt\")"),
"Should convert String param but not &str param. Generated:\n{}",
generated
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_chained_method_calls() {
let code = r#"
pub struct Builder {
value: string
}
impl Builder {
pub fn new() -> Builder {
return Builder { value: "".to_string() }
}
pub fn with_value(self, val: string) -> Builder {
self.value = val
return self
}
pub fn build(self) -> string {
return self.value
}
}
pub fn test() -> string {
return Builder::new().with_value("hello").build()
}
"#;
let generated = test_utils::compile_single_result(code).expect("Compilation failed");
assert!(
generated.contains("with_value(\"hello\".to_string())"),
"Should convert string literal in chained call. Generated:\n{}",
generated
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_hashmap_insert_string_keys() {
let code = r#"
use std::collections::HashMap
pub fn create_map() -> HashMap<string, int> {
let mut map = HashMap::new()
map.insert("key1", 1)
map.insert("key2", 2)
return map
}
"#;
let generated = test_utils::compile_single_result(code).expect("Compilation failed");
assert!(
generated.contains("insert(\"key1\".to_string(), 1)")
|| generated.contains("insert(\"key1\".to_string(), 1_i32)")
|| generated.contains("insert(\"key1\".to_string(), 1_i64)"),
"Should convert HashMap string key literals. Generated:\n{}",
generated
);
assert!(
generated.contains("insert(\"key2\".to_string(), 2)")
|| generated.contains("insert(\"key2\".to_string(), 2_i32)")
|| generated.contains("insert(\"key2\".to_string(), 2_i64)"),
"Should convert HashMap string key literals. Generated:\n{}",
generated
);
}