示例#1
0
 def test_if_elif_block(self):
     'If-elif block chaining test'
     data = 'if a > 1:\n    f()\n    1+2\nelif b < 2:\n    3 + 4'
     self.assertEqual(
         parser.parse(data), [('if', ('comparison', ['>'], ['a', 1]), [
             ('func', 'f', [], []), ('+', 1, 2)
         ], [('if', ('comparison', ['<'], ['b', 2]), [('+', 3, 4)], [])])])
示例#2
0
 def test_if_block(self):
     'If block parsing test'
     data = 'if a > 1:\n    f()\n    1+2'
     self.assertEqual(
         parser.parse(data),
         [('if', ('comparison', ['>'], ['a', 1]), [('func', 'f', [], []),
                                                   ('+', 1, 2)], [])])
示例#3
0
 def test_while_block_2(self):
     'While block parsing test'
     data = 'while (a > 1):\n    f()\n    1+2'
     self.assertEqual(parser.parse(data), [(
         'while',
         ('comparison', ['>'], ['a', 1]),
         [('func', 'f', [], []), ('+', 1, 2)],
     )])
示例#4
0
 def test_comparison(self):
     'Comparison parsing test'
     data = '1 < 2 + 3'
     self.assertEqual(parser.parse(data), [(
         'comparison',
         ['<'],
         [1, ('+', 2, 3)],
     )])
示例#5
0
 def test_comparison_chain(self):
     'Comparison chain parsing test'
     data = '1 < 2 + 3 <= 4 * 5 > 2'
     self.assertEqual(parser.parse(data), [(
         'comparison',
         ['<', '<=', '>'],
         [1, ('+', 2, 3), ('*', 4, 5), 2],
     )])
示例#6
0
 def test_block(self):
     'Block parsing test'
     data = 'f(a)\nb=1+2\nc\nreturn d'
     self.assertEqual(parser.parse(data), [
         ('func', 'f', ['a'], []),
         ('assign', 'b', ('+', 1, 2)),
         'c',
         ('return', 'd'),
     ])
示例#7
0
 def test_block(self):
     'Block parsing test'
     data = 'f(a)\nb=1+2\nc\nreturn d'
     self.assertEqual(
         parser.parse(data),
         [
             ('func', 'f', ['a'], []),
             ('assign', 'b', ('+', 1, 2)),
             'c',
             ('return', 'd'),
         ])
示例#8
0
 def test_while_block_2(self):
     'While block parsing test'
     data = 'while (a > 1):\n    f()\n    1+2'
     self.assertEqual(
         parser.parse(data),
         [('while',
             ('comparison', ['>'], ['a', 1]),
             [
                 ('func', 'f', [], []),
                 ('+', 1, 2)
             ],
         )])
示例#9
0
 def test_funcparam(self):
     'Function parameters test'
     data = 'f()\nf(1)\nf(1,)\nf(1,2)\nf(a=1)\nf(a=1,)\nf(a=1,b=2)\nf(1,2,a=1,b=2)'
     self.assertEqual(parser.parse(data), [
         ('func', 'f', [], []),
         ('func', 'f', [1], []),
         ('func', 'f', [1], []),
         ('func', 'f', [1, 2], []),
         ('func', 'f', [], [('assign', 'a', 1)]),
         ('func', 'f', [], [('assign', 'a', 1)]),
         ('func', 'f', [], [('assign', 'a', 1), ('assign', 'b', 2)]),
         ('func', 'f', [1, 2], [('assign', 'a', 1), ('assign', 'b', 2)]),
     ])
示例#10
0
 def test_if_block(self):
     'If block parsing test'
     data = 'if a > 1:\n    f()\n    1+2'
     self.assertEqual(
         parser.parse(data),
         [('if',
             ('comparison', ['>'], ['a', 1]),
             [
                 ('func', 'f', [], []),
                 ('+', 1, 2)
             ],
             []
         )])
示例#11
0
 def test_comparison(self):
     'Comparison parsing test'
     data = '1 < 2 + 3'
     self.assertEqual(
         parser.parse(data),
         [('comparison',
             ['<'],
             [
                 1,
                 ('+',
                     2,
                     3
                 )
             ],
         )])
示例#12
0
 def test_funcparam(self):
     'Function parameters test'
     data = 'f()\nf(1)\nf(1,)\nf(1,2)\nf(a=1)\nf(a=1,)\nf(a=1,b=2)\nf(1,2,a=1,b=2)'
     self.assertEqual(
         parser.parse(data),
         [
             ('func', 'f', [], []),
             ('func', 'f', [1], []),
             ('func', 'f', [1], []),
             ('func', 'f', [1, 2], []),
             ('func', 'f', [], [('assign', 'a', 1)]),
             ('func', 'f', [], [('assign', 'a', 1)]),
             ('func', 'f', [], [('assign', 'a', 1), ('assign', 'b', 2)]),
             ('func', 'f', [1, 2], [('assign', 'a', 1), ('assign', 'b', 2)]),
         ])
示例#13
0
 def test_expression(self):
     'Expression parsing test'
     data = '1 + (2 + 3) * -4 + f(5) + ()'
     self.assertEqual(
         parser.parse(data),
         [('+',
             ('+',
                 ('+',
                     1,
                     ('*',
                         ('+', 2, 3),
                         ('neg', 4)
                     )
                 ),
                 ('func', 'f', [5], [])
             ),
             ('tuple', [])
         )])
示例#14
0
 def test_comparison_chain(self):
     'Comparison chain parsing test'
     data = '1 < 2 + 3 <= 4 * 5 > 2'
     self.assertEqual(
         parser.parse(data),
         [('comparison',
             ['<', '<=', '>'],
             [
                 1,
                 ('+',
                     2,
                     3
                 ),
                 ('*',
                     4,
                     5
                 ),
                 2
             ],
         )])
示例#15
0
 def test_if_elif_block(self):
     'If-elif block chaining test'
     data = 'if a > 1:\n    f()\n    1+2\nelif b < 2:\n    3 + 4'
     self.assertEqual(
         parser.parse(data),
         [('if',
             ('comparison', ['>'], ['a', 1]),
             [
                 ('func', 'f', [], []),
                 ('+', 1, 2)
             ],
             [
                 ('if',
                     ('comparison', ['<'], ['b', 2]),
                     [
                         ('+', 3, 4)
                     ],
                     []
                 )
             ]
         )])
示例#16
0
 def test_expression(self):
     'Expression parsing test'
     data = '1 + (2 + 3) * -4 + f(5) + ()'
     self.assertEqual(parser.parse(data),
                      [('+', ('+', ('+', 1, ('*', ('+', 2, 3), ('neg', 4))),
                              ('func', 'f', [5], [])), ('tuple', []))])