Exemple #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)], [])])])
Exemple #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)], [])])
Exemple #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)],
     )])
Exemple #4
0
 def test_comparison(self):
     'Comparison parsing test'
     data = '1 < 2 + 3'
     self.assertEqual(parser.parse(data), [(
         'comparison',
         ['<'],
         [1, ('+', 2, 3)],
     )])
Exemple #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],
     )])
Exemple #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'),
     ])
Exemple #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'),
         ])
Exemple #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)
             ],
         )])
Exemple #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)]),
     ])
Exemple #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)
             ],
             []
         )])
Exemple #11
0
 def test_comparison(self):
     'Comparison parsing test'
     data = '1 < 2 + 3'
     self.assertEqual(
         parser.parse(data),
         [('comparison',
             ['<'],
             [
                 1,
                 ('+',
                     2,
                     3
                 )
             ],
         )])
Exemple #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)]),
         ])
Exemple #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', [])
         )])
Exemple #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
             ],
         )])
Exemple #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)
                     ],
                     []
                 )
             ]
         )])
Exemple #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', []))])