Skip to content

Commit

Permalink
Merge pull request rafaelsetragni#354 from WingCH/master
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelsetragni committed Dec 4, 2021
2 parents facacfa + 60f3af0 commit 09b92ae
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 22 deletions.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.6.18+1"
version: "0.6.18+2"
boolean_selector:
dependency: transitive
description:
Expand Down
75 changes: 54 additions & 21 deletions lib/src/utils/assert_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,68 @@ class AssertUtils {
dynamic defaultValue = _getDefaultValue(reference, T);
dynamic value = dataMap[reference];

// Color hexadecimal representation
if (T == int && value != null && value is String) {
if (value is String) {
String valueCasted = value;

final RegExpMatch? match =
switch (T) {
case String:
return valueCasted;

case Color:
case int:

// Color hexadecimal representation
final RegExpMatch? match =
RegExp(r'^(0x|#)(\w{2})?(\w{6,6})$').firstMatch(valueCasted);
if (match != null) {
String hex = (match.group(2) ?? 'FF') + match.group(3)!;
final int colorValue = int.parse(hex, radix: 16);
return colorValue;

if (match != null) {
String hex = (match.group(2) ?? 'FF') + match.group(3)!;
final int colorValue = int.parse(hex, radix: 16);
return (T == Color) ? Color(colorValue) : colorValue;
} else if (T == int) {
int? parsedValue = int.tryParse(valueCasted.replaceFirstMapped(
RegExp(r'^(\d+)\.\d+$'), (match) => '${match.group(1)}'));
return parsedValue ?? defaultValue;
}
break;

case double:
double? parsedValue = double.tryParse(valueCasted);
return parsedValue ?? defaultValue;
}
}

switch (value.runtimeType) {
case MaterialColor:
value = (value as MaterialColor).shade500;
break;

case MaterialAccentColor:
value = Color((value as MaterialAccentColor).value);
break;

case CupertinoDynamicColor:
value = Color((value as CupertinoDynamicColor).value);
break;
switch (T) {
case int:
if (value is int) return value;
if (value is double) return value.round();
return defaultValue;

case double:
if (value is int) return value;
if (value is double) return value;
return defaultValue;

case Color:
switch (value.runtimeType) {
case MaterialColor:
value = (value as MaterialColor).shade500;
break;

case MaterialAccentColor:
value = Color((value as MaterialAccentColor).value);
break;

case CupertinoDynamicColor:
value = Color((value as CupertinoDynamicColor).value);
break;
}
}

if (value == null || !(value.runtimeType == T)) return defaultValue;
if (value == null || value.runtimeType.hashCode != T.hashCode)
return defaultValue;

return value;
return null;
}

static extractMap<T, C>(Map dataMap, String reference) {
Expand All @@ -111,6 +143,7 @@ class AssertUtils {
if (value == null || !(value is String)) return defaultValue;

String castedValue = value;
castedValue = castedValue.trim();
if (AssertUtils.isNullOrEmptyOrInvalid(castedValue, String))
return defaultValue;

Expand Down
73 changes: 73 additions & 0 deletions test/extraction_values_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:flutter/material.dart' hide DateUtils;
import 'package:flutter_test/flutter_test.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

setUp(() {});

tearDown(() {});

test('extractValueTest', () async {
expect("title", AssertUtils.extractValue("test", {"test": "title"}, String));
expect(" title", AssertUtils.extractValue("test", {"test": " title"}, String));
expect("", AssertUtils.extractValue("test", {"test": ""}, String));
expect(" ", AssertUtils.extractValue("test", {"test": " "}, String));

expect(10, AssertUtils.extractValue("test", {"test": "10"}, int));
expect(10, AssertUtils.extractValue("test", {"test": 10}, int));
expect(10, AssertUtils.extractValue("test", {"test": "10.0"}, int));
expect(10.0, AssertUtils.extractValue("test", {"test": "10.0"}, double));
expect(0, AssertUtils.extractValue("test", {"test": "0"}, int));
expect(0.0, AssertUtils.extractValue("test", {"test": "0"}, double));
expect(0, AssertUtils.extractValue("test", {"test": "0.0"}, int));
expect(0, AssertUtils.extractValue("test", {"test": 0}, int));

expect(0xFFFF0000, AssertUtils.extractValue("test", {"test": "#FF0000"}, int));
expect(0xFFFF0000, AssertUtils.extractValue("test", {"test": "#ff0000"}, int));
expect(0xFFFF0000, AssertUtils.extractValue("test", {"test": "#FFFF0000"}, int));
expect(0x00FF0000, AssertUtils.extractValue("test", {"test": "#00FF0000"}, int));
expect(0xFFFF0000, AssertUtils.extractValue("test", {"test": "0xFF0000"}, int));
expect(0xFFFF0000, AssertUtils.extractValue("test", {"test": "0xFFff0000"}, int));

expect(Colors.black, AssertUtils.extractValue("test", {"test": "#000000"}, Color));
expect(Colors.black, AssertUtils.extractValue("test", {"test": "#FF000000"}, Color));
expect(Colors.transparent, AssertUtils.extractValue("test", {"test": "#00000000"}, Color));

expect(null, AssertUtils.extractValue("test", {"test": null}, Color));
expect(null, AssertUtils.extractValue("test", {"test": "#0004"}, Color));
expect(null, AssertUtils.extractValue("test", {"test": "#04"}, Color));
expect(null, AssertUtils.extractValue("test", {"test": " "}, Color));

expect(null, AssertUtils.extractValue("test", {"test": null}, int));
expect(null, AssertUtils.extractValue("test", {"test": ""}, int));
expect(null, AssertUtils.extractValue("test", {"test": " "}, int));

expect(null, AssertUtils.extractValue("test", {"test": 0}, String));
expect(null, AssertUtils.extractValue("test", {"test": null}, String));
});

test('extractEnumTest', () async {
expect(NotificationPrivacy.Private,
AssertUtils.extractEnum<NotificationPrivacy>(
"test", {"test": "Private"}, NotificationPrivacy.values));

expect(NotificationPrivacy.Public,
AssertUtils.extractEnum<NotificationPrivacy>(
"test", {"test": "Public"}, NotificationPrivacy.values));

expect(null,
AssertUtils.extractEnum<NotificationPrivacy>(
"test", {"test": ""}, NotificationPrivacy.values));

expect(null,
AssertUtils.extractEnum<NotificationPrivacy>(
"test", {"test": " "}, NotificationPrivacy.values));

expect(null,
AssertUtils.extractEnum<NotificationPrivacy>(
"test", {"test": null}, NotificationPrivacy.values));
});

}

0 comments on commit 09b92ae

Please sign in to comment.