Skip to content

Commit 76de54d

Browse files
authored
test: test case for item split (toeverything#450)
* test: add test case * chore: restore debug impl * chore: cleanup code
1 parent 5edec00 commit 76de54d

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

libs/jwst-codec/src/doc/codec/content.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,52 @@ mod tests {
365365
}
366366
}
367367

368+
#[test]
369+
fn test_content_split() {
370+
let contents = [
371+
Content::String("hello".to_string()),
372+
Content::JSON(vec![
373+
None,
374+
Some("test_1".to_string()),
375+
Some("test_2".to_string()),
376+
]),
377+
Content::Any(vec![Any::BigInt64(42), Any::String("Test Any".to_string())]),
378+
Content::Binary(vec![]),
379+
];
380+
381+
{
382+
let (left, right) = contents[0].split(1).unwrap();
383+
assert!(contents[0].splittable());
384+
assert_eq!(left, Content::String("h".to_string()));
385+
assert_eq!(right, Content::String("ello".to_string()));
386+
}
387+
388+
{
389+
let (left, right) = contents[1].split(1).unwrap();
390+
assert!(contents[1].splittable());
391+
assert_eq!(left, Content::JSON(vec![None, Some("test_1".to_string())]));
392+
assert_eq!(right, Content::JSON(vec![Some("test_2".to_string())]));
393+
}
394+
395+
{
396+
let (left, right) = contents[2].split(1).unwrap();
397+
assert!(contents[2].splittable());
398+
assert_eq!(
399+
left,
400+
Content::Any(vec![Any::BigInt64(42), Any::String("Test Any".to_string())])
401+
);
402+
assert_eq!(right, Content::Any(vec![]));
403+
}
404+
405+
{
406+
assert!(!contents[3].splittable());
407+
assert_eq!(
408+
contents[3].split(2),
409+
Err(JwstCodecError::ContentSplitNotSupport(2))
410+
);
411+
}
412+
}
413+
368414
proptest! {
369415
#[test]
370416
fn test_random_content(contents in vec(any::<Content>(), 0..10)) {

libs/jwst-codec/src/doc/codec/utils/items.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,29 @@ impl ItemBuilder {
5050
self.item
5151
}
5252
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
58+
#[test]
59+
fn test_item_builder() {
60+
let item = ItemBuilder::new()
61+
.id(Id::new(0, 1))
62+
.left_id(Some(Id::new(2, 3)))
63+
.right_id(Some(Id::new(4, 5)))
64+
.parent(Some(Parent::String("test".into())))
65+
.content(Content::Any(vec![Any::String("Hello".into())]))
66+
.build();
67+
68+
assert_eq!(item.id, Id::new(0, 1));
69+
assert_eq!(item.left_id, Some(Id::new(2, 3)));
70+
assert_eq!(item.right_id, Some(Id::new(4, 5)));
71+
assert!(matches!(item.parent, Some(Parent::String(text)) if text == "test"));
72+
assert_eq!(item.parent_sub, None);
73+
assert_eq!(
74+
item.content,
75+
std::sync::Arc::new(Content::Any(vec![Any::String("Hello".into())]))
76+
);
77+
}
78+
}

libs/jwst-codec/src/doc/store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ impl DocStore {
545545
item.delete();
546546
} else {
547547
// adjust parent length
548-
if item.parent_sub.is_none() && item.flags.countable() {
548+
if item.parent_sub.is_none() && item.countable() {
549549
parent.content_len += item.len();
550550
parent.item_len += 1;
551551
}

0 commit comments

Comments
 (0)