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