add tests and fix warnings

This commit is contained in:
appflowy 2021-08-04 07:55:54 +08:00
parent 4ebeac13f5
commit 958918e0a0
13 changed files with 51 additions and 61 deletions

View File

View File

@ -1,4 +1,4 @@
use crate::operation::Operation; use crate::core::Operation;
use std::{collections::HashMap, fmt}; use std::{collections::HashMap, fmt};
const PLAIN: &'static str = ""; const PLAIN: &'static str = "";

View File

@ -1,4 +1,7 @@
use crate::{attributes::*, errors::OTError, operation::*}; use crate::{
core::{attributes::*, operation::*},
errors::OTError,
};
use bytecount::num_chars; use bytecount::num_chars;
use std::{cmp::Ordering, fmt, iter::FromIterator, str::FromStr}; use std::{cmp::Ordering, fmt, iter::FromIterator, str::FromStr};

View File

@ -131,7 +131,7 @@ impl From<RangeToInclusive<usize>> for Interval {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::interval::Interval; use crate::core::Interval;
#[test] #[test]
fn contains() { fn contains() {

View File

@ -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::*;

View File

@ -1,4 +1,4 @@
use crate::attributes::Attributes; use crate::core::Attributes;
use bytecount::num_chars; use bytecount::num_chars;
use std::{ use std::{
fmt, fmt,

View File

@ -1,4 +1,4 @@
use crate::{attributes::Attributes, delta::Delta, operation::Operation}; use crate::core::{Attributes, Delta, Operation};
use serde::{ use serde::{
de, de,
de::{MapAccess, SeqAccess, Visitor}, de::{MapAccess, SeqAccess, Visitor},

View File

@ -1,6 +1,4 @@
pub mod attributes; pub mod client;
pub mod delta; pub mod core;
pub mod errors; pub mod errors;
pub mod interval; pub mod server;
pub mod operation;
mod operation_serde;

View File

View File

@ -1,10 +1,7 @@
pub mod helper; pub mod helper;
use crate::helper::{MergeTestOp::*, *}; use crate::helper::{MergeTestOp::*, *};
use flowy_ot::{ use flowy_ot::core::Interval;
interval::Interval,
operation::{OpBuilder, Operation, Retain},
};
#[test] #[test]
fn delta_insert_text() { fn delta_insert_text() {
@ -30,8 +27,27 @@ fn delta_insert_text_at_head() {
fn delta_insert_text_at_middle() { fn delta_insert_text_at_middle() {
let ops = vec![ let ops = vec![
Insert(0, "123", 0), Insert(0, "123", 0),
Insert(0, "456", 2), Insert(0, "456", 1),
AssertOpsJson(0, r#"[{"insert":"124563"}]"#), 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); MergeTest::new().run_script(ops);
} }

View File

@ -1,9 +1,4 @@
use flowy_ot::{ use flowy_ot::core::*;
attributes::{Attributes, AttributesData, AttrsBuilder},
delta::Delta,
interval::Interval,
operation::{OpBuilder, Operation},
};
use rand::{prelude::*, Rng as WrappedRng}; use rand::{prelude::*, Rng as WrappedRng};
use std::sync::Once; use std::sync::Once;
@ -113,28 +108,7 @@ impl MergeTest {
attributes = Attributes::Follow; attributes = Attributes::Follow;
} }
let insert = OpBuilder::insert(s).attributes(attributes).build(); let insert = OpBuilder::insert(s).attributes(attributes).build();
let new_delta = new_delta_with_op(old_delta, insert, Interval::new(index, index));
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();
self.deltas[delta_index] = new_delta; self.deltas[delta_index] = new_delta;
} }
@ -152,7 +126,7 @@ impl MergeTest {
let attrs = attributes_in_delta(old_delta, &interval); let attrs = attributes_in_delta(old_delta, &interval);
retain.extend_attributes(attrs); 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; self.deltas[delta_index] = new_delta;
} }
@ -162,14 +136,14 @@ impl MergeTest {
let attrs = attributes_in_delta(old_delta, &interval); let attrs = attributes_in_delta(old_delta, &interval);
delete.extend_attributes(attrs); 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; 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 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 // prefix
if prefix.is_empty() == false && prefix != interval { 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) 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); pub struct Rng(StdRng);

View File

@ -2,7 +2,7 @@ pub mod helper;
use crate::helper::MergeTestOp::*; use crate::helper::MergeTestOp::*;
use bytecount::num_chars; use bytecount::num_chars;
use flowy_ot::{attributes::*, delta::Delta, interval::Interval, operation::OpBuilder}; use flowy_ot::core::*;
use helper::*; use helper::*;
#[test] #[test]

View File

@ -1,8 +1,4 @@
use flowy_ot::{ use flowy_ot::core::*;
attributes::AttrsBuilder,
delta::Delta,
operation::{OpBuilder, Operation, Retain},
};
#[test] #[test]
fn operation_insert_serialize_test() { fn operation_insert_serialize_test() {