#![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_range_for_loop_no_borrow() {
let source = r#"
pub fn iterate_range(min: i32, max: i32) {
for i in min..max {
println!("{}", i)
}
}
"#;
let rust_code = test_utils::compile_single_result(source).expect("compile");
assert!(
rust_code.contains("for i in min..max"),
"Range bounds should not be borrowed, got: {}",
rust_code
);
assert!(
!rust_code.contains("&min.."),
"Should not borrow range start, got: {}",
rust_code
);
assert!(
!rust_code.contains("..&max"),
"Should not borrow range end, got: {}",
rust_code
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_range_for_loop_with_arithmetic() {
let source = r#"
pub fn iterate_calculated(start: i32, end: i32, size: i32) {
for i in start.max(0)..end + 1.min(size) {
let index = i as usize
}
}
"#;
let rust_code = test_utils::compile_single_result(source).expect("compile");
assert!(
rust_code.contains("for i in "),
"Has for-loop, got: {}",
rust_code
);
assert!(
!rust_code.contains("&start"),
"Start not borrowed, got: {}",
rust_code
);
assert!(
!rust_code.contains("..&end"),
"End not borrowed in range, got: {}",
rust_code
);
assert!(
rust_code.contains("i as usize"),
"Should be able to cast i directly, got: {}",
rust_code
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_for_loop_variable_is_owned() {
let source = r#"
pub fn use_loop_var(max: i32) {
for i in 0..max {
let doubled = i * 2
let index = i as usize
}
}
"#;
let rust_code = test_utils::compile_single_result(source).expect("compile");
assert!(
rust_code.contains("i * 2"),
"Should multiply i directly, got: {}",
rust_code
);
assert!(
rust_code.contains("i as usize"),
"Should cast i directly, got: {}",
rust_code
);
assert!(
!rust_code.contains("*i"),
"Should not need to dereference loop variable, got: {}",
rust_code
);
}