2 Test the format of API test suite assert failure messages
7 import lldbsuite
.test
.lldbutil
as lldbutil
8 from lldbsuite
.test
.lldbtest
import *
9 from lldbsuite
.test
.decorators
import *
10 from textwrap
import dedent
13 class AssertMessagesTestCase(TestBase
):
14 NO_DEBUG_INFO_TESTCASE
= True
16 def assert_expect_fails_with(self
, cmd
, expect_args
, expected_msg
):
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
)])
24 self
.fail("Initial expect should have raised AssertionError!")
26 @expectedFailureAll(remote
=True)
27 def test_createTestTarget(self
):
29 self
.createTestTarget("doesnt_exist")
30 except AssertionError as e
:
32 "Couldn't create target for path 'doesnt_exist': "
33 "error: unable to find executable for 'doesnt_exist'",
37 def test_expect(self
):
38 """Test format of messages produced by expect(...)"""
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.
45 # Will stop at startstr fail
46 self
.assert_expect_fails_with(
47 "settings list prompt",
48 dict(startstr
="dog", endstr
="cat"),
51 "settings list prompt"
54 prompt -- The debugger command line prompt displayed for the user.
56 Expecting start string: "dog" (was not found)""",
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"),
66 "settings list prompt"
69 prompt -- The debugger command line prompt displayed for the user.
71 Expecting start string: " prompt -- " (was found)
72 Expecting end string: "foo" (was not found)""",
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(
79 dict(substrs
=["abc", "ijk", "xyz"], patterns
=["foo", "bar"], exe
=False),
84 Expecting sub string: "abc" (was found)
85 Expecting sub string: "ijk" (was not found)""",
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(
96 substrs
=["345", "678"],
97 patterns
=["[0-9]+", "[a-f]+", "a|b|c"],
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)""",
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
117 patterns
=["[0-9]+", "fox"],
122 substrs
=["abc", "def"],
126 "the quick brown fox"
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")""",
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(
140 dict(startstr
="cat", exe
=False, msg
="Reason for check goes here!"),
145 Expecting start string: "cat" (was not found)
146 Reason for check goes here!""",
149 # Verify expect() preconditions.
150 # Both `patterns` and `substrs` cannot be of type string.
151 self
.assert_expect_fails_with(
153 dict(patterns
="some substring"),
154 "patterns must be a collection of strings",
156 self
.assert_expect_fails_with(
158 dict(substrs
="some substring"),
159 "substrs must be a collection of strings",
161 # Prevent `self.expect("cmd", "substr")`
162 self
.assert_expect_fails_with(
164 dict(msg
="some substring"),
165 "expect() missing a matcher argument",
167 # Prevent `self.expect("cmd", "msg", "substr")`
168 self
.assert_expect_fails_with(
170 dict(msg
="a message", patterns
="some substring"),
171 "must be a collection of strings",