|
| 1 | +/* |
| 2 | + * Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Florin Patan |
| 3 | + * |
| 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 |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 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. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.goide.editor; |
| 18 | + |
| 19 | +import com.goide.GoCodeInsightFixtureTestCase; |
| 20 | +import com.intellij.codeInsight.highlighting.HighlightUsagesHandler; |
| 21 | +import com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase; |
| 22 | +import com.intellij.openapi.util.TextRange; |
| 23 | +import com.intellij.psi.PsiElement; |
| 24 | +import com.intellij.testFramework.LightProjectDescriptor; |
| 25 | +import org.jetbrains.annotations.NotNull; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | + |
| 32 | +public class GoExitPointsHighlightingTest extends GoCodeInsightFixtureTestCase { |
| 33 | + @Override |
| 34 | + protected void setUp() throws Exception { |
| 35 | + super.setUp(); |
| 36 | + setUpProjectSdk(); |
| 37 | + } |
| 38 | + |
| 39 | + protected LightProjectDescriptor getProjectDescriptor() { |
| 40 | + return createMockProjectDescriptor(); |
| 41 | + } |
| 42 | + |
| 43 | + public void testBasicExitPoints() { |
| 44 | + String text = "package main;\n" + |
| 45 | + "func bar(x int) int {\n" + |
| 46 | + " if (x < 10) {\n" + |
| 47 | + " retur<caret>n -1" + |
| 48 | + " }" + |
| 49 | + " return x\n" + |
| 50 | + "}"; |
| 51 | + doTest(text, "return -1", "return x"); |
| 52 | + } |
| 53 | + |
| 54 | + public void testCaretOnFuncWithReturnAndPanic() { |
| 55 | + String text = "package main;\n" + |
| 56 | + "fun<caret>c bar(x int) int {\n" + |
| 57 | + " if (9 < 10) {\n" + |
| 58 | + " return -1" + |
| 59 | + " }" + |
| 60 | + " panic(x)\n" + |
| 61 | + "}"; |
| 62 | + doTest(text, "func", "return -1", "panic(x)"); |
| 63 | + } |
| 64 | + |
| 65 | + public void testCaretOnFuncWithNoExitPoints() { |
| 66 | + String text = "package main\n" + |
| 67 | + "import \"fmt\"\n" + |
| 68 | + "f<caret>unc main() {\n" + |
| 69 | + " fmt.Println(\"Hello, world!\"\n" + |
| 70 | + "}"; |
| 71 | + doTest(text, "func"); |
| 72 | + } |
| 73 | + |
| 74 | + public void testCaretOnFuncOnType() { |
| 75 | + String text = "package main\n" + |
| 76 | + "\n" + |
| 77 | + "type Demo struct {}\n" + |
| 78 | + "\n" + |
| 79 | + "f<caret>unc (a Demo) demo() int {\n" + |
| 80 | + " return -1\n" + |
| 81 | + "}"; |
| 82 | + doTest(text, "func", "return -1"); |
| 83 | + } |
| 84 | + |
| 85 | + private void doTest(@NotNull String text, String... usages) { |
| 86 | + myFixture.configureByText("foo.go", text); |
| 87 | + @SuppressWarnings("unchecked") |
| 88 | + HighlightUsagesHandlerBase<PsiElement> handler = HighlightUsagesHandler.createCustomHandler(myFixture.getEditor(), myFixture.getFile()); |
| 89 | + assertNotNull(handler); |
| 90 | + List<PsiElement> targets = handler.getTargets(); |
| 91 | + assertEquals(1, targets.size()); |
| 92 | + |
| 93 | + handler.computeUsages(targets); |
| 94 | + List<TextRange> readUsages = handler.getReadUsages(); |
| 95 | + assertEquals(usages.length, readUsages.size()); |
| 96 | + |
| 97 | + List<String> textUsages = new ArrayList<String>(); |
| 98 | + for (TextRange usage : readUsages) { |
| 99 | + String usageText = myFixture.getFile().getText().substring(usage.getStartOffset(), usage.getEndOffset()); |
| 100 | + textUsages.add(usageText); |
| 101 | + } |
| 102 | + assertSameElements(textUsages, Arrays.asList(usages)); |
| 103 | + } |
| 104 | +} |
0 commit comments