Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Dec 24, 2024
1 parent 0b47310 commit 9c45389
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pythonx/clap/fuzzymatch-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ name = "fuzzymatch_rs"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.23"
pyo3 = { version = "0.23", features = ["auto-initialize"] }

printer = { path = "../../../crates/printer" }
matcher = { path = "../../../crates/matcher" }
Expand Down
56 changes: 28 additions & 28 deletions pythonx/clap/fuzzymatch-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,38 +161,38 @@ mod tests {
#[test]
fn py_and_rs_subscore_should_work() {
use matcher::substring::substr_indices as substr_scorer;
use pyo3::ffi::c_str;
use pyo3::prelude::*;
use pyo3::types::PyModule;
use std::fs;
use types::CaseMatching;

let cur_dir = std::env::current_dir().unwrap();
let py_path = cur_dir.parent().unwrap().join("scorer.py");
let py_source_code = fs::read_to_string(py_path).unwrap();

pyo3::prepare_freethreaded_python();

let gil = Python::acquire_gil();
let py = gil.python();
let py_scorer = PyModule::from_code(py, &py_source_code, "scorer.py", "scorer").unwrap();

let test_cases = vec![
("su ou", "substr_scorer_should_work"),
("su ork", "substr_scorer_should_work"),
];

for (needle, haystack) in test_cases.into_iter() {
let py_result: (i32, Vec<usize>) = py_scorer
.getattr("substr_scorer")
.unwrap()
.call1((needle, haystack))
.unwrap()
.extract()
.map(|(score, positions): (f64, Vec<usize>)| (score as i32, positions))
.unwrap();
let rs_result = substr_scorer(haystack, needle, CaseMatching::Smart).unwrap();
assert_eq!(py_result, rs_result);
}
Python::with_gil(|py| {
let py_scorer = PyModule::from_code(
py,
c_str!(include_str!("../../scorer.py")),
c_str!("scorer.py"),
c_str!("scorer"),
)
.unwrap();

let test_cases = vec![
("su ou", "substr_scorer_should_work"),
("su ork", "substr_scorer_should_work"),
];

for (needle, haystack) in test_cases.into_iter() {
let py_result: (i32, Vec<usize>) = py_scorer
.getattr("substr_scorer")
.unwrap()
.call1((needle, haystack))
.unwrap()
.extract()
.map(|(score, positions): (f64, Vec<usize>)| (score as i32, positions))
.unwrap();
let rs_result = substr_scorer(haystack, needle, CaseMatching::Smart).unwrap();
assert_eq!(py_result, rs_result);
}
});
}

#[test]
Expand Down

0 comments on commit 9c45389

Please sign in to comment.