Skip to content

Commit

Permalink
Enhance For, While and Try string reprs
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkaklarck committed Mar 14, 2023
1 parent 9dec30c commit 10b03f1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 26 deletions.
32 changes: 23 additions & 9 deletions src/robot/model/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class For(BodyItem):
type = BodyItem.FOR
body_class = Body
repr_args = ('variables', 'flavor', 'values')
repr_args = ('variables', 'flavor', 'values', 'start', 'mode', 'fill')
__slots__ = ['variables', 'flavor', 'values', 'start', 'mode', 'fill']

def __init__(self, variables=(), flavor='IN', values=(), start=None, mode=None,
Expand Down Expand Up @@ -54,9 +54,16 @@ def visit(self, visitor):
visitor.visit_for(self)

def __str__(self):
variables = ' '.join(self.variables)
values = ' '.join(self.values)
return 'FOR %s %s %s' % (variables, self.flavor, values)
parts = ['FOR', *self.variables, self.flavor, *self.values]
for name, value in [('start', self.start),
('mode', self.mode),
('fill', self.fill)]:
if value is not None:
parts.append(f'{name}={value}')
return ' '.join(parts)

def _include_in_repr(self, name, value):
return name not in ('start', 'mode', 'fill') or value is not None

def to_dict(self):
data = {'type': self.type,
Expand Down Expand Up @@ -93,7 +100,15 @@ def visit(self, visitor):
visitor.visit_while(self)

def __str__(self):
return f'WHILE {self.condition}' + (f' {self.limit}' if self.limit else '')
parts = ['WHILE']
if self.condition is not None:
parts.append(self.condition)
if self.limit is not None:
parts.append(f'limit={self.limit}')
return ' '.join(parts)

def _include_in_repr(self, name, value):
return name == 'condition' or value is not None

def to_dict(self):
data = {'type': self.type}
Expand Down Expand Up @@ -208,16 +223,15 @@ def id(self):
def __str__(self):
if self.type != BodyItem.EXCEPT:
return self.type
parts = ['EXCEPT'] + list(self.patterns)
parts = ['EXCEPT', *self.patterns]
if self.pattern_type:
parts.append(f'type={self.pattern_type}')
if self.variable:
parts.extend(['AS', self.variable])
return ' '.join(parts)

def __repr__(self):
repr_args = self.repr_args if self.type == BodyItem.EXCEPT else ['type']
return self._repr(repr_args)
def _include_in_repr(self, name, value):
return name == 'type' or value

def visit(self, visitor):
visitor.visit_try_branch(self)
Expand Down
10 changes: 6 additions & 4 deletions src/robot/model/modelobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,13 @@ def deepcopy(self, **attributes):
return copied

def __repr__(self):
return self._repr(self.repr_args)
arguments = [(name, getattr(self, name)) for name in self.repr_args]
args_repr = ', '.join(f'{name}={value!r}' for name, value in arguments
if self._include_in_repr(name, value))
return f"{full_name(self)}({args_repr})"

def _repr(self, repr_args):
args = ', '.join(f'{a}={getattr(self, a)!r}' for a in repr_args)
return f"{full_name(self)}({args})"
def _include_in_repr(self, name, value):
return True


def full_name(obj):
Expand Down
7 changes: 3 additions & 4 deletions src/robot/running/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,6 @@ def __init__(self, type, name, args=(), alias=None, parent=None, lineno=None):
self.parent = parent
self.lineno = lineno

def _repr(self, repr_args):
repr_args = [a for a in repr_args if a in ('type', 'name') or getattr(self, a)]
return super()._repr(repr_args)

@property
def source(self) -> Path:
return self.parent.source if self.parent is not None else None
Expand Down Expand Up @@ -804,6 +800,9 @@ def to_dict(self):
data['lineno'] = self.lineno
return data

def _include_in_repr(self, name, value):
return name in ('type', 'name') or value


class Imports(model.ItemList):

Expand Down
39 changes: 30 additions & 9 deletions utest/model/test_control.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from robot.model import For, If, IfBranch, TestCase, Try, TryBranch
from robot.model import For, If, IfBranch, TestCase, Try, TryBranch, While
from robot.utils.asserts import assert_equal


Expand All @@ -17,22 +17,43 @@ class TestFor(unittest.TestCase):
def test_string_reprs(self):
for for_, exp_str, exp_repr in [
(For(),
'FOR IN ',
'FOR IN',
"For(variables=(), flavor='IN', values=())"),
(For(('${x}',), 'IN RANGE', ('10',)),
'FOR ${x} IN RANGE 10',
"For(variables=('${x}',), flavor='IN RANGE', values=('10',))"),
(For(('${x}', '${y}'), 'IN ENUMERATE', ('a', 'b')),
'FOR ${x} ${y} IN ENUMERATE a b',
"For(variables=('${x}', '${y}'), flavor='IN ENUMERATE', values=('a', 'b'))"),
(For([u'${\xfc}'], 'IN', [u'f\xf6\xf6']),
u'FOR ${\xfc} IN f\xf6\xf6',
u"For(variables=[%r], flavor='IN', values=[%r])" % (u'${\xfc}', u'f\xf6\xf6'))
(For(['${x}'], 'IN ENUMERATE', ['@{stuff}'], start='1'),
'FOR ${x} IN ENUMERATE @{stuff} start=1',
"For(variables=['${x}'], flavor='IN ENUMERATE', values=['@{stuff}'], start='1')"),
(For(('${x}', '${y}'), 'IN ZIP', ('${xs}', '${ys}'), mode='LONGEST', fill='-'),
'FOR ${x} ${y} IN ZIP ${xs} ${ys} mode=LONGEST fill=-',
"For(variables=('${x}', '${y}'), flavor='IN ZIP', values=('${xs}', '${ys}'), mode='LONGEST', fill='-')"),
(For(['${ü}'], 'IN', ['föö']),
'FOR ${ü} IN föö',
"For(variables=['${ü}'], flavor='IN', values=['föö'])")
]:
assert_equal(str(for_), exp_str)
assert_equal(repr(for_), 'robot.model.' + exp_repr)


class TestWhile(unittest.TestCase):

def test_string_reprs(self):
for while_, exp_str, exp_repr in [
(While(),
'WHILE',
"While(condition=None)"),
(While('$x', limit='100'),
'WHILE $x limit=100',
"While(condition='$x', limit='100')")
]:
assert_equal(str(while_), exp_str)
assert_equal(repr(while_), 'robot.model.' + exp_repr)


class TestIf(unittest.TestCase):

def test_type(self):
Expand Down Expand Up @@ -142,16 +163,16 @@ def test_string_reprs(self):
"TryBranch(type='TRY')"),
(TryBranch(EXCEPT),
'EXCEPT',
"TryBranch(type='EXCEPT', patterns=(), pattern_type=None, variable=None)"),
"TryBranch(type='EXCEPT')"),
(TryBranch(EXCEPT, ('Message',)),
'EXCEPT Message',
"TryBranch(type='EXCEPT', patterns=('Message',), pattern_type=None, variable=None)"),
"TryBranch(type='EXCEPT', patterns=('Message',))"),
(TryBranch(EXCEPT, ('M', 'S', 'G', 'S')),
'EXCEPT M S G S',
"TryBranch(type='EXCEPT', patterns=('M', 'S', 'G', 'S'), pattern_type=None, variable=None)"),
"TryBranch(type='EXCEPT', patterns=('M', 'S', 'G', 'S'))"),
(TryBranch(EXCEPT, (), None, '${x}'),
'EXCEPT AS ${x}',
"TryBranch(type='EXCEPT', patterns=(), pattern_type=None, variable='${x}')"),
"TryBranch(type='EXCEPT', variable='${x}')"),
(TryBranch(EXCEPT, ('Message',), 'glob', '${x}'),
'EXCEPT Message type=glob AS ${x}',
"TryBranch(type='EXCEPT', patterns=('Message',), pattern_type='glob', variable='${x}')"),
Expand Down

0 comments on commit 10b03f1

Please sign in to comment.