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