#![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;
#[allow(unused_imports)]
use test_utils::compile_single;
#[test]
fn test_hashmap_remove_auto_borrows_key() {
let input = r#"
use std::collections::HashMap
struct ChunkMap {
chunks: HashMap<(i32, i32, i32), bool>,
}
impl ChunkMap {
pub fn new() -> ChunkMap {
ChunkMap { chunks: HashMap::new() }
}
pub fn remove_chunk(self, pos: (i32, i32, i32)) {
self.chunks.remove(pos)
}
}
"#;
let output = compile_single(input);
assert!(
output.contains("self.chunks.remove(&pos)"),
"HashMap.remove should auto-borrow key with &.\nGenerated:\n{}",
output
);
}
#[test]
fn test_hashmap_remove_owned_custom_key_auto_borrows() {
let input = r#"
use std::collections::HashMap
struct TimerId {
value: u32,
}
struct TimerManager {
timers: HashMap<TimerId, f32>,
}
impl TimerManager {
pub fn new() -> TimerManager {
TimerManager { timers: HashMap::new() }
}
pub fn clear_finished(self) {
let mut to_remove = Vec::new()
for (id, _val) in self.timers {
to_remove.push(id)
}
for id in to_remove {
self.timers.remove(id)
}
}
}
"#;
let output = compile_single(input);
assert!(
output.contains(".remove(&id)"),
"HashMap.remove should auto-borrow owned key from loop iteration.\nGenerated:\n{}",
output
);
}
#[test]
fn test_vec_remove_no_ref_on_index() {
let input = r#"
struct TextBuffer {
codepoints: Vec<char>,
}
impl TextBuffer {
pub fn new() -> TextBuffer {
TextBuffer { codepoints: Vec::new() }
}
pub fn delete_at(self, pos: usize) {
self.codepoints.remove(pos)
}
}
"#;
let output = compile_single(input);
assert!(
!output.contains("codepoints.remove(&pos)"),
"Vec.remove should NOT add & to index.\nGenerated:\n{}",
output
);
assert!(
output.contains("codepoints.remove(pos)"),
"Vec.remove should pass index directly.\nGenerated:\n{}",
output
);
}