feat(core): 扩展 API、完善日志与代码文档规范
- 公开颜色过滤与字符集限制扩展 API,修复宏路径 - 库内打印替换为 tracing 日志,清理遗留废弃代码 - 补充核心逻辑单元测试与 crate 元数据 - 开启 missing_docs 并统一 rustfmt/clippy 格式
This commit is contained in:
72
ddddocr-core/tests/api_surface.rs
Normal file
72
ddddocr-core/tests/api_surface.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
//! 外部视角 API 测试:验证颜色过滤与字符集限制扩展点对外可用。
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use ddddocr_core::color_any_of;
|
||||
use ddddocr_core::{
|
||||
CharRestrict, ColorFilter, ColorPreset, HsvRange, IdRestrict, OcrBuilder, TokenFilter, any_of,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn color_filter_setter_accepts_owned_preset() {
|
||||
let builder = OcrBuilder::new().color_filter(ColorPreset::Red);
|
||||
let _ = builder;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn color_any_of_macro_expands_and_collects() {
|
||||
let ranges = color_any_of!(ColorPreset::Red, ColorPreset::Blue)
|
||||
.collect_to_vec()
|
||||
.expect("收集颜色区间失败")
|
||||
.expect("应存在有效颜色区间");
|
||||
// Red 两段 + Blue 一段
|
||||
assert_eq!(ranges.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn charset_restrict_setter_accepts_owned_restrict() {
|
||||
let builder = OcrBuilder::new().charset_restrict(CharRestrict::Digit);
|
||||
let _ = builder;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_of_macro_expands_and_filters() {
|
||||
let tokens: Vec<Cow<'static, str>> = vec![
|
||||
Cow::Borrowed(""),
|
||||
Cow::Borrowed("1"),
|
||||
Cow::Borrowed("a"),
|
||||
Cow::Borrowed("A"),
|
||||
];
|
||||
|
||||
let indices = any_of!(CharRestrict::Digit, CharRestrict::Lowercase)
|
||||
.apply_to_charset(&tokens)
|
||||
.expect("字符集应存在交集");
|
||||
// 0 号 blank 放行 + 数字 '1' + 小写 'a'
|
||||
assert_eq!(indices, vec![0, 1, 2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_restrict_filters_by_index() {
|
||||
let tokens: Vec<Cow<'static, str>> = vec![
|
||||
Cow::Borrowed(""),
|
||||
Cow::Borrowed("1"),
|
||||
Cow::Borrowed("2"),
|
||||
Cow::Borrowed("3"),
|
||||
];
|
||||
|
||||
let indices = IdRestrict::TopN(2)
|
||||
.apply_to_charset(&tokens)
|
||||
.expect("字符集应存在交集");
|
||||
assert_eq!(indices, vec![0, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hsv_range_validate() {
|
||||
let range = HsvRange::new((0, 50, 50), (10, 255, 255));
|
||||
assert!(range.validate().is_ok());
|
||||
assert!(
|
||||
HsvRange::new((200, 0, 0), (255, 255, 255))
|
||||
.validate()
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user