diff options
-rw-r--r-- | test/list_ext_test.dart | 36 | ||||
-rw-r--r-- | test/list_key_test.dart | 63 | ||||
-rw-r--r-- | test/test_device.dart | 14 |
3 files changed, 113 insertions, 0 deletions
diff --git a/test/list_ext_test.dart b/test/list_ext_test.dart new file mode 100644 index 0000000..cb6e68c --- /dev/null +++ b/test/list_ext_test.dart @@ -0,0 +1,36 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:uvok_epaper_badge/extensions/list_ext.dart'; + +void main() { + group('ListExt.addOrReplaceWhere', () { + test('adds element when predicate returns false for all', () { + final list = [1, 2, 3]; + list.addOrReplaceWhere(4, (x) => x == 5); + expect(list, equals([1, 2, 3, 4])); + }); + + test('replaces element when predicate matches and no replaceCondition', () { + final list = [1, 2, 3]; + list.addOrReplaceWhere(99, (x) => x == 2); + expect(list, equals([1, 99, 3])); + }); + + test('does not replace if replaceCondition returns false', () { + final list = [1, 2, 3]; + list.addOrReplaceWhere(99, (x) => x == 2, replaceCondition: (x) => false); + expect(list, equals([1, 2, 3])); + }); + + test('replaces if replaceCondition returns true', () { + final list = [1, 2, 3]; + list.addOrReplaceWhere(99, (x) => x == 2, replaceCondition: (x) => true); + expect(list, equals([1, 99, 3])); + }); + + test('only replaces the first matching element', () { + final list = [2, 2, 3]; + list.addOrReplaceWhere(99, (x) => x == 2); + expect(list, equals([99, 2, 3])); + }); + }); +} diff --git a/test/list_key_test.dart b/test/list_key_test.dart new file mode 100644 index 0000000..2b4fa6a --- /dev/null +++ b/test/list_key_test.dart @@ -0,0 +1,63 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:uvok_epaper_badge/extensions/list_ext.dart'; + +import 'test_device.dart'; + +void main() { + group('ListExt.addOrReplaceKey with Device', () { + test('adds device when no key matches', () { + final list = [TestDevice(1, 'A'), TestDevice(2, 'B')]; + final newDevice = TestDevice(3, 'C'); + + list.addOrReplaceKey(newDevice, keySelector: (d) => d.id); + + expect(list.length, 3); + expect(list.contains(newDevice), isTrue); + }); + + test('replaces device when key matches and no replaceCondition', () { + final list = [TestDevice(1, 'A'), TestDevice(2, 'B')]; + final replacement = TestDevice(2, 'Z'); + + list.addOrReplaceKey(replacement, keySelector: (d) => d.id); + + expect(list[1], equals(replacement)); + }); + + test('does not replace when replaceCondition is false', () { + final list = [TestDevice(1, 'A'), TestDevice(2, 'B')]; + final attemptedReplace = TestDevice(2, 'Z'); + + list.addOrReplaceKey( + attemptedReplace, + keySelector: (d) => d.id, + replaceCondition: (existing) => false, + ); + + expect(list[1].name, equals('B')); + }); + + test('replaces when replaceCondition is true', () { + final list = [TestDevice(1, 'A'), TestDevice(2, 'B')]; + final attemptedReplace = TestDevice(2, 'Z'); + + list.addOrReplaceKey( + attemptedReplace, + keySelector: (d) => d.id, + replaceCondition: (existing) => true, + ); + + expect(list[1], equals(attemptedReplace)); + }); + + test('replaces only the first matching key', () { + final list = [TestDevice(2, 'B1'), TestDevice(2, 'B2')]; + final newDevice = TestDevice(2, 'B0'); + + list.addOrReplaceKey(newDevice, keySelector: (d) => d.id); + + expect(list[0], equals(newDevice)); + expect(list[1].name, equals('B2')); + }); + }); +} diff --git a/test/test_device.dart b/test/test_device.dart new file mode 100644 index 0000000..24774bb --- /dev/null +++ b/test/test_device.dart @@ -0,0 +1,14 @@ +class TestDevice { + final int id; + final String name; + + TestDevice(this.id, this.name); + + @override + bool operator ==(Object other) => + identical(this, other) || + other is TestDevice && id == other.id && name == other.name; + + @override + int get hashCode => id.hashCode ^ name.hashCode; +} |