1"""
2Test lldb data formatter subsystem.
3"""
4
5
6import lldb
7from lldbsuite.test.lldbtest import *
8import lldbsuite.test.lldbutil as lldbutil
9
10
11class NamedSummariesDataFormatterTestCase(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    def test_with_run_command(self):
19        """Test that that file and class static variables display correctly."""
20        self.build()
21        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
22
23        lldbutil.run_break_set_by_file_and_line(
24            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
25        )
26
27        self.runCmd("run", RUN_SUCCEEDED)
28
29        # The stop reason of the thread should be breakpoint.
30        self.expect(
31            "thread list",
32            STOPPED_DUE_TO_BREAKPOINT,
33            substrs=["stopped", "stop reason = breakpoint"],
34        )
35
36        # This is the function to remove the custom formats in order to have a
37        # clean slate for the next test case.
38        def cleanup():
39            self.runCmd("type format clear", check=False)
40            self.runCmd("type summary clear", check=False)
41
42        # Execute the cleanup function during test case tear down.
43        self.addTearDownHook(cleanup)
44
45        self.runCmd(
46            'type summary add --summary-string "AllUseIt: x=${var.x} {y=${var.y}} {z=${var.z}}" --name AllUseIt'
47        )
48        self.runCmd(
49            'type summary add --summary-string "First: x=${var.x} y=${var.y} dummy=${var.dummy}" First'
50        )
51        self.runCmd(
52            'type summary add --summary-string "Second: x=${var.x} y=${var.y%hex}" Second'
53        )
54        self.runCmd(
55            'type summary add --summary-string "Third: x=${var.x} z=${var.z}" Third'
56        )
57
58        self.expect("type summary list", substrs=["AllUseIt"])
59
60        self.expect("frame variable first", substrs=["First: x=12"])
61
62        self.expect(
63            "frame variable first --summary AllUseIt", substrs=["AllUseIt: x=12"]
64        )
65
66        # We *DO NOT* remember the summary choice anymore
67        self.expect("frame variable first", matching=False, substrs=["AllUseIt: x=12"])
68        self.expect("frame variable first", substrs=["First: x=12"])
69
70        self.runCmd("thread step-over")  # 2
71
72        self.expect("frame variable first", substrs=["First: x=12"])
73
74        self.expect(
75            "frame variable first --summary AllUseIt",
76            substrs=["AllUseIt: x=12", "y=34"],
77        )
78
79        self.expect(
80            "frame variable second --summary AllUseIt",
81            substrs=["AllUseIt: x=65", "y=43.25"],
82        )
83
84        self.expect(
85            "frame variable third --summary AllUseIt",
86            substrs=["AllUseIt: x=96", "z=", "E"],
87        )
88
89        self.runCmd("thread step-over")  # 3
90
91        self.expect("frame variable second", substrs=["Second: x=65", "y=0x"])
92
93        # <rdar://problem/11576143> decided that invalid summaries will raise an error
94        # instead of just defaulting to the base summary
95        self.expect(
96            "frame variable second --summary NoSuchSummary",
97            error=True,
98            substrs=["must specify a valid named summary"],
99        )
100
101        self.runCmd("thread step-over")
102
103        self.runCmd(
104            'type summary add --summary-string "FirstAndFriends: x=${var.x} {y=${var.y}} {z=${var.z}}" First --name FirstAndFriends'
105        )
106
107        self.expect("frame variable first", substrs=["FirstAndFriends: x=12", "y=34"])
108
109        self.runCmd("type summary delete First")
110
111        self.expect(
112            "frame variable first --summary FirstAndFriends",
113            substrs=["FirstAndFriends: x=12", "y=34"],
114        )
115
116        self.expect("frame variable first", matching=True, substrs=["x = 12", "y = 34"])
117
118        self.runCmd("type summary delete FirstAndFriends")
119        self.expect("type summary delete NoSuchSummary", error=True)
120        self.runCmd("type summary delete AllUseIt")
121
122        self.expect("frame variable first", matching=False, substrs=["FirstAndFriends"])
123
124        self.runCmd("thread step-over")  # 4
125
126        self.expect(
127            "frame variable first",
128            matching=False,
129            substrs=["FirstAndFriends: x=12", "y=34"],
130        )
131