Add simple test infrastructure for JS DCE

Add test infrastructure that allows to test handwritten JS
to test various small aspects of DCE, as opposed to box tests
which test kotlin stdlib + kotlin generated examples
This commit is contained in:
Alexey Andreev
2017-04-27 13:00:26 +03:00
parent 522a56947c
commit e2fc808d83
6 changed files with 152 additions and 0 deletions
@@ -144,6 +144,7 @@ import org.jetbrains.kotlin.j2k.AbstractJavaToKotlinConverterSingleFileTest
import org.jetbrains.kotlin.jps.build.*
import org.jetbrains.kotlin.jps.build.android.AbstractAndroidJpsTestCase
import org.jetbrains.kotlin.jps.incremental.AbstractProtoComparisonTest
import org.jetbrains.kotlin.js.test.AbstractDceTest
import org.jetbrains.kotlin.js.test.semantics.*
import org.jetbrains.kotlin.jvm.compiler.*
import org.jetbrains.kotlin.jvm.runtime.AbstractJvm8RuntimeDescriptorLoaderTest
@@ -1351,6 +1352,10 @@ fun main(args: Array<String>) {
testClass<AbstractOutputPrefixPostfixTest> {
model("outputPrefixPostfix/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.JS)
}
testClass<AbstractDceTest> {
model("dce/", pattern = "(.+)\\.js", targetBackend = TargetBackend.JS)
}
}
testGroup("js/js.tests/test", "compiler/testData") {
@@ -0,0 +1,51 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.test
import junit.framework.TestCase
import org.jetbrains.kotlin.js.dce.DeadCodeElimination
import org.jetbrains.kotlin.js.dce.InputFile
import java.io.File
abstract class AbstractDceTest : TestCase() {
fun doTest(filePath: String) {
val file = File(filePath)
val fileContents = file.readText()
val inputFile = InputFile(filePath, File(pathToOutputDir, file.relativeTo(File(pathToTestDir)).path).path, "main")
val dceResult = DeadCodeElimination.run(setOf(inputFile), extractDeclarations(REQUEST_REACHABLE_PATTERN, fileContents)) {}
val reachableNodeStrings = dceResult.reachableNodes.map { it.toString().removePrefix("<unknown>.") }.toSet()
for (assertedDeclaration in extractDeclarations(ASSERT_REACHABLE_PATTERN, fileContents)) {
TestCase.assertTrue("Declaration $assertedDeclaration not reached", assertedDeclaration in reachableNodeStrings)
}
for (assertedDeclaration in extractDeclarations(ASSERT_UNREACHABLE_PATTERN, fileContents)) {
TestCase.assertTrue("Declaration $assertedDeclaration reached", assertedDeclaration !in reachableNodeStrings)
}
}
private fun extractDeclarations(regex: Regex, fileContents: String): Set<String> =
regex.findAll(fileContents).map { it.groupValues[1] }.toSet()
companion object {
private val ASSERT_REACHABLE_PATTERN = Regex("^ *// *ASSERT_REACHABLE: (.+) *$", RegexOption.MULTILINE)
private val ASSERT_UNREACHABLE_PATTERN = Regex("^ *// *ASSERT_UNREACHABLE: (.+) *$", RegexOption.MULTILINE)
private val REQUEST_REACHABLE_PATTERN = Regex("^ *// *REQUEST_REACHABLE: (.+) *$", RegexOption.MULTILINE)
private val pathToTestDir = "js/js.translator/testData/dce"
private val pathToOutputDir = "js/js.translator/testData/out/dce"
}
}
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.test;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("js/js.translator/testData/dce")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class DceTestGenerated extends AbstractDceTest {
public void testAllFilesPresentInDce() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("js/js.translator/testData/dce"), Pattern.compile("(.+)\\.js"), TargetBackend.JS, true);
}
@TestMetadata("amd.js")
public void testAmd() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/dce/amd.js");
doTest(fileName);
}
@TestMetadata("commonjs.js")
public void testCommonjs() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/dce/commonjs.js");
doTest(fileName);
}
@TestMetadata("cycle.js")
public void testCycle() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/dce/cycle.js");
doTest(fileName);
}
}
+19
View File
@@ -0,0 +1,19 @@
define(['exports'], function (exports) {
function foo() {
}
function bar() {
}
function ignore() {
}
bar();
exports.foo = foo;
exports.bar = bar;
exports.ignore = ignore;
});
// REQUEST_REACHABLE: main.foo
// ASSERT_REACHABLE: main.foo
// ASSERT_REACHABLE: main.bar
// ASSERT_UNREACHABLE: main.ignore
+17
View File
@@ -0,0 +1,17 @@
function foo() {
}
function bar() {
}
function ignore() {
}
bar();
module.exports.foo = foo;
module.exports.bar = bar;
module.exports.ignore = ignore;
// REQUEST_REACHABLE: main.foo
// ASSERT_REACHABLE: main.bar
// ASSERT_REACHABLE: main.foo
// ASSERT_UNREACHABLE: main.ignore
+4
View File
@@ -0,0 +1,4 @@
function A() {
}
A.prototype = Object.create(A);
A.prototype.constructor = A;