2021-08-01 06:39:30 +00:00
|
|
|
use flowy_ot::{
|
2021-08-02 10:35:25 +00:00
|
|
|
attributes::{Attributes, AttrsBuilder},
|
2021-08-01 06:39:30 +00:00
|
|
|
delta::Delta,
|
|
|
|
operation::{OpBuilder, Operation, Retain},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operation_insert_serialize_test() {
|
2021-08-02 10:35:25 +00:00
|
|
|
let attributes = AttrsBuilder::new().bold().italic().build();
|
2021-08-01 08:39:32 +00:00
|
|
|
let operation = OpBuilder::insert("123")
|
2021-08-01 06:39:30 +00:00
|
|
|
.attributes(Some(attributes))
|
|
|
|
.build();
|
|
|
|
let json = serde_json::to_string(&operation).unwrap();
|
|
|
|
eprintln!("{}", json);
|
|
|
|
|
|
|
|
let insert_op: Operation = serde_json::from_str(&json).unwrap();
|
|
|
|
assert_eq!(insert_op, operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operation_retain_serialize_test() {
|
|
|
|
let operation = Operation::Retain(12.into());
|
|
|
|
let json = serde_json::to_string(&operation).unwrap();
|
|
|
|
eprintln!("{}", json);
|
|
|
|
let insert_op: Operation = serde_json::from_str(&json).unwrap();
|
|
|
|
assert_eq!(insert_op, operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operation_delete_serialize_test() {
|
|
|
|
let operation = Operation::Delete(2);
|
|
|
|
let json = serde_json::to_string(&operation).unwrap();
|
|
|
|
let insert_op: Operation = serde_json::from_str(&json).unwrap();
|
|
|
|
assert_eq!(insert_op, operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_serialize_test() {
|
|
|
|
let mut delta = Delta::default();
|
|
|
|
|
2021-08-02 10:35:25 +00:00
|
|
|
let attributes = AttrsBuilder::new().bold().italic().build();
|
2021-08-01 08:39:32 +00:00
|
|
|
let retain = OpBuilder::insert("123")
|
2021-08-01 06:39:30 +00:00
|
|
|
.attributes(Some(attributes))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
delta.add(retain);
|
|
|
|
delta.add(Operation::Retain(5.into()));
|
|
|
|
delta.add(Operation::Delete(3));
|
|
|
|
|
|
|
|
let json = serde_json::to_string(&delta).unwrap();
|
|
|
|
eprintln!("{}", json);
|
|
|
|
|
|
|
|
let delta_from_json: Delta = serde_json::from_str(&json).unwrap();
|
|
|
|
assert_eq!(delta_from_json, delta);
|
|
|
|
}
|