type inference for closures in groovy1.8
[idea/community.git] / plugins / groovy / test / org / jetbrains / plugins / groovy / util / TestUtils.java
1 /*
2  * Copyright 2000-2008 JetBrains s.r.o.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 package org.jetbrains.plugins.groovy.util;
17
18 import com.intellij.openapi.application.PluginPathManager;
19 import com.intellij.openapi.fileTypes.FileTypeManager;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.util.io.FileUtil;
22 import com.intellij.openapi.util.text.StringUtil;
23 import com.intellij.psi.PsiFile;
24 import com.intellij.psi.PsiFileFactory;
25 import com.intellij.util.IncorrectOperationException;
26 import com.intellij.util.LocalTimeCounter;
27 import org.junit.Assert;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35  * Utility class, that contains various methods for testing
36  *
37  * @author Ilya.Sergey
38  */
39 public abstract class TestUtils {
40   public static final String TEMP_FILE = "temp.groovy";
41   public static final String GSP_TEMP_FILE = "temp.gsp";
42   public static final String CARET_MARKER = "<caret>";
43   public static final String BEGIN_MARKER = "<begin>";
44   public static final String END_MARKER = "<end>";
45   public static final String GROOVY_JAR = "groovy-all.jar";
46   public static final String GROOVY_JAR_17 = "groovy-all-1.7.jar";
47   public static final String GROOVY_JAR_18 = "groovy-1.8.0-beta-2.jar";
48
49   public static String getMockJdkHome() {
50     return getAbsoluteTestDataPath() + "/mockJDK";
51   }
52
53   public static String getMockGroovyLibraryHome() {
54     return getAbsoluteTestDataPath() + "/mockGroovyLib";
55   }
56
57   public static String getMockGroovy1_7LibraryHome() {
58     return getAbsoluteTestDataPath() + "/mockGroovyLib1.7";
59   }
60
61   public static String getMockGroovy1_7LibraryName() {
62     return getMockGroovy1_7LibraryHome()+"/groovy-all-1.7.3.jar";
63   }
64
65   public static String getMockGroovy1_8LibraryHome() {
66     return getAbsoluteTestDataPath() + "/mockGroovyLib1.8";
67   }
68
69   public static String getMockGroovy1_8LibraryName() {
70     return getMockGroovy1_8LibraryHome()+"/"+GROOVY_JAR_18;
71   }
72
73   public static PsiFile createPseudoPhysicalGroovyFile(final Project project, final String text) throws IncorrectOperationException {
74     return createPseudoPhysicalFile(project, TEMP_FILE, text);
75   }
76
77
78   public static PsiFile createPseudoPhysicalFile(final Project project, final String fileName, final String text) throws IncorrectOperationException {
79     return PsiFileFactory.getInstance(project).createFileFromText(
80         fileName,
81         FileTypeManager.getInstance().getFileTypeByFileName(fileName),
82         text,
83         LocalTimeCounter.currentTime(),
84         true);
85   }
86
87   public static String getAbsoluteTestDataPath() {
88     return FileUtil.toSystemIndependentName(PluginPathManager.getPluginHomePath("groovy")) + "/testdata/";
89   }
90
91   public static String getTestDataPath() {
92     return FileUtil.toSystemIndependentName(PluginPathManager.getPluginHomePathRelative("groovy")) + "/testdata/";
93   }
94
95   public static String removeBeginMarker(String text) {
96     int index = text.indexOf(BEGIN_MARKER);
97     return text.substring(0, index) + text.substring(index + BEGIN_MARKER.length());
98   }
99
100   public static String removeEndMarker(String text) {
101     int index = text.indexOf(END_MARKER);
102     return text.substring(0, index) + text.substring(index + END_MARKER.length());
103   }
104
105   public static List<String> readInput(String filePath) {
106     String content;
107     try {
108       content = new String(FileUtil.loadFileText(new File(filePath)));
109     }
110     catch (IOException e) {
111       throw new RuntimeException(e);
112     }
113     Assert.assertNotNull(content);
114
115     List<String> input = new ArrayList<String>();
116
117     int separatorIndex;
118     content = StringUtil.replace(content, "\r", ""); // for MACs
119
120     // Adding input  before -----
121     while ((separatorIndex = content.indexOf("-----")) >= 0) {
122       input.add(content.substring(0, separatorIndex - 1));
123       content = content.substring(separatorIndex);
124       while (StringUtil.startsWithChar(content, '-')) {
125         content = content.substring(1);
126       }
127       if (StringUtil.startsWithChar(content, '\n')) {
128         content = content.substring(1);
129       }
130     }
131     // Result - after -----
132     if (content.endsWith("\n")) {
133       content = content.substring(0, content.length() - 1);
134     }
135     input.add(content);
136
137     Assert.assertTrue("No data found in source file", input.size() > 0);
138     Assert.assertNotNull("Test output points to null", input.size() > 1);
139
140     return input;
141   }
142 }