Skip to content

Commit 6d2b828

Browse files
authored
[lldb] Support ordered patterns in lldbtest.expect (llvm#131475)
Change `lldbtest.expect` to require the regexes in `patterns` be found in order – when the `ordered` parameter is true. This matches the behavior of `substrs`. The `ordered` parameter is true by default, so this change also fixes tests by either tweaking the patterns to work in order, or by setting `ordered=False`. I have often wanted to test with `patterns` and also verify the order. This change allows that.
1 parent 166937b commit 6d2b828

File tree

9 files changed

+23
-18
lines changed

9 files changed

+23
-18
lines changed

lldb/packages/Python/lldbsuite/test/lldbtest.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,8 +2358,9 @@ def expect(
23582358
matches the patterns contained in 'patterns'.
23592359
23602360
When matching is true and ordered is true, which are both the default,
2361-
the strings in the substrs array have to appear in the command output
2362-
in the order in which they appear in the array.
2361+
the strings in the substrs array, and regex in the patterns array, have
2362+
to appear in the command output in the order in which they appear in
2363+
their respective array.
23632364
23642365
If the keyword argument error is set to True, it signifies that the API
23652366
client is expecting the command to fail. In this case, the error stream
@@ -2462,9 +2463,9 @@ def found_str(matched):
24622463
if substrs and matched == matching:
24632464
start = 0
24642465
for substr in substrs:
2465-
index = output[start:].find(substr)
2466-
start = start + index + len(substr) if ordered and matching else 0
2466+
index = output.find(substr, start)
24672467
matched = index != -1
2468+
start = index + len(substr) if ordered and matched else 0
24682469
log_lines.append(
24692470
'{} sub string: "{}" ({})'.format(
24702471
expecting_str, substr, found_str(matched)
@@ -2475,20 +2476,21 @@ def found_str(matched):
24752476
break
24762477

24772478
if patterns and matched == matching:
2479+
start = 0
24782480
for pattern in patterns:
2479-
matched = re.search(pattern, output)
2481+
pat = re.compile(pattern)
2482+
match = pat.search(output, start)
2483+
matched = bool(match)
2484+
start = match.end() if ordered and matched else 0
24802485

24812486
pattern_line = '{} regex pattern: "{}" ({}'.format(
24822487
expecting_str, pattern, found_str(matched)
24832488
)
2484-
if matched:
2485-
pattern_line += ', matched "{}"'.format(matched.group(0))
2489+
if match:
2490+
pattern_line += ', matched "{}"'.format(match.group(0))
24862491
pattern_line += ")"
24872492
log_lines.append(pattern_line)
24882493

2489-
# Convert to bool because match objects
2490-
# are True-ish but is not True itself
2491-
matched = bool(matched)
24922494
if matched != matching:
24932495
break
24942496

lldb/test/API/functionalities/alias/TestBtAliasRepeat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test(self):
99
lldbutil.run_to_source_breakpoint(self, "return", lldb.SBFileSpec("main.c"))
1010

1111
# Expect "frame #0" but not "frame #1".
12-
self.expect("bt 1", inHistory=True, patterns=["frame #0", "^(?!.*frame #1)"])
12+
self.expect("bt 1", inHistory=True, patterns=["frame #0", "(?!.*frame #1)"])
1313

1414
# Run an empty command to run the repeat command for `bt`.
1515
# The repeat command for `bt N` lists the subsequent N frames.

lldb/test/API/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjCNSContainer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def nscontainers_data_formatter_commands(self):
5252

5353
self.expect(
5454
"frame variable -d run-target *nscfDictionary",
55+
ordered=False,
5556
patterns=[
5657
r"\(__NSCFDictionary\) \*nscfDictionary =",
5758
'key = 0x.* @"foo"',
@@ -67,6 +68,7 @@ def nscontainers_data_formatter_commands(self):
6768

6869
self.expect(
6970
"frame variable -d run-target *cfDictionaryRef",
71+
ordered=False,
7072
patterns=[
7173
r"\(const __CFDictionary\) \*cfDictionaryRef =",
7274
'key = 0x.* @"foo"',

lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/unordered/TestDataFormatterGenericUnordered.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def cleanup():
122122
)
123123

124124
def look_for_content_and_continue(self, var_name, patterns):
125-
self.expect(("frame variable %s" % var_name), patterns=patterns)
126-
self.expect(("frame variable %s" % var_name), patterns=patterns)
125+
self.expect(("frame variable %s" % var_name), ordered=False, patterns=patterns)
126+
self.expect(("frame variable %s" % var_name), ordered=False, patterns=patterns)
127127
self.runCmd("continue")
128128

129129
@add_test_categories(["libstdcxx"])

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/TestDataFormatterLibcxxSpan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,4 @@ def test_ref_and_ptr(self):
172172

173173
# The pointer should just show the right number of elements:
174174

175-
self.expect("frame variable ptr", patterns=["ptr = 0x.*", " size=5"])
175+
self.expect("frame variable ptr", patterns=["ptr = 0x[0-9a-f]+ size=5"])

lldb/test/API/functionalities/data-formatter/root-reference-children/TestRootReferenceChildren.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def test(self):
2020
"v summary_and_children_ref", substrs=["some summary", "child = 30"]
2121
)
2222
self.expect(
23-
"v summary_only_ref", patterns=["some summary", "(?s)^(?!.*child = )"]
23+
"v summary_only_ref", patterns=["some summary", "(?s)(?!.*child = )"]
2424
)
2525
self.expect(
26-
"v children_only_ref", patterns=["(?s)^(?!.*some summary)", "child = 30"]
26+
"v children_only_ref", patterns=["(?s)(?!.*some summary)", "child = 30"]
2727
)

lldb/test/API/lang/cpp/signed_types/TestSignedTypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def test(self):
5757
"frame variable --show-types --no-args",
5858
VARIABLES_DISPLAYED_CORRECTLY,
5959
patterns=[
60-
r"\((short int|short)\) the_signed_short = 99",
6160
r"\((signed char|char)\) the_signed_char = 'c'",
61+
r"\((short int|short)\) the_signed_short = 99",
6262
],
6363
substrs=[
6464
"(int) the_signed_int = 99",

lldb/test/API/lang/objc/foundation/TestObjCMethods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_data_type_and_expr(self):
166166
"frame variable --show-types --scope",
167167
VARIABLES_DISPLAYED_CORRECTLY,
168168
substrs=["ARG: (MyString *) self"],
169-
patterns=[r"ARG: \(.*\) _cmd", "(objc_selector *)|(SEL)"],
169+
patterns=[r"ARG: \(SEL|objc_selector \*\) _cmd"],
170170
)
171171

172172
# rdar://problem/8651752

lldb/test/API/source-manager/TestSourceManager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def do_display_source_python_api(
129129
stream.GetData(),
130130
"Source code displayed correctly:\n" + stream.GetData(),
131131
exe=False,
132+
ordered=False,
132133
patterns=["=>", "%d.*Hello world" % self.line, needle_regex],
133134
)
134135

0 commit comments

Comments
 (0)