예제 #1
0
 def test_inline_non_assign_fails(self):
     src = 'CONSTANT1, CONSTANT2 = values'
     t = pasta.ast_parse(src, py_ver)
     with self.assertRaisesRegexp(
             inline.InlineError,
             '\'CONSTANT1\' is not declared in an assignment'):
         inline.inline_name(t, 'CONSTANT1', py_ver=py_ver)
예제 #2
0
  def test_inline_function_fails(self):
    src = 'def func(): pass\nfunc()\n'
    t = ast.parse(src)

    with self.assertRaisesRegexp(
        inline.InlineError,
        '\'func\' is not a constant; it has type %r' % ast.FunctionDef):
      inline.inline_name(t, 'func')
예제 #3
0
 def test_inline_non_constant_fails(self):
   src = textwrap.dedent('''\
       NOT_A_CONSTANT = "foo"
       NOT_A_CONSTANT += "bar"
       ''')
   t = ast.parse(src)
   with self.assertRaisesRegexp(inline.InlineError,
                                '\'NOT_A_CONSTANT\' is not a constant'):
     inline.inline_name(t, 'NOT_A_CONSTANT')
예제 #4
0
        def test_inline_function_fails(self):
            src = 'def func(): pass\nfunc()\n'
            t = pasta.ast_parse(src, py_ver)

            with self.assertRaisesRegexp(
                    inline.InlineError,
                    '\'func\' is not a constant; it has type %r' %
                (ast27.FunctionDef
                 if py_ver == 'PY27' else ast3.FunctionDef, )):
                inline.inline_name(t, 'func', py_ver=py_ver)
예제 #5
0
 def test_inline_non_constant_fails(self):
     src = textwrap.dedent("""\
   NOT_A_CONSTANT = "foo"
   NOT_A_CONSTANT += "bar"
   """)
     t = pasta.ast_parse(src, py_ver)
     with self.assertRaisesRegexp(
             inline.InlineError,
             '\'NOT_A_CONSTANT\' is not a constant'):
         inline.inline_name(t, 'NOT_A_CONSTANT', py_ver=py_ver)
예제 #6
0
 def test_inline_multiple_reads(self):
     src = textwrap.dedent("""\
   CONSTANT = "foo"
   def a(b=CONSTANT):
     return b == CONSTANT
   """)
     expected = textwrap.dedent("""\
   def a(b="foo"):
     return b == "foo"
   """)
     t = pasta.ast_parse(src, py_ver)
     inline.inline_name(t, 'CONSTANT', py_ver=py_ver)
     self.checkAstsEqual(t, pasta.ast_parse(expected, py_ver), py_ver)
예제 #7
0
 def test_inline_multiple_reads(self):
   src = textwrap.dedent('''\
       CONSTANT = "foo"
       def a(b=CONSTANT):
         return b == CONSTANT
       ''')
   expected = textwrap.dedent('''\
       def a(b="foo"):
         return b == "foo"
       ''')
   t = ast.parse(src)
   inline.inline_name(t, 'CONSTANT')
   self.checkAstsEqual(t, ast.parse(expected))
예제 #8
0
 def test_inline_conditional_fails(self):
     src = 'if define:\n  x = 1\na = x\n'
     t = pasta.ast_parse(src, py_ver)
     with self.assertRaisesRegexp(inline.InlineError,
                                  '\'x\' is not a top-level name'):
         inline.inline_name(t, 'x', py_ver=py_ver)
예제 #9
0
 def test_inline_multiple_targets(self):
     src = 'x = y = z = 1\na = x + y\n'
     t = pasta.ast_parse(src, py_ver)
     inline.inline_name(t, 'y', py_ver=py_ver)
     self.checkAstsEqual(
         t, pasta.ast_parse('x = z = 1\na = x + 1\n', py_ver), py_ver)
예제 #10
0
 def test_inline_simple(self):
     src = 'x = 1\na = x\n'
     t = pasta.ast_parse(src, py_ver)
     inline.inline_name(t, 'x', py_ver=py_ver)
     self.checkAstsEqual(t, pasta.ast_parse('a = 1\n', py_ver), py_ver)
예제 #11
0
 def test_inline_multiple_targets(self):
   src = 'x = y = z = 1\na = x + y\n'
   t = ast.parse(src)
   inline.inline_name(t, 'y')
   self.checkAstsEqual(t, ast.parse('x = z = 1\na = x + 1\n'))
예제 #12
0
 def test_inline_simple(self):
   src = 'x = 1\na = x\n'
   t = ast.parse(src)
   inline.inline_name(t, 'x')
   self.checkAstsEqual(t, ast.parse('a = 1\n'))