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