xref: /llvm-project/lldb/test/API/commands/expression/fixits/TestFixIts.py (revision 99451b4453688a94c6014cac233d371ab4cc342d)
1"""
2Test calling an expression with errors that a FixIt can fix.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class ExprCommandWithFixits(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20
21        self.main_source = "main.cpp"
22        self.main_source_spec = lldb.SBFileSpec(self.main_source)
23
24    @skipUnlessDarwin
25    def test_with_target(self):
26        """Test calling expressions with errors that can be fixed by the FixIts."""
27        self.build()
28        self.try_expressions()
29
30    def test_with_dummy_target(self):
31        """Test calling expressions in the dummy target with errors that can be fixed by the FixIts."""
32        ret_val = lldb.SBCommandReturnObject()
33        result = self.dbg.GetCommandInterpreter().HandleCommand("expression ((1 << 16) - 1))", ret_val)
34        self.assertEqual(result, lldb.eReturnStatusSuccessFinishResult, "The expression was successful.")
35        self.assertTrue("Fix-it applied" in ret_val.GetError(), "Found the applied FixIt.")
36
37    def try_expressions(self):
38        """Test calling expressions with errors that can be fixed by the FixIts."""
39        (target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
40                                        'Stop here to evaluate expressions', self.main_source_spec)
41
42        options = lldb.SBExpressionOptions()
43        options.SetAutoApplyFixIts(True)
44
45        frame = self.thread.GetFrameAtIndex(0)
46
47        # Try with one error:
48        value = frame.EvaluateExpression("my_pointer.first", options)
49        self.assertTrue(value.IsValid())
50        self.assertTrue(value.GetError().Success())
51        self.assertTrue(value.GetValueAsUnsigned() == 10)
52
53        # Try with two errors:
54        two_error_expression = "my_pointer.second->a"
55        value = frame.EvaluateExpression(two_error_expression, options)
56        self.assertTrue(value.IsValid())
57        self.assertTrue(value.GetError().Success())
58        self.assertTrue(value.GetValueAsUnsigned() == 20)
59
60        # Now turn off the fixits, and the expression should fail:
61        options.SetAutoApplyFixIts(False)
62        value = frame.EvaluateExpression(two_error_expression, options)
63        self.assertTrue(value.IsValid())
64        self.assertTrue(value.GetError().Fail())
65        error_string = value.GetError().GetCString()
66        self.assertTrue(
67            error_string.find("fixed expression suggested:") != -1,
68            "Fix was suggested")
69        self.assertTrue(
70            error_string.find("my_pointer->second.a") != -1,
71            "Fix was right")
72