Parcourir la source

fix: unit tests

Vincent Chan il y a 2 ans
Parent
commit
8bd748d7cd

+ 8 - 7
frontend/app_flowy/packages/flowy_editor/lib/document/text_delta.dart

@@ -22,7 +22,8 @@ class TextOperation {
 }
 }
 
 
 int _hashAttributes(Attributes attributes) {
 int _hashAttributes(Attributes attributes) {
-  return Object.hashAllUnordered(attributes.entries);
+  return Object.hashAllUnordered(
+      attributes.entries.map((e) => Object.hash(e.key, e.value)));
 }
 }
 
 
 class TextInsert extends TextOperation {
 class TextInsert extends TextOperation {
@@ -335,7 +336,7 @@ class Delta {
         if (otherOp is TextRetain && otherOp.length > 0) {
         if (otherOp is TextRetain && otherOp.length > 0) {
           TextOperation? newOp;
           TextOperation? newOp;
           if (thisOp is TextRetain) {
           if (thisOp is TextRetain) {
-            newOp = TextRetain(length: otherOp.length, attributes: attributes);
+            newOp = TextRetain(length: length, attributes: attributes);
           } else if (thisOp is TextInsert) {
           } else if (thisOp is TextInsert) {
             newOp = TextInsert(thisOp.content, attributes);
             newOp = TextInsert(thisOp.content, attributes);
           }
           }
@@ -363,7 +364,7 @@ class Delta {
     var ops = [...operations];
     var ops = [...operations];
     if (other.operations.isNotEmpty) {
     if (other.operations.isNotEmpty) {
       ops.add(other.operations[0]);
       ops.add(other.operations[0]);
-      ops = ops.sublist(1);
+      ops.addAll(other.operations.sublist(1));
     }
     }
     return Delta(ops);
     return Delta(ops);
   }
   }
@@ -399,15 +400,15 @@ Attributes? _composeMap(Attributes? a, Attributes? b) {
   final attributes = <String, Object>{};
   final attributes = <String, Object>{};
   attributes.addAll(b);
   attributes.addAll(b);
 
 
-  if (attributes.isEmpty) {
-    return null;
-  }
-
   for (final entry in a.entries) {
   for (final entry in a.entries) {
     if (!b.containsKey(entry.key)) {
     if (!b.containsKey(entry.key)) {
       attributes[entry.key] = entry.value;
       attributes[entry.key] = entry.value;
     }
     }
   }
   }
 
 
+  if (attributes.isEmpty) {
+    return null;
+  }
+
   return attributes;
   return attributes;
 }
 }

+ 41 - 41
frontend/app_flowy/packages/flowy_editor/test/delta_test.dart

@@ -73,16 +73,16 @@ void main() {
     final expected = Delta().delete(2);
     final expected = Delta().delete(2);
     expect(a.compose(b), expected);
     expect(a.compose(b), expected);
   });
   });
-  // test('retain + insert', () {
-  //   final a = Delta().retain(1, {
-  //     'color': 'blue'
-  //   });
-  //   final b = Delta().insert('B');
-  //   final expected = Delta().insert('B').retain(1, {
-  //     'color': 'blue',
-  //   });
-  //   expect(a.compose(b), expected);
-  // });
+  test('retain + insert', () {
+    final a = Delta().retain(1, {
+      'color': 'blue'
+    });
+    final b = Delta().insert('B');
+    final expected = Delta().insert('B').retain(1, {
+      'color': 'blue',
+    });
+    expect(a.compose(b), expected);
+  });
   test('retain + retain', () {
   test('retain + retain', () {
     final a = Delta().retain(1, {
     final a = Delta().retain(1, {
       'color': 'blue',
       'color': 'blue',
@@ -105,12 +105,12 @@ void main() {
     final expected = Delta().delete(1);
     final expected = Delta().delete(1);
     expect(a.compose(b), expected);
     expect(a.compose(b), expected);
   });
   });
-  // test('insert in middle of text', () {
-  //   final a = Delta().insert('Hello');
-  //   final b = Delta().retain(3).insert('X');
-  //   final expected = Delta().insert('HElXlo');
-  //   expect(a.compose(b), expected);
-  // });
+  test('insert in middle of text', () {
+    final a = Delta().insert('Hello');
+    final b = Delta().retain(3).insert('X');
+    final expected = Delta().insert('HelXlo');
+    expect(a.compose(b), expected);
+  });
   test('insert and delete ordering', () {
   test('insert and delete ordering', () {
     final a = Delta().insert('Hello');
     final a = Delta().insert('Hello');
     final b = Delta().insert('Hello');
     final b = Delta().insert('Hello');
@@ -147,29 +147,29 @@ void main() {
         .delete(1);
         .delete(1);
     expect(a.compose(b), expected);
     expect(a.compose(b), expected);
   });
   });
-  // test('retain end optimization', () {
-  //   final a = Delta()
-  //       .insert('A', {'bold': true})
-  //       .insert('B')
-  //       .insert('C', {'bold': true});
-  //   final b = Delta().delete(1);
-  //   final expected = Delta().insert('B').insert('C', {'bold': true});
-  //   expect(a.compose(b), expected);
-  // });
-  // test('retain end optimization join', () {
-  //   final a = Delta()
-  //       .insert('A', {'bold': true})
-  //       .insert('B')
-  //       .insert('C', {'bold': true})
-  //       .insert('D')
-  //       .insert('E', {'bold': true})
-  //       .insert('F');
-  //   final b = Delta().retain(1).delete(1);
-  //   final expected = Delta()
-  //       .insert('AC', {'bold': true})
-  //       .insert('D')
-  //       .insert('E', {'bold': true})
-  //       .insert('F');
-  //   expect(a.compose(b), expected);
-  // });
+  test('retain end optimization', () {
+    final a = Delta()
+        .insert('A', {'bold': true})
+        .insert('B')
+        .insert('C', {'bold': true});
+    final b = Delta().delete(1);
+    final expected = Delta().insert('B').insert('C', {'bold': true});
+    expect(a.compose(b), expected);
+  });
+  test('retain end optimization join', () {
+    final a = Delta()
+        .insert('A', {'bold': true})
+        .insert('B')
+        .insert('C', {'bold': true})
+        .insert('D')
+        .insert('E', {'bold': true})
+        .insert('F');
+    final b = Delta().retain(1).delete(1);
+    final expected = Delta()
+        .insert('AC', {'bold': true})
+        .insert('D')
+        .insert('E', {'bold': true})
+        .insert('F');
+    expect(a.compose(b), expected);
+  });
 }
 }