Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special cases of Merging values into a list #691

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
12 changes: 12 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ extension MapExtension<K> on Map<K, dynamic> {
continue;
}

if (oldValue is! Map<K, dynamic> && newValue is Map<K, dynamic> ||
oldValue is Map<K, dynamic> && newValue is! Map<K, dynamic>) {
int lostEntries = 1;
if (oldValue is Map<K, dynamic>) {
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;
}
}
Expand Down
69 changes: 68 additions & 1 deletion test/easy_localization_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> printLog = [];
dynamic overridePrint(Function() testFn) => () {
var spec = ZoneSpecification(print: (_, __, ___, String msg) {
// Add to log instead of printing to stdout
Expand All @@ -14,6 +14,7 @@ dynamic overridePrint(Function() testFn) => () {
};

void main() {

group('Utils', () {
group('Locales', () {
test('localeFromString only language code', () {
Expand Down Expand Up @@ -106,6 +107,72 @@ void main() {
},
});
});

test('should warn if possibly multiple values are lost', overridePrint(() {
printLog = [];
final Map<String, dynamic> map1 = {
'key1': 'value1',
'key2': 'value2',
};

final Map<String, dynamic> 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<String, dynamic> map1 = {
'key2': {
'key3': 'value3',
'key4': 'value4',
},
};

final Map<String, dynamic> 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<String, dynamic> map1 = {
'key1': 'value1',
'key2': 'value2',
};

final Map<String, dynamic> 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);
},
));
});
});
}
Loading