2 * Copyright 2000-2016 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.jetbrains.python;
18 import com.intellij.openapi.editor.colors.EditorColorsManager;
19 import com.intellij.openapi.editor.colors.EditorColorsScheme;
20 import com.intellij.openapi.editor.colors.TextAttributesKey;
21 import com.intellij.openapi.editor.markup.EffectType;
22 import com.intellij.openapi.editor.markup.TextAttributes;
23 import com.jetbrains.python.documentation.PyDocumentationSettings;
24 import com.jetbrains.python.documentation.docstrings.DocStringFormat;
25 import com.jetbrains.python.fixtures.PyTestCase;
26 import com.jetbrains.python.psi.LanguageLevel;
27 import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
32 * Test highlighting added by annotators.
36 public class PythonHighlightingTest extends PyTestCase {
37 private static final String TEST_PATH = "/highlighting/";
39 public void testBuiltins() {
40 EditorColorsManager manager = EditorColorsManager.getInstance();
41 EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
42 manager.addColorsScheme(scheme);
43 EditorColorsManager.getInstance().setGlobalScheme(scheme);
45 TextAttributesKey xKey;
46 TextAttributes xAttributes;
48 xKey = TextAttributesKey.find("PY.BUILTIN_NAME");
49 xAttributes = new TextAttributes(Color.green, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
50 scheme.setAttributes(xKey, xAttributes);
52 xKey = TextAttributesKey.find("PY.PREDEFINED_USAGE");
53 xAttributes = new TextAttributes(Color.yellow, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
54 scheme.setAttributes(xKey, xAttributes);
59 public void testDeclarations() {
60 EditorColorsManager manager = EditorColorsManager.getInstance();
61 EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
62 manager.addColorsScheme(scheme);
63 EditorColorsManager.getInstance().setGlobalScheme(scheme);
65 TextAttributesKey xKey = TextAttributesKey.find("PY.CLASS_DEFINITION");
66 TextAttributes xAttributes = new TextAttributes(Color.blue, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
67 scheme.setAttributes(xKey, xAttributes);
69 xKey = TextAttributesKey.find("PY.FUNC_DEFINITION");
70 xAttributes = new TextAttributes(Color.red, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
71 scheme.setAttributes(xKey, xAttributes);
73 xKey = TextAttributesKey.find("PY.PREDEFINED_DEFINITION");
74 xAttributes = new TextAttributes(Color.green, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
75 scheme.setAttributes(xKey, xAttributes);
80 public void testAssignmentTargets() {
81 setLanguageLevel(LanguageLevel.PYTHON26);
85 public void testAssignmentTargetWith() { // PY-7529
86 setLanguageLevel(LanguageLevel.PYTHON27);
90 public void testAssignmentTargets3K() {
91 doTest(LanguageLevel.PYTHON30, true, false);
94 public void testBreakOutsideOfLoop() {
98 public void testReturnOutsideOfFunction() {
102 public void testContinueInFinallyBlock() {
103 doTest(false, false);
106 public void testReturnWithArgumentsInGenerator() {
110 public void testYieldOutsideOfFunction() {
111 doTest(LanguageLevel.PYTHON27, true, true);
114 public void testYieldInDefaultValue() {
115 doTest(LanguageLevel.PYTHON34, true, false);
119 public void testYieldInLambda() {
123 public void testImportStarAtTopLevel() {
127 public void testMalformedStringUnterminated() {
131 public void testMalformedStringEscaped() {
132 doTest(false, false);
136 public void testStringEscapedOK() {
141 public void testStringMixedSeparatorsOK() { // PY-299
145 public void testStringBytesLiteralOK() {
146 doTest(LanguageLevel.PYTHON26, true, true);
149 public void testArgumentList() {
153 public void testRegularAfterVarArgs() {
154 doTest(LanguageLevel.PYTHON30, true, false);
157 public void testKeywordOnlyArguments() {
158 doTest(LanguageLevel.PYTHON30, true, false);
161 public void testMalformedStringTripleQuoteUnterminated() {
165 public void testMixedTripleQuotes() { // PY-2806
169 public void testOddNumberOfQuotes() { // PY-2802
173 public void testEscapedBackslash() { // PY-2994
177 public void testMultipleEscapedBackslashes() {
181 public void testUnsupportedFeaturesInPython3() {
182 doTest(LanguageLevel.PYTHON30, true, false);
186 public void testUnicode33() {
187 doTest(LanguageLevel.PYTHON33, true, false);
191 public void testYieldFromBefore33() {
192 doTest(LanguageLevel.PYTHON32, true, false);
195 public void testParenthesizedGenerator() {
196 doTest(false, false);
199 public void testStarInGenerator() { // PY-10177
200 doTest(LanguageLevel.PYTHON33, false, false);
203 public void testStarArgs() { // PY-6456
204 doTest(LanguageLevel.PYTHON32, true, false);
207 public void testDocstring() { // PY-8025
208 PyDocumentationSettings documentationSettings = PyDocumentationSettings.getInstance(myFixture.getModule());
209 documentationSettings.setFormat(DocStringFormat.REST);
214 documentationSettings.setFormat(DocStringFormat.PLAIN);
218 public void testYieldInNestedFunction() {
219 // highlight func declaration first, lest we get an "Extra fragment highlighted" error.
220 EditorColorsManager manager = EditorColorsManager.getInstance();
221 EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
222 manager.addColorsScheme(scheme);
223 EditorColorsManager.getInstance().setGlobalScheme(scheme);
225 TextAttributesKey xKey = TextAttributesKey.find("PY.FUNC_DEFINITION");
226 TextAttributes xAttributes = new TextAttributes(Color.red, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
227 scheme.setAttributes(xKey, xAttributes);
232 public void testAsync() {
233 doTest(LanguageLevel.PYTHON35, true, true);
236 public void testAwait() {
237 doTest(LanguageLevel.PYTHON35, true, true);
241 public void testAwaitInList() {
242 doTest(LanguageLevel.PYTHON35, true, false);
245 public void testAwaitInTuple() {
246 doTest(LanguageLevel.PYTHON35, true, false);
249 public void testAwaitInGenerator() {
250 doTest(LanguageLevel.PYTHON35, true, false);
253 public void testAwaitInSet() {
254 doTest(LanguageLevel.PYTHON35, true, false);
257 public void testAwaitInDict() {
258 doTest(LanguageLevel.PYTHON35, true, false);
261 public void testYieldInsideAsyncDef() {
262 doTest(LanguageLevel.PYTHON35, false, false);
265 public void testUnpackingStar() {
266 doTest(LanguageLevel.PYTHON35, false, false);
270 public void testMagicMethods() {
271 EditorColorsManager manager = EditorColorsManager.getInstance();
272 EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
273 manager.addColorsScheme(scheme);
274 EditorColorsManager.getInstance().setGlobalScheme(scheme);
276 TextAttributesKey xKey = TextAttributesKey.find("PY.PREDEFINED_DEFINITION");
277 TextAttributes xAttributes = new TextAttributes(Color.green, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
278 scheme.setAttributes(xKey, xAttributes);
284 public void testAsyncBuiltinMethods() {
285 doTest(LanguageLevel.PYTHON35, true, false);
288 public void testImplicitOctLongInteger() {
289 doTest(LanguageLevel.PYTHON35, true, false);
292 public void testUnderscoresInNumericLiterals() {
293 doTest(LanguageLevel.PYTHON35, true, false);
296 public void testVariableAnnotations() {
297 doTest(LanguageLevel.PYTHON35, true, false);
300 public void testIllegalVariableAnnotationTarget() {
301 doTest(LanguageLevel.PYTHON36, true, false);
305 private void doTest(final LanguageLevel languageLevel, final boolean checkWarnings, final boolean checkInfos) {
306 PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), languageLevel);
308 doTest(checkWarnings, checkInfos);
311 PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), null);
315 private void doTest() {
316 final String TEST_PATH = "/highlighting/";
317 myFixture.testHighlighting(true, true, false, TEST_PATH + getTestName(true) + PyNames.DOT_PY);
320 private void doTest(boolean checkWarnings, boolean checkInfos) {
321 myFixture.testHighlighting(checkWarnings, checkInfos, false, TEST_PATH + getTestName(true) + PyNames.DOT_PY);