Files
clang-p2996/lldb/test/API/commands/expression/context-object/TestContextObject.py
Pavel Labath 35674976f0 [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
2020-06-30 15:41:03 +02:00

146 lines
4.6 KiB
Python

"""
Tests expression evaluation in context of an object.
"""
import lldb
import lldbsuite.test.lldbutil as lldbutil
from lldbsuite.test.lldbtest import *
class ContextObjectTestCase(TestBase):
mydir = TestBase.compute_mydir(__file__)
def test_context_object(self):
"""Tests expression evaluation in context of an object."""
self.build()
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, '// Break here', self.main_source_spec)
frame = thread.GetFrameAtIndex(0)
#
# Test C++ struct variable
#
obj_val = frame.FindVariable("cpp_struct")
self.assertTrue(obj_val.IsValid())
# Test an empty expression evaluation
value = obj_val.EvaluateExpression("")
self.assertFalse(value.IsValid())
self.assertFalse(value.GetError().Success())
# Test retrieveing of a field (not a local with the same name)
value = obj_val.EvaluateExpression("field")
self.assertTrue(value.IsValid())
self.assertSuccess(value.GetError())
self.assertEqual(value.GetValueAsSigned(), 1111)
# Test functions evaluation
value = obj_val.EvaluateExpression("function()")
self.assertTrue(value.IsValid())
self.assertSuccess(value.GetError())
self.assertEqual(value.GetValueAsSigned(), 2222)
# Test that we retrieve the right global
value = obj_val.EvaluateExpression("global.field")
self.assertTrue(value.IsValid())
self.assertSuccess(value.GetError())
self.assertEqual(value.GetValueAsSigned(), 1111)
#
# Test C++ union variable
#
obj_val = frame.FindVariable("cpp_union")
self.assertTrue(obj_val.IsValid())
# Test retrieveing of a field
value = obj_val.EvaluateExpression("field_int")
self.assertTrue(value.IsValid())
self.assertSuccess(value.GetError())
self.assertEqual(value.GetValueAsSigned(), 5555)
#
# Test C++ scalar
#
obj_val = frame.FindVariable("cpp_scalar")
self.assertTrue(obj_val.IsValid())
# Test an expression evaluation
value = obj_val.EvaluateExpression("1")
self.assertFalse(value.IsValid())
self.assertFalse(value.GetError().Success())
#
# Test C++ array
#
obj_val = frame.FindVariable("cpp_array")
self.assertTrue(obj_val.IsValid())
# Test an expression evaluation
value = obj_val.EvaluateExpression("1")
self.assertFalse(value.IsValid())
self.assertFalse(value.GetError().Success())
# Test retrieveing of an element's field
value = obj_val.GetValueForExpressionPath("[7]").EvaluateExpression("field")
self.assertTrue(value.IsValid())
self.assertSuccess(value.GetError())
self.assertEqual(value.GetValueAsSigned(), 1111)
#
# Test C++ pointer
#
obj_val = frame.FindVariable("cpp_pointer")
self.assertTrue(obj_val.IsValid())
# Test an expression evaluation
value = obj_val.EvaluateExpression("1")
self.assertFalse(value.IsValid())
self.assertFalse(value.GetError().Success())
# Test retrieveing of a dereferenced object's field
value = obj_val.Dereference().EvaluateExpression("field")
self.assertTrue(value.IsValid())
self.assertSuccess(value.GetError())
self.assertEqual(value.GetValueAsSigned(), 1111)
#
# Test C++ computation result
#
obj_val = frame.EvaluateExpression("cpp_namespace::GetCppStruct()")
self.assertTrue(obj_val.IsValid())
# Test an expression evaluation
value = obj_val.EvaluateExpression("1")
self.assertTrue(value.IsValid())
self.assertFalse(value.GetError().Success())
#
# Test C++ computation result located in debuggee memory
#
obj_val = frame.EvaluateExpression("cpp_namespace::GetCppStructPtr()")
self.assertTrue(obj_val.IsValid())
# Test an expression evaluation
value = obj_val.EvaluateExpression("1")
self.assertFalse(value.IsValid())
self.assertFalse(value.GetError().Success())
# Test retrieveing of a dereferenced object's field
value = obj_val.Dereference().EvaluateExpression("field")
self.assertTrue(value.IsValid())
self.assertSuccess(value.GetError())
self.assertEqual(value.GetValueAsSigned(), 1111)
def setUp(self):
TestBase.setUp(self)
self.main_source = "main.cpp"
self.main_source_spec = lldb.SBFileSpec(self.main_source)