Skip to content

Commit

Permalink
test(dict): explicitly load user dictionary in CDB format
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Mar 3, 2024
1 parent 707a406 commit 017a731
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Test
run: |
cargo test
cargo test --features capi
ctest --test-dir out/build/${{ matrix.preset }} --verbose
coverage:
Expand Down Expand Up @@ -95,7 +95,7 @@ jobs:
CARGO_INCREMENTAL: 0
RUSTFLAGS: -Cinstrument-coverage -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off
run: |
cargo test
cargo test --features capi
ctest --test-dir out/build/${{ matrix.preset }} --verbose
./grcov . -s . -b . --keep-only 'src/*' --llvm -t lcov -o coverage.lcov
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ tempfile = "3"
members = ["tools", "xtask"]
resolver = "2"

[[test]]
name = "test-user-dictionary"
path = "tests/test-user-dictionary.rs"
required-features = ["capi"]

[profile.release]
lto = true
debug = true
Expand Down
Binary file added tests/data/chewing.cdb
Binary file not shown.
Binary file modified tests/data/tsi.dat
Binary file not shown.
Binary file modified tests/data/word.dat
Binary file not shown.
59 changes: 59 additions & 0 deletions tests/test-user-dictionary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::env;
use std::error::Error;
use std::ffi::c_int;
use std::ffi::CStr;
use std::ffi::CString;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::ptr::null_mut;

use chewing::capi::input::chewing_handle_Default;
use chewing::capi::output::chewing_buffer_String;
use chewing::capi::setup::chewing_delete;
use chewing::capi::setup::chewing_new2;
use tempfile::tempdir;
use tempfile::TempDir;

fn golden_data_path(filename: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/data")
.join(filename)
}

fn syspath() -> Result<CString, Box<dyn Error>> {
Ok(CString::new(format!(
"{}/tests/data",
env!("CARGO_MANIFEST_DIR")
))?)
}

fn tempdir_and_file(filename: &str) -> Result<(TempDir, CString), Box<dyn Error>> {
let dir = tempdir()?;
let path = CString::new(dir.path().join(filename).display().to_string())?;
Ok((dir, path))
}

#[test]
fn explicit_load_chewing_cdb() -> Result<(), Box<dyn Error>> {
let syspath = syspath()?;
let (tmpdir, userpath) = tempdir_and_file("chewing.cdb")?;
let chewing_cdb = golden_data_path("chewing.cdb");
fs::copy(chewing_cdb, tmpdir.path().join("chewing.cdb"))?;

let ctx = chewing_new2(syspath.as_ptr(), userpath.as_ptr(), None, null_mut());
assert!(!ctx.is_null());

chewing_handle_Default(ctx, b'h' as c_int);
chewing_handle_Default(ctx, b'k' as c_int);
chewing_handle_Default(ctx, b'4' as c_int);
chewing_handle_Default(ctx, b'g' as c_int);
chewing_handle_Default(ctx, b'4' as c_int);

let preedit = chewing_buffer_String(ctx);
let preedit = unsafe { CStr::from_ptr(preedit) };
assert_eq!(preedit, CString::new("策試")?.as_c_str());

chewing_delete(ctx);
Ok(())
}

0 comments on commit 017a731

Please sign in to comment.