diff options
author | David Shea <dshea@redhat.com> | 2014-07-08 09:04:39 -0400 |
---|---|---|
committer | David Shea <dshea@redhat.com> | 2014-07-08 09:04:39 -0400 |
commit | 110ad5ce0e67d042dacde86d5b3150b7b3064dfc (patch) | |
tree | e62d3e82b0fb6ae42bcd9ed915fa12ffab224188 | |
parent | bf5ee7f5b9f5258b8fc153aaf68ab5412ded336f (diff) | |
download | pylint-110ad5ce0e67d042dacde86d5b3150b7b3064dfc.tar.gz |
Rearrange the sequence and slice index tests for better clarity
-rw-r--r-- | test/input/func_invalid_sequence_index.py | 119 | ||||
-rw-r--r-- | test/input/func_invalid_slice_index.py | 6 | ||||
-rw-r--r-- | test/messages/func_invalid_sequence_index.txt | 32 | ||||
-rw-r--r-- | test/messages/func_invalid_slice_index.txt | 10 |
4 files changed, 90 insertions, 77 deletions
diff --git a/test/input/func_invalid_sequence_index.py b/test/input/func_invalid_sequence_index.py index 854eabe..b60e0b5 100644 --- a/test/input/func_invalid_sequence_index.py +++ b/test/input/func_invalid_sequence_index.py @@ -7,35 +7,60 @@ TESTLIST = [1, 2, 3] TESTTUPLE = (1, 2, 3) TESTSTR = '123' +# getitem tests with bad indices def function1(): """list index is a function""" return TESTLIST[id] def function2(): - """list index is a str constant""" - return TESTLIST['0'] - -def function3(): """list index is None""" return TESTLIST[None] -def function4(): +def function3(): """list index is a float expression""" return TESTLIST[float(0)] +def function4(): + """list index is a str constant""" + return TESTLIST['0'] + def function5(): + """list index does not implement __index__""" + class NonIndexType(object): + """Class without __index__ method""" + pass + + return TESTLIST[NonIndexType()] + +def function6(): + """Tuple index is None""" + return TESTTUPLE[None] + +def function7(): + """String index is None""" + return TESTSTR[None] + +def function8(): + """Index of subclass of tuple is None""" + class TupleTest(tuple): + """Subclass of tuple""" + pass + return TupleTest()[None] + +# getitem tests with good indices +def function9(): """list index is an int constant""" return TESTLIST[0] # no error -def function6(): +def function10(): """list index is a integer expression""" return TESTLIST[int(0.0)] # no error -def function7(): +def function11(): """list index is a slice""" return TESTLIST[slice(1, 2, 3)] # no error -def function8(): +def function12(): """list index implements __index__""" class IndexType(object): """Class with __index__ method""" @@ -45,7 +70,7 @@ def function8(): return TESTLIST[IndexType()] # no error -def function9(): +def function13(): """list index implements __index__ in a superclass""" class IndexType(object): """Class with __index__ method""" @@ -59,38 +84,14 @@ def function9(): return TESTLIST[IndexSubType()] # no error -def function10(): - """list index does not implement __index__""" - class NonIndexType(object): - """Class without __index__ method""" - pass - - return TESTLIST[NonIndexType()] - -# Repeat a handful of tests to ensure non-list types are caught -def function11(): - """Tuple index is None""" - return TESTTUPLE[None] - -def function12(): +def function14(): """Tuple index is an int constant""" return TESTTUPLE[0] -def function13(): - """String index is None""" - return TESTSTR[None] - -def function14(): +def function15(): """String index is an int constant""" return TESTSTR[0] -def function15(): - """Index of subclass of tuple is None""" - class TupleTest(tuple): - """Subclass of tuple""" - pass - return TupleTest()[None] - def function16(): """Index of subclass of tuple is an int constant""" class TupleTest(tuple): @@ -122,7 +123,6 @@ def function18(): return SubTupleTest()[None] # no error # Test with set and delete statements - def function19(): """Set with None and integer indices""" TESTLIST[None] = 0 @@ -139,9 +139,13 @@ def function21(): """Inherit all list get/set/del handlers""" pass test = ListTest() + + # Set and delete with invalid indices test[None] = 0 - test[0] = 0 # no error del test[None] + + # Set and delete with valid indices + test[0] = 0 # no error del test[0] # no error def function22(): @@ -151,12 +155,14 @@ def function22(): def __setitem__(self, key, value): pass test = ListTest() - test[None][0] = 0 - test[0][0] = 0 # no error - test[None] = 0 # no error - test[0] = 0 # no error + + test[None][0] = 0 # failure on the getitem with None del test[None] - del test[0] # no error + + test[0][0] = 0 # getitem with int and setitem with int, no error + test[None] = 0 # setitem overridden, no error + test[0] = 0 # setitem with int, no error + del test[0] # delitem with int, no error def function23(): """Get, set, and delete on a subclass of list that overrides __delitem__""" @@ -165,12 +171,14 @@ def function23(): def __delitem__(self, key): pass test = ListTest() - test[None][0] = 0 - test[0][0] = 0 # no error - test[None] = 0 - test[0] = 0 # no error - del test[None] # no error - del test[0] # no error + + test[None][0] = 0 # failure on the getitem with None + test[None] = 0 # setitem with invalid index + + test[0][0] = 0 # getitem with int and setitem with int, no error + test[0] = 0 # setitem with int, no error + del test[None] # delitem overriden, no error + del test[0] # delitem with int, no error def function24(): """Get, set, and delete on a subclass of list that overrides __getitem__""" @@ -179,13 +187,16 @@ def function24(): def __getitem__(self, key): pass test = ListTest() - test[None][0] = 0 # no error - test[0][0] = 0 # no error - test[None] = 0 - test[0] = 0 # no error - del test[None] - del test[0] # no error + test[None] = 0 # setitem with invalid index + del test[None] # delitem with invalid index + + test[None][0] = 0 # getitem overriden, no error + test[0][0] = 0 # getitem with int and setitem with int, no error + test[0] = 0 # setitem with int, no error + del test[0] # delitem with int, no error + +# Teest ExtSlice usage def function25(): """Extended slice used with a list""" return TESTLIST[..., 0] diff --git a/test/input/func_invalid_slice_index.py b/test/input/func_invalid_slice_index.py index fa72b8c..32f2f2d 100644 --- a/test/input/func_invalid_slice_index.py +++ b/test/input/func_invalid_slice_index.py @@ -5,6 +5,7 @@ __revision__ = 0 TESTLIST = [1, 2, 3] +# Invalid indices def function1(): """functions used as indices""" return TESTLIST[id:id:] @@ -22,13 +23,14 @@ def function3(): return TESTLIST[NoIndexTest()::] +# Valid indices def function4(): """integers used as indices""" return TESTLIST[0:0:0] # no error def function5(): """None used as indices""" - return TESTLIST[None:None:None] + return TESTLIST[None:None:None] # no error def function6(): """class with __index__ used as index""" @@ -56,4 +58,4 @@ def function7(): def function8(): """slice object used as index""" - return TESTLIST[slice(1, 2, 3)] + return TESTLIST[slice(1, 2, 3)] # no error diff --git a/test/messages/func_invalid_sequence_index.txt b/test/messages/func_invalid_sequence_index.txt index a9b172a..db9edab 100644 --- a/test/messages/func_invalid_sequence_index.txt +++ b/test/messages/func_invalid_sequence_index.txt @@ -1,19 +1,19 @@ -E: 12:function1: Sequence index is not an int, slice, or instance with __index__ -E: 16:function2: Sequence index is not an int, slice, or instance with __index__ -E: 20:function3: Sequence index is not an int, slice, or instance with __index__ -E: 24:function4: Sequence index is not an int, slice, or instance with __index__ -E: 68:function10: Sequence index is not an int, slice, or instance with __index__ -E: 73:function11: Sequence index is not an int, slice, or instance with __index__ -E: 81:function13: Sequence index is not an int, slice, or instance with __index__ -E: 92:function15: Sequence index is not an int, slice, or instance with __index__ +E: 13:function1: Sequence index is not an int, slice, or instance with __index__ +E: 17:function2: Sequence index is not an int, slice, or instance with __index__ +E: 21:function3: Sequence index is not an int, slice, or instance with __index__ +E: 25:function4: Sequence index is not an int, slice, or instance with __index__ +E: 33:function5: Sequence index is not an int, slice, or instance with __index__ +E: 37:function6: Sequence index is not an int, slice, or instance with __index__ +E: 41:function7: Sequence index is not an int, slice, or instance with __index__ +E: 48:function8: Sequence index is not an int, slice, or instance with __index__ E:128:function19: Sequence index is not an int, slice, or instance with __index__ E:133:function20: Sequence index is not an int, slice, or instance with __index__ -E:142:function21: Sequence index is not an int, slice, or instance with __index__ E:144:function21: Sequence index is not an int, slice, or instance with __index__ -E:154:function22: Sequence index is not an int, slice, or instance with __index__ -E:158:function22: Sequence index is not an int, slice, or instance with __index__ -E:168:function23: Sequence index is not an int, slice, or instance with __index__ -E:170:function23: Sequence index is not an int, slice, or instance with __index__ -E:184:function24: Sequence index is not an int, slice, or instance with __index__ -E:186:function24: Sequence index is not an int, slice, or instance with __index__ -E:191:function25: Sequence index is not an int, slice, or instance with __index__ +E:145:function21: Sequence index is not an int, slice, or instance with __index__ +E:159:function22: Sequence index is not an int, slice, or instance with __index__ +E:160:function22: Sequence index is not an int, slice, or instance with __index__ +E:175:function23: Sequence index is not an int, slice, or instance with __index__ +E:176:function23: Sequence index is not an int, slice, or instance with __index__ +E:191:function24: Sequence index is not an int, slice, or instance with __index__ +E:192:function24: Sequence index is not an int, slice, or instance with __index__ +E:202:function25: Sequence index is not an int, slice, or instance with __index__ diff --git a/test/messages/func_invalid_slice_index.txt b/test/messages/func_invalid_slice_index.txt index d6882b1..d5b9e86 100644 --- a/test/messages/func_invalid_slice_index.txt +++ b/test/messages/func_invalid_slice_index.txt @@ -1,5 +1,5 @@ -E: 10:function1: Slice index is not an int, None, or instance with __index__ -E: 10:function1: Slice index is not an int, None, or instance with __index__ -E: 14:function2: Slice index is not an int, None, or instance with __index__ -E: 14:function2: Slice index is not an int, None, or instance with __index__ -E: 23:function3: Slice index is not an int, None, or instance with __index__ +E: 11:function1: Slice index is not an int, None, or instance with __index__ +E: 11:function1: Slice index is not an int, None, or instance with __index__ +E: 15:function2: Slice index is not an int, None, or instance with __index__ +E: 15:function2: Slice index is not an int, None, or instance with __index__ +E: 24:function3: Slice index is not an int, None, or instance with __index__ |