xref: /llvm-project/lldb/test/API/assert_messages_test/TestAssertMessages.py (revision 792b673d36a86ab9d45ed3b259a3fc7020826b76)
1"""
2Test the format of API test suite assert failure messages
3"""
4
5
6import lldb
7import lldbsuite.test.lldbutil as lldbutil
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test.decorators import *
10from textwrap import dedent
11
12
13class AssertMessagesTestCase(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    def assert_expect_fails_with(self, cmd, expect_args, expected_msg):
17        try:
18            # This expect should fail
19            self.expect(cmd, **expect_args)
20        except AssertionError as e:
21            # Then check message from previous expect
22            self.expect(str(e), exe=False, substrs=[dedent(expected_msg)])
23        else:
24            self.fail("Initial expect should have raised AssertionError!")
25
26    @expectedFailureAll(oslist=no_match(["linux"]), remote=True)
27    def test_createTestTarget(self):
28        try:
29            self.createTestTarget("doesnt_exist")
30        except AssertionError as e:
31            self.assertIn(
32                "Couldn't create target for path 'doesnt_exist': "
33                "error: 'doesnt_exist' does not exist",
34                str(e),
35            )
36
37    def test_expect(self):
38        """Test format of messages produced by expect(...)"""
39
40        # When an expect passes the messages are sent to the trace
41        # file which we can't access here. So really, these only
42        # check what failures look like, but it *should* be the same
43        # content for the trace log too.
44
45        # Will stop at startstr fail
46        self.assert_expect_fails_with(
47            "settings list prompt",
48            dict(startstr="dog", endstr="cat"),
49            """\
50               Ran command:
51               "settings list prompt"
52
53               Got output:
54                 prompt -- The debugger command line prompt displayed for the user.
55
56               Expecting start string: "dog" (was not found)""",
57        )
58
59        # startstr passes, endstr fails
60        # We see both reported
61        self.assert_expect_fails_with(
62            "settings list prompt",
63            dict(startstr="  prompt -- ", endstr="foo"),
64            """\
65               Ran command:
66               "settings list prompt"
67
68               Got output:
69                 prompt -- The debugger command line prompt displayed for the user.
70
71               Expecting start string: "  prompt -- " (was found)
72               Expecting end string: "foo" (was not found)""",
73        )
74
75        # Same thing for substrs, regex patterns ignored because of substr failure
76        # Any substr after the first missing is also ignored
77        self.assert_expect_fails_with(
78            "abcdefg",
79            dict(substrs=["abc", "ijk", "xyz"], patterns=["foo", "bar"], exe=False),
80            """\
81               Checking string:
82               "abcdefg"
83
84               Expecting sub string: "abc" (was found)
85               Expecting sub string: "ijk" (was not found)""",
86        )
87
88        # Regex patterns also stop at first failure, subsequent patterns ignored
89        # They are last in the chain so no other check gets skipped
90        # Including the rest of the conditions here to prove they are run and shown
91        self.assert_expect_fails_with(
92            "0123456789",
93            dict(
94                startstr="012",
95                endstr="789",
96                substrs=["345", "678"],
97                patterns=["[0-9]+", "[a-f]+", "a|b|c"],
98                exe=False,
99            ),
100            """\
101               Checking string:
102               "0123456789"
103
104               Expecting start string: "012" (was found)
105               Expecting end string: "789" (was found)
106               Expecting sub string: "345" (was found)
107               Expecting sub string: "678" (was found)
108               Expecting regex pattern: "[0-9]+" (was found, matched "0123456789")
109               Expecting regex pattern: "[a-f]+" (was not found)""",
110        )
111
112        # This time we dont' want matches but we do get them
113        self.assert_expect_fails_with(
114            "the quick brown fox",
115            # Note that the second pattern *will* match
116            dict(
117                patterns=["[0-9]+", "fox"],
118                exe=False,
119                matching=False,
120                startstr="cat",
121                endstr="rabbit",
122                substrs=["abc", "def"],
123            ),
124            """\
125               Checking string:
126               "the quick brown fox"
127
128               Not expecting start string: "cat" (was not found)
129               Not expecting end string: "rabbit" (was not found)
130               Not expecting sub string: "abc" (was not found)
131               Not expecting sub string: "def" (was not found)
132               Not expecting regex pattern: "[0-9]+" (was not found)
133               Not expecting regex pattern: "fox" (was found, matched "fox")""",
134        )
135
136        # Extra assert messages are only printed when we get a failure
137        # So I can't test that from here, just how it looks when it's printed
138        self.assert_expect_fails_with(
139            "mouse",
140            dict(startstr="cat", exe=False, msg="Reason for check goes here!"),
141            """\
142               Checking string:
143               "mouse"
144
145               Expecting start string: "cat" (was not found)
146               Reason for check goes here!""",
147        )
148
149        # Verify expect() preconditions.
150        # Both `patterns` and `substrs` cannot be of type string.
151        self.assert_expect_fails_with(
152            "any command",
153            dict(patterns="some substring"),
154            "patterns must be a collection of strings",
155        )
156        self.assert_expect_fails_with(
157            "any command",
158            dict(substrs="some substring"),
159            "substrs must be a collection of strings",
160        )
161        # Prevent `self.expect("cmd", "substr")`
162        self.assert_expect_fails_with(
163            "any command",
164            dict(msg="some substring"),
165            "expect() missing a matcher argument",
166        )
167        # Prevent `self.expect("cmd", "msg", "substr")`
168        self.assert_expect_fails_with(
169            "any command",
170            dict(msg="a message", patterns="some substring"),
171            "must be a collection of strings",
172        )
173