fix: unit tests

This commit is contained in:
Vincent Chan
2022-07-13 15:27:05 +08:00
parent 2881edd505
commit 8bd748d7cd
2 changed files with 49 additions and 48 deletions

View File

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

View File

@ -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', () { test('retain + insert', () {
// final a = Delta().retain(1, { final a = Delta().retain(1, {
// 'color': 'blue' 'color': 'blue'
// }); });
// final b = Delta().insert('B'); final b = Delta().insert('B');
// final expected = Delta().insert('B').retain(1, { final expected = Delta().insert('B').retain(1, {
// 'color': 'blue', 'color': 'blue',
// }); });
// expect(a.compose(b), expected); 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', () { test('insert in middle of text', () {
// final a = Delta().insert('Hello'); final a = Delta().insert('Hello');
// final b = Delta().retain(3).insert('X'); final b = Delta().retain(3).insert('X');
// final expected = Delta().insert('HElXlo'); final expected = Delta().insert('HelXlo');
// expect(a.compose(b), expected); 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', () { test('retain end optimization', () {
// final a = Delta() final a = Delta()
// .insert('A', {'bold': true}) .insert('A', {'bold': true})
// .insert('B') .insert('B')
// .insert('C', {'bold': true}); .insert('C', {'bold': true});
// final b = Delta().delete(1); final b = Delta().delete(1);
// final expected = Delta().insert('B').insert('C', {'bold': true}); final expected = Delta().insert('B').insert('C', {'bold': true});
// expect(a.compose(b), expected); expect(a.compose(b), expected);
// }); });
// test('retain end optimization join', () { test('retain end optimization join', () {
// final a = Delta() final a = Delta()
// .insert('A', {'bold': true}) .insert('A', {'bold': true})
// .insert('B') .insert('B')
// .insert('C', {'bold': true}) .insert('C', {'bold': true})
// .insert('D') .insert('D')
// .insert('E', {'bold': true}) .insert('E', {'bold': true})
// .insert('F'); .insert('F');
// final b = Delta().retain(1).delete(1); final b = Delta().retain(1).delete(1);
// final expected = Delta() final expected = Delta()
// .insert('AC', {'bold': true}) .insert('AC', {'bold': true})
// .insert('D') .insert('D')
// .insert('E', {'bold': true}) .insert('E', {'bold': true})
// .insert('F'); .insert('F');
// expect(a.compose(b), expected); expect(a.compose(b), expected);
// }); });
} }