fix tests bugs

This commit is contained in:
appflowy 2021-08-06 23:06:27 +08:00
parent c667ee0f36
commit 481f158a4a
10 changed files with 69 additions and 47 deletions

View File

@ -16,14 +16,12 @@ use crate::{
pub struct Document {
data: Delta,
history: History,
rev_id_counter: u64,
rev_id_counter: usize,
}
impl Document {
pub fn new() -> Self {
let mut delta = Delta::new();
delta.insert("\n", Attributes::Empty);
let delta = Delta::new();
Document {
data: delta,
history: History::new(),
@ -74,7 +72,7 @@ impl Document {
Some(undo_delta) => {
let composed_delta = self.data.compose(&undo_delta)?;
let redo_delta = undo_delta.invert(&self.data);
let result = UndoResult::success(composed_delta.target_len as u64);
let result = UndoResult::success(composed_delta.target_len as usize);
self.data = composed_delta;
self.history.add_redo(redo_delta);
@ -88,7 +86,7 @@ impl Document {
None => Err(ErrorBuilder::new(RedoFail).build()),
Some(redo_delta) => {
let new_delta = self.data.compose(&redo_delta)?;
let result = UndoResult::success(new_delta.target_len as u64);
let result = UndoResult::success(new_delta.target_len as usize);
let undo_delta = redo_delta.invert(&self.data);
self.data = new_delta;
self.history.add_undo(undo_delta);
@ -98,7 +96,7 @@ impl Document {
}
pub fn delete(&mut self, interval: Interval) -> Result<(), OTError> {
let delete = OpBuilder::delete(interval.size() as u64).build();
let delete = OpBuilder::delete(interval.size()).build();
self.update_with_op(delete, interval)
}
@ -120,7 +118,7 @@ impl Document {
intervals.into_iter().for_each(|i| {
let attributes = self.data.get_attributes(i);
log::debug!("prefix attribute: {:?}, interval: {:?}", attributes, i);
new_delta.retain(i.size() as u64, attributes);
new_delta.retain(i.size() as usize, attributes);
});
}
@ -133,7 +131,7 @@ impl Document {
intervals.into_iter().for_each(|i| {
let attributes = self.data.get_attributes(i);
log::debug!("suffix attribute: {:?}, interval: {:?}", attributes, i);
new_delta.retain(i.size() as u64, attributes);
new_delta.retain(i.size() as usize, attributes);
});
}
@ -170,7 +168,7 @@ impl Document {
};
log::debug!("new attributes: {:?}", new_attributes);
let retain = OpBuilder::retain(interval.size() as u64)
let retain = OpBuilder::retain(interval.size())
.attributes(new_attributes)
.build();

View File

@ -3,7 +3,7 @@ use crate::core::Delta;
const MAX_UNDOS: usize = 20;
#[derive(Debug, Clone)]
pub struct RevId(pub u64);
pub struct RevId(pub usize);
#[derive(Debug, Clone)]
pub struct Revision {
@ -18,7 +18,7 @@ impl Revision {
#[derive(Debug, Clone)]
pub struct UndoResult {
success: bool,
len: u64,
len: usize,
}
impl UndoResult {
@ -29,7 +29,7 @@ impl UndoResult {
}
}
pub fn success(len: u64) -> Self { UndoResult { success: true, len } }
pub fn success(len: usize) -> Self { UndoResult { success: true, len } }
}
#[derive(Debug, Clone)]

View File

@ -81,7 +81,7 @@ impl Delta {
}
}
pub fn delete(&mut self, n: u64) {
pub fn delete(&mut self, n: usize) {
if n == 0 {
return;
}
@ -122,7 +122,7 @@ impl Delta {
}
}
pub fn retain(&mut self, n: u64, attrs: Attributes) {
pub fn retain(&mut self, n: usize, attrs: Attributes) {
if n == 0 {
return;
}
@ -201,10 +201,10 @@ impl Delta {
}
},
(Some(Operation::Insert(insert)), Some(Operation::Delete(o_num))) => {
match (num_chars(insert.as_bytes()) as u64).cmp(o_num) {
match (num_chars(insert.as_bytes()) as usize).cmp(o_num) {
Ordering::Less => {
next_op2 = Some(
OpBuilder::delete(*o_num - num_chars(insert.as_bytes()) as u64)
OpBuilder::delete(*o_num - num_chars(insert.as_bytes()) as usize)
.attributes(insert.attributes.clone())
.build(),
);
@ -523,12 +523,12 @@ impl Delta {
Operation::Retain(_) => {
match op.has_attribute() {
true => inverted_from_other(&mut inverted, op, index, index + len),
false => inverted.retain(len as u64, op.get_attributes()),
false => inverted.retain(len as usize, op.get_attributes()),
}
index += len;
},
Operation::Insert(_) => {
inverted.delete(len as u64);
inverted.delete(len as usize);
},
}
}
@ -554,9 +554,9 @@ impl Delta {
let mut ops_iter = self.ops.iter();
let mut next_op = ops_iter.next();
if offset < interval.end && next_op.is_some() {
while offset < interval.end && next_op.is_some() {
let op = next_op.take().unwrap();
let len = op.length() as usize;
let len = offset + op.length() as usize;
// log::info!("{:?}", op);
while offset < len {
if offset < interval.start {
@ -564,7 +564,7 @@ impl Delta {
} else {
if interval.contains(offset) {
ops.push(op.shrink_to_interval(interval));
offset += max(op.length() as usize, interval.size());
offset += min(op.length() as usize, interval.size());
} else {
break;
}

View File

@ -13,9 +13,9 @@ impl OpBuilder {
}
}
pub fn retain(n: u64) -> OpBuilder { OpBuilder::new(Operation::Retain(n.into())) }
pub fn retain(n: usize) -> OpBuilder { OpBuilder::new(Operation::Retain(n.into())) }
pub fn delete(n: u64) -> OpBuilder { OpBuilder::new(Operation::Delete(n)) }
pub fn delete(n: usize) -> OpBuilder { OpBuilder::new(Operation::Delete(n)) }
pub fn insert(s: &str) -> OpBuilder { OpBuilder::new(Operation::Insert(s.into())) }

View File

@ -9,7 +9,7 @@ use std::{
#[derive(Debug, Clone, PartialEq)]
pub enum Operation {
Delete(u64),
Delete(usize),
Retain(Retain),
Insert(Insert),
}
@ -59,7 +59,7 @@ impl Operation {
}
}
pub fn length(&self) -> u64 {
pub fn length(&self) -> usize {
match self {
Operation::Delete(n) => *n,
Operation::Retain(r) => r.n,
@ -71,10 +71,13 @@ impl Operation {
pub fn shrink_to_interval(&self, interval: Interval) -> Operation {
match self {
Operation::Delete(_) => Operation::Delete(interval.size() as u64),
Operation::Delete(n) => {
//
OpBuilder::delete(min(*n, interval.size())).build()
},
Operation::Retain(retain) => {
//
OpBuilder::retain(interval.size() as u64)
OpBuilder::retain(min(retain.n, interval.size()))
.attributes(retain.attributes.clone())
.build()
},
@ -120,13 +123,13 @@ impl fmt::Display for Operation {
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Retain {
#[serde(rename(serialize = "retain", deserialize = "retain"))]
pub n: u64,
pub n: usize,
#[serde(skip_serializing_if = "is_empty")]
pub attributes: Attributes,
}
impl Retain {
pub fn merge_or_new_op(&mut self, n: u64, attributes: Attributes) -> Option<Operation> {
pub fn merge_or_new_op(&mut self, n: usize, attributes: Attributes) -> Option<Operation> {
log::debug!(
"merge_retain_or_new_op: {:?}, {:?}",
self.attributes,
@ -153,8 +156,8 @@ impl Retain {
}
}
impl std::convert::From<u64> for Retain {
fn from(n: u64) -> Self {
impl std::convert::From<usize> for Retain {
fn from(n: usize) -> Self {
Retain {
n,
attributes: Attributes::default(),
@ -163,7 +166,7 @@ impl std::convert::From<u64> for Retain {
}
impl Deref for Retain {
type Target = u64;
type Target = usize;
fn deref(&self) -> &Self::Target { &self.n }
}
@ -186,7 +189,7 @@ impl Insert {
pub fn chars(&self) -> Chars<'_> { self.s.chars() }
pub fn num_chars(&self) -> u64 { num_chars(self.s.as_bytes()) as _ }
pub fn num_chars(&self) -> usize { num_chars(self.s.as_bytes()) as _ }
pub fn merge_or_new_op(&mut self, s: &str, attributes: Attributes) -> Option<Operation> {
match &attributes {

View File

@ -59,7 +59,7 @@ impl<'de> Deserialize<'de> for Operation {
if operation.is_some() {
return Err(de::Error::duplicate_field("operation"));
}
let i: u64 = map.next_value()?;
let i: usize = map.next_value()?;
operation = Some(Operation::Retain(i.into()));
},
"insert" => {

View File

@ -190,10 +190,10 @@ impl Rng {
delta.insert(&self.gen_string(i), Attributes::Empty);
},
f if f < 0.4 => {
delta.delete(i as u64);
delta.delete(i);
},
_ => {
delta.retain(i as u64, Attributes::Empty);
delta.retain(i, Attributes::Empty);
},
}
}

View File

@ -52,13 +52,17 @@ fn delta_invert_attribute_delta_with_no_attribute_delta2() {
Italic(0, Interval::new(2, 4), true),
AssertOpsJson(
0,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34","attributes":{"bold":"true","italic":"true"}},{"insert":"56","attributes":{"bold":"true"}}]"#,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34","attributes":
{"bold":"true","italic":"true"}},{"insert":"56","attributes":{"bold":"true"
}}]"#,
),
Insert(1, "abc", 0),
Invert(0, 1),
AssertOpsJson(
0,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34","attributes":{"bold":"true","italic":"true"}},{"insert":"56","attributes":{"bold":"true"}}]"#,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34","attributes":
{"bold":"true","italic":"true"}},{"insert":"56","attributes":{"bold":"true"
}}]"#,
),
];
OpTester::new().run_script(ops);
@ -91,7 +95,9 @@ fn delta_invert_no_attribute_delta_with_attribute_delta2() {
Italic(1, Interval::new(1, 3), true),
AssertOpsJson(
1,
r#"[{"insert":"a","attributes":{"bold":"true"}},{"insert":"bc","attributes":{"bold":"true","italic":"true"}},{"insert":"d","attributes":{"bold":"true"}}]"#,
r#"[{"insert":"a","attributes":{"bold":"true"}},{"insert":"bc","attributes":
{"bold":"true","italic":"true"}},{"insert":"d","attributes":{"bold":"true"
}}]"#,
),
Invert(0, 1),
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
@ -109,7 +115,9 @@ fn delta_invert_attribute_delta_with_attribute_delta() {
Italic(0, Interval::new(2, 4), true),
AssertOpsJson(
0,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34","attributes":{"bold":"true","italic":"true"}},{"insert":"56","attributes":{"bold":"true"}}]"#,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34","attributes":
{"bold":"true","italic":"true"}},{"insert":"56","attributes":{"bold":"true"
}}]"#,
),
Insert(1, "abc", 0),
Bold(1, Interval::new(0, 3), true),
@ -117,12 +125,16 @@ fn delta_invert_attribute_delta_with_attribute_delta() {
Italic(1, Interval::new(1, 3), true),
AssertOpsJson(
1,
r#"[{"insert":"a","attributes":{"bold":"true"}},{"insert":"bc","attributes":{"bold":"true","italic":"true"}},{"insert":"d","attributes":{"bold":"true"}}]"#,
r#"[{"insert":"a","attributes":{"bold":"true"}},{"insert":"bc","attributes":
{"bold":"true","italic":"true"}},{"insert":"d","attributes":{"bold":"true"
}}]"#,
),
Invert(0, 1),
AssertOpsJson(
0,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34","attributes":{"bold":"true","italic":"true"}},{"insert":"56","attributes":{"bold":"true"}}]"#,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34","attributes":
{"bold":"true","italic":"true"}},{"insert":"56","attributes":{"bold":"true"
}}]"#,
),
];
OpTester::new().run_script(ops);
@ -138,7 +150,7 @@ fn delta_get_ops_in_interval_1() {
delta.add(insert_b.clone());
assert_eq!(
delta.ops_in_interval(Interval::new(0, 3)),
delta.ops_in_interval(Interval::new(0, 4)),
vec![delta.ops.last().unwrap().clone()]
);
}

View File

@ -53,7 +53,7 @@ fn apply() {
delta_a.insert(&s, Attributes::Empty);
let mut delta_b = Delta::default();
delta_b.retain(s.len() as u64, Attributes::Empty);
delta_b.retain(s.len(), Attributes::Empty);
delta_b.insert("appflowy", Attributes::Empty);
let after_a = delta_a.apply("").unwrap();
@ -69,7 +69,7 @@ fn base_len_test() {
delta_a.insert("c", Attributes::Empty);
let s = "hello world,".to_owned();
delta_a.delete(s.len() as u64);
delta_a.delete(s.len());
let after_a = delta_a.apply(&s).unwrap();
delta_a.insert("d", Attributes::Empty);

View File

@ -4,13 +4,20 @@ use crate::helper::{TestOp::*, *};
#[test]
fn delta_undo_insert() {
let ops = vec![Insert(0, "123", 0), Undo(0), AssertOpsJson(0, r#"[]"#)];
let ops = vec![
//
Insert(0, "\n", 0),
Insert(0, "123", 0),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
];
OpTester::new().run_script(ops);
}
#[test]
fn delta_undo_insert2() {
let ops = vec![
Insert(0, "\n", 0),
Insert(0, "123", 0),
Insert(0, "456", 0),
Undo(0),
@ -24,6 +31,7 @@ fn delta_undo_insert2() {
#[test]
fn delta_redo_insert() {
let ops = vec![
Insert(0, "\n", 0),
Insert(0, "123", 0),
AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
Undo(0),
@ -37,6 +45,7 @@ fn delta_redo_insert() {
#[test]
fn delta_redo_insert2() {
let ops = vec![
Insert(0, "\n", 0),
Insert(0, "123", 0),
Insert(0, "456", 3),
AssertStr(0, "123456\n"),