127 lines
4.1 KiB
Rust
Raw Normal View History

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