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