Skip to content

Commit

Permalink
Fix model object id if its parent doesn't know about it
Browse files Browse the repository at this point in the history
Avoids failure reported in #4695.
  • Loading branch information
pekkaklarck committed Mar 28, 2023
1 parent 6618b6b commit c3bead5
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/robot/model/body.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ def _get_id(self, parent):
if step.type != self.MESSAGE)
if getattr(parent, 'has_teardown', False):
steps.append(parent.teardown)
my_id = steps.index(self) + 1
return f'{parent.id}-k{my_id}'
index = steps.index(self) if self in steps else len(steps)
parent_id = parent.id
return f'{parent_id}-k{index + 1}' if parent_id else f'k{index + 1}'

def to_dict(self):
raise NotImplementedError
Expand Down
4 changes: 2 additions & 2 deletions src/robot/model/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def id(self):
if not self.parent:
return 'k1'
if not self.parent.parent:
return 'k%d' % (self.parent.body.index(self) + 1)
return self._get_id(self.parent)
return self._get_id(self.parent.parent)

def __str__(self):
Expand Down Expand Up @@ -231,7 +231,7 @@ def id(self):
if not self.parent:
return 'k1'
if not self.parent.parent:
return 'k%d' % (self.parent.body.index(self) + 1)
return self._get_id(self.parent)
return self._get_id(self.parent.parent)

def __str__(self):
Expand Down
4 changes: 3 additions & 1 deletion src/robot/model/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def html_message(self):
def id(self):
if not self.parent:
return 'm1'
return '%s-m%d' % (self.parent.id, self.parent.messages.index(self) + 1)
messages = self.parent.messages
index = messages.index(self) if self in messages else len(messages)
return f'{self.parent.id}-m{index + 1}'

def visit(self, visitor):
""":mod:`Visitor interface <robot.model.visitor>` entry-point."""
Expand Down
6 changes: 4 additions & 2 deletions src/robot/model/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,16 @@ def id(self):
"""
if not self.parent:
return 't1'
return '%s-t%d' % (self.parent.id, self.parent.tests.index(self)+1)
tests = self.parent.tests
index = tests.index(self) if self in tests else len(tests)
return f'{self.parent.id}-t{index + 1}'

@property
def longname(self):
"""Test name prefixed with the long name of the parent suite."""
if not self.parent:
return self.name
return '%s.%s' % (self.parent.longname, self.name)
return f'{self.parent.longname}.{self.name}'

@property
def source(self):
Expand Down
3 changes: 2 additions & 1 deletion src/robot/model/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ def id(self):
"""
if not self.parent:
return 's1'
index = self.parent.suites.index(self)
suites = self.parent.suites
index = suites.index(self) if self in suites else len(suites)
return f'{self.parent.id}-s{index + 1}'

@property
Expand Down
10 changes: 9 additions & 1 deletion utest/model/test_body.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from robot.model import Body, BodyItem, If, For, Keyword, Message, TestCase
from robot.model import Body, BodyItem, If, For, Keyword, TestCase, TestSuite
from robot.result.model import Body as ResultBody
from robot.utils.asserts import assert_equal, assert_raises_with_msg

Expand Down Expand Up @@ -77,6 +77,14 @@ def test_id_with_parent_having_setup_and_teardown(self):
assert_equal(tc.setup.id, 't1-k1')
assert_equal(tc.teardown.id, 't1-k5')

def test_id_when_item_not_in_parent(self):
tc = TestCase(parent=TestSuite(parent=TestSuite()))
assert_equal(tc.id, 's1-s1-t1')
assert_equal(Keyword(parent=tc).id, 's1-s1-t1-k1')
tc.body.create_keyword()
tc.body.create_if().body.create_branch()
assert_equal(Keyword(parent=tc).id, 's1-s1-t1-k3')

def test_id_with_if(self):
tc = TestCase()
root = tc.body.create_if()
Expand Down
6 changes: 6 additions & 0 deletions utest/model/test_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def test_branch_id_with_only_root(self):
assert_equal(root.body.create_branch().id, 'k1')
assert_equal(root.body.create_branch().id, 'k2')

def test_branch_id_with_only_root_when_branch_not_in_root(self):
assert_equal(IfBranch(parent=If()).id, 'k1')

def test_branch_id_with_real_parent(self):
root = TestCase().body.create_if()
assert_equal(root.body.create_branch().id, 't1-k1')
Expand Down Expand Up @@ -143,6 +146,9 @@ def test_branch_id_with_only_root(self):
assert_equal(root.body.create_branch().id, 'k1')
assert_equal(root.body.create_branch().id, 'k2')

def test_branch_id_with_only_root_when_branch_not_in_root(self):
assert_equal(TryBranch(parent=Try()).id, 'k1')

def test_branch_id_with_real_parent(self):
root = TestCase().body.create_try()
assert_equal(root.body.create_branch().id, 't1-k1')
Expand Down
7 changes: 7 additions & 0 deletions utest/model/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def test_id_with_errors_parent(self):
assert_equal(errors.messages.create().id, 'errors-m1')
assert_equal(errors.messages.create().id, 'errors-m2')

def test_id_when_item_not_in_parent(self):
kw = Keyword()
assert_equal(Message(parent=kw).id, 'k1-m1')
assert_equal(kw.body.create_message().id, 'k1-m1')
assert_equal(kw.body.create_message().id, 'k1-m2')
assert_equal(Message(parent=kw).id, 'k1-m3')


class TestHtmlMessage(unittest.TestCase):

Expand Down

0 comments on commit c3bead5

Please sign in to comment.