xref: /llvm-project/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py (revision 2f88c07cf820cff829dec5906d298fc7147af8dd)
1"""
2Test some SBStructuredData API.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12import json
13
14class TestStructuredDataAPI(TestBase):
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def test(self):
18        self.structured_data_api_test()
19
20    def structured_data_api_test(self):
21        error = lldb.SBError()
22        s = lldb.SBStream()
23
24        dict_str = json.dumps(
25                {"key_dict":
26                 {"key_string":"STRING",
27                  "key_uint":0xffffffff00000000,
28                  "key_float":2.99,
29                  "key_bool":True,
30                  "key_array":["23","arr"]}})
31        s.Print(dict_str)
32        example = lldb.SBStructuredData()
33
34        # Check SetFromJSON API for dictionaries, integers, floating point
35        # values, strings and arrays
36        error = example.SetFromJSON(s)
37        if not error.Success():
38            self.fail("FAILED:   " + error.GetCString())
39
40        # Tests for invalid data type
41        self.invalid_struct_test(example)
42
43        # Test that GetDescription works:
44        s.Clear()
45        error = example.GetDescription(s)
46        self.assertSuccess(error, "GetDescription works")
47        if not "key_float" in s.GetData():
48            self.fail("FAILED: could not find key_float in description output")
49
50        dict_struct = lldb.SBStructuredData()
51        dict_struct = example.GetValueForKey("key_dict")
52
53        # Tests for dictionary data type
54        self.dictionary_struct_test(example)
55
56        # Tests for string data type
57        self.string_struct_test(dict_struct)
58
59        # Tests for integer data type
60        self.uint_struct_test(dict_struct)
61
62        # Tests for floating point data type
63        self.double_struct_test(dict_struct)
64
65        # Tests for boolean data type
66        self.bool_struct_test(dict_struct)
67
68        # Tests for array data type
69        self.array_struct_test(dict_struct)
70
71    def invalid_struct_test(self, example):
72        invalid_struct = lldb.SBStructuredData()
73        invalid_struct = example.GetValueForKey("invalid_key")
74        if invalid_struct.IsValid():
75            self.fail("An invalid object should have been returned")
76
77        # Check Type API
78        if not invalid_struct.GetType() == lldb.eStructuredDataTypeInvalid:
79            self.fail("Wrong type returned: " + str(invalid_struct.GetType()))
80
81    def dictionary_struct_test(self, example):
82        # Check API returning a valid SBStructuredData of 'dictionary' type
83        dict_struct = lldb.SBStructuredData()
84        dict_struct = example.GetValueForKey("key_dict")
85        if not dict_struct.IsValid():
86            self.fail("A valid object should have been returned")
87
88        # Check Type API
89        if not dict_struct.GetType() == lldb.eStructuredDataTypeDictionary:
90            self.fail("Wrong type returned: " + str(dict_struct.GetType()))
91
92        # Check Size API for 'dictionary' type
93        if not dict_struct.GetSize() == 5:
94            self.fail("Wrong no of elements returned: " +
95                      str(dict_struct.GetSize()))
96
97    def string_struct_test(self, dict_struct):
98        string_struct = lldb.SBStructuredData()
99        string_struct = dict_struct.GetValueForKey("key_string")
100        if not string_struct.IsValid():
101            self.fail("A valid object should have been returned")
102
103        # Check Type API
104        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
105            self.fail("Wrong type returned: " + str(string_struct.GetType()))
106
107        # Check API returning 'string' value
108        output = string_struct.GetStringValue(25)
109        if not "STRING" in output:
110            self.fail("wrong output: " + output)
111
112        # Calling wrong API on a SBStructuredData
113        # (e.g. getting an integer from a string type structure)
114        output = string_struct.GetIntegerValue()
115        if output:
116            self.fail(
117                "Valid integer value " +
118                str(output) +
119                " returned for a string object")
120
121    def uint_struct_test(self, dict_struct):
122        # Check a valid SBStructuredData containing an unsigned integer.
123        # We intentionally make this larger than what an int64_t can hold but
124        # still small enough to fit a uint64_t
125        uint_struct = lldb.SBStructuredData()
126        uint_struct = dict_struct.GetValueForKey("key_uint")
127        if not uint_struct.IsValid():
128            self.fail("A valid object should have been returned")
129
130        # Check Type API
131        if not uint_struct.GetType() == lldb.eStructuredDataTypeInteger:
132            self.fail("Wrong type returned: " + str(uint_struct.GetType()))
133
134        # Check API returning 'integer' value
135        output = uint_struct.GetIntegerValue()
136        if not output == 0xffffffff00000000:
137            self.fail("wrong output: " + str(output))
138
139        # Calling wrong API on a SBStructuredData
140        # (e.g. getting a string value from an integer type structure)
141        output = uint_struct.GetStringValue(25)
142        if output:
143            self.fail(
144                "Valid string " +
145                output +
146                " returned for an integer object")
147
148    def double_struct_test(self, dict_struct):
149        floating_point_struct = lldb.SBStructuredData()
150        floating_point_struct = dict_struct.GetValueForKey("key_float")
151        if not floating_point_struct.IsValid():
152            self.fail("A valid object should have been returned")
153
154        # Check Type API
155        if not floating_point_struct.GetType() == lldb.eStructuredDataTypeFloat:
156            self.fail("Wrong type returned: " +
157                      str(floating_point_struct.GetType()))
158
159        # Check API returning 'double' value
160        output = floating_point_struct.GetFloatValue()
161        if not output == 2.99:
162            self.fail("wrong output: " + str(output))
163
164    def bool_struct_test(self, dict_struct):
165        bool_struct = lldb.SBStructuredData()
166        bool_struct = dict_struct.GetValueForKey("key_bool")
167        if not bool_struct.IsValid():
168            self.fail("A valid object should have been returned")
169
170        # Check Type API
171        if not bool_struct.GetType() == lldb.eStructuredDataTypeBoolean:
172            self.fail("Wrong type returned: " + str(bool_struct.GetType()))
173
174        # Check API returning 'bool' value
175        output = bool_struct.GetBooleanValue()
176        if not output:
177            self.fail("wrong output: " + str(output))
178
179    def array_struct_test(self, dict_struct):
180        # Check API returning a valid SBStructuredData of 'array' type
181        array_struct = lldb.SBStructuredData()
182        array_struct = dict_struct.GetValueForKey("key_array")
183        if not array_struct.IsValid():
184            self.fail("A valid object should have been returned")
185
186        # Check Type API
187        if not array_struct.GetType() == lldb.eStructuredDataTypeArray:
188            self.fail("Wrong type returned: " + str(array_struct.GetType()))
189
190        # Check Size API for 'array' type
191        if not array_struct.GetSize() == 2:
192            self.fail("Wrong no of elements returned: " +
193                      str(array_struct.GetSize()))
194
195        # Check API returning a valid SBStructuredData for different 'array'
196        # indices
197        string_struct = array_struct.GetItemAtIndex(0)
198        if not string_struct.IsValid():
199            self.fail("A valid object should have been returned")
200        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
201            self.fail("Wrong type returned: " + str(string_struct.GetType()))
202        output = string_struct.GetStringValue(5)
203        if not output == "23":
204            self.fail("wrong output: " + str(output))
205
206        string_struct = array_struct.GetItemAtIndex(1)
207        if not string_struct.IsValid():
208            self.fail("A valid object should have been returned")
209        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
210            self.fail("Wrong type returned: " + str(string_struct.GetType()))
211        output = string_struct.GetStringValue(5)
212        if not output == "arr":
213            self.fail("wrong output: " + str(output))
214