Skip to content

Commit

Permalink
fix!: fix for the uk/gb country code case
Browse files Browse the repository at this point in the history
New file:
* `api_off_tag_test.dart`: Tests about OffTagged enums.

Impacted file:
* `country_helper.dart`: case-insensitive search.
  • Loading branch information
monsieurtanuki committed Sep 7, 2023
1 parent 5849c0a commit 1c23a24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/src/utils/country_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,12 @@ enum OpenFoodFactsCountry implements OffTagged {
@override
final String offTag;

/// Returns the first [OpenFoodFactsCountry] that matches the [offTag].
static OpenFoodFactsCountry? fromOffTag(final String? offTag) {
/// Returns the [OpenFoodFactsCountry] that matches the [offTag].
///
/// Case-insensitive.
/// Special case: "uk" and "gb" both mean United Kingdom.
static OpenFoodFactsCountry? fromOffTag(String? offTag) {
offTag = offTag?.toLowerCase();
// special case as we use 'uk' in off-dart
if (offTag == 'gb') {
return OpenFoodFactsCountry.UNITED_KINGDOM;
Expand Down
25 changes: 25 additions & 0 deletions test/api_off_tag_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:test/test.dart';

/// Tests about [OffTagged] `enum`s.
void main() {
test('$OpenFoodAPIClient country', () async {
const Map<String?, OpenFoodFactsCountry?> expectedCountries =
<String?, OpenFoodFactsCountry?>{
'fr': OpenFoodFactsCountry.FRANCE,
'FR': OpenFoodFactsCountry.FRANCE,
'fR': OpenFoodFactsCountry.FRANCE,
'Fr': OpenFoodFactsCountry.FRANCE,
'uk': OpenFoodFactsCountry.UNITED_KINGDOM,
'UK': OpenFoodFactsCountry.UNITED_KINGDOM,
'gb': OpenFoodFactsCountry.UNITED_KINGDOM,
'GB': OpenFoodFactsCountry.UNITED_KINGDOM,
null: null,
'this is not a country code': null,
};
for (final MapEntry<String?, OpenFoodFactsCountry?> entry
in expectedCountries.entries) {
expect(OpenFoodFactsCountry.fromOffTag(entry.key), entry.value);
}
});
}

0 comments on commit 1c23a24

Please sign in to comment.