Skip to content

Commit

Permalink
Fixes to run with dart2js
Browse files Browse the repository at this point in the history
  • Loading branch information
rbellens committed Jun 21, 2021
1 parent 5f2d496 commit 69d678b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

## 0.2.1

- Fixes to run with dart2js (see https://github.com/dart-lang/sdk/issues/46417)

## 0.2.0

- Null safety
Expand Down
8 changes: 5 additions & 3 deletions lib/src/deep_immutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ dynamic toDeepImmutable(dynamic input,
return _DeepImmutableSet(input.cast(), isDeepImmutable: isDeepImmutable);
}
if (input is Iterable) {
return _DeepImmutableList(input.cast() as List<dynamic>, isDeepImmutable: isDeepImmutable);
return _DeepImmutableList(input.cast() as List<dynamic>,
isDeepImmutable: isDeepImmutable);
}
if (input is Map) {
return _DeepImmutableMap(input.cast(), isDeepImmutable: isDeepImmutable);
Expand All @@ -109,8 +110,9 @@ class _DeepImmutableMap extends UnmodifiableMapView<String, dynamic>
implements DeepImmutable {
_DeepImmutableMap(Map<String, dynamic> map,
{bool Function(dynamic)? isDeepImmutable})
: super(Map<String, dynamic>.unmodifiable(map.map((k, v) =>
MapEntry<String, dynamic>(
: super(Map<String,
dynamic>.from /*.unmodifiable instead of .from causes an error in web environment: see https://github.com/dart-lang/sdk/issues/46417 */
(map.map((k, v) => MapEntry<String, dynamic>(
k, toDeepImmutable(v, isDeepImmutable: isDeepImmutable)))));
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: snapshot
description: A library that can be used to implement data classes and simplifies conversion of JSON to data classes
version: 0.2.0
version: 0.2.1
homepage: https://github.com/appsup-dart/snapshot

environment:
Expand Down
11 changes: 10 additions & 1 deletion test/deep_immutable_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dart:io';

import 'package:snapshot/src/deep_immutable.dart';
import 'package:test/test.dart';

const bool kIsWeb = identical(0, 0.0);

void main() {
group('toDeepImmutable', () {
test('Literal types should not be changed', () {
Expand Down Expand Up @@ -30,7 +34,12 @@ void main() {
MyUri(Uri.parse('https://pub.dev')),
MyRegExp(r'.*'),
]) {
expect(toDeepImmutable(v), v.delegateTo);
if (v is MyRegExp && kIsWeb) {
// on web, RegExp instances are not equal even when their properties are the same
expect(v.pattern, v.delegateTo.pattern);
} else {
expect(toDeepImmutable(v), v.delegateTo);
}
expect(toDeepImmutable(v), isNot(isA<DelegatesTo>()));
}
});
Expand Down
16 changes: 14 additions & 2 deletions test/snapshot_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:collection';

import 'package:intl/intl.dart';
import 'package:snapshot/snapshot.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -109,7 +111,7 @@ void main() {
test('Should return null when content is null and type is nullable', () {
var s = Snapshot.fromJson(null);

expect(s.as<String? >(), null);
expect(s.as<String?>(), null);
expect(s.as<dynamic>(), null);
});

Expand All @@ -118,7 +120,7 @@ void main() {
() {
var s = Snapshot.fromJson({'street': 'Mainstreet'},
decoder: decoderWithAddress);
expect(s.as<Address>(), same(s.as<Address? >()));
expect(s.as<Address>(), same(s.as<Address?>()));
});
});

Expand Down Expand Up @@ -363,6 +365,16 @@ void main() {
createParents: false),
throwsArgumentError);
});

test('Setting a null value', () {
var person = Snapshot.empty()
.setPath('firstname', 'Jane')
.setPath('address', null)
.setPath('lastname', 'Doe');

expect(person.value,
{'firstname': 'Jane', 'address': null, 'lastname': 'Doe'});
});
});

group('Snapshot.withDecoder()', () {
Expand Down

0 comments on commit 69d678b

Please sign in to comment.