diff --git a/src/tap/line.py b/src/tap/line.py index a48f839..646cf1e 100644 --- a/src/tap/line.py +++ b/src/tap/line.py @@ -111,7 +111,16 @@ def __str__(self): diagnostics = "" if self.diagnostics is not None: diagnostics = "\n" + self.diagnostics.rstrip() - return f"{is_not}ok {self.number} {self.description}{directive}{diagnostics}" + yaml_block = "" + if self._yaml_block is not None: + indented_yaml_block = "\n".join( + f" {line}" for line in self._yaml_block.splitlines() + ) + yaml_block = f"\n ---\n{indented_yaml_block}\n ..." + return ( + f"{is_not}ok {self.number} {self.description}{directive}" + f"{diagnostics}{yaml_block}" + ) class Plan(Line): diff --git a/src/tap/tracker.py b/src/tap/tracker.py index e208459..b9945dc 100644 --- a/src/tap/tracker.py +++ b/src/tap/tracker.py @@ -73,23 +73,39 @@ def _track(self, class_name): if self.combined: self.combined_test_cases_seen.append(class_name) - def add_ok(self, class_name, description, directive="", diagnostics=None): + def add_ok( + self, + class_name, + description, + directive="", + diagnostics=None, + raw_yaml_block=None, + ): result = Result( ok=True, number=self._get_next_line_number(class_name), description=description, diagnostics=diagnostics, directive=Directive(directive), + raw_yaml_block=raw_yaml_block, ) self._add_line(class_name, result) - def add_not_ok(self, class_name, description, directive="", diagnostics=None): + def add_not_ok( + self, + class_name, + description, + directive="", + diagnostics=None, + raw_yaml_block=None, + ): result = Result( ok=False, number=self._get_next_line_number(class_name), description=description, diagnostics=diagnostics, directive=Directive(directive), + raw_yaml_block=raw_yaml_block, ) self._add_line(class_name, result) diff --git a/tests/test_line.py b/tests/test_line.py index 9691533..fe855f9 100644 --- a/tests/test_line.py +++ b/tests/test_line.py @@ -3,6 +3,14 @@ from tap.directive import Directive from tap.line import Line, Result +try: + import yaml # noqa + from more_itertools import peekable # noqa + + have_yaml = True +except ImportError: + have_yaml = False + class TestLine(unittest.TestCase): """Tests for tap.line.Line""" @@ -38,5 +46,19 @@ def test_str_directive(self): self.assertEqual("ok 44 passing # SKIP a reason", str(result)) def test_str_diagnostics(self): - result = Result(False, 43, "failing", diagnostics="# more info") - self.assertEqual("not ok 43 failing\n# more info", str(result)) + result = Result(False, 45, "failing", diagnostics="# more info") + self.assertEqual("not ok 45 failing\n# more info", str(result)) + + def test_yaml_block(self): + raw_yaml_block = """\ +message: test_message +severity: fail +""" + result = Result(False, 46, "passing", raw_yaml_block=raw_yaml_block) + if have_yaml: + self.assertEqual(result.yaml_block["message"], "test_message") + self.assertIn( + " ---\n message: test_message\n severity: fail\n ...", str(result) + ) + else: + self.assertIsNone(result.yaml_block) diff --git a/tests/test_tracker.py b/tests/test_tracker.py index 1e1c10e..0ad6b01 100644 --- a/tests/test_tracker.py +++ b/tests/test_tracker.py @@ -7,6 +7,14 @@ from tap.tests import TestCase from tap.tracker import Tracker +try: + import yaml # noqa + from more_itertools import peekable # noqa + + have_yaml = True +except ImportError: + have_yaml = False + class TestTracker(TestCase): def _make_header(self, test_case): @@ -295,6 +303,38 @@ def test_adds_not_ok_with_diagnostics(self): line = tracker._test_cases["FakeTestCase"][0] self.assertEqual("# more info\n", line.diagnostics) + def test_adds_ok_with_yaml_block(self): + tracker = Tracker() + tracker.add_ok( + "FakeTestCase", + "a description", + raw_yaml_block="""\ +message: test_message +severity: pass +""", + ) + line = tracker._test_cases["FakeTestCase"][0] + if have_yaml: + self.assertEqual("test_message", line.yaml_block["message"]) + else: + self.assertIsNone(line.yaml_block) + + def test_adds_not_ok_with_yaml_block(self): + tracker = Tracker() + tracker.add_not_ok( + "FakeTestCase", + "a description", + raw_yaml_block="""\ +message: test_message +severity: fail +""", + ) + line = tracker._test_cases["FakeTestCase"][0] + if have_yaml: + self.assertEqual("test_message", line.yaml_block["message"]) + else: + self.assertIsNone(line.yaml_block) + def test_header_displayed_by_default(self): tracker = Tracker() self.assertTrue(tracker.header)