diff --git a/lib/src/utils.dart b/lib/src/utils.dart index b059b954..b60cec3e 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -74,6 +74,18 @@ extension MapExtension on Map { continue; } + if (oldValue is! Map && newValue is Map || + oldValue is Map && newValue is! Map) { + int lostEntries = 1; + if (oldValue is Map) { + lostEntries = oldValue.length; + } + EasyLocalization.logger.warning('The key "${entry.key}" exists as a map and as a value. Overwriting it. Number of lost entries: $lostEntries.'); + } + else if (oldValue != null && newValue != null){ + EasyLocalization.logger.warning('The key "${entry.key}" already exists in the map. Overwriting it.'); + } + this[entry.key] = newValue; } } diff --git a/test/easy_localization_utils_test.dart b/test/easy_localization_utils_test.dart index cab0ee2a..a75f24a7 100644 --- a/test/easy_localization_utils_test.dart +++ b/test/easy_localization_utils_test.dart @@ -4,7 +4,7 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; -var printLog = []; +List printLog = []; dynamic overridePrint(Function() testFn) => () { var spec = ZoneSpecification(print: (_, __, ___, String msg) { // Add to log instead of printing to stdout @@ -14,6 +14,7 @@ dynamic overridePrint(Function() testFn) => () { }; void main() { + group('Utils', () { group('Locales', () { test('localeFromString only language code', () { @@ -106,6 +107,72 @@ void main() { }, }); }); + + test('should warn if possibly multiple values are lost', overridePrint(() { + printLog = []; + final Map map1 = { + 'key1': 'value1', + 'key2': 'value2', + }; + + final Map map2 = { + 'key2': { + 'key3': 'value3', + 'key4': 'value4', + }, + }; + + map1.addAllRecursive(map2); + + expect(printLog.length, 1); + + expect(printLog.first.contains('[WARNING]'), true); + expect(printLog.first.contains('The key "key2" exists as a map and as a value. Overwriting it. Number of lost entries: 1'), true); + }, + )); + + test('should warn if possibly multiple values are lost', overridePrint(() { + printLog = []; + final Map map1 = { + 'key2': { + 'key3': 'value3', + 'key4': 'value4', + }, + }; + + final Map map2 = { + 'key1': 'value1', + 'key2': 'value2', + }; + + map1.addAllRecursive(map2); + + expect(printLog.length, 1); + + expect(printLog.first.contains('[WARNING]'), true); + expect(printLog.first.contains('The key "key2" exists as a map and as a value. Overwriting it. Number of lost entries: 2'), true); + }, + )); + + test('should warn if values are overwritten', overridePrint(() { + printLog = []; + final Map map1 = { + 'key1': 'value1', + 'key2': 'value2', + }; + + final Map map2 = { + 'key2': 'new_value2', + }; + + map1.addAllRecursive(map2); + + expect(printLog.length, 1); + + expect(printLog.first.contains('[WARNING]'), true); + expect(printLog.first.contains('The key "key2" already exists in the map. Overwriting it.'), true); + }, + )); }); }); }