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