Skip to content

[google_identity_services_web] - Enhance maybeEnum to handle if enum value is not exist with a return of null. #9006

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/google_identity_services_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.3+1

* Improves robustness of `maybeEnum` by catching `ArgumentError` when the enum name is not found and returning `null`.

## 0.3.3

* Moves all the JavaScript types to extend `JSObject`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
// found in the LICENSE file.

/// Attempts to retrieve an enum value from [haystack] if [needle] is not null.
/// Returns null if [needle] is null or if the enum value is not found in
/// [haystack].
T? maybeEnum<T extends Enum>(String? needle, List<T> haystack) {
if (needle == null) {
return null;
}
return haystack.byName(needle);

try {
return haystack.byName(needle);
} on ArgumentError catch (_) {
return null;
}
}

/// The type of several functions from the library, that don't receive
Expand Down
2 changes: 1 addition & 1 deletion packages/google_identity_services_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_identity_services_web
description: A Dart JS-interop layer for Google Identity Services. Google's new sign-in SDK for Web that supports multiple types of credentials.
repository: https://github.com/flutter/packages/tree/main/packages/google_identity_services_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_identiy_services_web%22
version: 0.3.3
version: 0.3.3+1

environment:
sdk: ^3.4.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:google_identity_services_web/src/js_interop/shared.dart'
show maybeEnum;
import 'package:test/test.dart';

enum TestEnum {
first,
second,
third,
camelCase,
UPPERCASE,
}

void main() {
group('maybeEnum tests', () {
test('should return null when needle is null', () {
expect(maybeEnum<TestEnum>(null, TestEnum.values), isNull);
});

test('should return the correct enum when needle exists', () {
expect(maybeEnum('first', TestEnum.values), equals(TestEnum.first));
expect(maybeEnum('second', TestEnum.values), equals(TestEnum.second));
expect(maybeEnum('third', TestEnum.values), equals(TestEnum.third));
});

test('should handle different casing in enum names', () {
expect(
maybeEnum('camelCase', TestEnum.values), equals(TestEnum.camelCase));
expect(
maybeEnum('UPPERCASE', TestEnum.values), equals(TestEnum.UPPERCASE));
});

test('should return null when needle is not found', () {
expect(maybeEnum('notFound', TestEnum.values), isNull);
expect(maybeEnum('Fourth', TestEnum.values), isNull);
expect(maybeEnum('', TestEnum.values), isNull);
});

test('should return null for case-mismatched enum names', () {
expect(maybeEnum('FIRST', TestEnum.values), isNull);
expect(maybeEnum('camelcase', TestEnum.values), isNull);
expect(maybeEnum('uppercase', TestEnum.values), isNull);
});

test('should handle special characters in needle', () {
expect(maybeEnum('first!', TestEnum.values), isNull);
expect(maybeEnum(' first ', TestEnum.values), isNull);
});
});
}