AppFlowy/shared-lib/lib-ot/tests/node/script.rs

159 lines
5.2 KiB
Rust
Raw Normal View History

2022-09-14 13:04:12 +00:00
use lib_ot::core::{Node, Transaction};
use lib_ot::{
2022-09-12 03:30:02 +00:00
core::attributes::Attributes,
core::{NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
2022-09-12 02:44:33 +00:00
text_delta::TextDelta,
2022-09-11 04:59:01 +00:00
};
2022-09-13 12:23:56 +00:00
use std::collections::HashMap;
2022-09-09 07:05:41 +00:00
pub enum NodeScript {
2022-09-13 12:23:56 +00:00
InsertNode {
path: Path,
node_data: NodeData,
rev_id: usize,
},
UpdateAttributes {
path: Path,
attributes: Attributes,
},
UpdateBody {
path: Path,
changeset: NodeBodyChangeset,
},
DeleteNode {
path: Path,
2022-09-14 01:23:33 +00:00
rev_id: usize,
2022-09-13 12:23:56 +00:00
},
AssertNumberOfNodesAtPath {
path: Option<Path>,
len: usize,
},
AssertNodeData {
path: Path,
expected: Option<NodeData>,
},
AssertNode {
path: Path,
2022-09-14 01:23:33 +00:00
expected: Option<Node>,
2022-09-13 12:23:56 +00:00
},
AssertNodeDelta {
path: Path,
expected: TextDelta,
},
2022-09-09 07:05:41 +00:00
}
pub struct NodeTest {
2022-09-13 12:23:56 +00:00
rev_id: usize,
rev_operations: HashMap<usize, Transaction>,
node_tree: NodeTree,
2022-09-09 07:05:41 +00:00
}
impl NodeTest {
pub fn new() -> Self {
Self {
2022-09-13 12:23:56 +00:00
rev_id: 0,
rev_operations: HashMap::new(),
node_tree: NodeTree::new("root"),
2022-09-09 07:05:41 +00:00
}
}
pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
for script in scripts {
self.run_script(script);
}
}
pub fn run_script(&mut self, script: NodeScript) {
match script {
2022-09-13 12:23:56 +00:00
NodeScript::InsertNode {
path,
node_data: node,
rev_id,
} => {
let mut transaction = TransactionBuilder::new(&self.node_tree)
2022-09-09 07:05:41 +00:00
.insert_node_at_path(path, node)
.finalize();
2022-09-13 12:23:56 +00:00
self.transform_transaction_if_need(&mut transaction, rev_id);
self.apply_transaction(transaction);
2022-09-09 07:05:41 +00:00
}
2022-09-11 04:59:01 +00:00
NodeScript::UpdateAttributes { path, attributes } => {
2022-09-09 07:05:41 +00:00
let transaction = TransactionBuilder::new(&self.node_tree)
2022-09-10 00:42:53 +00:00
.update_attributes_at_path(&path, attributes)
2022-09-09 07:05:41 +00:00
.finalize();
2022-09-13 12:23:56 +00:00
self.apply_transaction(transaction);
2022-09-09 07:05:41 +00:00
}
2022-09-11 04:59:01 +00:00
NodeScript::UpdateBody { path, changeset } => {
//
let transaction = TransactionBuilder::new(&self.node_tree)
.update_body_at_path(&path, changeset)
.finalize();
2022-09-13 12:23:56 +00:00
self.apply_transaction(transaction);
2022-09-11 04:59:01 +00:00
}
2022-09-14 01:23:33 +00:00
NodeScript::DeleteNode { path, rev_id } => {
let mut transaction = TransactionBuilder::new(&self.node_tree)
2022-09-09 07:05:41 +00:00
.delete_node_at_path(&path)
.finalize();
2022-09-14 01:23:33 +00:00
self.transform_transaction_if_need(&mut transaction, rev_id);
2022-09-13 12:23:56 +00:00
self.apply_transaction(transaction);
2022-09-09 07:05:41 +00:00
}
NodeScript::AssertNode { path, expected } => {
2022-09-14 01:23:33 +00:00
let node_id = self.node_tree.node_id_at_path(path);
if expected.is_none() && node_id.is_none() {
return;
}
let node = self.node_tree.get_node(node_id.unwrap()).cloned();
2022-09-13 12:23:56 +00:00
assert_eq!(node, expected);
}
NodeScript::AssertNodeData { path, expected } => {
2022-09-11 04:59:01 +00:00
let node_id = self.node_tree.node_id_at_path(path);
2022-09-09 07:05:41 +00:00
match node_id {
None => assert!(node_id.is_none()),
Some(node_id) => {
2022-09-13 12:23:56 +00:00
let node = self.node_tree.get_node(node_id).cloned();
assert_eq!(node, expected.map(|e| e.into()));
2022-09-09 07:05:41 +00:00
}
}
}
NodeScript::AssertNumberOfNodesAtPath {
2022-09-09 07:05:41 +00:00
path,
len: expected_len,
} => match path {
None => {
let len = self.node_tree.number_of_children(None);
assert_eq!(len, expected_len)
}
Some(path) => {
2022-09-11 04:59:01 +00:00
let node_id = self.node_tree.node_id_at_path(path).unwrap();
2022-09-09 07:05:41 +00:00
let len = self.node_tree.number_of_children(Some(node_id));
assert_eq!(len, expected_len)
}
},
2022-09-11 04:59:01 +00:00
NodeScript::AssertNodeDelta { path, expected } => {
let node = self.node_tree.get_node_at_path(&path).unwrap();
if let NodeBody::Delta(delta) = node.body.clone() {
debug_assert_eq!(delta, expected);
} else {
panic!("Node body type not match, expect Delta");
}
}
2022-09-13 12:23:56 +00:00
}
}
fn apply_transaction(&mut self, transaction: Transaction) {
self.rev_id += 1;
self.rev_operations.insert(self.rev_id, transaction.clone());
2022-09-14 13:04:12 +00:00
self.node_tree.apply_transaction(transaction).unwrap();
2022-09-13 12:23:56 +00:00
}
fn transform_transaction_if_need(&mut self, transaction: &mut Transaction, rev_id: usize) {
if self.rev_id >= rev_id {
2022-09-14 04:03:52 +00:00
for rev_id in rev_id..=self.rev_id {
2022-09-13 12:23:56 +00:00
let old_transaction = self.rev_operations.get(&rev_id).unwrap();
2022-09-14 13:04:12 +00:00
*transaction = old_transaction.transform(transaction).unwrap();
2022-09-13 12:23:56 +00:00
}
2022-09-09 07:05:41 +00:00
}
}
}