1"""
2Use lldb Python API to verify that expression evaluation for property references uses the correct getters and setters
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class ObjCPropertyTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20
21        # Find the line number to break for main.c.
22        self.source_name = 'main.m'
23
24    @skipUnlessDarwin
25    @add_test_categories(['pyapi'])
26    def test_objc_properties(self):
27        """Test that expr uses the correct property getters and setters"""
28        if self.getArchitecture() == 'i386':
29            self.skipTest("requires modern objc runtime")
30
31        self.build()
32        exe = self.getBuildArtifact("a.out")
33
34        # Create a target from the debugger.
35
36        target = self.dbg.CreateTarget(exe)
37        self.assertTrue(target, VALID_TARGET)
38
39        # Set up our breakpoints:
40
41        main_bkpt = target.BreakpointCreateBySourceRegex(
42            "Set a breakpoint here.", lldb.SBFileSpec(self.source_name))
43        self.assertTrue(main_bkpt and
44                        main_bkpt.GetNumLocations() == 1,
45                        VALID_BREAKPOINT)
46
47        # Now launch the process, and do not stop at the entry point.
48        process = target.LaunchSimple(
49            None, None, self.get_process_working_directory())
50
51        self.assertEquals(process.GetState(), lldb.eStateStopped,
52                        PROCESS_STOPPED)
53
54        threads = lldbutil.get_threads_stopped_at_breakpoint(
55            process, main_bkpt)
56        self.assertEquals(len(threads), 1)
57        thread = threads[0]
58        frame = thread.GetFrameAtIndex(0)
59
60        mine = frame.FindVariable("mine")
61        self.assertTrue(mine.IsValid())
62        access_count = mine.GetChildMemberWithName("_access_count")
63        self.assertTrue(access_count.IsValid())
64        start_access_count = access_count.GetValueAsUnsigned(123456)
65        self.assertNotEqual(start_access_count, 123456)
66
67        #
68        # The first set of tests test calling the getter & setter of
69        # a property that actually only has a getter & setter and no
70        # @property.
71        #
72        nonexistant_value = frame.EvaluateExpression(
73            "mine.nonexistantInt", False)
74        nonexistant_error = nonexistant_value.GetError()
75        self.assertTrue(nonexistant_error.Success())
76        nonexistant_int = nonexistant_value.GetValueAsUnsigned(123456)
77        self.assertEquals(nonexistant_int, 6)
78
79        # Calling the getter function would up the access count, so make sure
80        # that happened.
81
82        new_access_count = access_count.GetValueAsUnsigned(123456)
83        self.assertEquals(new_access_count - start_access_count, 1)
84        start_access_count = new_access_count
85
86        #
87        # Now call the setter, then make sure that
88        nonexistant_change = frame.EvaluateExpression(
89            "mine.nonexistantInt = 10", False)
90        nonexistant_error = nonexistant_change.GetError()
91        self.assertTrue(nonexistant_error.Success())
92
93        # Calling the setter function would up the access count, so make sure
94        # that happened.
95
96        new_access_count = access_count.GetValueAsUnsigned(123456)
97        self.assertEquals(new_access_count - start_access_count, 1)
98        start_access_count = new_access_count
99
100        #
101        # Now we call the getter of a property that is backed by an ivar,
102        # make sure it works and that we actually update the backing ivar.
103        #
104
105        backed_value = frame.EvaluateExpression("mine.backedInt", False)
106        backed_error = backed_value.GetError()
107        self.assertTrue(backed_error.Success())
108        backing_value = mine.GetChildMemberWithName("_backedInt")
109        self.assertTrue(backing_value.IsValid())
110        self.assertTrue(backed_value.GetValueAsUnsigned(12345)
111                        == backing_value.GetValueAsUnsigned(23456))
112
113        value_from_typedef = frame.EvaluateExpression("typedefd.backedInt", False)
114        self.assertTrue(value_from_typedef.GetError().Success())
115        self.assertEqual(value_from_typedef.GetValueAsUnsigned(12345),
116                         backing_value.GetValueAsUnsigned(23456))
117
118        unbacked_value = frame.EvaluateExpression("mine.unbackedInt", False)
119        unbacked_error = unbacked_value.GetError()
120        self.assertTrue(unbacked_error.Success())
121
122        idWithProtocol_value = frame.EvaluateExpression(
123            "mine.idWithProtocol", False)
124        idWithProtocol_error = idWithProtocol_value.GetError()
125        self.assertTrue(idWithProtocol_error.Success())
126        self.assertEquals(idWithProtocol_value.GetTypeName(), "id")
127
128        # Make sure that class property getter works as expected
129        value = frame.EvaluateExpression("BaseClass.classInt", False)
130        self.assertTrue(value.GetError().Success())
131        self.assertEquals(value.GetValueAsUnsigned(11111), 123)
132
133        # Make sure that class property setter works as expected
134        value = frame.EvaluateExpression("BaseClass.classInt = 234", False)
135        self.assertTrue(value.GetError().Success())
136
137        # Verify that setter above actually worked
138        value = frame.EvaluateExpression("BaseClass.classInt", False)
139        self.assertTrue(value.GetError().Success())
140        self.assertEquals(value.GetValueAsUnsigned(11111), 234)
141