From 958918e0a0b13a6d2a75fe9e18dd338d585a27ee Mon Sep 17 00:00:00 2001 From: appflowy Date: Wed, 4 Aug 2021 07:55:54 +0800 Subject: [PATCH] add tests and fix warnings --- rust-lib/flowy-ot/src/client/mod.rs | 0 .../flowy-ot/src/{ => core}/attributes.rs | 2 +- rust-lib/flowy-ot/src/{ => core}/delta.rs | 5 ++- rust-lib/flowy-ot/src/{ => core}/interval.rs | 2 +- rust-lib/flowy-ot/src/core/mod.rs | 10 +++++ rust-lib/flowy-ot/src/{ => core}/operation.rs | 2 +- .../src/{ => core}/operation_serde.rs | 2 +- rust-lib/flowy-ot/src/lib.rs | 8 ++-- rust-lib/flowy-ot/src/server/mod.rs | 0 rust-lib/flowy-ot/tests/attribute_test.rs | 28 +++++++++--- rust-lib/flowy-ot/tests/helper/mod.rs | 45 +++---------------- rust-lib/flowy-ot/tests/op_test.rs | 2 +- rust-lib/flowy-ot/tests/serde_test.rs | 6 +-- 13 files changed, 51 insertions(+), 61 deletions(-) create mode 100644 rust-lib/flowy-ot/src/client/mod.rs rename rust-lib/flowy-ot/src/{ => core}/attributes.rs (99%) rename rust-lib/flowy-ot/src/{ => core}/delta.rs (99%) rename rust-lib/flowy-ot/src/{ => core}/interval.rs (99%) create mode 100644 rust-lib/flowy-ot/src/core/mod.rs rename rust-lib/flowy-ot/src/{ => core}/operation.rs (99%) rename rust-lib/flowy-ot/src/{ => core}/operation_serde.rs (98%) create mode 100644 rust-lib/flowy-ot/src/server/mod.rs diff --git a/rust-lib/flowy-ot/src/client/mod.rs b/rust-lib/flowy-ot/src/client/mod.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/rust-lib/flowy-ot/src/attributes.rs b/rust-lib/flowy-ot/src/core/attributes.rs similarity index 99% rename from rust-lib/flowy-ot/src/attributes.rs rename to rust-lib/flowy-ot/src/core/attributes.rs index 464b3f272c..3e3f23f9ac 100644 --- a/rust-lib/flowy-ot/src/attributes.rs +++ b/rust-lib/flowy-ot/src/core/attributes.rs @@ -1,4 +1,4 @@ -use crate::operation::Operation; +use crate::core::Operation; use std::{collections::HashMap, fmt}; const PLAIN: &'static str = ""; diff --git a/rust-lib/flowy-ot/src/delta.rs b/rust-lib/flowy-ot/src/core/delta.rs similarity index 99% rename from rust-lib/flowy-ot/src/delta.rs rename to rust-lib/flowy-ot/src/core/delta.rs index 4df9441e50..cb98bcccab 100644 --- a/rust-lib/flowy-ot/src/delta.rs +++ b/rust-lib/flowy-ot/src/core/delta.rs @@ -1,4 +1,7 @@ -use crate::{attributes::*, errors::OTError, operation::*}; +use crate::{ + core::{attributes::*, operation::*}, + errors::OTError, +}; use bytecount::num_chars; use std::{cmp::Ordering, fmt, iter::FromIterator, str::FromStr}; diff --git a/rust-lib/flowy-ot/src/interval.rs b/rust-lib/flowy-ot/src/core/interval.rs similarity index 99% rename from rust-lib/flowy-ot/src/interval.rs rename to rust-lib/flowy-ot/src/core/interval.rs index 10d3e9fe26..795816a5ce 100644 --- a/rust-lib/flowy-ot/src/interval.rs +++ b/rust-lib/flowy-ot/src/core/interval.rs @@ -131,7 +131,7 @@ impl From> for Interval { #[cfg(test)] mod tests { - use crate::interval::Interval; + use crate::core::Interval; #[test] fn contains() { diff --git a/rust-lib/flowy-ot/src/core/mod.rs b/rust-lib/flowy-ot/src/core/mod.rs new file mode 100644 index 0000000000..58b6c0bec0 --- /dev/null +++ b/rust-lib/flowy-ot/src/core/mod.rs @@ -0,0 +1,10 @@ +mod attributes; +mod delta; +mod interval; +mod operation; +mod operation_serde; + +pub use attributes::*; +pub use delta::*; +pub use interval::*; +pub use operation::*; diff --git a/rust-lib/flowy-ot/src/operation.rs b/rust-lib/flowy-ot/src/core/operation.rs similarity index 99% rename from rust-lib/flowy-ot/src/operation.rs rename to rust-lib/flowy-ot/src/core/operation.rs index 4d11c51ea9..d1c157af23 100644 --- a/rust-lib/flowy-ot/src/operation.rs +++ b/rust-lib/flowy-ot/src/core/operation.rs @@ -1,4 +1,4 @@ -use crate::attributes::Attributes; +use crate::core::Attributes; use bytecount::num_chars; use std::{ fmt, diff --git a/rust-lib/flowy-ot/src/operation_serde.rs b/rust-lib/flowy-ot/src/core/operation_serde.rs similarity index 98% rename from rust-lib/flowy-ot/src/operation_serde.rs rename to rust-lib/flowy-ot/src/core/operation_serde.rs index f54c37bba2..4d3ab82688 100644 --- a/rust-lib/flowy-ot/src/operation_serde.rs +++ b/rust-lib/flowy-ot/src/core/operation_serde.rs @@ -1,4 +1,4 @@ -use crate::{attributes::Attributes, delta::Delta, operation::Operation}; +use crate::core::{Attributes, Delta, Operation}; use serde::{ de, de::{MapAccess, SeqAccess, Visitor}, diff --git a/rust-lib/flowy-ot/src/lib.rs b/rust-lib/flowy-ot/src/lib.rs index e0bf6884a2..f71d8666cf 100644 --- a/rust-lib/flowy-ot/src/lib.rs +++ b/rust-lib/flowy-ot/src/lib.rs @@ -1,6 +1,4 @@ -pub mod attributes; -pub mod delta; +pub mod client; +pub mod core; pub mod errors; -pub mod interval; -pub mod operation; -mod operation_serde; +pub mod server; diff --git a/rust-lib/flowy-ot/src/server/mod.rs b/rust-lib/flowy-ot/src/server/mod.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/rust-lib/flowy-ot/tests/attribute_test.rs b/rust-lib/flowy-ot/tests/attribute_test.rs index 8d6d768482..2c386dee8e 100644 --- a/rust-lib/flowy-ot/tests/attribute_test.rs +++ b/rust-lib/flowy-ot/tests/attribute_test.rs @@ -1,10 +1,7 @@ pub mod helper; use crate::helper::{MergeTestOp::*, *}; -use flowy_ot::{ - interval::Interval, - operation::{OpBuilder, Operation, Retain}, -}; +use flowy_ot::core::Interval; #[test] fn delta_insert_text() { @@ -30,8 +27,27 @@ fn delta_insert_text_at_head() { fn delta_insert_text_at_middle() { let ops = vec![ Insert(0, "123", 0), - Insert(0, "456", 2), - AssertOpsJson(0, r#"[{"insert":"124563"}]"#), + Insert(0, "456", 1), + AssertOpsJson(0, r#"[{"insert":"145623"}]"#), + ]; + MergeTest::new().run_script(ops); +} + +#[test] +fn delta_insert_text_with_attr() { + let ops = vec![ + Insert(0, "145", 0), + Insert(0, "23", 1), + Bold(0, Interval::new(0, 2), true), + AssertOpsJson( + 0, + r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"345"}]"#, + ), + Insert(0, "abc", 1), + AssertOpsJson( + 0, + r#"[{"insert":"1abc2","attributes":{"bold":"true"}},{"insert":"345"}]"#, + ), ]; MergeTest::new().run_script(ops); } diff --git a/rust-lib/flowy-ot/tests/helper/mod.rs b/rust-lib/flowy-ot/tests/helper/mod.rs index f1319fd418..3cd5df3c20 100644 --- a/rust-lib/flowy-ot/tests/helper/mod.rs +++ b/rust-lib/flowy-ot/tests/helper/mod.rs @@ -1,9 +1,4 @@ -use flowy_ot::{ - attributes::{Attributes, AttributesData, AttrsBuilder}, - delta::Delta, - interval::Interval, - operation::{OpBuilder, Operation}, -}; +use flowy_ot::core::*; use rand::{prelude::*, Rng as WrappedRng}; use std::sync::Once; @@ -113,28 +108,7 @@ impl MergeTest { attributes = Attributes::Follow; } let insert = OpBuilder::insert(s).attributes(attributes).build(); - - let mut new_delta = Delta::default(); - let prefix = Interval::new(0, index); - let suffix = Interval::new(index, old_delta.target_len); - - split_interval_with_delta(old_delta, &prefix) - .into_iter() - .for_each(|interval| { - let attrs = attributes_in_delta(old_delta, &interval); - new_delta.retain(interval.size() as u64, attrs); - }); - - new_delta.add(insert); - - split_interval_with_delta(old_delta, &suffix) - .into_iter() - .for_each(|interval| { - let attrs = attributes_in_delta(old_delta, &interval); - new_delta.retain(interval.size() as u64, attrs); - }); - - new_delta = old_delta.compose(&new_delta).unwrap(); + let new_delta = new_delta_with_op(old_delta, insert, Interval::new(index, index)); self.deltas[delta_index] = new_delta; } @@ -152,7 +126,7 @@ impl MergeTest { let attrs = attributes_in_delta(old_delta, &interval); retain.extend_attributes(attrs); - let new_delta = new_delta_with_op(old_delta, retain, interval); + let new_delta = new_delta_with_op(old_delta, retain, *interval); self.deltas[delta_index] = new_delta; } @@ -162,14 +136,14 @@ impl MergeTest { let attrs = attributes_in_delta(old_delta, &interval); delete.extend_attributes(attrs); - let new_delta = new_delta_with_op(old_delta, delete, interval); + let new_delta = new_delta_with_op(old_delta, delete, *interval); self.deltas[delta_index] = new_delta; } } -fn new_delta_with_op(delta: &Delta, op: Operation, interval: &Interval) -> Delta { +fn new_delta_with_op(delta: &Delta, op: Operation, interval: Interval) -> Delta { let mut new_delta = Delta::default(); - let (prefix, interval, suffix) = target_length_split_with_interval(delta.target_len, *interval); + let (prefix, interval, suffix) = target_length_split_with_interval(delta.target_len, interval); // prefix if prefix.is_empty() == false && prefix != interval { @@ -268,13 +242,6 @@ pub fn attributes_in_delta(delta: &Delta, interval: &Interval) -> Attributes { Attributes::Custom(attributes_data) } } -fn attributes_in_operation(op: &Operation, interval: &Interval) -> Attributes { - match op { - Operation::Delete(_) => Attributes::Empty, - Operation::Retain(retain) => Attributes::Empty, - Operation::Insert(insert) => Attributes::Empty, - } -} pub struct Rng(StdRng); diff --git a/rust-lib/flowy-ot/tests/op_test.rs b/rust-lib/flowy-ot/tests/op_test.rs index a931b0e264..da43e74aae 100644 --- a/rust-lib/flowy-ot/tests/op_test.rs +++ b/rust-lib/flowy-ot/tests/op_test.rs @@ -2,7 +2,7 @@ pub mod helper; use crate::helper::MergeTestOp::*; use bytecount::num_chars; -use flowy_ot::{attributes::*, delta::Delta, interval::Interval, operation::OpBuilder}; +use flowy_ot::core::*; use helper::*; #[test] diff --git a/rust-lib/flowy-ot/tests/serde_test.rs b/rust-lib/flowy-ot/tests/serde_test.rs index dba59e7875..2d821f4ead 100644 --- a/rust-lib/flowy-ot/tests/serde_test.rs +++ b/rust-lib/flowy-ot/tests/serde_test.rs @@ -1,8 +1,4 @@ -use flowy_ot::{ - attributes::AttrsBuilder, - delta::Delta, - operation::{OpBuilder, Operation, Retain}, -}; +use flowy_ot::core::*; #[test] fn operation_insert_serialize_test() {