114 lines
3.5 KiB
Rust
Raw Normal View History

use flowy_sync::client_document::{ClientDocument, EmptyDocument};
2022-10-22 21:57:44 +08:00
use lib_ot::text_delta::DeltaTextOperation;
2021-12-07 19:59:08 +08:00
use lib_ot::{
core::*,
2022-10-22 21:57:44 +08:00
text_delta::{BuildInTextAttribute, DeltaTextOperations},
2021-12-07 19:59:08 +08:00
};
2021-08-01 14:39:30 +08:00
#[test]
fn operation_insert_serialize_test() {
2022-09-13 11:38:19 +08:00
let attributes = AttributeBuilder::new()
.insert("bold", true)
.insert("italic", true)
2021-09-13 15:51:13 +08:00
.build();
let operation = DeltaOperation::insert_with_attributes("123", attributes);
2021-08-01 14:39:30 +08:00
let json = serde_json::to_string(&operation).unwrap();
eprintln!("{}", json);
2022-10-22 21:57:44 +08:00
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
2021-08-01 14:39:30 +08:00
assert_eq!(insert_op, operation);
}
#[test]
fn operation_retain_serialize_test() {
let operation = DeltaOperation::Retain(12.into());
2021-08-01 14:39:30 +08:00
let json = serde_json::to_string(&operation).unwrap();
eprintln!("{}", json);
2022-10-22 21:57:44 +08:00
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
2021-08-01 14:39:30 +08:00
assert_eq!(insert_op, operation);
}
#[test]
fn operation_delete_serialize_test() {
2022-10-22 21:57:44 +08:00
let operation = DeltaTextOperation::Delete(2);
2021-08-01 14:39:30 +08:00
let json = serde_json::to_string(&operation).unwrap();
2022-10-22 21:57:44 +08:00
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
2021-08-01 14:39:30 +08:00
assert_eq!(insert_op, operation);
}
#[test]
2021-09-15 15:01:24 +08:00
fn attributes_serialize_test() {
2022-09-13 11:38:19 +08:00
let attributes = AttributeBuilder::new()
.insert_entry(BuildInTextAttribute::Bold(true))
.insert_entry(BuildInTextAttribute::Italic(true))
2021-09-15 15:01:24 +08:00
.build();
let retain = DeltaOperation::insert_with_attributes("123", attributes);
2021-09-15 15:01:24 +08:00
let json = serde_json::to_string(&retain).unwrap();
eprintln!("{}", json);
}
#[test]
fn delta_serialize_multi_attribute_test() {
let mut delta = DeltaOperations::default();
2021-08-01 14:39:30 +08:00
2022-09-13 11:38:19 +08:00
let attributes = AttributeBuilder::new()
.insert_entry(BuildInTextAttribute::Bold(true))
.insert_entry(BuildInTextAttribute::Italic(true))
2021-09-13 15:51:13 +08:00
.build();
let retain = DeltaOperation::insert_with_attributes("123", attributes);
2021-08-01 14:39:30 +08:00
delta.add(retain);
delta.add(DeltaOperation::Retain(5.into()));
delta.add(DeltaOperation::Delete(3));
2021-08-01 14:39:30 +08:00
let json = serde_json::to_string(&delta).unwrap();
eprintln!("{}", json);
let delta_from_json = DeltaOperations::from_json(&json).unwrap();
2021-08-01 14:39:30 +08:00
assert_eq!(delta_from_json, delta);
}
2021-09-12 22:19:59 +08:00
2021-09-15 15:01:24 +08:00
#[test]
fn delta_deserialize_test() {
let json = r#"[
{"retain":2,"attributes":{"italic":true}},
{"retain":2,"attributes":{"italic":123}},
2022-09-13 11:38:19 +08:00
{"retain":2,"attributes":{"italic":true,"bold":true}},
2021-09-15 15:01:24 +08:00
{"retain":2,"attributes":{"italic":true,"bold":true}}
]"#;
2022-10-22 21:57:44 +08:00
let delta = DeltaTextOperations::from_json(json).unwrap();
2021-09-15 15:01:24 +08:00
eprintln!("{}", delta);
}
#[test]
fn delta_deserialize_null_test() {
let json = r#"[
2021-09-15 16:35:40 +08:00
{"retain":7,"attributes":{"bold":null}}
2021-09-15 15:01:24 +08:00
]"#;
2022-10-22 21:57:44 +08:00
let delta1 = DeltaTextOperations::from_json(json).unwrap();
2022-09-13 11:38:19 +08:00
let mut attribute = BuildInTextAttribute::Bold(true);
2022-10-29 20:54:11 +08:00
attribute.clear();
2022-09-13 11:38:19 +08:00
2022-10-22 21:57:44 +08:00
let delta2 = DeltaOperationBuilder::new()
2022-09-12 10:44:33 +08:00
.retain_with_attributes(7, attribute.into())
.build();
2022-09-13 11:38:19 +08:00
assert_eq!(delta2.json_str(), r#"[{"retain":7,"attributes":{"bold":null}}]"#);
assert_eq!(delta1, delta2);
}
2021-09-12 22:19:59 +08:00
#[test]
fn document_insert_serde_test() {
let mut document = ClientDocument::new::<EmptyDocument>();
2021-09-22 14:42:14 +08:00
document.insert(0, "\n").unwrap();
document.insert(0, "123").unwrap();
let json = document.get_operations_json();
2021-09-12 22:19:59 +08:00
assert_eq!(r#"[{"insert":"123\n"}]"#, json);
2022-01-12 17:08:50 +08:00
assert_eq!(
r#"[{"insert":"123\n"}]"#,
ClientDocument::from_json(&json).unwrap().get_operations_json()
2022-01-12 17:08:50 +08:00
);
2021-09-12 22:19:59 +08:00
}