Skip to content

Commit 3567497

Browse files
committed
[lldb/Test] Introduce "assertSuccess"
Summary: A lot of our tests do 'self.assertTrue(error.Success()'. The problem with that is that when this fails, it produces a completely useless error message (False is not True) and the most important piece of information -- the actual error message -- is completely hidden. Sometimes we mitigate that by including the error message in the "msg" argument, but this has two additional problems: - as the msg argument is evaluated unconditionally, one needs to be careful to not trigger an exception when the operation was actually successful. - it requires more typing, which means we often don't do it assertSuccess solves these problems by taking the entire SBError object as an argument. If the operation was unsuccessful, it can format a reasonable error message itself. The function still accepts a "msg" argument, which can include any additional context, but this context now does not need to include the error message. To demonstrate usage, I replace a number of existing assertTrue assertions with the new function. As this process is not easily automatable, I have just manually updated a representative sample. In some cases, I did not update the code to use assertSuccess, but I went for even higher-level assertion apis (runCmd, expect_expr), as these are even shorter, and can produce even better failure messages. Reviewers: teemperor, JDevlieghere Subscribers: arphaman, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D82759
1 parent 82de018 commit 3567497

File tree

17 files changed

+73
-121
lines changed

17 files changed

+73
-121
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -2481,9 +2481,7 @@ def expect_expr(
24812481
else:
24822482
eval_result = self.target().EvaluateExpression(expr, options)
24832483

2484-
if not eval_result.GetError().Success():
2485-
self.assertTrue(eval_result.GetError().Success(),
2486-
"Unexpected failure with msg: " + eval_result.GetError().GetCString())
2484+
self.assertSuccess(eval_result.GetError())
24872485

24882486
if result_type:
24892487
self.assertEqual(result_type, eval_result.GetDisplayTypeName())
@@ -2535,6 +2533,13 @@ def run_platform_command(self, cmd):
25352533
err = platform.Run(shell_command)
25362534
return (err, shell_command.GetStatus(), shell_command.GetOutput())
25372535

2536+
"""Assert that an lldb.SBError is in the "success" state."""
2537+
def assertSuccess(self, obj, msg=None):
2538+
if not obj.Success():
2539+
error = obj.GetCString()
2540+
self.fail(self._formatMessage(msg,
2541+
"'{}' is not success".format(error)))
2542+
25382543
# =================================================
25392544
# Misc. helper methods for debugging test execution
25402545
# =================================================

lldb/test/API/commands/expression/call-restarts/TestCallThatRestarts.py

+14-27
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ def call_function(self):
5252
'Stop here in main.', self.main_source_spec)
5353

5454
# Make sure the SIGCHLD behavior is pass/no-stop/no-notify:
55-
return_obj = lldb.SBCommandReturnObject()
56-
self.dbg.GetCommandInterpreter().HandleCommand(
57-
"process handle SIGCHLD -s 0 -p 1 -n 0", return_obj)
58-
self.assertTrue(return_obj.Succeeded(), "Set SIGCHLD to pass, no-stop")
55+
self.runCmd("process handle SIGCHLD -s 0 -p 1 -n 0")
5956

6057
# The sigchld_no variable should be 0 at this point.
6158
self.sigchld_no = target.FindFirstGlobalVariable("sigchld_no")
@@ -84,7 +81,7 @@ def call_function(self):
8481
"call_me (%d)" %
8582
(num_sigchld), options)
8683
self.assertTrue(value.IsValid())
87-
self.assertTrue(value.GetError().Success())
84+
self.assertSuccess(value.GetError())
8885
self.assertEquals(value.GetValueAsSigned(-1), num_sigchld)
8986

9087
self.check_after_call(num_sigchld)
@@ -101,23 +98,21 @@ def call_function(self):
10198
"call_me (%d)" %
10299
(num_sigchld), options)
103100

104-
self.assertTrue(value.IsValid() and value.GetError().Success())
101+
self.assertTrue(value.IsValid())
102+
self.assertSuccess(value.GetError())
105103
self.assertEquals(value.GetValueAsSigned(-1), num_sigchld)
106104
self.check_after_call(num_sigchld)
107105

108106
# Now set the signal to print but not stop and make sure that calling
109107
# still works:
110-
self.dbg.GetCommandInterpreter().HandleCommand(
111-
"process handle SIGCHLD -s 0 -p 1 -n 1", return_obj)
112-
self.assertTrue(
113-
return_obj.Succeeded(),
114-
"Set SIGCHLD to pass, no-stop, notify")
108+
self.runCmd("process handle SIGCHLD -s 0 -p 1 -n 1")
115109

116110
value = frame.EvaluateExpression(
117111
"call_me (%d)" %
118112
(num_sigchld), options)
119113

120-
self.assertTrue(value.IsValid() and value.GetError().Success())
114+
self.assertTrue(value.IsValid())
115+
self.assertSuccess(value.GetError())
121116
self.assertEquals(value.GetValueAsSigned(-1), num_sigchld)
122117
self.check_after_call(num_sigchld)
123118

@@ -128,36 +123,28 @@ def call_function(self):
128123
"call_me (%d)" %
129124
(num_sigchld), options)
130125

131-
self.assertTrue(value.IsValid() and value.GetError().Success())
126+
self.assertTrue(value.IsValid())
127+
self.assertSuccess(value.GetError())
132128
self.assertEquals(value.GetValueAsSigned(-1), num_sigchld)
133129
self.check_after_call(num_sigchld)
134130

135131
# Okay, now set UnwindOnError to true, and then make the signal behavior to stop
136132
# and see that now we do stop at the signal point:
137133

138-
self.dbg.GetCommandInterpreter().HandleCommand(
139-
"process handle SIGCHLD -s 1 -p 1 -n 1", return_obj)
140-
self.assertTrue(
141-
return_obj.Succeeded(),
142-
"Set SIGCHLD to pass, stop, notify")
134+
self.runCmd("process handle SIGCHLD -s 1 -p 1 -n 1")
143135

144136
value = frame.EvaluateExpression(
145137
"call_me (%d)" %
146138
(num_sigchld), options)
147-
self.assertTrue(
148-
value.IsValid() and value.GetError().Success() == False)
139+
self.assertTrue(value.IsValid())
140+
self.assertFalse(value.GetError().Success())
149141

150142
# Set signal handling back to no-stop, and continue and we should end
151143
# up back in out starting frame:
152-
self.dbg.GetCommandInterpreter().HandleCommand(
153-
"process handle SIGCHLD -s 0 -p 1 -n 1", return_obj)
154-
self.assertTrue(
155-
return_obj.Succeeded(),
156-
"Set SIGCHLD to pass, no-stop, notify")
144+
self.runCmd("process handle SIGCHLD -s 0 -p 1 -n 1")
157145

158146
error = process.Continue()
159-
self.assertTrue(
160-
error.Success(),
147+
self.assertSuccess(error,
161148
"Continuing after stopping for signal succeeds.")
162149

163150
frame = self.thread.GetFrameAtIndex(0)

lldb/test/API/commands/expression/call-throws/TestCallThatThrows.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def call_function(self):
8888
options.SetTrapExceptions(False)
8989
value = frame.EvaluateExpression("[my_class iCatchMyself]", options)
9090
self.assertTrue(value.IsValid())
91-
self.assertTrue(value.GetError().Success())
91+
self.assertSuccess(value.GetError())
9292
self.assertEquals(value.GetValueAsUnsigned(), 57)
9393
self.check_after_call()
9494
options.SetTrapExceptions(True)

lldb/test/API/commands/expression/context-object-objc/TestContextObjectObjc.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ def test_context_object_objc(self):
4444
# Test retrieving of an objcClass's property through the self pointer
4545
value = obj_val.EvaluateExpression("self.property")
4646
self.assertTrue(value.IsValid())
47-
self.assertTrue(value.GetError().Success())
47+
self.assertSuccess(value.GetError())
4848
self.assertEqual(value.GetValueAsSigned(), 2222)
4949

5050
# Test objcClass's methods evaluation through the self pointer
5151
value = obj_val.EvaluateExpression("[self method]")
5252
self.assertTrue(value.IsValid())
53-
self.assertTrue(value.GetError().Success())
53+
self.assertSuccess(value.GetError())
5454
self.assertEqual(value.GetValueAsSigned(), 3333)
5555

5656
# Test if we can use a computation result reference object correctly
@@ -63,12 +63,12 @@ def test_context_object_objc(self):
6363
# Test an expression evaluation on it
6464
value = obj_val.EvaluateExpression("1")
6565
self.assertTrue(value.IsValid())
66-
self.assertTrue(value.GetError().Success())
66+
self.assertSuccess(value.GetError())
6767

6868
# Test retrieving of a field on it
6969
value = obj_val.EvaluateExpression("field")
7070
self.assertTrue(value.IsValid())
71-
self.assertTrue(value.GetError().Success())
71+
self.assertSuccess(value.GetError())
7272
self.assertEqual(value.GetValueAsSigned(), 1111)
7373

7474
def setUp(self):

lldb/test/API/commands/expression/context-object/TestContextObject.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ def test_context_object(self):
3232
# Test retrieveing of a field (not a local with the same name)
3333
value = obj_val.EvaluateExpression("field")
3434
self.assertTrue(value.IsValid())
35-
self.assertTrue(value.GetError().Success())
35+
self.assertSuccess(value.GetError())
3636
self.assertEqual(value.GetValueAsSigned(), 1111)
3737

3838
# Test functions evaluation
3939
value = obj_val.EvaluateExpression("function()")
4040
self.assertTrue(value.IsValid())
41-
self.assertTrue(value.GetError().Success())
41+
self.assertSuccess(value.GetError())
4242
self.assertEqual(value.GetValueAsSigned(), 2222)
4343

4444
# Test that we retrieve the right global
4545
value = obj_val.EvaluateExpression("global.field")
4646
self.assertTrue(value.IsValid())
47-
self.assertTrue(value.GetError().Success())
47+
self.assertSuccess(value.GetError())
4848
self.assertEqual(value.GetValueAsSigned(), 1111)
4949

5050
#
@@ -57,7 +57,7 @@ def test_context_object(self):
5757
# Test retrieveing of a field
5858
value = obj_val.EvaluateExpression("field_int")
5959
self.assertTrue(value.IsValid())
60-
self.assertTrue(value.GetError().Success())
60+
self.assertSuccess(value.GetError())
6161
self.assertEqual(value.GetValueAsSigned(), 5555)
6262

6363
#
@@ -87,7 +87,7 @@ def test_context_object(self):
8787
# Test retrieveing of an element's field
8888
value = obj_val.GetValueForExpressionPath("[7]").EvaluateExpression("field")
8989
self.assertTrue(value.IsValid())
90-
self.assertTrue(value.GetError().Success())
90+
self.assertSuccess(value.GetError())
9191
self.assertEqual(value.GetValueAsSigned(), 1111)
9292

9393
#
@@ -105,7 +105,7 @@ def test_context_object(self):
105105
# Test retrieveing of a dereferenced object's field
106106
value = obj_val.Dereference().EvaluateExpression("field")
107107
self.assertTrue(value.IsValid())
108-
self.assertTrue(value.GetError().Success())
108+
self.assertSuccess(value.GetError())
109109
self.assertEqual(value.GetValueAsSigned(), 1111)
110110

111111
#
@@ -135,7 +135,7 @@ def test_context_object(self):
135135
# Test retrieveing of a dereferenced object's field
136136
value = obj_val.Dereference().EvaluateExpression("field")
137137
self.assertTrue(value.IsValid())
138-
self.assertTrue(value.GetError().Success())
138+
self.assertSuccess(value.GetError())
139139
self.assertEqual(value.GetValueAsSigned(), 1111)
140140

141141
def setUp(self):

lldb/test/API/commands/expression/dont_allow_jit/TestAllowJIT.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def expr_options_test(self):
5858

5959
# Now use the options:
6060
result = frame.EvaluateExpression("call_me(10)", options)
61-
self.assertTrue(result.GetError().Success(), "expression succeeded")
61+
self.assertSuccess(result.GetError())
6262
self.assertEqual(result.GetValueAsSigned(), 18, "got the right value.")
6363

6464
# Now disallow JIT and make sure it fails:
@@ -77,6 +77,6 @@ def expr_options_test(self):
7777

7878
# And again, make sure this works:
7979
result = frame.EvaluateExpression("call_me(10)", options)
80-
self.assertTrue(result.GetError().Success(), "expression succeeded")
80+
self.assertSuccess(result.GetError())
8181
self.assertEqual(result.GetValueAsSigned(), 18, "got the right value.")
8282

lldb/test/API/commands/expression/fixits/TestFixIts.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_with_target(self):
4242
# Try with one error:
4343
value = frame.EvaluateExpression("my_pointer.first", options)
4444
self.assertTrue(value.IsValid())
45-
self.assertTrue(value.GetError().Success())
45+
self.assertSuccess(value.GetError())
4646
self.assertEquals(value.GetValueAsUnsigned(), 10)
4747

4848
# Try with one error in a top-level expression.
@@ -58,15 +58,15 @@ def test_with_target(self):
5858
two_error_expression = "my_pointer.second->a"
5959
value = frame.EvaluateExpression(two_error_expression, options)
6060
self.assertTrue(value.IsValid())
61-
self.assertTrue(value.GetError().Success())
61+
self.assertSuccess(value.GetError())
6262
self.assertEquals(value.GetValueAsUnsigned(), 20)
6363

6464
# Try a Fix-It that is stored in the 'note:' diagnostic of an error.
6565
# The Fix-It here is adding parantheses around the ToStr parameters.
6666
fixit_in_note_expr ="#define ToStr(x) #x\nToStr(0 {, })"
6767
value = frame.EvaluateExpression(fixit_in_note_expr, options)
6868
self.assertTrue(value.IsValid())
69-
self.assertTrue(value.GetError().Success(), value.GetError())
69+
self.assertSuccess(value.GetError())
7070
self.assertEquals(value.GetSummary(), '"(0 {, })"')
7171

7272
# Now turn off the fixits, and the expression should fail:

lldb/test/API/commands/expression/options/TestExprOptions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ def test_expr_options(self):
4646
# Make sure we can evaluate a C++11 expression.
4747
val = frame.EvaluateExpression('foo != nullptr')
4848
self.assertTrue(val.IsValid())
49-
self.assertTrue(val.GetError().Success())
49+
self.assertSuccess(val.GetError())
5050
self.DebugSBValue(val)
5151

5252
# Make sure it still works if language is set to C++11:
5353
options.SetLanguage(lldb.eLanguageTypeC_plus_plus_11)
5454
val = frame.EvaluateExpression('foo != nullptr', options)
5555
self.assertTrue(val.IsValid())
56-
self.assertTrue(val.GetError().Success())
56+
self.assertSuccess(val.GetError())
5757
self.DebugSBValue(val)
5858

5959
# Make sure it fails if language is set to C:
@@ -80,7 +80,7 @@ def test_expr_options_lang(self):
8080
options.SetLanguage(lldb.eLanguageTypeC_plus_plus_11)
8181
val = frame.EvaluateExpression('id == 0', options)
8282
self.assertTrue(val.IsValid())
83-
self.assertTrue(val.GetError().Success())
83+
self.assertSuccess(val.GetError())
8484
self.DebugSBValue(val)
8585

8686
# Make sure we can't retrieve `id` variable if language is set to ObjC:

lldb/test/API/commands/expression/pr35310/TestExprsBug35310.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,8 @@ def test_issue35310(self):
2323
"""
2424
self.build()
2525

26-
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
26+
lldbutil.run_to_source_breakpoint(self,
2727
'// Break here', self.main_source_spec)
28-
frame = thread.GetFrameAtIndex(0)
2928

30-
value = frame.EvaluateExpression("a.test_abi_tag()")
31-
self.assertTrue(value.IsValid())
32-
self.assertTrue(value.GetError().Success())
33-
self.assertEqual(value.GetValueAsSigned(0), 1)
34-
35-
value = frame.EvaluateExpression("a.test_asm_name()")
36-
self.assertTrue(value.IsValid())
37-
self.assertTrue(value.GetError().Success())
38-
self.assertEqual(value.GetValueAsSigned(0), 2)
29+
self.expect_expr("a.test_abi_tag()", result_value='1')
30+
self.expect_expr("a.test_asm_name()", result_value='2')

lldb/test/API/commands/expression/result_numbering/TestResultNumbering.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def do_numbering_test(self):
3333

3434
# Get the number of the last expression:
3535
result = thread.frames[0].EvaluateExpression("call_me(200)")
36-
self.assertTrue(result.GetError().Success(), "Our expression succeeded")
36+
self.assertSuccess(result.GetError(), "Our expression succeeded")
3737
name = result.GetName()
3838
ordinal = int(name[1:])
3939

@@ -42,7 +42,7 @@ def do_numbering_test(self):
4242
# The condition evaluation had to run a 4 expressions, but we haven't
4343
# run any user expressions.
4444
result = thread.frames[0].EvaluateExpression("call_me(200)")
45-
self.assertTrue(result.GetError().Success(), "Our expression succeeded the second time")
45+
self.assertSuccess(result.GetError(), "Our expression succeeded the second time")
4646
after_name = result.GetName()
4747
after_ordinal = int(after_name[1:])
4848
self.assertEqual(ordinal + 1, after_ordinal)

lldb/test/API/commands/expression/scoped_enums/TestScopedEnumType.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,8 @@ def test(self):
2323
self.expect("expr f == Foo::FooBar",
2424
substrs=['(bool) $0 = true'])
2525

26-
value = frame.EvaluateExpression("f == Foo::FooBar")
27-
self.assertTrue(value.IsValid())
28-
self.assertTrue(value.GetError().Success())
29-
self.assertEqual(value.GetValueAsUnsigned(), 1)
30-
31-
value = frame.EvaluateExpression("b == BarBar")
32-
self.assertTrue(value.IsValid())
33-
self.assertTrue(value.GetError().Success())
34-
self.assertEqual(value.GetValueAsUnsigned(), 1)
26+
self.expect_expr("f == Foo::FooBar", result_value='true')
27+
self.expect_expr("b == BarBar", result_value='true')
3528

3629
## b is not a Foo
3730
value = frame.EvaluateExpression("b == Foo::FooBar")

lldb/test/API/commands/expression/timeout/TestCallWithTimeout.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test(self):
5959
options.SetTimeoutInMicroSeconds(1000000)
6060
value = frame.EvaluateExpression("wait_a_while (1000)", options)
6161
self.assertTrue(value.IsValid())
62-
self.assertTrue(value.GetError().Success())
62+
self.assertSuccess(value.GetError())
6363

6464
# Now do the same thingwith the command line command, and make sure it
6565
# works too.
@@ -77,4 +77,4 @@ def test(self):
7777
options.SetOneThreadTimeoutInMicroSeconds(500000)
7878
value = frame.EvaluateExpression("wait_a_while (1000)", options)
7979
self.assertTrue(value.IsValid())
80-
self.assertTrue(value.GetError().Success())
80+
self.assertSuccess(value.GetError())

lldb/test/API/commands/expression/unwind_expression/TestUnwindExpression.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def test_conditional_bktp(self):
4545

4646
main_frame = self.thread.GetFrameAtIndex(0)
4747
val = main_frame.EvaluateExpression("second_function(47)", options)
48-
self.assertTrue(
49-
val.GetError().Success(),
50-
"We did complete the execution.")
48+
self.assertSuccess(val.GetError(), "We did complete the execution.")
5149
self.assertEquals(47, val.GetValueAsSigned())
5250

5351

@@ -92,8 +90,8 @@ def do_unwind_test(self, thread, bkpt, timeout):
9290

9391
# Now unwind the expression, and make sure we got back to where we
9492
# started.
95-
error = thread.UnwindInnermostExpression()
96-
self.assertTrue(error.Success(), "We succeeded in unwinding")
93+
self.assertSuccess(thread.UnwindInnermostExpression(),
94+
"We succeeded in unwinding")
9795

9896
cur_frame = thread.GetFrameAtIndex(0)
9997
self.assertTrue(

lldb/test/API/commands/expression/weak_symbols/TestWeakSymbols.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def run_weak_var_check (self, weak_varname, present):
3939
# the bug that expressions with no result currently return False for Success()...
4040
expr = "if (&" + weak_varname + " != NULL) { present_weak_int = 10; } else { present_weak_int = 20;}; 10"
4141
result = self.frame.EvaluateExpression(expr)
42-
self.assertTrue(result.GetError().Success(), "absent_weak_int expr failed: %s"%(result.GetError().GetCString()))
42+
self.assertSuccess(result.GetError(), "absent_weak_int expr failed")
4343
self.assertEqual(value.GetValueAsSigned(), correct_value, "Didn't change present_weak_int correctly.")
4444

4545
def do_test(self):

0 commit comments

Comments
 (0)