diff --git a/lldb/packages/Python/lldbsuite/test/lldbtest.py b/lldb/packages/Python/lldbsuite/test/lldbtest.py --- a/lldb/packages/Python/lldbsuite/test/lldbtest.py +++ b/lldb/packages/Python/lldbsuite/test/lldbtest.py @@ -2388,7 +2388,35 @@ self.assertTrue(cmd_status == 0) - def expect( + + def expect(self, str, *args, **kwargs): + "Validate `expect` arguments before calling `_expect_impl`" + # Support success/fail only checks like: + # self.expect("lldb command") + if not args and not kwargs: + return self._expect(str) + + # Prevent this ambiguous mistake: + # self.expect("lldb command", "some string") + # Instead, this invocation must be changed to either: + # self.expect("lldb command", msg="some string") + # or use a matcher of some kind, such as: + # self.expect("lldb command", substrs=["some string"]) + if args: + needed = ("patterns", "startstr", "endstr", "substrs", "error") + assert any(arg in kwargs for arg in needed), \ + "expect() requires a keyword argument" + + # Check `patterns` and `substrs` are not accidentally given as strings. + assert not isinstance(kwargs.get("patterns"), six.string_types), \ + "patterns must be a collection of strings" + assert not isinstance(kwargs.get("substrs"), six.string_types), \ + "substrs must be a collection of strings" + + return self._expect(str, **kwargs) + + + def _expect_impl( self, str, msg=None, @@ -2429,22 +2457,6 @@ set to False, the 'str' is treated as a string to be matched/not-matched against the golden input. """ - # Catch cases where `expect` has been miscalled. Specifically, prevent - # this easy to make mistake: - # self.expect("lldb command", "some substr") - # The `msg` parameter is used only when a failed match occurs. A failed - # match can only occur when one of `patterns`, `startstr`, `endstr`, or - # `substrs` has been given. Thus, if a `msg` is given, it's an error to - # not also provide one of the matcher parameters. - if msg and not (patterns or startstr or endstr or substrs or error): - assert False, "expect() missing a matcher argument" - - # Check `patterns` and `substrs` are not accidentally given as strings. - assert not isinstance(patterns, six.string_types), \ - "patterns must be a collection of strings" - assert not isinstance(substrs, six.string_types), \ - "substrs must be a collection of strings" - trace = (True if traceAlways else trace) if exe: diff --git a/lldb/test/API/assert_messages_test/TestAssertMessages.py b/lldb/test/API/assert_messages_test/TestAssertMessages.py --- a/lldb/test/API/assert_messages_test/TestAssertMessages.py +++ b/lldb/test/API/assert_messages_test/TestAssertMessages.py @@ -122,10 +122,6 @@ self.assert_expect_fails_with("any command", dict(substrs="some substring"), "substrs must be a collection of strings") - # Prevent `self.expect("cmd", "substr")` - self.assert_expect_fails_with("any command", - dict(msg="some substring"), - "expect() missing a matcher argument") # Prevent `self.expect("cmd", "msg", "substr")` self.assert_expect_fails_with("any command", dict(msg="a message", patterns="some substring"),