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 # Test that GetDescription works: 39 s.Clear() 40 error = example.GetDescription(s) 41 self.assertTrue(error.Success(), "GetDescription works") 42 if not "key_float" in s.GetData(): 43 self.fail("FAILED: could not find key_float in description output") 44 45 dict_struct = lldb.SBStructuredData() 46 dict_struct = example.GetValueForKey("key_dict") 47 48 # Tests for dictionary data type 49 self.dictionary_struct_test(example) 50 51 # Tests for string data type 52 self.string_struct_test(dict_struct) 53 54 # Tests for integer data type 55 self.int_struct_test(dict_struct) 56 57 # Tests for floating point data type 58 self.double_struct_test(dict_struct) 59 60 # Tests for boolean data type 61 self.bool_struct_test(dict_struct) 62 63 # Tests for array data type 64 self.array_struct_test(dict_struct) 65 66 def invalid_struct_test(self, example): 67 invalid_struct = lldb.SBStructuredData() 68 invalid_struct = example.GetValueForKey("invalid_key") 69 if invalid_struct.IsValid(): 70 self.fail("An invalid object should have been returned") 71 72 # Check Type API 73 if not invalid_struct.GetType() == lldb.eStructuredDataTypeInvalid: 74 self.fail("Wrong type returned: " + str(invalid_struct.GetType())) 75 76 def dictionary_struct_test(self, example): 77 # Check API returning a valid SBStructuredData of 'dictionary' type 78 dict_struct = lldb.SBStructuredData() 79 dict_struct = example.GetValueForKey("key_dict") 80 if not dict_struct.IsValid(): 81 self.fail("A valid object should have been returned") 82 83 # Check Type API 84 if not dict_struct.GetType() == lldb.eStructuredDataTypeDictionary: 85 self.fail("Wrong type returned: " + str(dict_struct.GetType())) 86 87 # Check Size API for 'dictionary' type 88 if not dict_struct.GetSize() == 5: 89 self.fail("Wrong no of elements returned: " + 90 str(dict_struct.GetSize())) 91 92 def string_struct_test(self, dict_struct): 93 string_struct = lldb.SBStructuredData() 94 string_struct = dict_struct.GetValueForKey("key_string") 95 if not string_struct.IsValid(): 96 self.fail("A valid object should have been returned") 97 98 # Check Type API 99 if not string_struct.GetType() == lldb.eStructuredDataTypeString: 100 self.fail("Wrong type returned: " + str(string_struct.GetType())) 101 102 # Check API returning 'string' value 103 output = string_struct.GetStringValue(25) 104 if not "STRING" in output: 105 self.fail("wrong output: " + output) 106 107 # Calling wrong API on a SBStructuredData 108 # (e.g. getting an integer from a string type structure) 109 output = string_struct.GetIntegerValue() 110 if output: 111 self.fail( 112 "Valid integer value " + 113 str(output) + 114 " returned for a string object") 115 116 def int_struct_test(self, dict_struct): 117 # Check a valid SBStructuredData containing an 'integer' by 118 int_struct = lldb.SBStructuredData() 119 int_struct = dict_struct.GetValueForKey("key_int") 120 if not int_struct.IsValid(): 121 self.fail("A valid object should have been returned") 122 123 # Check Type API 124 if not int_struct.GetType() == lldb.eStructuredDataTypeInteger: 125 self.fail("Wrong type returned: " + str(int_struct.GetType())) 126 127 # Check API returning 'integer' value 128 output = int_struct.GetIntegerValue() 129 if not output == 3: 130 self.fail("wrong output: " + str(output)) 131 132 # Calling wrong API on a SBStructuredData 133 # (e.g. getting a string value from an integer type structure) 134 output = int_struct.GetStringValue(25) 135 if output: 136 self.fail( 137 "Valid string " + 138 output + 139 " returned for an integer object") 140 141 def double_struct_test(self, dict_struct): 142 floating_point_struct = lldb.SBStructuredData() 143 floating_point_struct = dict_struct.GetValueForKey("key_float") 144 if not floating_point_struct.IsValid(): 145 self.fail("A valid object should have been returned") 146 147 # Check Type API 148 if not floating_point_struct.GetType() == lldb.eStructuredDataTypeFloat: 149 self.fail("Wrong type returned: " + 150 str(floating_point_struct.GetType())) 151 152 # Check API returning 'double' value 153 output = floating_point_struct.GetFloatValue() 154 if not output == 2.99: 155 self.fail("wrong output: " + str(output)) 156 157 def bool_struct_test(self, dict_struct): 158 bool_struct = lldb.SBStructuredData() 159 bool_struct = dict_struct.GetValueForKey("key_bool") 160 if not bool_struct.IsValid(): 161 self.fail("A valid object should have been returned") 162 163 # Check Type API 164 if not bool_struct.GetType() == lldb.eStructuredDataTypeBoolean: 165 self.fail("Wrong type returned: " + str(bool_struct.GetType())) 166 167 # Check API returning 'bool' value 168 output = bool_struct.GetBooleanValue() 169 if not output: 170 self.fail("wrong output: " + str(output)) 171 172 def array_struct_test(self, dict_struct): 173 # Check API returning a valid SBStructuredData of 'array' type 174 array_struct = lldb.SBStructuredData() 175 array_struct = dict_struct.GetValueForKey("key_array") 176 if not array_struct.IsValid(): 177 self.fail("A valid object should have been returned") 178 179 # Check Type API 180 if not array_struct.GetType() == lldb.eStructuredDataTypeArray: 181 self.fail("Wrong type returned: " + str(array_struct.GetType())) 182 183 # Check Size API for 'array' type 184 if not array_struct.GetSize() == 2: 185 self.fail("Wrong no of elements returned: " + 186 str(array_struct.GetSize())) 187 188 # Check API returning a valid SBStructuredData for different 'array' 189 # indices 190 string_struct = array_struct.GetItemAtIndex(0) 191 if not string_struct.IsValid(): 192 self.fail("A valid object should have been returned") 193 if not string_struct.GetType() == lldb.eStructuredDataTypeString: 194 self.fail("Wrong type returned: " + str(string_struct.GetType())) 195 output = string_struct.GetStringValue(5) 196 if not output == "23": 197 self.fail("wrong output: " + str(output)) 198 199 string_struct = array_struct.GetItemAtIndex(1) 200 if not string_struct.IsValid(): 201 self.fail("A valid object should have been returned") 202 if not string_struct.GetType() == lldb.eStructuredDataTypeString: 203 self.fail("Wrong type returned: " + str(string_struct.GetType())) 204 output = string_struct.GetStringValue(5) 205 if not output == "arr": 206 self.fail("wrong output: " + str(output)) 207