use super::*;
use crate::agent::dsel::parse;
use crate::model::control::{Bookmark, Field, FieldType};
use crate::model::document::Section;
use crate::model::paragraph::CharShapeRef;
use crate::model::table::{Cell, Table};
fn para(text: &str) -> Paragraph {
let mut p = Paragraph {
text: text.to_string(),
..Default::default()
};
let mut utf16 = 0u32;
for ch in text.chars() {
p.char_offsets.push(utf16);
utf16 += ch.len_utf16() as u32;
}
p
}
fn para_styled(text: &str, style_id: u8) -> Paragraph {
let mut p = para(text);
p.style_id = style_id;
p
}
fn cell(row: u16, col: u16, text: &str) -> Cell {
Cell {
row,
col,
row_span: 1,
col_span: 1,
paragraphs: vec![para(text)],
..Default::default()
}
}
fn table_para(rows: u16, cols: u16, texts: &[&str]) -> Paragraph {
let mut t = Table {
row_count: rows,
col_count: cols,
..Default::default()
};
for (i, text) in texts.iter().enumerate() {
let r = (i as u16) / cols;
let c = (i as u16) % cols;
t.cells.push(cell(r, c, text));
}
let mut p = para("");
p.controls.push(Control::Table(Box::new(t)));
p
}
fn doc_with(paragraphs: Vec<Paragraph>) -> Document {
Document {
sections: vec![Section {
paragraphs,
..Default::default()
}],
..Default::default()
}
}
fn sel(src: &str, doc: &Document) -> Vec<String> {
let s = parse(src).unwrap_or_else(|e| panic!("{}", e.render(src)));
select(&s, doc)
.unwrap_or_else(|e| panic!("{e}"))
.into_iter()
.map(|n| n.id.to_string())
.collect()
}
fn texts(src: &str, doc: &Document) -> Vec<String> {
let s = parse(src).unwrap();
select(&s, doc)
.unwrap()
.into_iter()
.filter_map(|n| n.node.text())
.collect()
}
#[test]
fn selects_paragraphs_in_document_order() {
let doc = doc_with(vec![para("가"), para("나"), para("다")]);
assert_eq!(
sel("para", &doc),
vec![
"/section[0]/para[0]",
"/section[0]/para[1]",
"/section[0]/para[2]"
]
);
}
#[test]
fn first_step_reaches_any_depth() {
let doc = doc_with(vec![para("바깥"), table_para(1, 2, &["안1", "안2"])]);
let all = texts("para", &doc);
assert!(all.contains(&"바깥".to_string()));
assert!(all.contains(&"안1".to_string()));
}
#[test]
fn child_combinator_does_not_cross_levels() {
let doc = doc_with(vec![para("바깥"), table_para(1, 2, &["안1", "안2"])]);
let direct = texts("section > para", &doc);
assert!(direct.contains(&"바깥".to_string()));
assert!(!direct.contains(&"안1".to_string()));
}
#[test]
fn descendant_combinator_crosses_levels() {
let doc = doc_with(vec![table_para(1, 2, &["안1", "안2"])]);
let inner = texts("table para", &doc);
assert_eq!(inner, vec!["안1".to_string(), "안2".to_string()]);
}
#[test]
fn cell_addressing_by_row_and_col() {
let doc = doc_with(vec![table_para(2, 2, &["A", "B", "C", "D"])]);
assert_eq!(texts("cell[row=1][col=0]", &doc), vec!["C".to_string()]);
}
#[test]
fn sibling_combinators_respect_node_kind() {
let doc = doc_with(vec![para("가"), para("나"), para("다")]);
assert_eq!(texts("para[index=0] + para", &doc), vec!["나".to_string()]);
assert_eq!(
texts("para[index=0] ~ para", &doc),
vec!["나".to_string(), "다".to_string()]
);
}
#[test]
fn positional_pseudos_count_over_the_result_set() {
let doc = doc_with(vec![
table_para(1, 1, &["첫"]),
table_para(1, 1, &["둘"]),
table_para(1, 1, &["셋"]),
]);
assert_eq!(texts("table:last cell", &doc), vec!["셋".to_string()]);
assert_eq!(texts("table:nth(1) cell", &doc), vec!["둘".to_string()]);
assert_eq!(texts("table:first cell", &doc), vec!["첫".to_string()]);
}
#[test]
fn negative_nth_counts_from_the_end() {
let doc = doc_with(vec![para("가"), para("나"), para("다")]);
assert_eq!(texts("para:nth(-1)", &doc), vec!["다".to_string()]);
assert_eq!(texts("para:nth(-3)", &doc), vec!["가".to_string()]);
assert!(texts("para:nth(-9)", &doc).is_empty());
assert!(texts("para:nth(9)", &doc).is_empty());
}
#[test]
fn range_clamps_instead_of_failing() {
let doc = doc_with(vec![para("가"), para("나"), para("다")]);
assert_eq!(
texts("para:range(1..99)", &doc),
vec!["나".to_string(), "다".to_string()]
);
assert_eq!(texts("para:range(0..1)", &doc), vec!["가".to_string()]);
assert!(texts("para:range(5..9)", &doc).is_empty());
}
#[test]
fn value_predicates_run_before_positional_ones() {
let doc = doc_with(vec![
para_styled("가", 1),
para_styled("나", 2),
para_styled("다", 1),
]);
assert_eq!(texts("para[styleId=2]:last", &doc), vec!["나".to_string()]);
}
#[test]
fn contains_and_matches_use_visible_text() {
let doc = doc_with(vec![para("합\u{0003}계")]);
assert_eq!(texts(r#"para:contains("합계")"#, &doc).len(), 1);
assert_eq!(texts(r#"para:matches("합*")"#, &doc).len(), 1);
assert_eq!(texts(r#"para[text="합계"]"#, &doc).len(), 1);
}
#[test]
fn empty_only_matches_nodes_that_have_text() {
let mut p = para("");
p.controls.push(Control::Bookmark(Bookmark {
name: "표시".into(),
}));
let doc = doc_with(vec![p]);
assert_eq!(sel("para:empty", &doc).len(), 1);
assert!(sel("bookmark:empty", &doc).is_empty());
}
#[test]
fn not_excludes_the_inner_result_set() {
let doc = doc_with(vec![para("가"), para(""), para("다")]);
assert_eq!(
texts("para:not(:empty)", &doc),
vec!["가".to_string(), "다".to_string()]
);
}
#[test]
fn has_matches_ancestors_of_the_inner_result() {
let doc = doc_with(vec![
table_para(1, 1, &["합계"]),
table_para(1, 1, &["기타"]),
]);
let hits = sel(r#"table:has(cell:contains("합계"))"#, &doc);
assert_eq!(hits.len(), 1);
assert!(hits[0].ends_with("/control[0]"));
assert!(hits[0].starts_with("/section[0]/para[0]"));
}
#[test]
fn union_paths_are_merged_in_document_order_without_duplicates() {
let doc = doc_with(vec![para("가"), para("나")]);
assert_eq!(sel("para, para[index=0]", &doc).len(), 2);
}
#[test]
fn missing_attribute_never_matches_even_with_ne() {
let doc = doc_with(vec![table_para(1, 1, &["A"])]);
assert!(sel(r#"cell[name!="합계"]"#, &doc).is_empty());
assert!(sel("cell[name]", &doc).is_empty());
}
#[test]
fn bare_boolean_attribute_means_true() {
let mut t = Table {
row_count: 1,
col_count: 1,
..Default::default()
};
let mut c = cell(0, 0, "제목");
c.is_header = true;
t.cells.push(c);
t.cells.push(cell(0, 1, "값"));
let mut p = para("");
p.controls.push(Control::Table(Box::new(t)));
let doc = doc_with(vec![p]);
assert_eq!(texts("cell[header]", &doc), vec!["제목".to_string()]);
assert_eq!(texts("cell[header=false]", &doc), vec!["값".to_string()]);
}
#[test]
fn field_name_prefers_ctrl_data_and_ignores_empty() {
let mut p = para("");
p.controls.push(Control::Field(Field {
field_type: FieldType::ClickHere,
command: "안내문".into(),
ctrl_data_name: Some("수급자성명".into()),
..Default::default()
}));
p.controls.push(Control::Field(Field {
field_type: FieldType::ClickHere,
command: String::new(),
ctrl_data_name: None,
..Default::default()
}));
let doc = doc_with(vec![p]);
assert_eq!(sel(r#"field[name="수급자성명"]"#, &doc).len(), 1);
assert_eq!(sel("field[name]", &doc).len(), 1);
assert_eq!(sel(r#"field[type=clickHere]"#, &doc).len(), 2);
}
#[test]
fn table_axis_and_control_kind_select_the_same_thing() {
let doc = doc_with(vec![table_para(1, 1, &["A"])]);
assert_eq!(sel("table", &doc), sel("control[kind=table]", &doc));
}
#[test]
fn runs_are_selectable_by_char_shape() {
let mut p = para("가나다라");
p.char_shapes = vec![
CharShapeRef {
start_pos: 0,
char_shape_id: 7,
},
CharShapeRef {
start_pos: 2,
char_shape_id: 9,
},
];
let doc = doc_with(vec![p]);
assert_eq!(texts("run[charShapeId=9]", &doc), vec!["다라".to_string()]);
}
#[test]
fn node_limit_is_enforced_as_an_error_not_an_oom() {
let doc = doc_with((0..50).map(|i| para(&format!("문단{i}"))).collect());
let s = parse("para").unwrap();
let limits = EvalLimits {
max_nodes: 10,
..Default::default()
};
let err = select_with(&s, &doc, limits).unwrap_err();
assert!(err.message.contains("노드가 너무 많다"));
}
#[test]
fn result_limit_is_enforced() {
let doc = doc_with((0..20).map(|i| para(&format!("문단{i}"))).collect());
let s = parse("para").unwrap();
let limits = EvalLimits {
max_results: 5,
..Default::default()
};
let err = select_with(&s, &doc, limits).unwrap_err();
assert!(err.message.contains("결과가 너무 많다"));
assert_eq!(count(&s, &doc, limits).unwrap(), 20);
}
#[test]
fn empty_document_selects_nothing_without_error() {
let doc = Document::default();
assert!(sel("para", &doc).is_empty());
assert!(sel("table cell", &doc).is_empty());
}
#[test]
fn index_attribute_stays_sibling_relative() {
let doc = doc_with(vec![table_para(1, 1, &["첫"]), table_para(1, 1, &["둘"])]);
assert_eq!(sel("table[index=0]", &doc).len(), 2);
assert_eq!(sel("table:first", &doc).len(), 1);
}