tests/test-check-code.t
author Adrian Buehlmann <adrian@cadifra.com>
Sun, 21 Nov 2010 11:52:27 +0100
changeset 13026 53391819f195
parent 12632 6c98107f787e
child 13077 6b8d2ee24ce2
permissions -rw-r--r--
check-code: catch Python 'is' comparing number or string literals The Python 'is' operator compares object identity, so it should definitely not be applied to string or number literals, which Python implementations are free to represent with a temporary object. This should catch the following kinds of bogus expressions (examples): x is 'foo' x is not 'foo' x is "bar" x is not "bar" x is 42 x is not 42 x is -36 x is not -36 As originally proposed by Martin Geisler, amended with catching negative numbers.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12632
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     1
  $ cat > correct.py <<EOF
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     2
  > def toto(arg1, arg2):
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     3
  >     del arg2
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     4
  >     return (5 + 6, 9)
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     5
  > EOF
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     6
  $ cat > wrong.py <<EOF
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     7
  > def toto( arg1, arg2):
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     8
  >     del(arg2)
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
     9
  >     return ( 5+6, 9)
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    10
  > EOF
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    11
  $ cat > quote.py <<EOF
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    12
  > # let's use quote in comments
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    13
  > (''' ( 4x5 )
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    14
  > but """\\''' and finally''',
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    15
  > """let's fool checkpatch""", '1+2',
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    16
  > '"""', 42+1, """and
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    17
  > ( 4-1 ) """, "( 1+1 )\" and ")
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    18
  > a, '\\\\\\\\', "\\\\\\" x-2", "c-1"
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    19
  > EOF
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    20
  $ cat > non-py24.py <<EOF
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    21
  > # Using builtins that does not exist in Python 2.4
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    22
  > if any():
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    23
  >     x = all()
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    24
  >     y = format(x)
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    25
  > 
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    26
  > # Do not complain about our own definition
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    27
  > def any(x):
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    28
  >     pass
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    29
  > EOF
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    30
  $ check_code="$TESTDIR"/../contrib/check-code.py
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    31
  $ "$check_code" ./wrong.py ./correct.py ./quote.py ./non-py24.py
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    32
  ./wrong.py:1:
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    33
   > def toto( arg1, arg2):
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    34
   gratuitous whitespace in () or []
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    35
  ./wrong.py:2:
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    36
   >     del(arg2)
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    37
   del isn't a function
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    38
  ./wrong.py:3:
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    39
   >     return ( 5+6, 9)
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    40
   missing whitespace in expression
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    41
   gratuitous whitespace in () or []
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    42
  ./quote.py:5:
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    43
   > '"""', 42+1, """and
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    44
   missing whitespace in expression
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    45
  ./non-py24.py:2:
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    46
   > if any():
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    47
   any/all/format not available in Python 2.4
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    48
  ./non-py24.py:3:
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    49
   >     x = all()
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    50
   any/all/format not available in Python 2.4
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    51
  ./non-py24.py:4:
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    52
   >     y = format(x)
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    53
   any/all/format not available in Python 2.4
6c98107f787e tests: unify test-check-code
Brodie Rao <brodie@bitheap.org>
parents: 11822
diff changeset
    54
  [1]
13026
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    55
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    56
  $ cat > is-op.py <<EOF
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    57
  > # is-operator comparing number or string literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    58
  > x = None
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    59
  > y = x is 'foo'
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    60
  > y = x is "foo"
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    61
  > y = x is 5346
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    62
  > y = x is -6
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    63
  > y = x is not 'foo'
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    64
  > y = x is not "foo"
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    65
  > y = x is not 5346
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    66
  > y = x is not -6
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    67
  > EOF
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    68
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    69
  $ "$check_code" ./is-op.py
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    70
  ./is-op.py:3:
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    71
   > y = x is 'foo'
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    72
   object comparison with literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    73
  ./is-op.py:4:
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    74
   > y = x is "foo"
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    75
   object comparison with literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    76
  ./is-op.py:5:
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    77
   > y = x is 5346
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    78
   object comparison with literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    79
  ./is-op.py:6:
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    80
   > y = x is -6
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    81
   object comparison with literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    82
  ./is-op.py:7:
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    83
   > y = x is not 'foo'
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    84
   object comparison with literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    85
  ./is-op.py:8:
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    86
   > y = x is not "foo"
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    87
   object comparison with literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    88
  ./is-op.py:9:
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    89
   > y = x is not 5346
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    90
   object comparison with literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    91
  ./is-op.py:10:
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    92
   > y = x is not -6
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    93
   object comparison with literal
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    94
  [1]
53391819f195 check-code: catch Python 'is' comparing number or string literals
Adrian Buehlmann <adrian@cadifra.com>
parents: 12632
diff changeset
    95