create StringValue only for primitive compile time values

This commit is contained in:
Natalia Ukhorskaya
2013-11-22 16:07:21 +04:00
parent 9593edc6e7
commit e6923ba29e
5 changed files with 142 additions and 3 deletions
@@ -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
}
}
+26
View File
@@ -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<Int>()}"
@@ -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<String> {
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(".+(?=:)")
}
}
@@ -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");
}
}
@@ -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<VariableDescriptor> properties = memberScope.getProperties(propertyName);