Converted PyStringFormatInspection tests into hightlighing-based tests

This commit is contained in:
Andrey Vlasovskikh
2015-12-15 21:39:40 +03:00
parent 4b543465bd
commit 8c5ea6ecc0
9 changed files with 192 additions and 345 deletions
@@ -0,0 +1,127 @@
'#%(language)s has %(#)03d quote types.' % {'language': "Python", "#": 2} #ok
'%d %s' % <warning descr="Too few arguments for format string">5</warning> #Too few arguments for format string
'Hello world' % <warning descr="Too many arguments for format string">25</warning> #Too many arguments for format string
"%(name)f(name)" % {'name': 23.2} #ok
"%()s" % {'': "name"} #ok
<warning descr="Format specifier character missing">'test%(name)'</warning> % {'name': 23} #There are no format specifier character
'work%*d' % (2, 34) #ok
<warning descr="Can't use '*' in formats when using a mapping">'work%(name)*d'</warning> % (12, 32) #Can't use '*' in formats when using a mapping
'%*.*d' % (2, 5, 5) #ok
'%*.*d' % (<warning descr="Too few arguments for format string">2, 4</warning>) #Too few arguments for format string
'%*.*d' % (<warning descr="Too many arguments for format string">2, 4, 5, 6</warning>) #Too many arguments for format string
<warning descr="Format specifier character missing">'%**d'</warning> % (2, 5) #There are no format specifier character
<warning descr="Too few mapping keys">'%(name1)s %(name2)s (name3) %s'</warning> % {'name1': 'a', 'name2': 'b', 'name3': 'c'} #Too few mapping keys
<warning descr="Too few mapping keys">'%(name1s'</warning> % {'name1': 'a'} #Too few mapping keys
'%%%(name)ld' % {'name': 12} #ok
"%(name)f(name)" % <warning descr="Format requires a mapping">23.2</warning> #Format requires a mapping
"%(name)f(name)" % (<warning descr="Format requires a mapping">23.2</warning>) #Format requires a mapping
'%d%d' % <warning descr="Format doesn't require a mapping">{'name1': 2, 'name2': 3}</warning> #Format doesn't require a mapping
'%12.2f' % 2.74 #ok
'Hello world' % () #ok
'Hello world' % [] #ok
'Hello world' % {} #ok
'%d%d' % ((5), (5)) #ok
"%(name)d %(name)d" % {"name": 43} #ok
"%(name)d" % {'a': 4, "name": 5} #ok
'%% name %(name)c' % <warning descr="Key 'name' has no following argument">{'a': 4}</warning> #Key 'name' has no following argument
'%d %u %f %F %s %r' % (2, 3, 4.1, 4.0, "name", "str") #ok
'%d %d %d' % (4, <warning descr="Unexpected type str">"a"</warning>, <warning descr="Unexpected type str">"b"</warning>) #Unexpected type
'%f %f %f' % (4, 5, <warning descr="Unexpected type str">"test"</warning>) #Unexpected type
'%d' % <warning descr="Unexpected type str">"name"</warning> #Unexpected type
m = {'language': "Python", "#": 2}
'#%(language)s has %(#)03d quote types.' % m #ok
i = "test"
'%(name)s' % {'name': i} #ok
'%s' % i #ok
'%f' % <warning descr="Unexpected type str">i</warning> #Unexpected type
'%f' % (2 * 3 + 5) #ok
s = "%s" % "a".upper() #ok
x = ['a', 'b', 'c']
print "%d: %s" % (len(x), ", ".join(x)) #ok
m = [1, 2, 3, 4, 5]
"%d" % m[0] #ok
"%d %s" % (m[0], m[4]) #ok
"%s" % m #ok
"%s" % m[1:3] #ok
"%d" % <warning descr="Unexpected type str">m[1:2]</warning> #ok
"%d" % <warning descr="Unexpected type str">m</warning> #Unexpected type
"%d" % <warning descr="Unexpected type str">[]</warning> #Unexpected type
def greet(all):
print "Hello %s" % ("World" if all else "Human") #ok
"%s" % [x + 1 for x in [1, 2, 3, 4]] #ok
"%s" % [x + y for x in []] #ok
"%s" % [] #ok
"%f" % <warning descr="Unexpected type str">[x + 1 for x in [1, 2, 3, 4]]</warning> #Unexpected type
"%d %d" % (3, 5) #ok
"Hello %s %s" % tuple(['world', '!']) #ok
def foo(a):
if a == 1:
return "a", "b"
else:
return "c", "d"
print "%s" % <warning descr="Too many arguments for format string">foo(1)</warning> #Too many arguments for format string
print("| [%(issue_id)s|http://youtrack.jetbrains.net/issue/%(issue_id)s] (%(issue_type)s)|%(summary)s|" % (<warning descr="Format requires a mapping">issue_id, issue_type, summary</warning>)) #Format requires a mapping (PY-704)
my_list = list()
for i in range(0,3):
my_list.append( ("hey", "you") )
for item in my_list:
print '%s %s' % item # ok (PY-734)
def bar():
return None
"%s %s" % <warning descr="Too few arguments for format string">bar()</warning> #Too few arguments for format string
"%s" % {} # ok; str() works
"%s" % {'a': 1, 'b': 2} # ok, no names in template and arg counts don't match
"%s" % object() # ok, str() works
"foo" % {'bar':1, 'baz':2} # ok: empty template that could use names
a = ('a', 1) if 1 else ('b', 2)
"%s is %d" % a # ok, must infer unified tuple type
#PY-3064, because original type of a is tuple, not list
a = (1,2,3)
print '%d:%d' % a[:2]
print '%d:%d' % <warning descr="Too few arguments for format string">a[1:2]</warning>
string = "qwerty"
print '%d:%d' % <warning descr="Too few arguments for format string"><warning descr="Unexpected type str">string[:2]</warning></warning>
print '%s:%s' % <warning descr="Too few arguments for format string">string[:2]</warning>
print '%s' % string[:2]
print '%d' % <warning descr="Unexpected type str">string[:2]</warning>
my_tuple = (1,2,3,4,5,6,7,8)
print '%d, %d' % <warning descr="Too many arguments for format string">my_tuple[:7:3]</warning>
print '%d, %d, %d' % my_tuple[:7:3]
print '%d, %d, %d, %d' % <warning descr="Too few arguments for format string">my_tuple[:7:3]</warning>
# PY-12801
print '%d %s' % ((42,) + ('spam',))
print '%d %s' % (<warning descr="Unexpected type str">('ham',) + ('spam',)</warning>)
print '%d %s' % (<warning descr="Too few arguments for format string">(42,) + ()</warning>)
print '%d' % (<warning descr="Too many arguments for format string">(42,) + ('spam',)</warning>)
# PY-11274
import collections
print '%(foo)s' % collections.OrderedDict(foo=None)
class MyDict(collections.Mapping):
def __getitem__(self, key):
return 'spam'
def __iter__(self):
yield 'spam'
def __len__(self):
return 1
print '%(foo)s' % MyDict()
foo = {1, 2, 3}
print('%s %s %s' % <warning descr="Too few arguments for format string">foo</warning>)
'%s %s %s' % <warning descr="Too few arguments for format string">(x for x in range(10))</warning>
@@ -0,0 +1,9 @@
my_dict = {'class': 3}
my_dict['css_class'] = ""
if my_dict['class']:
my_dict['css_class'] = 'class %(class)s' % my_dict
my_dict['tmp'] = 'classes %(css_class)s' % my_dict
my_dict['tmp'] = 'classes %(claz)s' % <warning descr="Key 'claz' has no following argument">my_dict</warning>
@@ -1,5 +1,5 @@
def foo(x):
return x
return x
artist = foo(1)
print('%s' % (artist.lower()[0:10]))
@@ -0,0 +1,5 @@
argument_pattern = re.compile(r'(%s)\s*(\(\s*(%s)\s*\)\s*)?$'
% ((states.Inliner.simplename,) * 2))
t, num = ('foo',), 2
res = '%d %d' % (<warning descr="Unexpected type str"><warning descr="Unexpected type str">t * num</warning></warning>)
@@ -1,189 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>string-format.py</file>
<line>2</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>3</line>
<description>Too many arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>6</line>
<description>Format specifier character missing</description>
</problem>
<problem>
<file>string-format.py</file>
<line>8</line>
<description>Can't use '*' in formats when using a mapping</description>
</problem>
<problem>
<file>string-format.py</file>
<line>10</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>11</line>
<description>Too many arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>12</line>
<description>Format specifier character missing</description>
</problem>
<problem>
<file>string-format.py</file>
<line>13</line>
<description>Too few mapping keys</description>
</problem>
<problem>
<file>string-format.py</file>
<line>14</line>
<description>Too few mapping keys</description>
</problem>
<problem>
<file>string-format.py</file>
<line>16</line>
<description>Format requires a mapping</description>
</problem>
<problem>
<file>string-format.py</file>
<line>17</line>
<description>Format requires a mapping</description>
</problem>
<problem>
<file>string-format.py</file>
<line>18</line>
<description>Format doesn't require a mapping</description>
</problem>
<problem>
<file>string-format.py</file>
<line>26</line>
<description>Key 'name' has no following argument</description>
</problem>
<problem>
<file>string-format.py</file>
<line>28</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>28</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>29</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>30</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>36</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>46</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>47</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>48</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>54</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>63</line>
<description>Too many arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>65</line>
<description>Format requires a mapping</description>
</problem>
<problem>
<file>string-format.py</file>
<line>76</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>88</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>91</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>91</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>92</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>94</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>97</line>
<description>Too many arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>99</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>103</line>
<description>Unexpected type</description>
</problem>
<problem>
<file>string-format.py</file>
<line>104</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>105</line>
<description>Too many arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>124</line>
<description>Too few arguments for format string</description>
</problem>
<problem>
<file>string-format.py</file>
<line>126</line>
<description>Too few arguments for format string</description>
</problem>
</problems>
@@ -1,126 +0,0 @@
'#%(language)s has %(#)03d quote types.' % {'language': "Python", "#": 2} #ok
'%d %s' % 5 #Too few arguments for format string
'Hello world' % 25 #Too many arguments for format string
"%(name)f(name)" % {'name': 23.2} #ok
"%()s" % {'': "name"} #ok
'test%(name)' % {'name': 23} #There are no format specifier character
'work%*d' % (2, 34) #ok
'work%(name)*d' % (12, 32) #Can't use '*' in formats when using a mapping
'%*.*d' % (2, 5, 5) #ok
'%*.*d' % (2, 4) #Too few arguments for format string
'%*.*d' % (2, 4, 5, 6) #Too many arguments for format string
'%**d' % (2, 5) #There are no format specifier character
'%(name1)s %(name2)s (name3) %s' % {'name1': 'a', 'name2': 'b', 'name3': 'c'} #Too few mapping keys
'%(name1s' % {'name1': 'a'} #Too few mapping keys
'%%%(name)ld' % {'name': 12} #ok
"%(name)f(name)" % 23.2 #Format requires a mapping
"%(name)f(name)" % (23.2) #Format requires a mapping
'%d%d' % {'name1': 2, 'name2': 3} #Format doesn't require a mapping
'%12.2f' % 2.74 #ok
'Hello world' % () #ok
'Hello world' % [] #ok
'Hello world' % {} #ok
'%d%d' % ((5), (5)) #ok
"%(name)d %(name)d" % {"name": 43} #ok
"%(name)d" % {'a': 4, "name": 5} #ok
'%% name %(name)c' % {'a': 4} #Key 'name' has no following argument
'%d %u %f %F %s %r' % (2, 3, 4.1, 4.0, "name", "str") #ok
'%d %d %d' % (4, "a", "b") #Unexpected type
'%f %f %f' % (4, 5, "test") #Unexpected type
'%d' % "name" #Unexpected type
m = {'language': "Python", "#": 2}
'#%(language)s has %(#)03d quote types.' % m #ok
i = "test"
'%(name)s' % {'name': i} #ok
'%s' % i #ok
'%f' % i #Unexpected type
'%f' % (2 * 3 + 5) #ok
s = "%s" % "a".upper() #ok
x = ['a', 'b', 'c']
print "%d: %s" % (len(x), ", ".join(x)) #ok
m = [1, 2, 3, 4, 5]
"%d" % m[0] #ok
"%d %s" % (m[0], m[4]) #ok
"%s" % m #ok
"%s" % m[1:3] #ok
"%d" % m[1:2] #ok
"%d" % m #Unexpected type
"%d" % [] #Unexpected type
def greet(all):
print "Hello %s" % ("World" if all else "Human") #ok
"%s" % [x + 1 for x in [1, 2, 3, 4]] #ok
"%s" % [x + y for x in []] #ok
"%s" % [] #ok
"%f" % [x + 1 for x in [1, 2, 3, 4]] #Unexpected type
"%d %d" % (3, 5) #ok
"Hello %s %s" % tuple(['world', '!']) #ok
def foo(a):
if a == 1:
return "a", "b"
else:
return "c", "d"
print "%s" % foo(1) #Too many arguments for format string
print("| [%(issue_id)s|http://youtrack.jetbrains.net/issue/%(issue_id)s] (%(issue_type)s)|%(summary)s|" % (issue_id, issue_type, summary)) #Format requires a mapping (PY-704)
my_list = list()
for i in range(0,3):
my_list.append( ("hey", "you") )
for item in my_list:
print '%s %s' % item # ok (PY-734)
def bar():
return None
"%s %s" % bar() #Too few arguments for format string
"%s" % {} # ok; str() works
"%s" % {'a': 1, 'b': 2} # ok, no names in template and arg counts don't match
"%s" % object() # ok, str() works
"foo" % {'bar':1, 'baz':2} # ok: empty template that could use names
a = ('a', 1) if 1 else ('b', 2)
"%s is %d" % a # ok, must infer unified tuple type
#PY-3064, because original type of a is tuple, not list
a = (1,2,3)
print '%d:%d' % a[:2]
print '%d:%d' % a[1:2]
string = "qwerty"
print '%d:%d' % string[:2]
print '%s:%s' % string[:2]
print '%s' % string[:2]
print '%d' % string[:2]
my_tuple = (1,2,3,4,5,6,7,8)
print '%d, %d' % my_tuple[:7:3]
print '%d, %d, %d' % my_tuple[:7:3]
print '%d, %d, %d, %d' % my_tuple[:7:3]
# PY-12801
print '%d %s' % ((42,) + ('spam',))
print '%d %s' % (('ham',) + ('spam',))
print '%d %s' % ((42,) + ())
print '%d' % ((42,) + ('spam',))
# PY-11274
import collections
print '%(foo)s' % collections.OrderedDict(foo=None)
class MyDict(collections.Mapping):
def __getitem__(self, key):
return 'spam'
def __iter__(self):
yield 'spam'
def __len__(self):
return 1
print '%(foo)s' % MyDict()
foo = {1, 2, 3}
print('%s %s %s' % foo)
'%s %s %s' % (x for x in range(10))
@@ -1,16 +0,0 @@
my_dict = {'class': 3}
my_dict['css_class'] = ""
if my_dict['class']:
my_dict['css_class'] = 'class %(class)s' % my_dict
my_dict['tmp'] = 'classes %(css_class)s' % my_dict
my_dict['tmp'] = 'classes %(claz)s' % <warning descr="Key 'claz' has no following argument">my_dict</warning>
#PY-4647
argument_pattern = re.compile(r'(%s)\s*(\(\s*(%s)\s*\)\s*)?$'
% ((states.Inliner.simplename,) * 2))
t, num = ('foo',), 2
res = '%d %d' % (<warning descr="Unexpected type str"><warning descr="Unexpected type str">t * num</warning></warning>)
@@ -70,11 +70,6 @@ public class PythonInspectionsTest extends PyTestCase {
doHighlightingTest(PyRedeclarationInspection.class);
}
public void testPyStringFormatInspection() {
LocalInspectionTool inspection = new PyStringFormatInspection();
doTest(getTestName(false), inspection);
}
public void testPyTrailingSemicolonInspection() {
LocalInspectionTool inspection = new PyTrailingSemicolonInspection();
doTest(getTestName(false), inspection);
@@ -287,14 +282,6 @@ public class PythonInspectionsTest extends PyTestCase {
doHighlightingTest(PyListCreationInspection.class);
}
public void testPyStringFormatInspection1() { //PY-2836
doHighlightingTest(PyStringFormatInspection.class);
}
public void testPyStringFormatInspectionSlice() { //PY-6756
doHighlightingTest(PyStringFormatInspection.class);
}
public void testPyUnnecessaryBackslashInspection() { //PY-2952
setLanguageLevel(LanguageLevel.PYTHON27);
doHighlightingTest(PyUnnecessaryBackslashInspection.class);
@@ -0,0 +1,50 @@
/*
* Copyright 2000-2015 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 com.jetbrains.python.inspections;
import com.jetbrains.python.fixtures.PyTestCase;
/**
* @author vlan
*/
public class PyStringFormatInspectionTest extends PyTestCase {
public static final String TEST_DIRECTORY = "inspections/PyStringFormatInspection/";
public void testBasic() {
doTest();
}
// PY-2836
public void testDictionaryArgument() {
doTest();
}
// PY-4647
public void testTupleMultiplication() {
doTest();
}
// PY-6756
public void testSlice() {
doTest();
}
private void doTest() {
myFixture.configureByFile(TEST_DIRECTORY + getTestName(false) + ".py");
myFixture.enableInspections(PyStringFormatInspection.class);
myFixture.checkHighlighting(true, false, true);
}
}