-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
261 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
/// Makes sure no invalid data is sent over method channels. | ||
@internal | ||
class MethodChannelHelper { | ||
static dynamic normalize(dynamic data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
if (_isPrimitive(data)) { | ||
return data; | ||
} else if (data is List<dynamic>) { | ||
return _normalizeList(data); | ||
} else if (data is Map<String, dynamic>) { | ||
return normalizeMap(data); | ||
} else { | ||
return data.toString(); | ||
} | ||
} | ||
|
||
static Map<String, dynamic>? normalizeMap(Map<String, dynamic>? data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
return data.map((key, value) => MapEntry(key, normalize(value))); | ||
} | ||
|
||
static List<dynamic>? _normalizeList(List<dynamic>? data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
return data.map((e) => normalize(e)).toList(); | ||
} | ||
|
||
static bool _isPrimitive(dynamic value) { | ||
return value == null || value is String || value is num || value is bool; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:sentry_flutter/src/method_channel_helper.dart'; | ||
import 'package:collection/collection.dart'; | ||
|
||
void main() { | ||
group('normalize', () { | ||
test('primitives', () { | ||
var expected = <String, dynamic>{ | ||
'null': null, | ||
'int': 1, | ||
'float': 1.1, | ||
'bool': true, | ||
'string': 'Foo', | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(expected); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
|
||
expect(MethodChannelHelper.normalize(null), null); | ||
expect(MethodChannelHelper.normalize(1), 1); | ||
expect(MethodChannelHelper.normalize(1.1), 1.1); | ||
expect(MethodChannelHelper.normalize(true), true); | ||
expect(MethodChannelHelper.normalize('Foo'), 'Foo'); | ||
}); | ||
|
||
test('object', () { | ||
expect(MethodChannelHelper.normalize(_CustomObject()), 'CustomObject()'); | ||
}); | ||
|
||
test('object in list', () { | ||
var input = <String, dynamic>{ | ||
'object': [_CustomObject()] | ||
}; | ||
var expected = <String, dynamic>{ | ||
'object': ['CustomObject()'] | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalize(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('object in map', () { | ||
var input = <String, dynamic>{ | ||
'object': <String, dynamic>{'object': _CustomObject()} | ||
}; | ||
var expected = <String, dynamic>{ | ||
'object': <String, dynamic>{'object': 'CustomObject()'} | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalize(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
}); | ||
|
||
group('normalizeMap', () { | ||
test('primitives', () { | ||
var expected = <String, dynamic>{ | ||
'null': null, | ||
'int': 1, | ||
'float': 1.1, | ||
'bool': true, | ||
'string': 'Foo', | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(expected); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('list with primitives', () { | ||
var expected = <String, dynamic>{ | ||
'list': [null, 1, 1.1, true, 'Foo'], | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(expected); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('map with primitives', () { | ||
var expected = <String, dynamic>{ | ||
'map': <String, dynamic>{ | ||
'null': null, | ||
'int': 1, | ||
'float': 1.1, | ||
'bool': true, | ||
'string': 'Foo', | ||
}, | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(expected); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('object', () { | ||
var input = <String, dynamic>{'object': _CustomObject()}; | ||
var expected = <String, dynamic>{'object': 'CustomObject()'}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('object in list', () { | ||
var input = <String, dynamic>{ | ||
'object': [_CustomObject()] | ||
}; | ||
var expected = <String, dynamic>{ | ||
'object': ['CustomObject()'] | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('object in map', () { | ||
var input = <String, dynamic>{ | ||
'object': <String, dynamic>{'object': _CustomObject()} | ||
}; | ||
var expected = <String, dynamic>{ | ||
'object': <String, dynamic>{'object': 'CustomObject()'} | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
class _CustomObject { | ||
@override | ||
String toString() { | ||
return 'CustomObject()'; | ||
} | ||
} |
Oops, something went wrong.