From 8bd748d7cd614a1f88b49e4c69f6e6580774cc45 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Wed, 13 Jul 2022 15:27:05 +0800 Subject: [PATCH] fix: unit tests --- .../flowy_editor/lib/document/text_delta.dart | 15 ++-- .../flowy_editor/test/delta_test.dart | 82 +++++++++---------- 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/document/text_delta.dart b/frontend/app_flowy/packages/flowy_editor/lib/document/text_delta.dart index 8d05099291..c53d243137 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/document/text_delta.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/document/text_delta.dart @@ -22,7 +22,8 @@ class TextOperation { } 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 { @@ -335,7 +336,7 @@ class Delta { if (otherOp is TextRetain && otherOp.length > 0) { TextOperation? newOp; if (thisOp is TextRetain) { - newOp = TextRetain(length: otherOp.length, attributes: attributes); + newOp = TextRetain(length: length, attributes: attributes); } else if (thisOp is TextInsert) { newOp = TextInsert(thisOp.content, attributes); } @@ -363,7 +364,7 @@ class Delta { var ops = [...operations]; if (other.operations.isNotEmpty) { ops.add(other.operations[0]); - ops = ops.sublist(1); + ops.addAll(other.operations.sublist(1)); } return Delta(ops); } @@ -399,15 +400,15 @@ Attributes? _composeMap(Attributes? a, Attributes? b) { final attributes = {}; attributes.addAll(b); - if (attributes.isEmpty) { - return null; - } - for (final entry in a.entries) { if (!b.containsKey(entry.key)) { attributes[entry.key] = entry.value; } } + if (attributes.isEmpty) { + return null; + } + return attributes; } diff --git a/frontend/app_flowy/packages/flowy_editor/test/delta_test.dart b/frontend/app_flowy/packages/flowy_editor/test/delta_test.dart index 1123ad1bc0..f90d487b18 100644 --- a/frontend/app_flowy/packages/flowy_editor/test/delta_test.dart +++ b/frontend/app_flowy/packages/flowy_editor/test/delta_test.dart @@ -73,16 +73,16 @@ void main() { final expected = Delta().delete(2); 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', () { final a = Delta().retain(1, { 'color': 'blue', @@ -105,12 +105,12 @@ void main() { final expected = Delta().delete(1); 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', () { final a = Delta().insert('Hello'); final b = Delta().insert('Hello'); @@ -147,29 +147,29 @@ void main() { .delete(1); 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); + }); }