diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/jet/lang/evaluate/ConstantExpressionEvaluator.kt index c16999784fa..e6731e783f5 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/evaluate/ConstantExpressionEvaluator.kt @@ -340,7 +340,11 @@ private fun createStringConstant(value: CompileTimeConstant<*>?): StringValue? { return when (value) { null -> null is StringValue -> value - else -> StringValue(value.getValue().toString()) + is IntValue, is ByteValue, is ShortValue, is LongValue, + is CharValue, + is DoubleValue, is FloatValue, + is BooleanValue -> StringValue(value.getValue().toString()) + else -> null } } diff --git a/compiler/testData/evaluate/strings.kt b/compiler/testData/evaluate/strings.kt new file mode 100644 index 00000000000..5196a0f653b --- /dev/null +++ b/compiler/testData/evaluate/strings.kt @@ -0,0 +1,26 @@ +package test + +enum class MyEnum { + A +} + +// val prop1: "2" +val prop1 = "${1 + 1}" + +// val prop2: null +val prop2 = "myEnum=${MyEnum.A}" + +// val prop3: "1" +val prop3 = "${1}" + +// val prop4: null +val prop4 = "${null}" + +// val prop5: "1.0" +val prop5 = "${1.toFloat()}" + +// val prop6: "1.0" +val prop6 = "${1.0}" + +// val prop7: null +val prop7 = "${javaClass()}" diff --git a/compiler/tests/org/jetbrains/jet/evaluate/AbstractEvaluateExpressionTest.kt b/compiler/tests/org/jetbrains/jet/evaluate/AbstractEvaluateExpressionTest.kt new file mode 100644 index 00000000000..511899b20bc --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/evaluate/AbstractEvaluateExpressionTest.kt @@ -0,0 +1,65 @@ +/* + * Copyright 2010-2013 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.jet.evaluate + +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.resolve.annotation.AbstractAnnotationDescriptorResolveTest +import java.io.File +import com.intellij.openapi.util.io.FileUtil +import org.jetbrains.jet.lang.resolve.BindingContextUtils +import org.jetbrains.jet.lang.psi.JetProperty +import org.jetbrains.jet.InTextDirectivesUtils +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import java.util.regex.Pattern +import org.intellij.lang.annotations.RegExp +import com.intellij.openapi.util.text.StringUtil + +abstract class AbstractEvaluateExpressionTest: AbstractAnnotationDescriptorResolveTest() { + + // Test directives should look like [// val testedPropertyName: expectedValue] + fun doTest(path: String) { + val fileText = FileUtil.loadFile(File(path)) + val namespaceDescriptor = getNamespaceDescriptor(fileText) + + val propertiesForTest = getObjectsToTest(fileText) + + for (propertyName in propertiesForTest) { + val property = AbstractAnnotationDescriptorResolveTest.getPropertyDescriptor(namespaceDescriptor, propertyName) + val jetProperty = BindingContextUtils.descriptorToDeclaration(context!!, property) as JetProperty + val compileTimeConstant = context!!.get(BindingContext.COMPILE_TIME_VALUE, jetProperty.getInitializer()) + + val expected = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// val ${propertyName}: ") + assertNotNull(expected, "Failed to find expected directive: // val ${propertyName}: ") + assertEquals(expected, StringUtil.unquoteString(compileTimeConstant.toString()), "Failed for $propertyName") + } + } + + fun getObjectsToTest(fileText: String): List { + return InTextDirectivesUtils.findListWithPrefixes(fileText, "// val").map { + val matcher = pattern.matcher(it) + if (matcher.find()) { + matcher.group(0) ?: "Couldn't match tested object $it" + } + else "Couldn't match tested object $it" + } + } + + class object { + val pattern = Pattern.compile(".+(?=:)") + } +} diff --git a/compiler/tests/org/jetbrains/jet/evaluate/EvaluateExpressionTestGenerated.java b/compiler/tests/org/jetbrains/jet/evaluate/EvaluateExpressionTestGenerated.java new file mode 100644 index 00000000000..5efe33fa036 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/evaluate/EvaluateExpressionTestGenerated.java @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2013 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.jet.evaluate; + +import junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestSuite; + +import java.io.File; +import java.util.regex.Pattern; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.test.InnerTestClasses; +import org.jetbrains.jet.test.TestMetadata; + +import org.jetbrains.jet.evaluate.AbstractEvaluateExpressionTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("compiler/testData/evaluate") +public class EvaluateExpressionTestGenerated extends AbstractEvaluateExpressionTest { + public void testAllFilesPresentInEvaluate() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/evaluate"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("strings.kt") + public void testStrings() throws Exception { + doTest("compiler/testData/evaluate/strings.kt"); + } + +} diff --git a/compiler/tests/org/jetbrains/jet/resolve/annotation/AbstractAnnotationDescriptorResolveTest.java b/compiler/tests/org/jetbrains/jet/resolve/annotation/AbstractAnnotationDescriptorResolveTest.java index 7de3bfe6421..35653a2673a 100644 --- a/compiler/tests/org/jetbrains/jet/resolve/annotation/AbstractAnnotationDescriptorResolveTest.java +++ b/compiler/tests/org/jetbrains/jet/resolve/annotation/AbstractAnnotationDescriptorResolveTest.java @@ -46,7 +46,7 @@ public abstract class AbstractAnnotationDescriptorResolveTest extends JetLiteFix private static final FqName NAMESPACE = new FqName("test"); - private BindingContext context; + protected BindingContext context; @Override protected JetCoreEnvironment createEnvironment() { @@ -115,7 +115,7 @@ public abstract class AbstractAnnotationDescriptorResolveTest extends JetLiteFix } @NotNull - private static PropertyDescriptor getPropertyDescriptor(@NotNull NamespaceDescriptor namespaceDescriptor, @NotNull String name) { + protected static PropertyDescriptor getPropertyDescriptor(@NotNull NamespaceDescriptor namespaceDescriptor, @NotNull String name) { Name propertyName = Name.identifier(name); JetScope memberScope = namespaceDescriptor.getMemberScope(); Collection properties = memberScope.getProperties(propertyName);