Skip to content

Commit 7075ae7

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[cfe+dart2js] Move features to CFE
To share support for annotated tests based on Features. Change-Id: Ic319442e2e016bf4d1584825ea019d13a74acedb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/109560 Reviewed-by: Stephen Adams <[email protected]>
1 parent b912aeb commit 7075ae7

File tree

14 files changed

+131
-119
lines changed

14 files changed

+131
-119
lines changed

pkg/compiler/lib/src/ssa/logging.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import '../elements/entities.dart';
6-
import '../util/features.dart';
6+
import 'package:front_end/src/testing/features.dart';
77
import 'nodes.dart';
88

99
/// Log used for unit testing optimizations.

pkg/compiler/lib/src/util/features.dart renamed to pkg/front_end/lib/src/testing/features.dart

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,50 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// Set of features used in annotations.
5+
import 'id_testing.dart';
6+
7+
/// Utility class for annotated testing representing a set of features.
68
class Features {
79
Map<String, Object> _features = {};
810

11+
/// Mark the feature [key] as existing. If [value] is provided, the feature
12+
/// [key] is set to have this value.
913
void add(String key, {var value: ''}) {
1014
_features[key] = value.toString();
1115
}
1216

17+
/// Add [value] as an element of the list values of feature [key].
1318
void addElement(String key, [var value]) {
1419
List<String> list = _features.putIfAbsent(key, () => <String>[]);
1520
if (value != null) {
1621
list.add(value.toString());
1722
}
1823
}
1924

25+
/// Returns `true` if feature [key] exists.
2026
bool containsKey(String key) {
2127
return _features.containsKey(key);
2228
}
2329

30+
/// Set the feature [key] to exist with the [value].
2431
void operator []=(String key, String value) {
2532
_features[key] = value;
2633
}
2734

35+
/// Returns the value set for feature [key].
2836
Object operator [](String key) => _features[key];
2937

38+
/// Removes the value set for feature [key]. Returns the existing value.
3039
Object remove(String key) => _features.remove(key);
3140

41+
/// Returns `true` if this feature set is empty.
3242
bool get isEmpty => _features.isEmpty;
3343

44+
/// Returns `true` if this feature set is non-empty.
3445
bool get isNotEmpty => _features.isNotEmpty;
3546

47+
/// Call [f] for each feature in this feature set with its corresponding
48+
/// value.
3649
void forEach(void Function(String, Object) f) {
3750
_features.forEach(f);
3851
}
@@ -156,3 +169,106 @@ class Features {
156169
return features;
157170
}
158171
}
172+
173+
class FeaturesDataInterpreter implements DataInterpreter<Features> {
174+
const FeaturesDataInterpreter();
175+
176+
@override
177+
String isAsExpected(Features actualFeatures, String expectedData) {
178+
if (expectedData == '*') {
179+
return null;
180+
} else if (expectedData == '') {
181+
return actualFeatures.isNotEmpty ? "Expected empty data." : null;
182+
} else {
183+
List<String> errorsFound = [];
184+
Features expectedFeatures = Features.fromText(expectedData);
185+
Set<String> validatedFeatures = new Set<String>();
186+
expectedFeatures.forEach((String key, Object expectedValue) {
187+
bool expectMatch = true;
188+
if (key.startsWith('!')) {
189+
key = key.substring(1);
190+
expectMatch = false;
191+
}
192+
validatedFeatures.add(key);
193+
Object actualValue = actualFeatures[key];
194+
if (!expectMatch) {
195+
if (actualFeatures.containsKey(key)) {
196+
errorsFound.add('Unexpected data found for $key=$actualValue');
197+
}
198+
} else if (!actualFeatures.containsKey(key)) {
199+
errorsFound.add('No data found for $key');
200+
} else if (expectedValue == '') {
201+
if (actualValue != '') {
202+
errorsFound.add('Non-empty data found for $key');
203+
}
204+
} else if (expectedValue == '*') {
205+
return;
206+
} else if (expectedValue is List) {
207+
if (actualValue is List) {
208+
List actualList = actualValue.toList();
209+
for (Object expectedObject in expectedValue) {
210+
String expectedText = '$expectedObject';
211+
bool matchFound = false;
212+
if (expectedText.endsWith('*')) {
213+
// Wildcard matcher.
214+
String prefix =
215+
expectedText.substring(0, expectedText.indexOf('*'));
216+
List matches = [];
217+
for (Object actualObject in actualList) {
218+
if ('$actualObject'.startsWith(prefix)) {
219+
matches.add(actualObject);
220+
matchFound = true;
221+
}
222+
}
223+
for (Object match in matches) {
224+
actualList.remove(match);
225+
}
226+
} else {
227+
for (Object actualObject in actualList) {
228+
if (expectedText == '$actualObject') {
229+
actualList.remove(actualObject);
230+
matchFound = true;
231+
break;
232+
}
233+
}
234+
}
235+
if (!matchFound) {
236+
errorsFound.add("No match found for $key=[$expectedText]");
237+
}
238+
}
239+
if (actualList.isNotEmpty) {
240+
errorsFound
241+
.add("Extra data found $key=[${actualList.join(',')}]");
242+
}
243+
} else {
244+
errorsFound.add("List data expected for $key: "
245+
"expected '$expectedValue', found '${actualValue}'");
246+
}
247+
} else if (expectedValue != actualValue) {
248+
errorsFound.add(
249+
"Mismatch for $key: expected '$expectedValue', found '${actualValue}'");
250+
}
251+
});
252+
actualFeatures.forEach((String key, Object value) {
253+
if (!validatedFeatures.contains(key)) {
254+
if (value == '') {
255+
errorsFound.add("Extra data found '$key'");
256+
} else {
257+
errorsFound.add("Extra data found $key=$value");
258+
}
259+
}
260+
});
261+
return errorsFound.isNotEmpty ? errorsFound.join('\n ') : null;
262+
}
263+
}
264+
265+
@override
266+
String getText(Features actualData) {
267+
return actualData.getText();
268+
}
269+
270+
@override
271+
bool isEmpty(Features actualData) {
272+
return actualData == null || actualData.isEmpty;
273+
}
274+
}

tests/compiler/dart2js/annotations/annotations_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'package:compiler/src/js_backend/annotations.dart';
1313
import 'package:compiler/src/js_model/element_map.dart';
1414
import 'package:compiler/src/js_model/js_world.dart';
1515
import 'package:compiler/src/util/enumset.dart';
16-
import 'package:compiler/src/util/features.dart';
16+
import 'package:front_end/src/testing/features.dart';
1717
import 'package:kernel/ast.dart' as ir;
1818
import '../equivalence/id_equivalence.dart';
1919
import '../equivalence/id_equivalence_helper.dart';

tests/compiler/dart2js/closure/closure_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import 'package:compiler/src/elements/entities.dart';
1212
import 'package:compiler/src/js_model/element_map.dart';
1313
import 'package:compiler/src/js_model/js_world.dart';
1414
import 'package:compiler/src/js_model/locals.dart';
15-
import 'package:compiler/src/util/features.dart';
1615
import 'package:compiler/src/world.dart';
1716
import 'package:expect/expect.dart';
18-
import '../equivalence/id_equivalence.dart';
19-
import '../equivalence/id_equivalence_helper.dart';
2017
import 'package:front_end/src/fasta/util/link.dart' show Link;
18+
import 'package:front_end/src/testing/features.dart';
2119
import 'package:kernel/ast.dart' as ir;
20+
import '../equivalence/id_equivalence.dart';
21+
import '../equivalence/id_equivalence_helper.dart';
2222

2323
main(List<String> args) {
2424
asyncTest(() async {

tests/compiler/dart2js/codegen/model_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'package:compiler/src/js_backend/namer.dart';
1515
import 'package:compiler/src/js_emitter/model.dart';
1616
import 'package:compiler/src/js_model/element_map.dart';
1717
import 'package:compiler/src/js_model/js_world.dart';
18-
import 'package:compiler/src/util/features.dart';
18+
import 'package:front_end/src/testing/features.dart';
1919
import 'package:js_ast/js_ast.dart' as js;
2020
import 'package:kernel/ast.dart' as ir;
2121
import '../equivalence/id_equivalence.dart';

tests/compiler/dart2js/equivalence/id_equivalence_helper.dart

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'package:compiler/src/common_elements.dart';
1111
import 'package:compiler/src/commandline_options.dart';
1212
import 'package:compiler/src/compiler.dart';
1313
import 'package:compiler/src/elements/entities.dart';
14-
import 'package:compiler/src/util/features.dart';
1514
import 'package:expect/expect.dart';
1615
import 'package:front_end/src/testing/id_testing.dart';
1716

@@ -480,109 +479,6 @@ Future<bool> runTestForConfiguration<T>(TestConfig testConfiguration,
480479
onFailure: Expect.fail);
481480
}
482481

483-
class FeaturesDataInterpreter implements DataInterpreter<Features> {
484-
const FeaturesDataInterpreter();
485-
486-
@override
487-
String isAsExpected(Features actualFeatures, String expectedData) {
488-
if (expectedData == '*') {
489-
return null;
490-
} else if (expectedData == '') {
491-
return actualFeatures.isNotEmpty ? "Expected empty data." : null;
492-
} else {
493-
List<String> errorsFound = [];
494-
Features expectedFeatures = Features.fromText(expectedData);
495-
Set<String> validatedFeatures = new Set<String>();
496-
expectedFeatures.forEach((String key, Object expectedValue) {
497-
bool expectMatch = true;
498-
if (key.startsWith('!')) {
499-
key = key.substring(1);
500-
expectMatch = false;
501-
}
502-
validatedFeatures.add(key);
503-
Object actualValue = actualFeatures[key];
504-
if (!expectMatch) {
505-
if (actualFeatures.containsKey(key)) {
506-
errorsFound.add('Unexpected data found for $key=$actualValue');
507-
}
508-
} else if (!actualFeatures.containsKey(key)) {
509-
errorsFound.add('No data found for $key');
510-
} else if (expectedValue == '') {
511-
if (actualValue != '') {
512-
errorsFound.add('Non-empty data found for $key');
513-
}
514-
} else if (expectedValue == '*') {
515-
return;
516-
} else if (expectedValue is List) {
517-
if (actualValue is List) {
518-
List actualList = actualValue.toList();
519-
for (Object expectedObject in expectedValue) {
520-
String expectedText = '$expectedObject';
521-
bool matchFound = false;
522-
if (expectedText.endsWith('*')) {
523-
// Wildcard matcher.
524-
String prefix =
525-
expectedText.substring(0, expectedText.indexOf('*'));
526-
List matches = [];
527-
for (Object actualObject in actualList) {
528-
if ('$actualObject'.startsWith(prefix)) {
529-
matches.add(actualObject);
530-
matchFound = true;
531-
}
532-
}
533-
for (Object match in matches) {
534-
actualList.remove(match);
535-
}
536-
} else {
537-
for (Object actualObject in actualList) {
538-
if (expectedText == '$actualObject') {
539-
actualList.remove(actualObject);
540-
matchFound = true;
541-
break;
542-
}
543-
}
544-
}
545-
if (!matchFound) {
546-
errorsFound.add("No match found for $key=[$expectedText]");
547-
}
548-
}
549-
if (actualList.isNotEmpty) {
550-
errorsFound
551-
.add("Extra data found $key=[${actualList.join(',')}]");
552-
}
553-
} else {
554-
errorsFound.add("List data expected for $key: "
555-
"expected '$expectedValue', found '${actualValue}'");
556-
}
557-
} else if (expectedValue != actualValue) {
558-
errorsFound.add(
559-
"Mismatch for $key: expected '$expectedValue', found '${actualValue}'");
560-
}
561-
});
562-
actualFeatures.forEach((String key, Object value) {
563-
if (!validatedFeatures.contains(key)) {
564-
if (value == '') {
565-
errorsFound.add("Extra data found '$key'");
566-
} else {
567-
errorsFound.add("Extra data found $key=$value");
568-
}
569-
}
570-
});
571-
return errorsFound.isNotEmpty ? errorsFound.join('\n ') : null;
572-
}
573-
}
574-
575-
@override
576-
String getText(Features actualData) {
577-
return actualData.getText();
578-
}
579-
580-
@override
581-
bool isEmpty(Features actualData) {
582-
return actualData == null || actualData.isEmpty;
583-
}
584-
}
585-
586482
/// Compute a [Spannable] from an [id] in the library [mainUri].
587483
Spannable computeSpannable(
588484
ElementEnvironment elementEnvironment, Uri mainUri, Id id) {

tests/compiler/dart2js/field_analysis/jfield_analysis_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:compiler/src/compiler.dart';
88
import 'package:compiler/src/elements/entities.dart';
99
import 'package:compiler/src/js_backend/field_analysis.dart';
1010
import 'package:compiler/src/js_model/js_world.dart';
11-
import 'package:compiler/src/util/features.dart';
11+
import 'package:front_end/src/testing/features.dart';
1212
import 'package:kernel/ast.dart' as ir;
1313
import '../equivalence/id_equivalence.dart';
1414
import '../equivalence/id_equivalence_helper.dart';

tests/compiler/dart2js/field_analysis/kfield_analysis_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:compiler/src/compiler.dart';
88
import 'package:compiler/src/elements/entities.dart';
99
import 'package:compiler/src/js_backend/field_analysis.dart';
1010
import 'package:compiler/src/kernel/kernel_strategy.dart';
11-
import 'package:compiler/src/util/features.dart';
11+
import 'package:front_end/src/testing/features.dart';
1212
import 'package:kernel/ast.dart' as ir;
1313
import '../equivalence/id_equivalence.dart';
1414
import '../equivalence/id_equivalence_helper.dart';

tests/compiler/dart2js/impact/impact_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:compiler/src/kernel/kernel_strategy.dart';
1111
import 'package:compiler/src/universe/feature.dart';
1212
import 'package:compiler/src/universe/use.dart';
1313
import 'package:compiler/src/universe/world_impact.dart';
14-
import 'package:compiler/src/util/features.dart';
14+
import 'package:front_end/src/testing/features.dart';
1515
import 'package:kernel/ast.dart' as ir;
1616
import '../equivalence/id_equivalence.dart';
1717
import '../equivalence/id_equivalence_helper.dart';

tests/compiler/dart2js/inference/inference_data_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:compiler/src/elements/entities.dart';
1212
import 'package:compiler/src/js_backend/inferred_data.dart';
1313
import 'package:compiler/src/js_model/element_map.dart';
1414
import 'package:compiler/src/js_model/js_world.dart';
15-
import 'package:compiler/src/util/features.dart';
15+
import 'package:front_end/src/testing/features.dart';
1616
import 'package:kernel/ast.dart' as ir;
1717
import '../equivalence/id_equivalence.dart';
1818
import '../equivalence/id_equivalence_helper.dart';

0 commit comments

Comments
 (0)