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