#![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_get_becomes_get_mut_when_value_mutated() {
let input = r#"
use std::collections::HashMap
struct Skeleton {
bones: Vec<f32>,
}
impl Skeleton {
pub fn new() -> Skeleton {
Skeleton { bones: Vec::new() }
}
pub fn update(self) {
self.bones.push(1.0)
}
}
struct Renderer {
skeletons: HashMap<u32, Skeleton>,
}
impl Renderer {
pub fn new() -> Renderer {
Renderer { skeletons: HashMap::new() }
}
pub fn animate(self, id: u32) {
let skel_opt = self.skeletons.get(id)
match skel_opt {
Some(skel) => {
skel.update()
},
None => {},
}
}
}
"#;
let output = compile_single(input);
assert!(
output.contains("get_mut("),
"HashMap.get() should become get_mut() when bound value is mutated.\nGenerated:\n{}",
output
);
}
#[test]
fn test_hashmap_get_stays_get_when_value_only_read() {
let input = r#"
use std::collections::HashMap
struct Skeleton {
bones: Vec<f32>,
}
impl Skeleton {
pub fn new() -> Skeleton {
Skeleton { bones: Vec::new() }
}
pub fn bone_count(self) -> usize {
self.bones.len()
}
}
struct Renderer {
skeletons: HashMap<u32, Skeleton>,
}
impl Renderer {
pub fn new() -> Renderer {
Renderer { skeletons: HashMap::new() }
}
pub fn count_bones(self, id: u32) -> usize {
let skel_opt = self.skeletons.get(id)
match skel_opt {
Some(skel) => skel.bone_count(),
None => 0,
}
}
}
"#;
let output = compile_single(input);
assert!(
output.contains(".get(") && !output.contains(".get_mut("),
"HashMap.get() should stay get() when bound value is only read.\nGenerated:\n{}",
output
);
}