Chromium Code Reviews

Unified Diff: compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java

Issue 8322014: Adds static type checking to the foreach loop. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased past r505 (Array -> List in TATs) Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Index: compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java
diff --git a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java
index 1b54361a88e6570517e0768bcb26e08cd2249331..fd8f57095b833a53c749c3d6d23c7fd25d1cfda8 100644
--- a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java
+++ b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java
@@ -751,8 +751,48 @@ public class TypeAnalyzerTest extends TypeTestCase {
DartCompilerErrorCode.TYPE_NOT_ASSIGNMENT_COMPATIBLE);
analyzeFail("for (;'';) {}",
DartCompilerErrorCode.TYPE_NOT_ASSIGNMENT_COMPATIBLE);
+
+ // Foreach tests
+ analyze("{ List<String> strings = ['1','2','3']; for (String s in strings) {} }");
+ analyzeFail("{ List<int> ints = [1,2,3]; for (String s in ints) {} }",
+ DartCompilerErrorCode.TYPE_NOT_ASSIGNMENT_COMPATIBLE);
+ analyzeFail("for (String s in true) {}", DartCompilerErrorCode.INTERFACE_HAS_NO_METHOD_NAMED);
+ }
+
+ public void testForEachStatement() {
+ Map<String, ClassElement> invalidReturnType = loadSource(
+ "class A {",
+ " Iterator<int> iterator() {}",
+ "}",
+ "class B {",
+ " main() { for (int i in new A()) {}}",
+ "}");
+ analyzeClasses(invalidReturnType);
}
+ public void testForEachStatement_Negative1() {
+ Map<String, ClassElement> fieldNotMethod = loadSource(
+ "class A {",
+ " int iterator;",
+ "}",
+ "class B {",
+ " main() { for (int i in new A()) {}}",
+ "}");
+ analyzeClasses(fieldNotMethod, DartCompilerErrorCode.FOR_IN_WITH_ITERATOR_FIELD);
+ }
+
+ public void testForEachStatement_Negative2() {
+ Map<String, ClassElement> invalidReturnType = loadSource(
+ "class A {",
+ " int iterator() {}",
+ "}",
+ "class B {",
+ " main() { for (int i in new A()) {}}",
+ "}");
+ analyzeClasses(invalidReturnType, DartCompilerErrorCode.FOR_IN_WITH_INVALID_ITERATOR_RETURN_TYPE);
+ }
+
+
public void testIfStatement() {
analyze("if (true) {}");
analyze("if (null) {}");
@@ -1612,5 +1652,11 @@ public class TypeAnalyzerTest extends TypeTestCase {
public InterfaceType getIsolateType() {
throw new AssertionError();
}
+
+ @Override
+ public InterfaceType getIteratorType(Type elementType) {
+ InterfaceType iteratorType = iterElement.getType();
+ return iteratorType.subst(Arrays.asList(elementType), iterElement.getTypeParameters());
+ }
}
}

Powered by Google App Engine