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) {
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 = <String, Object>{};
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;
}

View File

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