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};
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 std::{cmp::Ordering, fmt, iter::FromIterator, str::FromStr};

View File

@ -131,7 +131,7 @@ impl From<RangeToInclusive<usize>> for Interval {
#[cfg(test)]
mod tests {
use crate::interval::Interval;
use crate::core::Interval;
#[test]
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 std::{
fmt,

View File

@ -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},

View File

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

View File

View File

@ -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);
}

View File

@ -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);

View File

@ -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]

View File

@ -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() {