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
15    mydir = TestBase.compute_mydir(__file__)
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def assert_expect_fails_with(self, cmd, expect_args, expected_msg):
19        try:
20            # This expect should fail
21            self.expect(cmd, **expect_args)
22        except AssertionError as e:
23            # Then check message from previous expect
24            self.expect(str(e), exe=False, substrs=[dedent(expected_msg)])
25        else:
26            self.fail("Initial expect should have raised AssertionError!")
27
28    @expectedFailureAll(remote=True)
29    def test_createTestTarget(self):
30        try:
31           self.createTestTarget("doesnt_exist")
32        except AssertionError as e:
33           self.assertIn("Couldn't create target for path 'doesnt_exist': "
34                         "error: unable to find executable for 'doesnt_exist'",
35                         str(e))
36
37
38    def test_expect(self):
39        """Test format of messages produced by expect(...)"""
40
41        # When an expect passes the messages are sent to the trace
42        # file which we can't access here. So really, these only
43        # check what failures look like, but it *should* be the same
44        # content for the trace log too.
45
46        # Will stop at startstr fail
47        self.assert_expect_fails_with("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        # startstr passes, endstr fails
59        # We see both reported
60        self.assert_expect_fails_with("settings list prompt",
61            dict(startstr="  prompt -- ", endstr="foo"),
62            """\
63               Ran command:
64               "settings list prompt"
65
66               Got output:
67                 prompt -- The debugger command line prompt displayed for the user.
68
69               Expecting start string: "  prompt -- " (was found)
70               Expecting end string: "foo" (was not found)""")
71
72        # Same thing for substrs, regex patterns ignored because of substr failure
73        # Any substr after the first missing is also ignored
74        self.assert_expect_fails_with("abcdefg",
75            dict(substrs=["abc", "ijk", "xyz"],
76            patterns=["foo", "bar"], exe=False),
77            """\
78               Checking string:
79               "abcdefg"
80
81               Expecting sub string: "abc" (was found)
82               Expecting sub string: "ijk" (was not found)""")
83
84        # Regex patterns also stop at first failure, subsequent patterns ignored
85        # They are last in the chain so no other check gets skipped
86        # Including the rest of the conditions here to prove they are run and shown
87        self.assert_expect_fails_with("0123456789",
88            dict(startstr="012", endstr="789", substrs=["345", "678"],
89            patterns=["[0-9]+", "[a-f]+", "a|b|c"], exe=False),
90            """\
91               Checking string:
92               "0123456789"
93
94               Expecting start string: "012" (was found)
95               Expecting end string: "789" (was found)
96               Expecting sub string: "345" (was found)
97               Expecting sub string: "678" (was found)
98               Expecting regex pattern: "[0-9]+" (was found, matched "0123456789")
99               Expecting regex pattern: "[a-f]+" (was not found)""")
100
101        # This time we dont' want matches but we do get them
102        self.assert_expect_fails_with("the quick brown fox",
103            # Note that the second pattern *will* match
104            dict(patterns=["[0-9]+", "fox"], exe=False, matching=False,
105            startstr="cat", endstr="rabbit", substrs=["abc", "def"]),
106            """\
107               Checking string:
108               "the quick brown fox"
109
110               Not expecting start string: "cat" (was not found)
111               Not expecting end string: "rabbit" (was not found)
112               Not expecting sub string: "abc" (was not found)
113               Not expecting sub string: "def" (was not found)
114               Not expecting regex pattern: "[0-9]+" (was not found)
115               Not expecting regex pattern: "fox" (was found, matched "fox")""")
116
117        # Extra assert messages are only printed when we get a failure
118        # So I can't test that from here, just how it looks when it's printed
119        self.assert_expect_fails_with("mouse",
120            dict(startstr="cat", exe=False, msg="Reason for check goes here!"),
121            """\
122               Checking string:
123               "mouse"
124
125               Expecting start string: "cat" (was not found)
126               Reason for check goes here!""")
127
128        # Verify expect() preconditions.
129        # Both `patterns` and `substrs` cannot be of type string.
130        self.assert_expect_fails_with("any command",
131            dict(patterns="some substring"),
132            "patterns must be a collection of strings")
133        self.assert_expect_fails_with("any command",
134            dict(substrs="some substring"),
135            "substrs must be a collection of strings")
136        # Prevent `self.expect("cmd", "substr")`
137        self.assert_expect_fails_with("any command",
138            dict(msg="some substring"),
139            "expect() missing a matcher argument")
140        # Prevent `self.expect("cmd", "msg", "substr")`
141        self.assert_expect_fails_with("any command",
142            dict(msg="a message", patterns="some substring"),
143            "must be a collection of strings")
144