Example #1
0
 def test_constructor_invocation(self):
     footype = model.Type(model.Name('Foo'))
     bartype = model.Type(model.Name('Bar'))
     for call in ['super', 'this']:
         self.assert_stmt('{}();'.format(call),
                          model.ConstructorInvocation(call))
         self.assert_stmt(
             '{}(1);'.format(call),
             model.ConstructorInvocation(call, arguments=[one]))
         self.assert_stmt(
             '{}(1, foo, "bar");'.format(call),
             model.ConstructorInvocation(
                 call, arguments=[one, foo,
                                  model.Literal('"bar"')]))
         self.assert_stmt(
             '<Foo> {}();'.format(call),
             model.ConstructorInvocation(call, type_arguments=[footype]))
         self.assert_stmt(
             'Foo.{}();'.format(call),
             model.ConstructorInvocation(call, target=model.Name('Foo')))
         self.assert_stmt(
             'Foo.<Bar> {}();'.format(call),
             model.ConstructorInvocation(call,
                                         type_arguments=[bartype],
                                         target=model.Name('Foo')))
    def test_variable_declaration(self):
        var_i = model.Variable('i')
        var_i_decltor = model.VariableDeclarator(var_i)
        var_j = model.Variable('j')
        var_j_decltor = model.VariableDeclarator(var_j)

        self.assert_stmt('int i;',
                         model.VariableDeclaration('int', [var_i_decltor]))
        self.assert_stmt(
            'int i, j;',
            model.VariableDeclaration('int', [var_i_decltor, var_j_decltor]))

        var_i_decltor.initializer = one
        self.assert_stmt('int i = 1;',
                         model.VariableDeclaration('int', [var_i_decltor]))
        self.assert_stmt(
            'int i = 1, j;',
            model.VariableDeclaration('int', [var_i_decltor, var_j_decltor]))

        var_j_decltor.initializer = i
        self.assert_stmt(
            'int i = 1, j = i;',
            model.VariableDeclaration('int', [var_i_decltor, var_j_decltor]))

        int_ar = model.Type('int', dimensions=1)
        var_i_decltor.initializer = None
        self.assert_stmt('int[] i;',
                         model.VariableDeclaration(int_ar, [var_i_decltor]))

        foo_ar = model.Type(
            name=model.Name('Foo'),
            type_arguments=[model.Type(name=model.Name(value='T'))],
            dimensions=1)
        self.assert_stmt('Foo<T>[] i;',
                         model.VariableDeclaration(foo_ar, [var_i_decltor]))
Example #3
0
 def test_import(self):
     m = self.parser.parse_string('''
     import foo;
     import foo.bar;
     ''')
     self.assertEqual(m.import_declarations, [
         model.ImportDeclaration(model.Name('foo')),
         model.ImportDeclaration(model.Name('foo.bar'))
     ])
Example #4
0
 def test_package_annotation(self):
     m = self.parser.parse_string('''
     @Annot
     package foo;
     ''')
     self.assertEqual(
         m.package_declaration,
         model.PackageDeclaration(
             model.Name('foo'),
             modifiers=[model.Annotation(model.Name('Annot'))]))
Example #5
0
    def test_annotations(self):
        # bug #13
        m = self.parser.parse_string('''
        @Annot(key = 1)
        class Foo {}
        ''')
        t = self._assert_declaration(m, 'Foo')

        self.assertEqual(t.modifiers, [
            model.Annotation(name=model.Name('Annot'),
                             members=[
                                 model.AnnotationMember(
                                     name=model.Name('key'),
                                     value=model.Literal('1'))
                             ])
        ])
Example #6
0
 def test_wildcard_import(self):
     m = self.parser.parse_string('''
     import foo.bar.*;
     ''')
     self.assertEqual(
         m.import_declarations,
         [model.ImportDeclaration(model.Name('foo.bar'), on_demand=True)])
Example #7
0
 def test_static_import(self):
     m = self.parser.parse_string('''
     import static foo.bar;
     ''')
     self.assertEqual(
         m.import_declarations,
         [model.ImportDeclaration(model.Name('foo.bar'), static=True)])
Example #8
0
 def test_package(self):
     m = self.parser.parse_string('''
     package foo.bar;
     ''')
     self.assertEqual(
         m.package_declaration,
         model.PackageDeclaration(model.Name('foo.bar', lineno=2),
                                  lineno=2))
Example #9
0
 def test_static_wildcard_import(self):
     m = self.parser.parse_string('''
     import static foo.bar.*;
     ''')
     self.assertEqual(m.import_declarations, [
         model.ImportDeclaration(model.Name('foo.bar', lineno=2),
                                 static=True,
                                 on_demand=True,
                                 lineno=2)
     ])
Example #10
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])))
Example #11
0
import unittest

import plyj.parser as plyj
import plyj.model as model

foo = model.Name('foo')
bar = model.Name('bar')
i = model.Name('i')
j = model.Name('j')
zero = model.Literal('0')
one = model.Literal('1')
two = model.Literal('2')
three = model.Literal('3')
ten = model.Literal('10')


class StatementTest(unittest.TestCase):
    def setUp(self):
        self.parser = plyj.Parser()

    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()])))
import unittest

import plyj.parser as plyj
import plyj.model as model

one = model.Literal('1')
two = model.Literal('2')
three = model.Literal('3')
a = model.Name('a')
b = model.Name('b')
c = model.Name('c')
d = model.Name('d')
e = model.Name('e')


def bin(operator, operand1, operand2):
    return model.BinaryExpression(operator, operand1, operand2)


def u(operator, operand):
    return model.Unary(operator, operand)


expression_tests = [
    # simple test for each operator
    ('1+2', bin('+', one, two)),
    (' 1 + 2 ', bin('+', one, two)),
    ('1-2', bin('-', one, two)),
    ('1*2', bin('*', one, two)),
    ('1/2', bin('/', one, two)),
    ('1%2', bin('%', one, two)),
Example #13
0
import unittest

import plyj.parser as plyj
import plyj.model as model

one = model.Literal('1')
two = model.Literal('2')
three = model.Literal('3')
a = model.Name('a')
b = model.Name('b')
c = model.Name('c')
d = model.Name('d')
e = model.Name('e')

def bin(operator, operand1, operand2):
    return model.BinaryExpression(operator, operand1, operand2)

def u(operator, operand):
    return model.Unary(operator, operand)

expression_tests = [
    # simple test for each operator
    ('1+2', bin('+', one, two)),
    (' 1 + 2 ', bin('+', one, two)),
    ('1-2', bin('-', one, two)),
    ('1*2', bin('*', one, two)),
    ('1/2', bin('/', one, two)),
    ('1%2', bin('%', one, two)),
    ('1^2', bin('^', one, two)),
    ('1&2', bin('&', one, two)),
    ('1&&2', bin('&&', one, two)),