| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package com.google.dart.compiler; | 5 package com.google.dart.compiler; |
| 6 | 6 |
| 7 import com.google.common.base.Joiner; | |
| 8 import com.google.dart.compiler.ast.DartUnit; | 7 import com.google.dart.compiler.ast.DartUnit; |
| 9 import com.google.dart.compiler.ast.LibraryUnit; | 8 import com.google.dart.compiler.ast.LibraryUnit; |
| 10 import com.google.dart.compiler.end2end.inc.MemoryLibrarySource; | |
| 11 import com.google.dart.compiler.resolver.ClassElement; | 9 import com.google.dart.compiler.resolver.ClassElement; |
| 12 import com.google.dart.compiler.resolver.Element; | 10 import com.google.dart.compiler.resolver.Element; |
| 13 import com.google.dart.compiler.resolver.LibraryElement; | 11 import com.google.dart.compiler.resolver.LibraryElement; |
| 14 import com.google.dart.compiler.resolver.MethodElement; | 12 import com.google.dart.compiler.resolver.MethodElement; |
| 15 import com.google.dart.compiler.testing.TestCompilerConfiguration; | 13 import com.google.dart.compiler.testing.TestCompilerConfiguration; |
| 16 import com.google.dart.compiler.testing.TestCompilerContext; | 14 import com.google.dart.compiler.testing.TestCompilerContext; |
| 17 import com.google.dart.compiler.testing.TestDartArtifactProvider; | 15 import com.google.dart.compiler.testing.TestDartArtifactProvider; |
| 16 import com.google.dart.compiler.testing.TestLibrarySource; |
| 18 import com.google.dart.compiler.util.DartSourceString; | 17 import com.google.dart.compiler.util.DartSourceString; |
| 19 | 18 |
| 20 import junit.framework.TestCase; | 19 import junit.framework.TestCase; |
| 21 | 20 |
| 22 import java.io.IOException; | 21 import java.io.IOException; |
| 23 | 22 |
| 24 public class DeltaAnalyzerTest extends TestCase { | 23 public class DeltaAnalyzerTest extends TestCase { |
| 25 private final TestCompilerConfiguration config = new TestCompilerConfiguration
(); | 24 private final TestCompilerConfiguration config = new TestCompilerConfiguration
(); |
| 26 private final DartCompilerListener listener = new TestCompilerContext(); | 25 private final DartCompilerListener listener = new TestCompilerContext(); |
| 27 private final DartArtifactProvider provider = new TestDartArtifactProvider(); | 26 private final DartArtifactProvider provider = new TestDartArtifactProvider(); |
| 28 | 27 |
| 29 public void testNoChangeSingleFile() throws IOException { | 28 public void testNoChangeSingleFile() throws IOException { |
| 30 MemoryLibrarySource librarySource = new MemoryLibrarySource("App.dart"); | 29 TestLibrarySource librarySource = new TestLibrarySource(getName()); |
| 31 librarySource.setContent("App.dart", "library App; part 'before.dart';"); | 30 librarySource.addSource("before.dart", |
| 32 librarySource.setContent("before.dart", | |
| 33 Joiner.on("\n").join(new String[] { | |
| 34 "part of App;", | |
| 35 "class Foo {}", | 31 "class Foo {}", |
| 36 "m() {}"})); | 32 "m() {}"); |
| 37 DartUnit change = analyzeNoChange(librarySource); | 33 DartUnit change = analyzeNoChange(librarySource); |
| 38 assertEquals(2, change.getTopLevelNodes().size()); | 34 assertEquals(2, change.getTopLevelNodes().size()); |
| 39 ClassElement cls = (ClassElement) change.getTopLevelNodes().get(0).getElemen
t(); | 35 ClassElement cls = (ClassElement) change.getTopLevelNodes().get(0).getElemen
t(); |
| 40 assertNotNull(cls); | 36 assertNotNull(cls); |
| 41 assertEquals("Foo", cls.getName()); | 37 assertEquals("Foo", cls.getName()); |
| 42 Element element = change.getLibrary().getElement().lookupLocalElement("Foo")
; | 38 Element element = change.getLibrary().getElement().lookupLocalElement("Foo")
; |
| 43 assertEquals(cls, element); | 39 assertEquals(cls, element); |
| 44 MethodElement method = (MethodElement) change.getTopLevelNodes().get(1).getE
lement(); | 40 MethodElement method = (MethodElement) change.getTopLevelNodes().get(1).getE
lement(); |
| 45 assertNotNull(method); | 41 assertNotNull(method); |
| 46 assertEquals("m", method.getName()); | 42 assertEquals("m", method.getName()); |
| 47 element = change.getLibrary().getElement().lookupLocalElement("m"); | 43 element = change.getLibrary().getElement().lookupLocalElement("m"); |
| 48 assertSame(method, element); | 44 assertSame(method, element); |
| 49 } | 45 } |
| 50 | 46 |
| 51 public void testNoChangeTwoFiles() throws IOException { | 47 public void testNoChangeTwoFiles() throws IOException { |
| 52 MemoryLibrarySource librarySource = new MemoryLibrarySource("App.dart"); | 48 TestLibrarySource librarySource = new TestLibrarySource(getName()); |
| 53 librarySource.setContent("App.dart", "library App; part 'before.dart'; part
'common.dart';"); | 49 librarySource.addSource("before.dart", |
| 54 librarySource.setContent("before.dart", | |
| 55 Joiner.on("\n").join(new String[] { | |
| 56 "part of App;", | |
| 57 "class Foo extends Bar {}", | 50 "class Foo extends Bar {}", |
| 58 "m() {}"})); | 51 "m() {}"); |
| 59 librarySource.setContent("common.dart", | 52 librarySource.addSource("common.dart", |
| 60 Joiner.on("\n").join(new String[] { | 53 "class Bar {}"); |
| 61 "part of App;", | |
| 62 "class Bar {}"})); | |
| 63 DartUnit change = analyzeNoChange(librarySource); | 54 DartUnit change = analyzeNoChange(librarySource); |
| 64 assertEquals(2, change.getTopLevelNodes().size()); | 55 assertEquals(2, change.getTopLevelNodes().size()); |
| 65 ClassElement cls = (ClassElement) change.getTopLevelNodes().get(0).getElemen
t(); | 56 ClassElement cls = (ClassElement) change.getTopLevelNodes().get(0).getElemen
t(); |
| 66 assertNotNull(cls); | 57 assertNotNull(cls); |
| 67 assertEquals("Foo", cls.getName()); | 58 assertEquals("Foo", cls.getName()); |
| 68 assertNotNull(change.getLibrary().getElement().lookupLocalElement("Foo")); | 59 assertNotNull(change.getLibrary().getElement().lookupLocalElement("Foo")); |
| 69 assertEquals("Bar", cls.getSupertype().toString()); | 60 assertEquals("Bar", cls.getSupertype().toString()); |
| 70 assertNotNull(change.getLibrary().getElement().lookupLocalElement("Bar")); | 61 assertNotNull(change.getLibrary().getElement().lookupLocalElement("Bar")); |
| 71 MethodElement method = (MethodElement) change.getTopLevelNodes().get(1).getE
lement(); | 62 MethodElement method = (MethodElement) change.getTopLevelNodes().get(1).getE
lement(); |
| 72 assertNotNull(method); | 63 assertNotNull(method); |
| 73 assertEquals("m", method.getName()); | 64 assertEquals("m", method.getName()); |
| 74 Element element = change.getLibrary().getElement().lookupLocalElement("m"); | 65 Element element = change.getLibrary().getElement().lookupLocalElement("m"); |
| 75 assertSame(method, element); | 66 assertSame(method, element); |
| 76 } | 67 } |
| 77 | 68 |
| 78 public void testChangeSingleFile() throws IOException { | 69 public void testChangeSingleFile() throws IOException { |
| 79 MemoryLibrarySource librarySource = new MemoryLibrarySource("App.dart"); | 70 TestLibrarySource librarySource = new TestLibrarySource(getName()); |
| 80 librarySource.setContent("App.dart", "library App;"); | 71 librarySource.addSource("before.dart", |
| 81 librarySource.setContent( | 72 "class Foo {}", |
| 82 "before.dart", | 73 "m() {}"); |
| 83 Joiner.on("\n").join(new String[] {"part of App;", "class Foo {}", "m()
{}"})); | |
| 84 DartSource sourceBefore = librarySource.getSourceFor("before.dart"); | 74 DartSource sourceBefore = librarySource.getSourceFor("before.dart"); |
| 85 DartSource sourceAfter = new DartSourceString("after.dart", Joiner.on("\n").
join( | 75 DartSource sourceAfter = new DartSourceString("after.dart", "class Foo {}"); |
| 86 new String[] {"part of App;", "class Foo {}", ""})); | |
| 87 DartUnit change = analyze(librarySource, sourceBefore, sourceAfter); | 76 DartUnit change = analyze(librarySource, sourceBefore, sourceAfter); |
| 88 assertEquals(1, change.getTopLevelNodes().size()); | 77 assertEquals(1, change.getTopLevelNodes().size()); |
| 89 Element element = change.getLibrary().getElement().lookupLocalElement("m"); | 78 Element element = change.getLibrary().getElement().lookupLocalElement("m"); |
| 90 assertNull(element); | 79 assertNull(element); |
| 91 element = change.getLibrary().getElement().lookupLocalElement("Foo"); | 80 element = change.getLibrary().getElement().lookupLocalElement("Foo"); |
| 92 assertNotNull(element); | 81 assertNotNull(element); |
| 93 ClassElement cls = (ClassElement) change.getTopLevelNodes().get(0).getElemen
t(); | 82 ClassElement cls = (ClassElement) change.getTopLevelNodes().get(0).getElemen
t(); |
| 94 assertEquals("Foo", cls.getName()); | 83 assertEquals("Foo", cls.getName()); |
| 95 assertSame(cls, element); | 84 assertSame(cls, element); |
| 96 } | 85 } |
| 97 | 86 |
| 98 public void testChangeTwoFiles() throws IOException { | 87 public void testChangeTwoFiles() throws IOException { |
| 99 MemoryLibrarySource librarySource = new MemoryLibrarySource("App.dart"); | 88 TestLibrarySource librarySource = new TestLibrarySource(getName()); |
| 100 librarySource.setContent("App.dart", "library App; part 'before.dart'; part
'common.dart';"); | 89 librarySource.addSource("before.dart", |
| 101 librarySource.setContent("before.dart", | |
| 102 Joiner.on("\n").join(new String[] { | |
| 103 "part of App;", | |
| 104 "class Foo extends Bar {}", | 90 "class Foo extends Bar {}", |
| 105 "m() {}"})); | 91 "m() {}"); |
| 106 librarySource.setContent("common.dart", | 92 librarySource.addSource("common.dart", |
| 107 Joiner.on("\n").join(new String[] { | 93 "class Bar {}"); |
| 108 "part of App;", | |
| 109 "class Bar {}"})); | |
| 110 DartSource sourceBefore = librarySource.getSourceFor("before.dart"); | 94 DartSource sourceBefore = librarySource.getSourceFor("before.dart"); |
| 111 DartSource sourceAfter = new DartSourceString("after.dart", "part of App; cl
ass Foo extends Bar {}"); | 95 DartSource sourceAfter = new DartSourceString("after.dart", "class Foo exten
ds Bar {}"); |
| 112 DartUnit change = analyze(librarySource, sourceBefore, sourceAfter); | 96 DartUnit change = analyze(librarySource, sourceBefore, sourceAfter); |
| 113 assertEquals(1, change.getTopLevelNodes().size()); | 97 assertEquals(1, change.getTopLevelNodes().size()); |
| 114 assertNull(change.getLibrary().getElement().lookupLocalElement("m")); | 98 assertNull(change.getLibrary().getElement().lookupLocalElement("m")); |
| 115 ClassElement cls = (ClassElement) change.getTopLevelNodes().get(0).getElemen
t(); | 99 ClassElement cls = (ClassElement) change.getTopLevelNodes().get(0).getElemen
t(); |
| 116 assertNotNull(cls); | 100 assertNotNull(cls); |
| 117 assertEquals("Foo", cls.getName()); | 101 assertEquals("Foo", cls.getName()); |
| 118 assertEquals("Bar", cls.getSupertype().toString()); | 102 assertEquals("Bar", cls.getSupertype().toString()); |
| 119 Element element = change.getLibrary().getElement().lookupLocalElement("Foo")
; | 103 Element element = change.getLibrary().getElement().lookupLocalElement("Foo")
; |
| 120 assertSame(cls, element); | 104 assertSame(cls, element); |
| 121 } | 105 } |
| 122 | 106 |
| 123 private DartUnit analyzeNoChange(LibrarySource librarySource) throws IOExcepti
on { | 107 private DartUnit analyzeNoChange(LibrarySource librarySource) throws IOExcepti
on { |
| 124 DartSource sourceBefore = librarySource.getSourceFor("before.dart"); | 108 DartSource sourceBefore = librarySource.getSourceFor("before.dart"); |
| 125 DartSource sourceAfter = sourceBefore; | 109 DartSource sourceAfter = sourceBefore; |
| 126 return analyze(librarySource, sourceBefore, sourceAfter); | 110 return analyze(librarySource, sourceBefore, sourceAfter); |
| 127 } | 111 } |
| 128 | 112 |
| 129 private DartUnit analyze(LibrarySource librarySource, DartSource sourceBefore, | 113 private DartUnit analyze(LibrarySource librarySource, DartSource sourceBefore, |
| 130 DartSource sourceAfter) throws IOException { | 114 DartSource sourceAfter) throws IOException { |
| 131 LibraryUnit libraryUnit = DartCompiler.analyzeLibrary(librarySource, null, | 115 LibraryUnit libraryUnit = DartCompiler.analyzeLibrary(librarySource, null, |
| 132 config, provider, list
ener); | 116 config, provider, list
ener); |
| 133 LibraryElement enclosingLibrary = libraryUnit.getElement(); | 117 LibraryElement enclosingLibrary = libraryUnit.getElement(); |
| 134 LibraryElement coreLibrary = libraryUnit.getImportedLibraries().iterator().n
ext().getElement(); | 118 LibraryElement coreLibrary = libraryUnit.getImportedLibraries().iterator().n
ext().getElement(); |
| 135 return (DartUnit) DartCompiler.analyzeDelta(SourceDelta.before(sourceBefore)
.after(sourceAfter), | 119 return (DartUnit) DartCompiler.analyzeDelta(SourceDelta.before(sourceBefore)
.after(sourceAfter), |
| 136 enclosingLibrary, coreLibrary, | 120 enclosingLibrary, coreLibrary, |
| 137 null, -1, -1, config, listener); | 121 null, -1, -1, config, listener); |
| 138 } | 122 } |
| 139 } | 123 } |
| OLD | NEW |