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

148 lines
5.0 KiB
Rust
Raw Normal View History

2022-08-23 11:39:00 +00:00
use lib_ot::core::{DocumentTree, NodeAttributes, NodeSubTree, Position, 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);
tb.insert_nodes_at_path(&vec![0].into(), &[Box::new(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-08-19 04:04:43 +00:00
assert!(document.node_at_path(&vec![0].into()).is_some());
let node = document.node_at_path(&vec![0].into()).unwrap();
2022-08-18 12:15:34 +00:00
let node_data = document.arena.get(node).unwrap().get();
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-08-19 04:04:43 +00:00
assert!(document.node_at_path(&vec![0].into()).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);
tb.insert_nodes_at_path(&vec![0].into(), &[Box::new(NodeSubTree::new("text"))]);
tb.insert_nodes_at_path(&vec![1].into(), &[Box::new(NodeSubTree::new("text"))]);
tb.insert_nodes_at_path(&vec![2].into(), &[Box::new(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);
tb.insert_nodes_at_path(&vec![1].into(), &[Box::new(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);
tb.insert_nodes_at_path(
&vec![0].into(),
&[Box::new(NodeSubTree {
2022-08-23 11:39:00 +00:00
node_type: "text".into(),
attributes: NodeAttributes::new(),
delta: None,
children: vec![Box::new(NodeSubTree::new("image"))],
2022-08-23 11:39:00 +00:00
})],
);
tb.finalize()
};
document.apply(transaction).unwrap();
let node = document.node_at_path(&Position(vec![0, 0])).unwrap();
let data = document.arena.get(node).unwrap().get();
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);
tb.insert_nodes_at_path(&vec![0].into(), &[Box::new(NodeSubTree::new("text"))]);
tb.insert_nodes_at_path(&vec![1].into(), &[Box::new(NodeSubTree::new("text"))]);
tb.insert_nodes_at_path(&vec![2].into(), &[Box::new(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(&Position(vec![1])).unwrap();
let node_data = document.arena.get(node).unwrap().get();
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);
tb.insert_nodes_at_path(&vec![0].into(), &[Box::new(NodeSubTree::new("text"))]);
tb.insert_nodes_at_path(&vec![1].into(), &[Box::new(NodeSubTree::new("text"))]);
tb.insert_nodes_at_path(&vec![2].into(), &[Box::new(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(&Position(vec![1]));
tb.finalize()
};
2022-08-22 08:46:24 +00:00
document.apply(transaction).unwrap();
let len = document.root.children(&document.arena).fold(0, |count, _| count + 1);
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);
tb.insert_nodes_at_path(&vec![0].into(), &[Box::new(NodeSubTree::new("text"))]);
tb.insert_nodes_at_path(&vec![100].into(), &[Box::new(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);
}