1"""
2Check if changing Format on an SBValue correctly propagates that new format to children as it should
3"""
4
5
6import lldb
7from lldbsuite.test.lldbtest import *
8import lldbsuite.test.lldbutil as lldbutil
9
10
11class FormatPropagationTestCase(TestBase):
12    def setUp(self):
13        # Call super's setUp().
14        TestBase.setUp(self)
15        # Find the line number to break at.
16        self.line = line_number("main.cpp", "// Set break point at this line.")
17
18    # rdar://problem/14035604
19    def test_with_run_command(self):
20        """Check for an issue where capping does not work because the Target pointer appears to be changing behind our backs."""
21        self.build()
22        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
23
24        lldbutil.run_break_set_by_file_and_line(
25            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
26        )
27
28        self.runCmd("run", RUN_SUCCEEDED)
29
30        # The stop reason of the thread should be breakpoint.
31        self.expect(
32            "thread list",
33            STOPPED_DUE_TO_BREAKPOINT,
34            substrs=["stopped", "stop reason = breakpoint"],
35        )
36
37        # This is the function to remove the custom formats in order to have a
38        # clean slate for the next test case.
39        def cleanup():
40            pass
41
42        # Execute the cleanup function during test case tear down.
43        self.addTearDownHook(cleanup)
44
45        # extract the parent and the children
46        frame = self.frame()
47        parent = self.frame().FindVariable("f")
48        self.assertTrue(parent is not None and parent.IsValid(), "could not find f")
49        X = parent.GetChildMemberWithName("X")
50        self.assertTrue(X is not None and X.IsValid(), "could not find X")
51        Y = parent.GetChildMemberWithName("Y")
52        self.assertTrue(Y is not None and Y.IsValid(), "could not find Y")
53        # check their values now
54        self.assertEqual(X.GetValue(), "1", "X has an invalid value")
55        self.assertEqual(Y.GetValue(), "2", "Y has an invalid value")
56        # set the format on the parent
57        parent.SetFormat(lldb.eFormatHex)
58        self.assertEqual(X.GetValue(), "0x00000001", "X has not changed format")
59        self.assertEqual(Y.GetValue(), "0x00000002", "Y has not changed format")
60        # Step and check if the values make sense still
61        self.runCmd("next")
62        self.assertEqual(X.GetValue(), "0x00000004", "X has not become 4")
63        self.assertEqual(Y.GetValue(), "0x00000002", "Y has not stuck as hex")
64        # Check that children can still make their own choices
65        Y.SetFormat(lldb.eFormatDecimal)
66        self.assertEqual(X.GetValue(), "0x00000004", "X is still hex")
67        self.assertEqual(Y.GetValue(), "2", "Y has not been reset")
68        # Make a few more changes
69        parent.SetFormat(lldb.eFormatDefault)
70        X.SetFormat(lldb.eFormatHex)
71        Y.SetFormat(lldb.eFormatDefault)
72        self.assertEqual(X.GetValue(), "0x00000004", "X is not hex as it asked")
73        self.assertEqual(Y.GetValue(), "2", "Y is not defaulted")
74