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_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_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]))
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()))
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])))