Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Unified Diff: tests/compiler/dart2js/field_type_inferer_test.dart

Issue 10964016: Change the type inference for fields in dart2js (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fixes and rebased Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tests/compiler/dart2js/field_type_inferer_test.dart
diff --git a/tests/compiler/dart2js/field_type_inferer_test.dart b/tests/compiler/dart2js/field_type_inferer_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..18de2d6bab1d9f90ada31a4c1d5baec5e14b1406
--- /dev/null
+++ b/tests/compiler/dart2js/field_type_inferer_test.dart
@@ -0,0 +1,166 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#import("dart:uri");
+
+#import("../../../lib/compiler/implementation/js_backend/js_backend.dart");
+#import("../../../lib/compiler/implementation/ssa/ssa.dart");
+#import("../../../lib/compiler/implementation/scanner/scannerlib.dart");
+
+#import('compiler_helper.dart');
+#import('parser_helper.dart');
+
+void compileAndFind(String code,
+ String className,
+ String memberName,
+ check(compiler, element)) {
+ Uri uri = new Uri.fromComponents(scheme: 'source');
+ var compiler = compilerFor(code, uri);
+ compiler.runCompiler(uri);
+ var cls = findElement(compiler, className);
+ var member = cls.lookupLocalMember(buildSourceString(memberName));
+ return check(compiler.backend, member);
+}
+
+const String TEST_1 = @"""
+ class A {
+ int f;
+ }
+ main() { new A(); }
+""";
+
+const String TEST_2 = @"""
+ class A {
+ int f1;
+ int f2 = 1;
+ }
+ main() { new A(); }
+""";
+
+const String TEST_3 = @"""
+ class A {
+ int f1;
+ int f2;
+ A() : f1 = 1;
+ }
+ main() { new A().f2 = 2; }
+""";
+
+const String TEST_4 = @"""
+ class A {
+ int f1;
+ int f2;
+ A() : f1 = 1;
+ }
+ main() {
+ A a = new A();
+ a.f1 = "a";
+ a.f2 = "a";
+ }
+""";
+
+const String TEST_5 = @"""
+ class A {
+ int f1 = 1;
+ int f2 = 1;
+ A(x) {
+ f1 = "1";
+ if (x) {
+ f2 = "1";
+ } else {
+ f2 = "2";
+ }
+ }
+ }
+ main() {
+ new A(true);
+ new A(false);
+ }
+""";
+
+const String TEST_6 = @"""
+ class A {
+ int f1 = 1;
+ int f2 = 1;
+ A(x) {
+ f1 = "1";
+ if (x) {
+ f2 = "1";
+ } else {
+ f2 = "2";
+ }
+ if (x) {
+ f2 = new List();
+ } else {
+ f2 = new List();
+ }
+ }
+ }
+ main() {
+ new A(true);
+ new A(false);
+ }
+""";
+
+const String TEST_7 = @"""
+ class A {
+ int f;
+ A(x) {
+ if (x) {
+ f = "1";
+ }
+ }
+ }
+ main() {
+ new A(true);
+ new A(false);
+ }
+""";
+
+const String TEST_8 = @"""
+ class A {
+ int f;
+ A() {
+ f = 1;
+ }
+ x() => f + 1;
+ }
+ void f(x) { x.f = "2"; }
+ main() {
+ A a;
+ f(a);
+ a = new A();
+ a.x();
+ }
+""";
+
+void runTest(String test, Map<String, HType> fields) {
+ fields.forEach((String name, HType type) {
+ compileAndFind(
+ test,
+ 'A',
+ name,
+ (backend, field) {
+ HType inferredType = backend.optimisticFieldType(field);
+ Expect.equals(type, inferredType);
+ });
+ });
+}
+
+void test() {
+ OptionalParameterTypes defaultTypes;
+
+ runTest(TEST_1, {'f': HType.NULL});
+ runTest(TEST_2, {'f1': HType.NULL, 'f2': HType.INTEGER});
+ runTest(TEST_3, {'f1': HType.INTEGER, 'f2': HType.INTEGER_OR_NULL});
+ runTest(TEST_4, {'f1': HType.UNKNOWN, 'f2': HType.STRING_OR_NULL});
+ runTest(TEST_5, {'f1': HType.STRING, 'f2': HType.STRING});
+ runTest(TEST_6, {'f1': HType.STRING, 'f2': HType.EXTENDABLE_ARRAY});
+ runTest(TEST_7, {'f': HType.UNKNOWN});
+ runTest(TEST_8, {'f': HType.UNKNOWN});
+}
+
+void main() {
+ test();
+}

Powered by Google App Engine
This is Rietveld 408576698