From 5683ee456fed1eaee1c32ec64ceb3e3b44550346 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 5 Jul 2012 17:13:59 +0400 Subject: [PATCH] TestGenerator -> JSTestGenerator, extract single test generation functionality to a separate class --- .../k2js/translate/general/Translation.java | 4 +- ...estGenerator.java => JSTestGenerator.java} | 27 ++++------ .../k2js/translate/test/JSTester.java | 50 +++++++++++++++++++ 3 files changed, 63 insertions(+), 18 deletions(-) rename js/js.translator/src/org/jetbrains/k2js/translate/test/{TestGenerator.java => JSTestGenerator.java} (69%) create mode 100644 js/js.translator/src/org/jetbrains/k2js/translate/test/JSTester.java diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java b/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java index e3a657f5859..4f8b441875f 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java @@ -44,7 +44,7 @@ import org.jetbrains.k2js.translate.expression.WhenTranslator; import org.jetbrains.k2js.translate.initializer.ClassInitializerTranslator; import org.jetbrains.k2js.translate.initializer.NamespaceInitializerTranslator; import org.jetbrains.k2js.translate.reference.CallBuilder; -import org.jetbrains.k2js.translate.test.TestGenerator; +import org.jetbrains.k2js.translate.test.JSTestGenerator; import org.jetbrains.k2js.translate.utils.JsAstUtils; import org.jetbrains.k2js.translate.utils.TranslationUtils; import org.jetbrains.k2js.translate.utils.dangerous.DangerousData; @@ -186,7 +186,7 @@ public final class Translation { statements.add(statement); } } - TestGenerator.generateTestCalls(context, files, rootBlock); + JSTestGenerator.generateTestCalls(context, files, rootBlock); performSimpleNameMangling(context.program()); return context.program(); } diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/test/TestGenerator.java b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTestGenerator.java similarity index 69% rename from js/js.translator/src/org/jetbrains/k2js/translate/test/TestGenerator.java rename to js/js.translator/src/org/jetbrains/k2js/translate/test/JSTestGenerator.java index f2bb3a18c79..1d901464e93 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/test/TestGenerator.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTestGenerator.java @@ -16,8 +16,10 @@ package org.jetbrains.k2js.translate.test; -import com.google.dart.compiler.backend.js.ast.*; -import com.google.dart.compiler.util.AstUtil; +import com.google.dart.compiler.backend.js.ast.JsBlock; +import com.google.dart.compiler.backend.js.ast.JsExpression; +import com.google.dart.compiler.backend.js.ast.JsNew; +import com.google.dart.compiler.backend.js.ast.JsStringLiteral; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; @@ -31,13 +33,11 @@ import org.jetbrains.k2js.translate.utils.JsDescriptorUtils; import java.util.Collection; import java.util.List; -import static com.google.dart.compiler.util.AstUtil.newBlock; - /** * @author Pavel Talanov */ -public final class TestGenerator { - private TestGenerator() { +public final class JSTestGenerator { + private JSTestGenerator() { } public static void generateTestCalls(@NotNull TranslationContext context, @@ -56,16 +56,11 @@ public final class TestGenerator { return; } JsExpression expression = ReferenceTranslator.translateAsFQReference(classDescriptor, context); - JsNew constructClassExpr = new JsNew(expression); - JsExpression functionToTestCall = - CallBuilder.build(context).descriptor(functionDescriptor).receiver(constructClassExpr).translate(); - JsNameRef qUnitTestFunRef = AstUtil.newQualifiedNameRef("QUnit.test"); - JsFunction functionToTest = new JsFunction(context.jsScope()); - functionToTest.setBody(newBlock(functionToTestCall.makeStmt())); - String testName = classDescriptor.getName() + "." + functionDescriptor.getName(); - block.getStatements() - .add(AstUtil.newInvocation(qUnitTestFunRef, context.program().getStringLiteral(testName), functionToTest) - .makeStmt()); + JsNew testClass = new JsNew(expression); + JsExpression functionToTestCall = CallBuilder.build(context).descriptor(functionDescriptor).receiver(testClass).translate(); + + JsStringLiteral testName = context.program().getStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName()); + (new JSTester(block, context)).constructTestMethodInvocation(functionToTestCall, testName); } } } diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTester.java b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTester.java new file mode 100644 index 00000000000..53623f93ca6 --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTester.java @@ -0,0 +1,50 @@ +/* + * Copyright 2010-2012 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.k2js.translate.test; + +import com.google.dart.compiler.backend.js.ast.*; +import com.google.dart.compiler.util.AstUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.k2js.translate.context.TranslationContext; + +import static com.google.dart.compiler.util.AstUtil.newBlock; +import static com.google.dart.compiler.util.AstUtil.newInvocation; + +/** + * @author Pavel Talanov + */ +public class JSTester { + + @NotNull + private final JsBlock block; + @NotNull + private final TranslationContext context; + @NotNull + private static final JsNameRef TEST_FUN_REF = AstUtil.newQualifiedNameRef("QUnit.test"); + + public JSTester(@NotNull JsBlock block, @NotNull TranslationContext context) { + this.block = block; + this.context = context; + } + + void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall, + @NotNull JsStringLiteral testName) { + JsFunction functionToTest = new JsFunction(this.context.jsScope()); + functionToTest.setBody(newBlock(functionToTestCall.makeStmt())); + block.getStatements().add(newInvocation(TEST_FUN_REF, testName, functionToTest).makeStmt()); + } +}