xref: /llvm-project/lldb/test/API/assert_messages_test/TestAssertMessages.py (revision 792b673d36a86ab9d45ed3b259a3fc7020826b76)
19f18f3c8SDavid Spickett"""
29f18f3c8SDavid SpickettTest the format of API test suite assert failure messages
39f18f3c8SDavid Spickett"""
49f18f3c8SDavid Spickett
59f18f3c8SDavid Spickett
69f18f3c8SDavid Spickettimport lldb
79f18f3c8SDavid Spickettimport lldbsuite.test.lldbutil as lldbutil
89f18f3c8SDavid Spickettfrom lldbsuite.test.lldbtest import *
9de2ddc8fSJonas Devliegherefrom lldbsuite.test.decorators import *
109f18f3c8SDavid Spickettfrom textwrap import dedent
119f18f3c8SDavid Spickett
129f18f3c8SDavid Spickett
139f18f3c8SDavid Spickettclass AssertMessagesTestCase(TestBase):
149f18f3c8SDavid Spickett    NO_DEBUG_INFO_TESTCASE = True
159f18f3c8SDavid Spickett
169f18f3c8SDavid Spickett    def assert_expect_fails_with(self, cmd, expect_args, expected_msg):
179f18f3c8SDavid Spickett        try:
189f18f3c8SDavid Spickett            # This expect should fail
199f18f3c8SDavid Spickett            self.expect(cmd, **expect_args)
209f18f3c8SDavid Spickett        except AssertionError as e:
219f18f3c8SDavid Spickett            # Then check message from previous expect
229f18f3c8SDavid Spickett            self.expect(str(e), exe=False, substrs=[dedent(expected_msg)])
239f18f3c8SDavid Spickett        else:
249f18f3c8SDavid Spickett            self.fail("Initial expect should have raised AssertionError!")
259f18f3c8SDavid Spickett
26*792b673dSVladislav Dzhidzhoev    @expectedFailureAll(oslist=no_match(["linux"]), remote=True)
2754c26872SRaphael Isemann    def test_createTestTarget(self):
2854c26872SRaphael Isemann        try:
2954c26872SRaphael Isemann            self.createTestTarget("doesnt_exist")
3054c26872SRaphael Isemann        except AssertionError as e:
312238dcc3SJonas Devlieghere            self.assertIn(
322238dcc3SJonas Devlieghere                "Couldn't create target for path 'doesnt_exist': "
33bf3e3289SJonas Devlieghere                "error: 'doesnt_exist' does not exist",
342238dcc3SJonas Devlieghere                str(e),
352238dcc3SJonas Devlieghere            )
3654c26872SRaphael Isemann
379f18f3c8SDavid Spickett    def test_expect(self):
389f18f3c8SDavid Spickett        """Test format of messages produced by expect(...)"""
399f18f3c8SDavid Spickett
409f18f3c8SDavid Spickett        # When an expect passes the messages are sent to the trace
419f18f3c8SDavid Spickett        # file which we can't access here. So really, these only
429f18f3c8SDavid Spickett        # check what failures look like, but it *should* be the same
439f18f3c8SDavid Spickett        # content for the trace log too.
449f18f3c8SDavid Spickett
459f18f3c8SDavid Spickett        # Will stop at startstr fail
462238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
472238dcc3SJonas Devlieghere            "settings list prompt",
489f18f3c8SDavid Spickett            dict(startstr="dog", endstr="cat"),
499f18f3c8SDavid Spickett            """\
509f18f3c8SDavid Spickett               Ran command:
519f18f3c8SDavid Spickett               "settings list prompt"
529f18f3c8SDavid Spickett
539f18f3c8SDavid Spickett               Got output:
549f18f3c8SDavid Spickett                 prompt -- The debugger command line prompt displayed for the user.
559f18f3c8SDavid Spickett
562238dcc3SJonas Devlieghere               Expecting start string: "dog" (was not found)""",
572238dcc3SJonas Devlieghere        )
589f18f3c8SDavid Spickett
599f18f3c8SDavid Spickett        # startstr passes, endstr fails
609f18f3c8SDavid Spickett        # We see both reported
612238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
622238dcc3SJonas Devlieghere            "settings list prompt",
639f18f3c8SDavid Spickett            dict(startstr="  prompt -- ", endstr="foo"),
649f18f3c8SDavid Spickett            """\
659f18f3c8SDavid Spickett               Ran command:
669f18f3c8SDavid Spickett               "settings list prompt"
679f18f3c8SDavid Spickett
689f18f3c8SDavid Spickett               Got output:
699f18f3c8SDavid Spickett                 prompt -- The debugger command line prompt displayed for the user.
709f18f3c8SDavid Spickett
719f18f3c8SDavid Spickett               Expecting start string: "  prompt -- " (was found)
722238dcc3SJonas Devlieghere               Expecting end string: "foo" (was not found)""",
732238dcc3SJonas Devlieghere        )
749f18f3c8SDavid Spickett
759f18f3c8SDavid Spickett        # Same thing for substrs, regex patterns ignored because of substr failure
769f18f3c8SDavid Spickett        # Any substr after the first missing is also ignored
772238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
782238dcc3SJonas Devlieghere            "abcdefg",
792238dcc3SJonas Devlieghere            dict(substrs=["abc", "ijk", "xyz"], patterns=["foo", "bar"], exe=False),
809f18f3c8SDavid Spickett            """\
819f18f3c8SDavid Spickett               Checking string:
829f18f3c8SDavid Spickett               "abcdefg"
839f18f3c8SDavid Spickett
849f18f3c8SDavid Spickett               Expecting sub string: "abc" (was found)
852238dcc3SJonas Devlieghere               Expecting sub string: "ijk" (was not found)""",
862238dcc3SJonas Devlieghere        )
879f18f3c8SDavid Spickett
889f18f3c8SDavid Spickett        # Regex patterns also stop at first failure, subsequent patterns ignored
899f18f3c8SDavid Spickett        # They are last in the chain so no other check gets skipped
909f18f3c8SDavid Spickett        # Including the rest of the conditions here to prove they are run and shown
912238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
922238dcc3SJonas Devlieghere            "0123456789",
932238dcc3SJonas Devlieghere            dict(
942238dcc3SJonas Devlieghere                startstr="012",
952238dcc3SJonas Devlieghere                endstr="789",
962238dcc3SJonas Devlieghere                substrs=["345", "678"],
972238dcc3SJonas Devlieghere                patterns=["[0-9]+", "[a-f]+", "a|b|c"],
982238dcc3SJonas Devlieghere                exe=False,
992238dcc3SJonas Devlieghere            ),
1009f18f3c8SDavid Spickett            """\
1019f18f3c8SDavid Spickett               Checking string:
1029f18f3c8SDavid Spickett               "0123456789"
1039f18f3c8SDavid Spickett
1049f18f3c8SDavid Spickett               Expecting start string: "012" (was found)
1059f18f3c8SDavid Spickett               Expecting end string: "789" (was found)
1069f18f3c8SDavid Spickett               Expecting sub string: "345" (was found)
1079f18f3c8SDavid Spickett               Expecting sub string: "678" (was found)
1089f18f3c8SDavid Spickett               Expecting regex pattern: "[0-9]+" (was found, matched "0123456789")
1092238dcc3SJonas Devlieghere               Expecting regex pattern: "[a-f]+" (was not found)""",
1102238dcc3SJonas Devlieghere        )
1119f18f3c8SDavid Spickett
1129f18f3c8SDavid Spickett        # This time we dont' want matches but we do get them
1132238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
1142238dcc3SJonas Devlieghere            "the quick brown fox",
1159f18f3c8SDavid Spickett            # Note that the second pattern *will* match
1162238dcc3SJonas Devlieghere            dict(
1172238dcc3SJonas Devlieghere                patterns=["[0-9]+", "fox"],
1182238dcc3SJonas Devlieghere                exe=False,
1192238dcc3SJonas Devlieghere                matching=False,
1202238dcc3SJonas Devlieghere                startstr="cat",
1212238dcc3SJonas Devlieghere                endstr="rabbit",
1222238dcc3SJonas Devlieghere                substrs=["abc", "def"],
1232238dcc3SJonas Devlieghere            ),
1249f18f3c8SDavid Spickett            """\
1259f18f3c8SDavid Spickett               Checking string:
1269f18f3c8SDavid Spickett               "the quick brown fox"
1279f18f3c8SDavid Spickett
1289f18f3c8SDavid Spickett               Not expecting start string: "cat" (was not found)
1299f18f3c8SDavid Spickett               Not expecting end string: "rabbit" (was not found)
1309f18f3c8SDavid Spickett               Not expecting sub string: "abc" (was not found)
1319f18f3c8SDavid Spickett               Not expecting sub string: "def" (was not found)
1329f18f3c8SDavid Spickett               Not expecting regex pattern: "[0-9]+" (was not found)
1332238dcc3SJonas Devlieghere               Not expecting regex pattern: "fox" (was found, matched "fox")""",
1342238dcc3SJonas Devlieghere        )
1359f18f3c8SDavid Spickett
1369f18f3c8SDavid Spickett        # Extra assert messages are only printed when we get a failure
1379f18f3c8SDavid Spickett        # So I can't test that from here, just how it looks when it's printed
1382238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
1392238dcc3SJonas Devlieghere            "mouse",
1409f18f3c8SDavid Spickett            dict(startstr="cat", exe=False, msg="Reason for check goes here!"),
1419f18f3c8SDavid Spickett            """\
1429f18f3c8SDavid Spickett               Checking string:
1439f18f3c8SDavid Spickett               "mouse"
1449f18f3c8SDavid Spickett
1459f18f3c8SDavid Spickett               Expecting start string: "cat" (was not found)
1462238dcc3SJonas Devlieghere               Reason for check goes here!""",
1472238dcc3SJonas Devlieghere        )
148010d7a38SDave Lee
149010d7a38SDave Lee        # Verify expect() preconditions.
150010d7a38SDave Lee        # Both `patterns` and `substrs` cannot be of type string.
1512238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
1522238dcc3SJonas Devlieghere            "any command",
153010d7a38SDave Lee            dict(patterns="some substring"),
1542238dcc3SJonas Devlieghere            "patterns must be a collection of strings",
1552238dcc3SJonas Devlieghere        )
1562238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
1572238dcc3SJonas Devlieghere            "any command",
158010d7a38SDave Lee            dict(substrs="some substring"),
1592238dcc3SJonas Devlieghere            "substrs must be a collection of strings",
1602238dcc3SJonas Devlieghere        )
161010d7a38SDave Lee        # Prevent `self.expect("cmd", "substr")`
1622238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
1632238dcc3SJonas Devlieghere            "any command",
164010d7a38SDave Lee            dict(msg="some substring"),
1652238dcc3SJonas Devlieghere            "expect() missing a matcher argument",
1662238dcc3SJonas Devlieghere        )
167010d7a38SDave Lee        # Prevent `self.expect("cmd", "msg", "substr")`
1682238dcc3SJonas Devlieghere        self.assert_expect_fails_with(
1692238dcc3SJonas Devlieghere            "any command",
170010d7a38SDave Lee            dict(msg="a message", patterns="some substring"),
1712238dcc3SJonas Devlieghere            "must be a collection of strings",
1722238dcc3SJonas Devlieghere        )
173