2 * Copyright 2000-2013 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ide.macro;
18 import com.intellij.openapi.actionSystem.CommonDataKeys;
19 import com.intellij.openapi.actionSystem.DataContext;
20 import com.intellij.openapi.actionSystem.PlatformDataKeys;
21 import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.util.text.StringUtil;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.testFramework.fixtures.CodeInsightFixtureTestCase;
27 import gnu.trove.THashMap;
32 public class MacroManagerTest extends CodeInsightFixtureTestCase {
33 private void doTest(String filePath, String str, String expected) throws Macro.ExecutionCancelledException {
34 PsiFile file = myFixture.addFileToProject(filePath, "");
35 String actual = MacroManager.getInstance().expandMacrosInString(str, false, getContext(file.getVirtualFile()));
36 assertEquals(expected, actual);
39 public DataContext getContext(VirtualFile file) {
40 Project project = myFixture.getProject();
41 Map<String, Object> dataId2data = new THashMap<>();
42 dataId2data.put(CommonDataKeys.PROJECT.getName(), project);
43 dataId2data.put(CommonDataKeys.VIRTUAL_FILE.getName(), file);
44 dataId2data.put(PlatformDataKeys.PROJECT_FILE_DIRECTORY.getName(), project.getBaseDir());
45 return SimpleDataContext.getSimpleContext(dataId2data, null);
48 public void testFileParentDirMacro() throws Throwable {
50 "foo/bar/baz/test.txt",
51 "ans: $FileParentDir(bar)$ ",
52 "ans: " + myFixture.getTempDirPath() + File.separator + "foo "
55 "foo/bar/baz/test2.txt",
56 "ans: $FileDirPathFromParent(bar)$ ",
60 "foo/bar/baz/test3.txt",
61 "ans: $FileDirPathFromParent(foo/bar)$ ",
65 "foo/bar/baz/test4.txt",
66 "ans: $FileDirPathFromParent(foo/bar/baz/)$ ",
71 public void testFileDirPathFromParentMacro_NoParentFound() throws Throwable {
72 PsiFile file = myFixture.addFileToProject("foo/bar/baz/test.txt", "");
73 String args = "ans: $FileDirPathFromParent(qqq/)$ ";
74 String actual = MacroManager.getInstance().expandMacrosInString(args, false, getContext(file.getVirtualFile()));
75 String expected = "ans: " + StringUtil.trimEnd(file.getVirtualFile().getParent().getPath(), "/") + "/ ";
76 assertEquals(expected, actual);