[lldb][NFC] Migrate several tests to expect_expr

expect_expr is the stricter and safer way of testing these expressions.
This commit is contained in:
Raphael Isemann 2020-01-15 13:30:04 +01:00
parent 00c74d0b64
commit 39d6b6c21f
21 changed files with 70 additions and 175 deletions

View File

@ -41,4 +41,4 @@ class TestExprLookupAnonStructTypedef(TestBase):
)
self.runCmd("run", RUN_SUCCEEDED)
self.expect("expr multiply(&s)", substrs=['$0 = 1'])
self.expect_expr("multiply(&s)", result_type="double", result_value="1")

View File

@ -34,9 +34,6 @@ class ExprCommandCallBuiltinFunction(TestBase):
self.runCmd("run", RUN_SUCCEEDED)
interp = self.dbg.GetCommandInterpreter()
result = lldb.SBCommandReturnObject()
# Test different builtin functions.
self.expect_expr("__builtin_isinf(0.0f)", result_type="int", result_value="0")

View File

@ -39,19 +39,15 @@ class ExprCommandCallUserDefinedFunction(TestBase):
self.runCmd("run", RUN_SUCCEEDED)
# Test recursive function call.
self.expect("expr fib(5)", substrs=['$0 = 5'])
self.expect_expr("fib(5)", result_type="unsigned int", result_value="5")
# Test function with more than one paramter
self.expect("expr add(4,8)", substrs=['$1 = 12'])
self.expect_expr("add(4, 8)", result_type="int", result_value="12")
# Test nesting function calls in function paramters
self.expect("expr add(add(5,2),add(3,4))", substrs=['$2 = 14'])
self.expect("expr add(add(5,2),fib(5))", substrs=['$3 = 12'])
self.expect_expr("add(add(5,2),add(3,4))", result_type="int", result_value="14")
self.expect_expr("add(add(5,2),fib(5))", result_type="int", result_value="12")
# Test function with pointer paramter
self.expect(
"exp stringCompare((const char*) \"Hello world\")",
substrs=['$4 = true'])
self.expect(
"exp stringCompare((const char*) \"Hellworld\")",
substrs=['$5 = false'])
self.expect_expr('stringCompare((const char*) \"Hello world\")', result_type="bool", result_value="true")
self.expect_expr('stringCompare((const char*) \"Hellworld\")', result_type="bool", result_value="false")

View File

@ -40,10 +40,10 @@ class ExprCommandCallOverriddenMethod(TestBase):
# Test call to method in base class (this should always work as the base
# class method is never an override).
self.expect("expr b->foo()", substrs=["= 2"])
self.expect_expr("b->foo()", result_type="int", result_value="2")
# Test calling the base class.
self.expect("expr realbase.foo()", substrs=["= 1"])
self.expect_expr("realbase.foo()", result_type="int", result_value="1")
@skipIfLinux # Returns wrong result code on some platforms.
def test_call_on_derived(self):
@ -61,7 +61,7 @@ class ExprCommandCallOverriddenMethod(TestBase):
# Test call to overridden method in derived class (this will fail if the
# overrides table is not correctly set up, as Derived::foo will be assigned
# a vtable entry that does not exist in the compiled program).
self.expect("expr d.foo()", substrs=["= 2"])
self.expect_expr("d.foo()", result_type="int", result_value="2")
@skipIf(oslist=["linux"], archs=["aarch64"])
@expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr43707")
@ -78,5 +78,5 @@ class ExprCommandCallOverriddenMethod(TestBase):
self.runCmd("run", RUN_SUCCEEDED)
# Test with locally constructed instances.
self.expect("expr Base().foo()", substrs=["= 1"])
self.expect("expr Derived().foo()", substrs=["= 2"])
self.expect_expr("Base().foo()", result_type="int", result_value="1")
self.expect_expr("Derived().foo()", result_type="int", result_value="2")

View File

@ -19,5 +19,4 @@ class TestClassTemplateSpecializationParametersHandling(TestBase):
lldbutil.run_to_source_breakpoint(self, '// break here',
lldb.SBFileSpec("main.cpp", False))
self.expect("expr -u 0 -- b.foo()", substrs=['$0 = 1'])
self.expect_expr("b.foo()", result_type="int", result_value="1")

View File

@ -26,7 +26,4 @@ class ExprEntryBPTestCase(TestBase):
entry_bp = target.BreakpointCreateBySBAddress(entry)
self.assertTrue(entry_bp.IsValid(), "Can't set a breakpoint on the module entry point")
result = target.EvaluateExpression("sum(7, 1)")
self.assertTrue(result.IsValid(), "Can't evaluate expression")
self.assertEqual(8, result.GetValueAsSigned())
self.expect_expr("sum(7, 1)", result_type="int", result_value="8")

View File

@ -22,13 +22,13 @@ class ImportStdModule(TestBase):
# Activate importing of std module.
self.runCmd("settings set target.import-std-module true")
# Calling some normal std functions that return non-template types.
self.expect("expr std::abs(-42)", substrs=['(int) $0 = 42'])
self.expect("expr std::div(2, 1).quot", substrs=['(int) $1 = 2'])
self.expect_expr("std::abs(-42)", result_type="int", result_value="42")
self.expect_expr("std::div(2, 1).quot", result_type="int", result_value="2")
# Using types from std.
self.expect("expr (std::size_t)33U", substrs=['(size_t) $2 = 33'])
self.expect_expr("(std::size_t)33U", result_type="size_t", result_value="33")
# Calling templated functions that return non-template types.
self.expect("expr char char_a = 'b'; char char_b = 'a'; std::swap(char_a, char_b); char_a",
substrs=["(char) $3 = 'a'"])
self.expect_expr("char char_a = 'b'; char char_b = 'a'; std::swap(char_a, char_b); char_a",
result_type="char", result_value="'a'")
@add_test_categories(["libc++"])
@skipIf(compiler=no_match("clang"))

View File

@ -25,8 +25,8 @@ class TestImportStdModuleConflicts(TestBase):
"// Set break point at this line.", lldb.SBFileSpec("main.cpp"))
self.runCmd("settings set target.import-std-module true")
self.expect("expr std::abs(-42)", substrs=['(int) $0 = 42'])
self.expect("expr std::div(2, 1).quot", substrs=['(int) $1 = 2'])
self.expect("expr (std::size_t)33U", substrs=['(size_t) $2 = 33'])
self.expect_expr("std::abs(-42)", result_type="int", result_value="42")
self.expect_expr("std::div(2, 1).quot", result_type="int", result_value="2")
self.expect_expr("(std::size_t)33U", result_type="size_t", result_value="33")
self.expect("expr char char_a = 'b'; char char_b = 'a'; std::swap(char_a, char_b); char_a",
substrs=["(char) $3 = 'a'"])

View File

@ -20,18 +20,18 @@ class TestBasicDeque(TestBase):
self.runCmd("settings set target.import-std-module true")
self.expect("expr (size_t)a.size()", substrs=['(size_t) $0 = 3'])
self.expect("expr (int)a.front()", substrs=['(int) $1 = 3'])
self.expect("expr (int)a.back()", substrs=['(int) $2 = 2'])
self.expect_expr("(size_t)a.size()", result_type="size_t", result_value="3")
self.expect_expr("(int)a.front()", result_type="int", result_value="3")
self.expect_expr("(int)a.back()", result_type="int", result_value="2")
self.expect("expr std::sort(a.begin(), a.end())")
self.expect("expr (int)a.front()", substrs=['(int) $3 = 1'])
self.expect("expr (int)a.back()", substrs=['(int) $4 = 3'])
self.expect_expr("(int)a.front()", result_type="int", result_value="1")
self.expect_expr("(int)a.back()", result_type="int", result_value="3")
self.expect("expr std::reverse(a.begin(), a.end())")
self.expect("expr (int)a.front()", substrs=['(int) $5 = 3'])
self.expect("expr (int)a.back()", substrs=['(int) $6 = 1'])
self.expect_expr("(int)a.front()", result_type="int", result_value="3")
self.expect_expr("(int)a.back()", result_type="int", result_value="1")
self.expect("expr (int)(*a.begin())", substrs=['(int) $7 = 3'])
self.expect("expr (int)(*a.rbegin())", substrs=['(int) $8 = 1'])
self.expect_expr("(int)(*a.begin())", result_type="int", result_value="3")
self.expect_expr("(int)(*a.rbegin())", result_type="int", result_value="1")

View File

@ -20,7 +20,7 @@ class TestInlineNamespace(TestBase):
# The 'A::B::f' function must be found via 'A::f' as 'B' is an inline
# namespace.
self.expect("expr A::f()", substrs=['$0 = 3'])
self.expect_expr("A::f()", result_type="int", result_value="3")
# But we should still find the function when we pretend the inline
# namespace is not inline.
self.expect("expr A::B::f()", substrs=['$1 = 3'])
self.expect_expr("A::B::f()", result_type="int", result_value="3")

View File

@ -15,10 +15,6 @@ class TestNamespaceLocalVarSameNameCppAndC(TestBase):
(self.target, self.process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here',
lldb.SBFileSpec("main.cpp", False))
self.expect("expr error",
substrs=['(int) $0 = 1'])
self.expect_expr("error", result_type="int", result_value="1")
lldbutil.continue_to_breakpoint(self.process, bkpt)
self.expect("expr error",
substrs=['(int) $1 = 1'])
self.expect_expr("error", result_type="int", result_value="1")

View File

@ -22,13 +22,8 @@ class Radar8638051TestCase(TestBase):
self.runCmd("run", RUN_SUCCEEDED)
self.expect("expression val",
startstr="(int) $0 = 1")
# (int) $0 = 1
self.expect("expression *(&val)",
startstr="(int) $1 = 1")
# (int) $1 = 1
self.expect_expr("val", result_type="int", result_value="1")
self.expect_expr("*(&val)", result_type="int", result_value="1")
# rdar://problem/8638051
# lldb expression command: Could this crash be avoided

View File

@ -17,9 +17,9 @@ class StaticInitializers(TestBase):
lldb.SBFileSpec("main.cpp", False))
# We use counter to observe if the initializer was called.
self.expect("expr counter", substrs=["(int) $", " = 0"])
self.expect_expr("counter", result_type="int", result_value="0")
self.expect("expr -p -- struct Foo { Foo() { inc_counter(); } }; Foo f;")
self.expect("expr counter", substrs=["(int) $", " = 1"])
self.expect_expr("counter", result_type="int", result_value="1")
def test_failing_init(self):
""" Test a static initializer that fails to execute. """

View File

@ -61,37 +61,14 @@ class BasicExprCommandsTestCase(TestBase):
def test_many_expr_commands(self):
self.build_and_run()
self.expect("expression 2",
patterns=["\(int\) \$.* = 2"])
# (int) $0 = 1
self.expect("expression 2ull",
patterns=["\(unsigned long long\) \$.* = 2"])
# (unsigned long long) $1 = 2
self.expect("expression 0.5f",
patterns=["\(float\) \$.* = 0\.5"])
# (float) $2 = 0.5
self.expect("expression 2.234",
patterns=["\(double\) \$.* = 2\.234"])
# (double) $3 = 2.234
self.expect("expression 2+3",
patterns=["\(int\) \$.* = 5"])
# (int) $4 = 5
self.expect("expression argc",
patterns=["\(int\) \$.* = 1"])
# (int) $5 = 1
self.expect("expression argc + 22",
patterns=["\(int\) \$.* = 23"])
# (int) $6 = 23
self.expect("expression argv",
patterns=["\(const char \*\*\) \$.* = 0x"])
# (const char *) $7 = ...
self.expect_expr("2", result_type="int", result_value="2")
self.expect_expr("2ull", result_type="unsigned long long", result_value="2")
self.expect_expr("0.5f", result_type="float", result_value="0.5")
self.expect_expr("2.234", result_type="double", result_value="2.234")
self.expect_expr("2+3", result_type="int", result_value="5")
self.expect_expr("argc", result_type="int", result_value="1")
self.expect_expr("argc + 22", result_type="int", result_value="23")
self.expect_expr("argv", result_type="const char **")
self.expect("expression argv[0]",
substrs=["(const char *)",

View File

@ -27,7 +27,7 @@ class CPPAutoTestCase(TestBase):
self.runCmd("process launch", RUN_SUCCEEDED)
self.expect('expr auto f = 123456; f', substrs=['int', '123456'])
self.expect_expr('auto f = 123456; f', result_type='int', result_value='123456')
self.expect(
'expr struct Test { int x; int y; Test() : x(123), y(456) {} }; auto t = Test(); t',
substrs=[

View File

@ -21,8 +21,5 @@ class CPPBoolTestCase(TestBase):
self.runCmd("process launch", RUN_SUCCEEDED)
self.expect("expression -- bool second_bool = my_bool; second_bool",
startstr="(bool) $0 = false")
self.expect("expression -- my_bool = true",
startstr="(bool) $1 = true")
self.expect_expr("bool second_bool = my_bool; second_bool", result_type="bool", result_value="false")
self.expect_expr("my_bool = true", result_type="bool", result_value="true")

View File

@ -31,5 +31,4 @@ class CallCPPFunctionTestCase(TestBase):
STOPPED_DUE_TO_BREAKPOINT,
substrs=['stopped', 'stop reason = breakpoint'])
self.expect("expression -- a_function_to_call()",
startstr="(int) $0 = 0")
self.expect_expr("a_function_to_call()", result_type="int", result_value="0")

View File

@ -41,59 +41,16 @@ class TestCppChainedCalls(TestBase):
self.assertTrue(
process.GetState() == lldb.eStateStopped,
PROCESS_STOPPED)
thread = lldbutil.get_stopped_thread(
process, lldb.eStopReasonBreakpoint)
# Get frame for current thread
frame = thread.GetSelectedFrame()
lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
# Test chained calls
test_result = frame.EvaluateExpression("get(set(true))")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "true",
"get(set(true)) = true")
test_result = frame.EvaluateExpression("get(set(false))")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "false",
"get(set(false)) = false")
test_result = frame.EvaluateExpression("get(t & f)")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "false",
"get(t & f) = false")
test_result = frame.EvaluateExpression("get(f & t)")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "false",
"get(f & t) = false")
test_result = frame.EvaluateExpression("get(t & t)")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "true",
"get(t & t) = true")
test_result = frame.EvaluateExpression("get(f & f)")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "false",
"get(f & f) = false")
test_result = frame.EvaluateExpression("get(t & f)")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "false",
"get(t & f) = false")
test_result = frame.EvaluateExpression("get(f) && get(t)")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "false",
"get(f) && get(t) = false")
test_result = frame.EvaluateExpression("get(f) && get(f)")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "false",
"get(f) && get(t) = false")
test_result = frame.EvaluateExpression("get(t) && get(t)")
self.assertTrue(
test_result.IsValid() and test_result.GetValue() == "true",
"get(t) && get(t) = true")
self.expect_expr("get(set(true))", result_type="bool", result_value="true")
self.expect_expr("get(set(false))", result_type="bool", result_value="false")
self.expect_expr("get(t & f)", result_type="bool", result_value="false")
self.expect_expr("get(f & t)", result_type="bool", result_value="false")
self.expect_expr("get(t & t)", result_type="bool", result_value="true")
self.expect_expr("get(f & f)", result_type="bool", result_value="false")
self.expect_expr("get(t & f)", result_type="bool", result_value="false")
self.expect_expr("get(f) && get(t)", result_type="bool", result_value="false")
self.expect_expr("get(f) && get(f)", result_type="bool", result_value="false")
self.expect_expr("get(t) && get(t)", result_type="bool", result_value="true")

View File

@ -107,13 +107,9 @@ class Char1632TestCase(TestBase):
'""'])
# check that zero values are properly handles
self.expect('frame variable cs16_zero', substrs=["U+0000 u'\\0'"])
self.expect(
'frame variable cs32_zero',
substrs=["U+0x00000000 U'\\0'"])
self.expect('expression cs16_zero', substrs=["U+0000 u'\\0'"])
self.expect('expression cs32_zero', substrs=["U+0x00000000 U'\\0'"])
self.expect_expr('cs16_zero', result_summary="U+0000 u'\\0'")
self.expect_expr('cs32_zero', result_summary="U+0x00000000 U'\\0'")
# Check that we can run expressions that return charN_t
self.expect("expression u'a'", substrs=['(char16_t) $', "61 u'a'"])
self.expect("expression U'a'", substrs=['(char32_t) $', "61 U'a'"])
self.expect_expr("u'a'", result_type="char16_t", result_summary="U+0061 u'a'")
self.expect_expr("U'a'", result_type="char32_t", result_summary="U+0x00000061 U'a'")

View File

@ -29,11 +29,6 @@ class CxxChar8_tTestCase(TestBase):
lldbutil.run_break_set_by_symbol(self, 'main')
self.runCmd("run", RUN_SUCCEEDED)
self.expect(
"frame variable a", substrs=["(char8_t)", "0x61 u8'a'"])
self.expect(
"frame variable ab", substrs=['(const char8_t *)' , 'u8"你好"'])
self.expect(
"frame variable abc", substrs=['(char8_t [9])', 'u8"你好"'])
self.expect_expr("a", result_type="char8_t", result_summary="0x61 u8'a'")
self.expect_expr("ab", result_type="const char8_t *", result_summary='u8"你好"')
self.expect_expr("abc", result_type="char8_t [9]", result_summary='u8"你好"')

View File

@ -46,13 +46,7 @@ class TestWithLimitDebugInfo(TestBase):
# Get frame for current thread
frame = thread.GetSelectedFrame()
v1 = frame.EvaluateExpression("1")
self.assertTrue(
v1.IsValid(),
"'expr 1' results in a valid SBValue object")
self.assertTrue(
v1.GetError().Success(),
"'expr 1' succeeds without an error.")
self.expect_expr("1", result_type="int", result_value="1")
v2 = frame.EvaluateExpression("this")
self.assertTrue(