Skip to content

Commit 39a1a9e

Browse files
committed
[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. (cherry picked from commit 6d2b828)
1 parent 2ec15cf commit 39a1a9e

File tree

9 files changed

+27
-22
lines changed

9 files changed

+27
-22
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,8 +2330,9 @@ def expect(
23302330
matches the patterns contained in 'patterns'.
23312331
23322332
When matching is true and ordered is true, which are both the default,
2333-
the strings in the substrs array have to appear in the command output
2334-
in the order in which they appear in the array.
2333+
the strings in the substrs array, and regex in the patterns array, have
2334+
to appear in the command output in the order in which they appear in
2335+
their respective array.
23352336
23362337
If the keyword argument error is set to True, it signifies that the API
23372338
client is expecting the command to fail. In this case, the error stream
@@ -2434,9 +2435,9 @@ def found_str(matched):
24342435
if substrs and matched == matching:
24352436
start = 0
24362437
for substr in substrs:
2437-
index = output[start:].find(substr)
2438-
start = start + index + len(substr) if ordered and matching else 0
2438+
index = output.find(substr, start)
24392439
matched = index != -1
2440+
start = index + len(substr) if ordered and matched else 0
24402441
log_lines.append(
24412442
'{} sub string: "{}" ({})'.format(
24422443
expecting_str, substr, found_str(matched)
@@ -2447,20 +2448,21 @@ def found_str(matched):
24472448
break
24482449

24492450
if patterns and matched == matching:
2451+
start = 0
24502452
for pattern in patterns:
2451-
matched = re.search(pattern, output)
2453+
pat = re.compile(pattern)
2454+
match = pat.search(output, start)
2455+
matched = bool(match)
2456+
start = match.end() if ordered and matched else 0
24522457

24532458
pattern_line = '{} regex pattern: "{}" ({}'.format(
24542459
expecting_str, pattern, found_str(matched)
24552460
)
2456-
if matched:
2457-
pattern_line += ', matched "{}"'.format(matched.group(0))
2461+
if match:
2462+
pattern_line += ', matched "{}"'.format(match.group(0))
24582463
pattern_line += ")"
24592464
log_lines.append(pattern_line)
24602465

2461-
# Convert to bool because match objects
2462-
# are True-ish but is not True itself
2463-
matched = bool(matched)
24642466
if matched != matching:
24652467
break
24662468

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/breakpoint/breakpoint_locations/TestBreakpointLocations.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ def set_breakpoint(self):
4747
self.expect(
4848
"breakpoint list -f",
4949
"Breakpoint locations shown correctly",
50+
ordered=False,
5051
substrs=[
51-
"1: file = 'main.c', line = %d, exact_match = 0, locations = 3"
52-
% self.line
52+
f"1: file = 'main.c', line = {self.line}, exact_match = 0, locations = 3"
5353
],
5454
patterns=[
55-
"where = a.out`func_inlined .+unresolved, hit count = 0",
56-
"where = a.out`main .+\[inlined\].+unresolved, hit count = 0",
55+
"where = a.out`func_inlined .+?unresolved, hit count = 0",
56+
r"where = a.out`main .+?\[inlined\].+?unresolved, hit count = 0",
5757
],
5858
)
5959

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
"\(__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
"\(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/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: 2 additions & 2 deletions
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-
"\((short int|short)\) the_signed_short = 99",
61-
"\((signed char|char)\) the_signed_char = 'c'",
60+
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=["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)