Skip to content

Commit 7db9632

Browse files
committed
Add tests for validate function
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 4dbed7d commit 7db9632

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ use std::path::Path;
77

88
static VALIDATOR: Lazy<Set<Mmap>> = Lazy::new(|| {
99
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("purls.fst");
10+
load_fst(&path)
11+
});
12+
13+
fn load_fst(path: &Path) -> Set<Mmap> {
1014
let file = File::open(path).expect("Failed to open FST file");
1115
let mmap = unsafe { Mmap::map(&file).expect("Failed to mmap FST file") };
1216
Set::new(mmap).expect("Failed to load FST from mmap")
13-
});
17+
}
1418

1519
pub fn validate(packageurl: &str) -> bool {
1620
let trimmed_packageurl = packageurl.trim_end_matches("/");
1721
VALIDATOR.contains(trimmed_packageurl)
1822
}
23+
24+
#[cfg(test)]
25+
mod validate_tests;

src/validate_tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use super::*;
2+
use std::path::Path;
3+
4+
#[test]
5+
fn test_validate_with_custom_file() {
6+
let test_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/test_purls.fst");
7+
let validator = load_fst(&test_path);
8+
9+
assert!(validator.contains("pkg:nuget/FluentUtils.EnumExtensions"));
10+
assert!(!validator.contains("pkg:example/nonexistent"));
11+
}

tests/data/test_purls.fst

103 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)