Esempio n. 1
0
    def test_while(self):
        self.assert_stmt('while(foo) return;',
                         model.While(foo, body=model.Return()))
        self.assert_stmt('while(foo) { return; }',
                         model.While(foo, body=model.Block([model.Return()])))

        self.assert_stmt('do return; while(foo);',
                         model.DoWhile(foo, body=model.Return()))
        self.assert_stmt(
            'do { return; } while(foo);',
            model.DoWhile(foo, body=model.Block([model.Return()])))
Esempio n. 2
0
    def test_if(self):
        self.assert_stmt('if(foo) return;', model.IfThenElse(foo, if_true=model.Return()))
        self.assert_stmt('if(foo) { return; }', model.IfThenElse(foo, if_true=model.Block([model.Return()])))

        r1 = model.Return(result=one)
        r2 = model.Return(result=two)
        self.assert_stmt('if(foo) return 1; else return 2;', model.IfThenElse(foo, if_true=r1, if_false=r2))
        self.assert_stmt('if(foo) {return 1;} else return 2;',
                         model.IfThenElse(foo, if_true=model.Block([r1]), if_false=r2))
        self.assert_stmt('if(foo) return 1; else {return 2;}',
                         model.IfThenElse(foo, if_true=r1, if_false=model.Block([r2])))
        self.assert_stmt('if(foo) {return 1;} else {return 2;}',
                         model.IfThenElse(foo, if_true=model.Block([r1]), if_false=model.Block([r2])))

        # test proper nesting, every parser writer's favorite
        self.assert_stmt('if(foo) if(bar) return 1;', model.IfThenElse(foo, if_true=model.IfThenElse(bar, if_true=r1)))
        self.assert_stmt('if(foo) if(bar) return 1; else return 2;',
                         model.IfThenElse(foo, if_true=model.IfThenElse(bar, if_true=r1, if_false=r2)))
        self.assert_stmt('if(foo) return 1; else if(bar) return 2;',
                         model.IfThenElse(foo, if_true=r1, if_false=model.IfThenElse(bar, if_true=r2)))
Esempio n. 3
0
    def test_for(self):
        initializer = model.VariableDeclaration(
            'int',
            [model.VariableDeclarator(model.Variable('i'), initializer=zero)])
        predicate = model.BinaryExpression('<', i, ten)
        update = model.Unary('x++', i)

        self.assert_stmt('for(;;);',
                         model.For(None, None, None, body=model.Empty()))
        self.assert_stmt('for(;;) return;',
                         model.For(None, None, None, body=model.Return()))
        self.assert_stmt(
            'for(;;) { return; }',
            model.For(None, None, None, body=model.Block([model.Return()])))
        self.assert_stmt(
            'for(int i=0;;) return;',
            model.For(initializer, None, None, body=model.Return()))
        self.assert_stmt('for(;i<10;) return;',
                         model.For(None, predicate, None, body=model.Return()))
        self.assert_stmt('for(;;i++) return;',
                         model.For(None, None, [update], body=model.Return()))
        self.assert_stmt(
            'for(int i=0; i<10; i++) return;',
            model.For(initializer, predicate, [update], body=model.Return()))

        initializer2 = [
            model.Assignment('=', i, zero),
            model.Assignment('=', j, ten)
        ]
        self.assert_stmt(
            'for(i=0, j=10;;) return;',
            model.For(initializer2, None, None, body=model.Return()))

        update2 = model.Unary('x++', j)
        self.assert_stmt(
            'for(;;i++, j++) return;',
            model.For(None, None, [update, update2], body=model.Return()))

        self.assert_stmt(
            'for(int i : foo) return;',
            model.ForEach('int', model.Variable('i'), foo,
                          body=model.Return()))
Esempio n. 4
0
    def test_try(self):
        r1 = model.Return(one)
        r2 = model.Return(two)
        r3 = model.Return(three)
        c1 = model.Catch(model.Variable('e'),
                         types=[model.Type(model.Name('Exception'))],
                         block=model.Block([r2]))
        self.assert_stmt('try { return 1; } catch (Exception e) { return 2; }',
                         model.Try(model.Block([r1]), catches=[c1]))
        self.assert_stmt(
            'try { return 1; } catch (Exception e) { return 2; } finally { return 3; }',
            model.Try(model.Block([r1]),
                      catches=[c1],
                      _finally=model.Block([r3])))
        self.assert_stmt(
            'try { return 1; } finally { return 2; }',
            model.Try(model.Block([r1]), _finally=model.Block([r2])))

        c2 = model.Catch(model.Variable('e'),
                         types=[
                             model.Type(model.Name('Exception1')),
                             model.Type(model.Name('Exception2'))
                         ],
                         block=model.Block([r3]))
        self.assert_stmt(
            'try { return 1; } catch (Exception1 | Exception2 e) { return 3; }',
            model.Try(model.Block([r1]), catches=[c2]))
        self.assert_stmt(
            'try { return 1; } catch (Exception e) { return 2; } catch (Exception1 | Exception2 e) { return 3; }',
            model.Try(model.Block([r1]), catches=[c1, c2]))
        self.assert_stmt(
            'try { return 1; } catch (Exception e) { return 2; } catch (Exception1 | Exception2 e) { return 3; } finally { return 3; }',
            model.Try(model.Block([r1]),
                      catches=[c1, c2],
                      _finally=model.Block([r3])))

        res1 = model.Resource(model.Variable('r'),
                              type=model.Type(model.Name('Resource')),
                              initializer=model.Name('foo'))
        res2 = model.Resource(model.Variable('r2'),
                              type=model.Type(model.Name('Resource2')),
                              initializer=model.Name('bar'))
        self.assert_stmt('try(Resource r = foo) { return 1; }',
                         model.Try(model.Block([r1]), resources=[res1]))
        self.assert_stmt('try(Resource r = foo;) { return 1; }',
                         model.Try(model.Block([r1]), resources=[res1]))
        self.assert_stmt(
            'try(Resource r = foo; Resource2 r2 = bar) { return 1; }',
            model.Try(model.Block([r1]), resources=[res1, res2]))
        self.assert_stmt(
            'try(Resource r = foo; Resource2 r2 = bar;) { return 1; }',
            model.Try(model.Block([r1]), resources=[res1, res2]))
        self.assert_stmt(
            'try(Resource r = foo) { return 1; } catch (Exception e) { return 2; }',
            model.Try(model.Block([r1]), resources=[res1], catches=[c1]))
        self.assert_stmt(
            'try(Resource r = foo) { return 1; } catch (Exception1 | Exception2 e) { return 3;}',
            model.Try(model.Block([r1]), resources=[res1], catches=[c2]))
        self.assert_stmt(
            'try(Resource r = foo) { return 1; } catch (Exception e) { return 2; } catch (Exception1 | Exception2 e) { return 3; }',
            model.Try(model.Block([r1]), resources=[res1], catches=[c1, c2]))
        self.assert_stmt(
            'try(Resource r = foo) { return 1; } finally { return 3; }',
            model.Try(model.Block([r1]),
                      resources=[res1],
                      _finally=model.Block([r3])))
        self.assert_stmt(
            'try(Resource r = foo) { return 1; } catch (Exception e) { return 2; } finally { return 3; }',
            model.Try(model.Block([r1]),
                      resources=[res1],
                      catches=[c1],
                      _finally=model.Block([r3])))
        self.assert_stmt(
            'try(Resource r = foo) { return 1; } catch (Exception e) { return 2; } catch (Exception1 | Exception2 e) { return 3; } finally { return 3; }',
            model.Try(model.Block([r1]),
                      resources=[res1],
                      catches=[c1, c2],
                      _finally=model.Block([r3])))
Esempio n. 5
0
 def test_synchronized(self):
     self.assert_stmt(
         'synchronized (foo) { return; }',
         model.Synchronized(foo, body=model.Block([model.Return()])))