55import 'dart:convert' show jsonEncode;
66import 'dart:io' ;
77
8+ import 'package:collection/collection.dart' ;
89import 'package:const_finder/const_finder.dart' ;
910import 'package:path/path.dart' as path;
1011
@@ -16,6 +17,22 @@ void expect<T>(T value, T expected) {
1617 }
1718}
1819
20+ void expectInstances (dynamic value, dynamic expected) {
21+ // To ensure we ignore insertion order into maps as well as lists we use
22+ // DeepCollectionEquality as well as sort the lists.
23+
24+ int compareByStringValue (dynamic a, dynamic b) {
25+ return a['stringValue' ].compareTo (b['stringValue' ]) as int ;
26+ }
27+ value['constantInstances' ].sort (compareByStringValue);
28+ expected['constantInstances' ].sort (compareByStringValue);
29+ if (! const DeepCollectionEquality ().equals (value, expected)) {
30+ stderr.writeln ('Expected: ${jsonEncode (expected )}' );
31+ stderr.writeln ('Actual: ${jsonEncode (value )}' );
32+ exitCode = - 1 ;
33+ }
34+ }
35+
1936final String basePath =
2037 path.canonicalize (path.join (path.dirname (Platform .script.path), '..' ));
2138final String fixtures = path.join (basePath, 'test' , 'fixtures' );
@@ -50,9 +67,9 @@ void _checkConsts() {
5067 classLibraryUri: 'package:const_finder_fixtures/target.dart' ,
5168 className: 'Target' ,
5269 );
53- expect < String > (
54- jsonEncode ( finder.findInstances () ),
55- jsonEncode ( < String , dynamic > {
70+ expectInstances (
71+ finder.findInstances (),
72+ < String , dynamic > {
5673 'constantInstances' : < Map <String , dynamic >> [
5774 < String , dynamic > {'stringValue' : '100' , 'intValue' : 100 , 'targetValue' : null },
5875 < String , dynamic > {'stringValue' : '102' , 'intValue' : 102 , 'targetValue' : null },
@@ -76,22 +93,22 @@ void _checkConsts() {
7693 < String , dynamic > {'stringValue' : 'package' , 'intValue' : - 1 , 'targetValue' : null },
7794 ],
7895 'nonConstantLocations' : < dynamic > [],
79- }) ,
96+ },
8097 );
8198
8299 final ConstFinder finder2 = ConstFinder (
83100 kernelFilePath: constsDill,
84101 classLibraryUri: 'package:const_finder_fixtures/target.dart' ,
85102 className: 'MixedInTarget' ,
86103 );
87- expect < String > (
88- jsonEncode ( finder2.findInstances () ),
89- jsonEncode ( < String , dynamic > {
104+ expectInstances (
105+ finder2.findInstances (),
106+ < String , dynamic > {
90107 'constantInstances' : < Map <String , dynamic >> [
91108 < String , dynamic > {'val' : '13' },
92109 ],
93110 'nonConstantLocations' : < dynamic > [],
94- }) ,
111+ },
95112 );
96113}
97114
@@ -103,9 +120,9 @@ void _checkNonConsts() {
103120 className: 'Target' ,
104121 );
105122
106- expect < String > (
107- jsonEncode ( finder.findInstances () ),
108- jsonEncode ( < String , dynamic > {
123+ expectInstances (
124+ finder.findInstances (),
125+ < String , dynamic > {
109126 'constantInstances' : < dynamic > [
110127 < String , dynamic > {'stringValue' : '1' , 'intValue' : 1 , 'targetValue' : null },
111128 < String , dynamic > {'stringValue' : '6' , 'intValue' : 6 , 'targetValue' : null },
@@ -141,7 +158,7 @@ void _checkNonConsts() {
141158 'column' : 25 ,
142159 }
143160 ]
144- }) ,
161+ },
145162 );
146163}
147164
0 commit comments