xref: /llvm-project/lldb/test/API/commands/expression/fixits/TestFixIts.py (revision eecef3af2ca8b27f4070be41c73bff989a096f9b)
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
33        # Enable fix-its as they were intentionally disabled by TestBase.setUp.
34        self.runCmd("settings set target.auto-apply-fixits true")
35
36        ret_val = lldb.SBCommandReturnObject()
37        result = self.dbg.GetCommandInterpreter().HandleCommand("expression ((1 << 16) - 1))", ret_val)
38        self.assertEqual(result, lldb.eReturnStatusSuccessFinishResult, "The expression was successful.")
39        self.assertTrue("Fix-it applied" in ret_val.GetError(), "Found the applied FixIt.")
40
41    def try_expressions(self):
42        """Test calling expressions with errors that can be fixed by the FixIts."""
43        (target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
44                                        'Stop here to evaluate expressions', self.main_source_spec)
45
46        options = lldb.SBExpressionOptions()
47        options.SetAutoApplyFixIts(True)
48
49        frame = self.thread.GetFrameAtIndex(0)
50
51        # Try with one error:
52        value = frame.EvaluateExpression("my_pointer.first", options)
53        self.assertTrue(value.IsValid())
54        self.assertTrue(value.GetError().Success())
55        self.assertEquals(value.GetValueAsUnsigned(), 10)
56
57        # Try with two errors:
58        two_error_expression = "my_pointer.second->a"
59        value = frame.EvaluateExpression(two_error_expression, options)
60        self.assertTrue(value.IsValid())
61        self.assertTrue(value.GetError().Success())
62        self.assertEquals(value.GetValueAsUnsigned(), 20)
63
64        # Now turn off the fixits, and the expression should fail:
65        options.SetAutoApplyFixIts(False)
66        value = frame.EvaluateExpression(two_error_expression, options)
67        self.assertTrue(value.IsValid())
68        self.assertTrue(value.GetError().Fail())
69        error_string = value.GetError().GetCString()
70        self.assertTrue(
71            error_string.find("fixed expression suggested:") != -1,
72            "Fix was suggested")
73        self.assertTrue(
74            error_string.find("my_pointer->second.a") != -1,
75            "Fix was right")
76