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]))
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]))
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])))
def test_array(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) int_ar = model.Type('int', dimensions=1) arr_init = model.ArrayInitializer([one, three]) var_i_decltor.initializer = arr_init self.assert_stmt('int[] i = {1, 3};', model.VariableDeclaration(int_ar, [var_i_decltor])) arr_creation = model.ArrayCreation('int', dimensions=[None], initializer=arr_init) var_i_decltor.initializer = arr_creation self.assert_stmt('int[] i = new int[] {1, 3};', model.VariableDeclaration(int_ar, [var_i_decltor])) arr_creation.dimensions = [two] self.assert_stmt('int[] i = new int[2] {1, 3};', model.VariableDeclaration(int_ar, [var_i_decltor]))
('a--', u('x--', a)), # assignment expressions ('a = 1', model.Assignment('=', a, one)), ('a += 1', model.Assignment('+=', a, one)), ('a -= 1', model.Assignment('-=', a, one)), ('a *= 1', model.Assignment('*=', a, one)), ('a /= 1', model.Assignment('/=', a, one)), ('a %= 1', model.Assignment('%=', a, one)), ('a ^= 1', model.Assignment('^=', a, one)), ('a &= 1', model.Assignment('&=', a, one)), ('a |= 1', model.Assignment('|=', a, one)), ('a <<= 1', model.Assignment('<<=', a, one)), ('a >>= 1', model.Assignment('>>=', a, one)), ('a >>>= 1', model.Assignment('>>>=', a, one)), # casts ('(Foo) a', model.Cast(model.Type(model.Name('Foo')), a)), ('(int[]) a', model.Cast(model.Type('int', dimensions=1), a)), ('(Foo[]) a', model.Cast(model.Type(model.Name('Foo'), dimensions=1), a)), ('(Foo<T>) a', model.Cast( model.Type(model.Name('Foo'), type_arguments=[model.Type(model.Name('T'))]), a)), ('(Foo<T>.Bar) a', model.Cast( model.Type(model.Name('Bar'), enclosed_in=model.Type( model.Name('Foo'), type_arguments=[model.Type(model.Name('T'))])), a)), # method invocation ('foo.bar()', model.MethodInvocation(name='bar', target=model.Name(value='foo'))),