AppFlowy/shared-lib/lib-ot/tests/main.rs

148 lines
4.6 KiB
Rust
Raw Normal View History

2022-09-08 11:21:06 +00:00
use lib_ot::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree, Path, TransactionBuilder};
2022-08-22 08:46:24 +00:00
use lib_ot::errors::OTErrorCode;
2022-08-19 06:43:11 +00:00
use std::collections::HashMap;
2021-12-15 15:01:50 +00:00
2022-08-16 08:25:52 +00:00
#[test]
fn main() {
// Create a new arena
let _document = DocumentTree::new();
}
2022-08-17 08:48:45 +00:00
#[test]
fn test_documents() {
2022-08-17 09:43:58 +00:00
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
2022-09-08 09:38:11 +00:00
tb.insert_node_at_path(0, NodeSubTree::new("text"));
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
2022-08-18 12:15:34 +00:00
2022-09-08 09:38:11 +00:00
assert!(document.node_at_path(0).is_some());
let node = document.node_at_path(0).unwrap();
2022-09-08 11:27:15 +00:00
let node_data = document.get_node_data(node).unwrap();
2022-08-18 12:15:34 +00:00
assert_eq!(node_data.node_type, "text");
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.update_attributes_at_path(
&vec![0].into(),
HashMap::from([("subtype".into(), Some("bullet-list".into()))]),
);
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
2022-08-19 04:04:43 +00:00
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.delete_node_at_path(&vec![0].into());
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
2022-09-08 09:38:11 +00:00
assert!(document.node_at_path(0).is_none());
2022-08-17 08:48:45 +00:00
}
2022-08-22 03:10:06 +00:00
#[test]
2022-08-22 06:44:59 +00:00
fn test_inserts_nodes() {
2022-08-22 03:10:06 +00:00
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
2022-09-08 09:38:11 +00:00
tb.insert_node_at_path(0, NodeSubTree::new("text"));
tb.insert_node_at_path(1, NodeSubTree::new("text"));
tb.insert_node_at_path(2, NodeSubTree::new("text"));
2022-08-22 03:10:06 +00:00
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
2022-08-22 03:10:06 +00:00
let transaction = {
let mut tb = TransactionBuilder::new(&document);
2022-09-08 09:38:11 +00:00
tb.insert_node_at_path(1, NodeSubTree::new("text"));
2022-08-22 03:10:06 +00:00
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
2022-08-22 03:10:06 +00:00
}
2022-08-22 06:44:59 +00:00
2022-08-23 11:39:00 +00:00
#[test]
fn test_inserts_subtrees() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
2022-09-08 09:38:11 +00:00
tb.insert_node_at_path(
0,
NodeSubTree {
2022-08-23 11:39:00 +00:00
node_type: "text".into(),
attributes: NodeAttributes::new(),
delta: None,
children: vec![NodeSubTree::new("image")],
2022-09-08 09:38:11 +00:00
},
2022-08-23 11:39:00 +00:00
);
tb.finalize()
};
document.apply(transaction).unwrap();
let node = document.node_at_path(&Path(vec![0, 0])).unwrap();
2022-09-08 11:27:15 +00:00
let data = document.get_node_data(node).unwrap();
2022-08-23 11:39:00 +00:00
assert_eq!(data.node_type, "image");
}
2022-08-22 06:44:59 +00:00
#[test]
fn test_update_nodes() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
2022-09-08 09:38:11 +00:00
tb.insert_node_at_path(&vec![0], NodeSubTree::new("text"));
tb.insert_node_at_path(&vec![1], NodeSubTree::new("text"));
tb.insert_node_at_path(vec![2], NodeSubTree::new("text"));
2022-08-22 06:44:59 +00:00
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
2022-08-22 06:44:59 +00:00
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.update_attributes_at_path(&vec![1].into(), HashMap::from([("bolded".into(), Some("true".into()))]));
2022-08-22 06:44:59 +00:00
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
2022-08-22 06:44:59 +00:00
let node = document.node_at_path(&Path(vec![1])).unwrap();
2022-09-08 11:27:15 +00:00
let node_data = document.get_node_data(node).unwrap();
2022-08-22 08:46:24 +00:00
let is_bold = node_data.attributes.0.get("bolded").unwrap().clone();
2022-08-22 06:44:59 +00:00
assert_eq!(is_bold.unwrap(), "true");
}
#[test]
fn test_delete_nodes() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
2022-09-08 09:38:11 +00:00
tb.insert_node_at_path(0, NodeSubTree::new("text"));
tb.insert_node_at_path(1, NodeSubTree::new("text"));
tb.insert_node_at_path(2, NodeSubTree::new("text"));
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.delete_node_at_path(&Path(vec![1]));
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
2022-09-08 11:27:15 +00:00
let len = document.number_of_children();
assert_eq!(len, 2);
}
2022-08-22 08:46:24 +00:00
#[test]
fn test_errors() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
2022-09-08 09:38:11 +00:00
tb.insert_node_at_path(0, NodeSubTree::new("text"));
tb.insert_node_at_path(100, NodeSubTree::new("text"));
2022-08-22 08:46:24 +00:00
tb.finalize()
};
let result = document.apply(transaction);
assert_eq!(result.err().unwrap().code, OTErrorCode::PathNotFound);
}