From 2c88ecc63880c4c720436bb5609c442f21fea00d Mon Sep 17 00:00:00 2001 From: Martin Kamleithner Date: Sat, 16 Dec 2023 18:54:39 +0100 Subject: [PATCH] potential fix: wrong postponed update calc (#22) * potential fix: wrong postponed update calc * add regression tests --- lib/src/diffutil_impl.dart | 1 - test/diffutil_data_test.dart | 15 +++++++++++++++ test/diffutil_test.dart | 17 ++++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/src/diffutil_impl.dart b/lib/src/diffutil_impl.dart index fdd49b7..6a86c2a 100644 --- a/lib/src/diffutil_impl.dart +++ b/lib/src/diffutil_impl.dart @@ -554,7 +554,6 @@ class DiffResult { if (update.posInOwnerList == posInList && update.removal == removal) { postponedUpdate = update; postponedUpdates.removeAt(i); - i++; break; } i++; diff --git a/test/diffutil_data_test.dart b/test/diffutil_data_test.dart index f9697f5..7652d4f 100644 --- a/test/diffutil_data_test.dart +++ b/test/diffutil_data_test.dart @@ -324,6 +324,21 @@ void main() { ], ); }); + + test("github issue #21: move detection bug", () { + final start = [1, 2, 3, 4, 5, 6]; + final end = [1, 4, 2, 5, 6, 3]; + + final updates = diffutil + .calculateListDiff(start, end, detectMoves: true) + .getUpdatesWithData() + .toList(); + + expect(updates, const [ + DataMove(from: 2, to: 5, data: 3), + DataMove(from: 1, to: 2, data: 2), + ]); + }); }); } diff --git a/test/diffutil_test.dart b/test/diffutil_test.dart index 6506cb6..65e74e1 100644 --- a/test/diffutil_test.dart +++ b/test/diffutil_test.dart @@ -242,7 +242,7 @@ void main() { ]); }); - group('test list result calculaction', () { + group('test list result calculation', () { test('insert works in result list', () { expect( diffutil @@ -334,6 +334,21 @@ void main() { ); }); }); + + test("github issue #21: move detection bug", () { + final start = [1, 2, 3, 4, 5, 6]; + final end = [1, 4, 2, 5, 6, 3]; + + final updates = diffutil + .calculateListDiff(start, end, detectMoves: true) + .getUpdates(batch: false) + .toList(); + + expect(updates, const [ + Move(from: 2, to: 5), + Move(from: 1, to: 2), + ]); + }); } class DataObjectListDiff extends diffutil.ListDiffDelegate {