Add test: builtin_mappings_are_lazily_evaluated

This commit is contained in:
cyqsimon 2023-11-06 12:57:15 +08:00
parent e30161ac3c
commit 586c804b1e
No known key found for this signature in database
GPG Key ID: 1D8CE2F297390D65
3 changed files with 48 additions and 0 deletions

19
Cargo.lock generated
View File

@ -155,6 +155,7 @@ dependencies = [
"predicates",
"regex",
"run_script",
"rusty-fork",
"semver",
"serde",
"serde_with",
@ -1039,6 +1040,12 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quick-xml"
version = "0.30.0"
@ -1171,6 +1178,18 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "rusty-fork"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb3dcc6e454c328bb824492db107ab7c0ae8fcffe4ad210136ef014458c1bc4f"
dependencies = [
"fnv",
"quick-error",
"tempfile",
"wait-timeout",
]
[[package]]
name = "ryu"
version = "1.0.11"

View File

@ -67,6 +67,7 @@ bytesize = { version = "1.3.0" }
encoding_rs = "0.8.33"
os_str_bytes = { version = "~6.6", optional = true }
run_script = { version = "^0.10.1", optional = true}
rusty-fork = "0.3.0"
[dependencies.git2]
version = "0.18"

View File

@ -128,6 +128,9 @@ impl<'a> SyntaxMapping<'a> {
#[cfg(test)]
mod tests {
use once_cell::sync::Lazy;
use rusty_fork::rusty_fork_test;
use super::*;
#[test]
fn builtin_mappings_work() {
@ -148,6 +151,31 @@ mod tests {
let _mappings = map.builtin_mappings().collect::<Vec<_>>();
}
// lazy initialisation test needs to be run on a separate instance because
// it will race with other tests
// see: https://github.com/rust-lang/rust/issues/47506
rusty_fork_test! {
#[test]
fn builtin_mappings_are_lazily_evaluated() {
let map = SyntaxMapping::new();
assert!(BUILTIN_MAPPINGS
.iter()
.all(|(matcher, _)| Lazy::get(matcher).is_none()));
// calling `builtin_mappings` should not trigger evaluation
let mappings_iter = map.builtin_mappings();
assert!(BUILTIN_MAPPINGS
.iter()
.all(|(matcher, _)| Lazy::get(matcher).is_none()));
let _mappings: Vec<_> = mappings_iter.collect();
assert!(BUILTIN_MAPPINGS
.iter()
.all(|(matcher, _)| Lazy::get(matcher).is_some()));
}
}
#[test]
fn builtin_mappings_matcher_only_compile_once() {
let map = SyntaxMapping::new();