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