xref: /llvm-project/lldb/test/API/commands/expression/persistent_result/TestPersistentResult.py (revision 385496385476fc9735da5fa4acabc34654e8b30d)
1"""
2Test controlling `expression` result variables are persistent.
3"""
4
5import lldb
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test.decorators import *
8from lldbsuite.test import lldbutil
9
10
11class TestCase(TestBase):
12    def setUp(self):
13        TestBase.setUp(self)
14        self.build()
15        lldbutil.run_to_source_breakpoint(self, "break here", lldb.SBFileSpec("main.c"))
16
17    def test_enable_persistent_result(self):
18        """Test explicitly enabling result variables persistence."""
19        self.expect("expression --persistent-result on -- i", substrs=["(int) $0 = 30"])
20        # Verify the lifetime of $0 extends beyond the `expression` it was created in.
21        self.expect("expression $0", substrs=["(int) $0 = 30"])
22
23    def test_disable_persistent_result(self):
24        """Test explicitly disabling persistent result variables."""
25        self.expect("expression --persistent-result off -- i", substrs=["(int) 30"])
26        # Verify a persistent result was not silently created.
27        self.expect("expression $0", error=True)
28
29    def test_expression_persists_result(self):
30        """Test `expression`'s default behavior is to persist a result variable."""
31        self.expect("expression i", substrs=["(int) $0 = 30"])
32        self.expect("expression $0", substrs=["(int) $0 = 30"])
33
34    def test_p_does_not_persist_results(self):
35        """Test `p` does not persist a result variable."""
36        self.expect("p i", startstr="(int) 30")
37        self.expect("p $0", error=True)
38