Skip to content

Commit

Permalink
test: add test for _repr_pretty() method
Browse files Browse the repository at this point in the history
Since _repr_pretty() uses output of str(), and the latter is already
heavily tested in tests/test_language_c.py, we can simply test whether
p.text is call made instead of duplicating all the test cases.

Signed-off-by: Shung-Hsi Yu <[email protected]>
  • Loading branch information
shunghsiyu committed Aug 17, 2022
1 parent 46df920 commit 8019b75
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
11 changes: 11 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
from typing import Any, NamedTuple, Optional
import unittest
from unittest.mock import Mock

from drgn import (
Architecture,
Expand Down Expand Up @@ -108,6 +109,16 @@ def mock_object_find(prog, name, flags, filename):
return prog


def assertReprPrettyEqualsStr(obj):
pretty_printer_mock = Mock()

obj._repr_pretty_(pretty_printer_mock, False)
pretty_printer_mock.text.assert_called_with(str(obj))

obj._repr_pretty_(p=pretty_printer_mock, cycle=True)
pretty_printer_mock.text.assert_called_with("...")


def identical(a, b):
"""
Return whether two objects are "identical".
Expand Down
11 changes: 11 additions & 0 deletions tests/linux_kernel/test_stack_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from drgn import Object, Program, cast
from drgn.helpers.linux.pid import find_task
from tests import assertReprPrettyEqualsStr
from tests.linux_kernel import (
LinuxKernelTestCase,
fork_and_pause,
Expand Down Expand Up @@ -97,3 +98,13 @@ def test_prog(self):
self.prog.stack_trace(Object(self.prog, "struct pt_regs", value={})).prog,
self.prog,
)

def test_stack__repr_pretty_(self):
pid = fork_and_pause()
wait_until(proc_blocked, pid)
trace = self.prog.stack_trace(pid)
assertReprPrettyEqualsStr(trace)
for frame in trace:
assertReprPrettyEqualsStr(frame)
os.kill(pid, signal.SIGKILL)
os.waitpid(pid, 0)
11 changes: 10 additions & 1 deletion tests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
reinterpret,
sizeof,
)
from tests import MockMemorySegment, MockProgramTestCase, mock_program
from tests import (
MockMemorySegment,
MockProgramTestCase,
assertReprPrettyEqualsStr,
mock_program,
)


class TestInit(MockProgramTestCase):
Expand Down Expand Up @@ -1718,3 +1723,7 @@ def test_iter(self):
iter,
Object(self.prog, "int []", address=0),
)

def test__repr_pretty_(self):
obj = Object(self.prog, "int", value=0)
assertReprPrettyEqualsStr(obj)
11 changes: 10 additions & 1 deletion tests/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
offsetof,
sizeof,
)
from tests import DEFAULT_LANGUAGE, MockProgramTestCase, mock_program
from tests import (
DEFAULT_LANGUAGE,
MockProgramTestCase,
assertReprPrettyEqualsStr,
mock_program,
)


class TestType(MockProgramTestCase):
Expand Down Expand Up @@ -973,6 +978,10 @@ def test_language_repr(self):
"prog.void_type(language=Language.CPP)",
)

def test__repr_pretty_(self):
t = self.prog.void_type()
assertReprPrettyEqualsStr(t)

def test_different_programs_compound(self):
self.assertRaisesRegex(
ValueError,
Expand Down

0 comments on commit 8019b75

Please sign in to comment.