@@ -48,10 +48,11 @@ def structured_data_api_test(self):
48
48
s .Clear ()
49
49
error = example .GetDescription (s )
50
50
self .assertSuccess (error , "GetDescription works" )
51
+ # Ensure str() doesn't raise an exception.
52
+ self .assertTrue (str (example ))
51
53
if not "key_float" in s .GetData ():
52
54
self .fail ("FAILED: could not find key_float in description output" )
53
55
54
- dict_struct = lldb .SBStructuredData ()
55
56
dict_struct = example .GetValueForKey ("key_dict" )
56
57
57
58
# Tests for dictionary data type
@@ -113,18 +114,22 @@ class MyRandomClass:
113
114
self .assertSuccess (example .SetFromJSON ("1" ))
114
115
self .assertEqual (example .GetType (), lldb .eStructuredDataTypeInteger )
115
116
self .assertEqual (example .GetIntegerValue (), 1 )
117
+ self .assertEqual (int (example ), 1 )
116
118
117
119
self .assertSuccess (example .SetFromJSON ("4.19" ))
118
120
self .assertEqual (example .GetType (), lldb .eStructuredDataTypeFloat )
119
121
self .assertEqual (example .GetFloatValue (), 4.19 )
122
+ self .assertEqual (float (example ), 4.19 )
120
123
121
124
self .assertSuccess (example .SetFromJSON ('"Bonjour, 123!"' ))
122
125
self .assertEqual (example .GetType (), lldb .eStructuredDataTypeString )
123
126
self .assertEqual (example .GetStringValue (42 ), "Bonjour, 123!" )
127
+ self .assertEqual (str (example ), "Bonjour, 123!" )
124
128
125
129
self .assertSuccess (example .SetFromJSON ("true" ))
126
130
self .assertEqual (example .GetType (), lldb .eStructuredDataTypeBoolean )
127
131
self .assertTrue (example .GetBooleanValue ())
132
+ self .assertTrue (example )
128
133
129
134
self .assertSuccess (example .SetFromJSON ("null" ))
130
135
self .assertEqual (example .GetType (), lldb .eStructuredDataTypeNull )
@@ -187,38 +192,35 @@ class MyRandomClass:
187
192
self .assertEqual (sb_data , example_arr )
188
193
189
194
def invalid_struct_test (self , example ):
190
- invalid_struct = lldb .SBStructuredData ()
191
195
invalid_struct = example .GetValueForKey ("invalid_key" )
192
196
if invalid_struct .IsValid ():
193
197
self .fail ("An invalid object should have been returned" )
194
198
195
199
# Check Type API
196
- if not invalid_struct .GetType () = = lldb .eStructuredDataTypeInvalid :
200
+ if invalid_struct .GetType () ! = lldb .eStructuredDataTypeInvalid :
197
201
self .fail ("Wrong type returned: " + str (invalid_struct .GetType ()))
198
202
199
203
def dictionary_struct_test (self , example ):
200
204
# Check API returning a valid SBStructuredData of 'dictionary' type
201
- dict_struct = lldb .SBStructuredData ()
202
205
dict_struct = example .GetValueForKey ("key_dict" )
203
206
if not dict_struct .IsValid ():
204
207
self .fail ("A valid object should have been returned" )
205
208
206
209
# Check Type API
207
- if not dict_struct .GetType () = = lldb .eStructuredDataTypeDictionary :
210
+ if dict_struct .GetType () ! = lldb .eStructuredDataTypeDictionary :
208
211
self .fail ("Wrong type returned: " + str (dict_struct .GetType ()))
209
212
210
213
# Check Size API for 'dictionary' type
211
- if not dict_struct .GetSize () = = 6 :
214
+ if dict_struct .GetSize () ! = 6 :
212
215
self .fail ("Wrong no of elements returned: " + str (dict_struct .GetSize ()))
213
216
214
217
def string_struct_test (self , dict_struct ):
215
- string_struct = lldb .SBStructuredData ()
216
218
string_struct = dict_struct .GetValueForKey ("key_string" )
217
219
if not string_struct .IsValid ():
218
220
self .fail ("A valid object should have been returned" )
219
221
220
222
# Check Type API
221
- if not string_struct .GetType () = = lldb .eStructuredDataTypeString :
223
+ if string_struct .GetType () ! = lldb .eStructuredDataTypeString :
222
224
self .fail ("Wrong type returned: " + str (string_struct .GetType ()))
223
225
224
226
# Check API returning 'string' value
@@ -238,18 +240,17 @@ def uint_struct_test(self, dict_struct):
238
240
# Check a valid SBStructuredData containing an unsigned integer.
239
241
# We intentionally make this larger than what an int64_t can hold but
240
242
# still small enough to fit a uint64_t
241
- uint_struct = lldb .SBStructuredData ()
242
243
uint_struct = dict_struct .GetValueForKey ("key_uint" )
243
244
if not uint_struct .IsValid ():
244
245
self .fail ("A valid object should have been returned" )
245
246
246
247
# Check Type API
247
- if not uint_struct .GetType () = = lldb .eStructuredDataTypeInteger :
248
+ if uint_struct .GetType () ! = lldb .eStructuredDataTypeInteger :
248
249
self .fail ("Wrong type returned: " + str (uint_struct .GetType ()))
249
250
250
251
# Check API returning unsigned integer value
251
252
output = uint_struct .GetUnsignedIntegerValue ()
252
- if not output = = 0xFFFFFFFF00000000 :
253
+ if output ! = 0xFFFFFFFF00000000 :
253
254
self .fail ("wrong output: " + str (output ))
254
255
255
256
# Calling wrong API on a SBStructuredData
@@ -262,18 +263,17 @@ def sint_struct_test(self, dict_struct):
262
263
# Check a valid SBStructuredData containing an signed integer.
263
264
# We intentionally make this smaller than what an uint64_t can hold but
264
265
# still small enough to fit a int64_t
265
- sint_struct = lldb .SBStructuredData ()
266
266
sint_struct = dict_struct .GetValueForKey ("key_sint" )
267
267
if not sint_struct .IsValid ():
268
268
self .fail ("A valid object should have been returned" )
269
269
270
270
# Check Type API
271
- if not sint_struct .GetType () = = lldb .eStructuredDataTypeSignedInteger :
271
+ if sint_struct .GetType () ! = lldb .eStructuredDataTypeSignedInteger :
272
272
self .fail ("Wrong type returned: " + str (sint_struct .GetType ()))
273
273
274
274
# Check API returning signed integer value
275
275
output = sint_struct .GetSignedIntegerValue ()
276
- if not output = = - 42 :
276
+ if output ! = - 42 :
277
277
self .fail ("wrong output: " + str (output ))
278
278
279
279
# Calling wrong API on a SBStructuredData
@@ -283,28 +283,26 @@ def sint_struct_test(self, dict_struct):
283
283
self .fail ("Valid string " + output + " returned for an integer object" )
284
284
285
285
def double_struct_test (self , dict_struct ):
286
- floating_point_struct = lldb .SBStructuredData ()
287
286
floating_point_struct = dict_struct .GetValueForKey ("key_float" )
288
287
if not floating_point_struct .IsValid ():
289
288
self .fail ("A valid object should have been returned" )
290
289
291
290
# Check Type API
292
- if not floating_point_struct .GetType () = = lldb .eStructuredDataTypeFloat :
291
+ if floating_point_struct .GetType () ! = lldb .eStructuredDataTypeFloat :
293
292
self .fail ("Wrong type returned: " + str (floating_point_struct .GetType ()))
294
293
295
294
# Check API returning 'double' value
296
295
output = floating_point_struct .GetFloatValue ()
297
- if not output = = 2.99 :
296
+ if output ! = 2.99 :
298
297
self .fail ("wrong output: " + str (output ))
299
298
300
299
def bool_struct_test (self , dict_struct ):
301
- bool_struct = lldb .SBStructuredData ()
302
300
bool_struct = dict_struct .GetValueForKey ("key_bool" )
303
301
if not bool_struct .IsValid ():
304
302
self .fail ("A valid object should have been returned" )
305
303
306
304
# Check Type API
307
- if not bool_struct .GetType () = = lldb .eStructuredDataTypeBoolean :
305
+ if bool_struct .GetType () ! = lldb .eStructuredDataTypeBoolean :
308
306
self .fail ("Wrong type returned: " + str (bool_struct .GetType ()))
309
307
310
308
# Check API returning 'bool' value
@@ -314,35 +312,90 @@ def bool_struct_test(self, dict_struct):
314
312
315
313
def array_struct_test (self , dict_struct ):
316
314
# Check API returning a valid SBStructuredData of 'array' type
317
- array_struct = lldb .SBStructuredData ()
318
315
array_struct = dict_struct .GetValueForKey ("key_array" )
319
316
if not array_struct .IsValid ():
320
317
self .fail ("A valid object should have been returned" )
321
318
322
319
# Check Type API
323
- if not array_struct .GetType () = = lldb .eStructuredDataTypeArray :
320
+ if array_struct .GetType () ! = lldb .eStructuredDataTypeArray :
324
321
self .fail ("Wrong type returned: " + str (array_struct .GetType ()))
325
322
326
323
# Check Size API for 'array' type
327
- if not array_struct .GetSize () = = 2 :
324
+ if array_struct .GetSize () ! = 2 :
328
325
self .fail ("Wrong no of elements returned: " + str (array_struct .GetSize ()))
329
326
330
327
# Check API returning a valid SBStructuredData for different 'array'
331
328
# indices
332
329
string_struct = array_struct .GetItemAtIndex (0 )
333
330
if not string_struct .IsValid ():
334
331
self .fail ("A valid object should have been returned" )
335
- if not string_struct .GetType () = = lldb .eStructuredDataTypeString :
332
+ if string_struct .GetType () ! = lldb .eStructuredDataTypeString :
336
333
self .fail ("Wrong type returned: " + str (string_struct .GetType ()))
337
334
output = string_struct .GetStringValue (5 )
338
- if not output = = "23" :
335
+ if output ! = "23" :
339
336
self .fail ("wrong output: " + str (output ))
340
337
341
338
string_struct = array_struct .GetItemAtIndex (1 )
342
339
if not string_struct .IsValid ():
343
340
self .fail ("A valid object should have been returned" )
344
- if not string_struct .GetType () = = lldb .eStructuredDataTypeString :
341
+ if string_struct .GetType () ! = lldb .eStructuredDataTypeString :
345
342
self .fail ("Wrong type returned: " + str (string_struct .GetType ()))
346
343
output = string_struct .GetStringValue (5 )
347
- if not output = = "arr" :
344
+ if output ! = "arr" :
348
345
self .fail ("wrong output: " + str (output ))
346
+
347
+ def test_round_trip_scalars (self ):
348
+ for original in (0 , 11 , - 1 , 0.0 , 4.5 , - 0.25 ):
349
+ constructor = type (original )
350
+ data = lldb .SBStructuredData ()
351
+ data .SetFromJSON (json .dumps (original ))
352
+ round_tripped = constructor (data )
353
+ self .assertEqual (round_tripped , original )
354
+
355
+ def test_dynamic (self ):
356
+ for original in (0 , 11 , - 1 , 0.0 , 4.5 , - 0.25 , "" , "dirk" , True , False ):
357
+ data = lldb .SBStructuredData ()
358
+ data .SetFromJSON (json .dumps (original ))
359
+ self .assertEqual (data .dynamic , original )
360
+
361
+ def test_round_trip_int (self ):
362
+ for original in (0 , 11 , - 1 ):
363
+ data = lldb .SBStructuredData ()
364
+ data .SetFromJSON (json .dumps (original ))
365
+ self .assertEqual (int (data ), int (original ))
366
+
367
+ def test_round_trip_float (self ):
368
+ for original in (0 , 11 , - 1 , 0.0 , 4.5 , - 0.25 ):
369
+ data = lldb .SBStructuredData ()
370
+ data .SetFromJSON (json .dumps (original ))
371
+ self .assertEqual (float (data ), float (original ))
372
+
373
+ def test_iterate_array (self ):
374
+ array = [0 , 1 , 2 ]
375
+ data = lldb .SBStructuredData ()
376
+ data .SetFromJSON (json .dumps (array ))
377
+ for value in data :
378
+ self .assertEqual (value , array .pop (0 ))
379
+
380
+ def test_iterate_dictionary (self ):
381
+ dictionary = {"0" : 0 , "1" : 1 , "2" : 2 }
382
+ keys = set (dictionary .keys ())
383
+ data = lldb .SBStructuredData ()
384
+ data .SetFromJSON (json .dumps (dictionary ))
385
+ for key in data :
386
+ self .assertIn (key , keys )
387
+ keys .remove (key )
388
+
389
+ def test_getitem_array (self ):
390
+ array = [1 , 2 , 3 ]
391
+ data = lldb .SBStructuredData ()
392
+ data .SetFromJSON (json .dumps (array ))
393
+ for i in range (len (array )):
394
+ self .assertEqual (data [i ], array [i ])
395
+
396
+ def test_getitem_dictionary (self ):
397
+ dictionary = {"one" : 1 , "two" : 2 , "three" : 3 }
398
+ data = lldb .SBStructuredData ()
399
+ data .SetFromJSON (json .dumps (dictionary ))
400
+ for key in dictionary :
401
+ self .assertEqual (data [key ], dictionary [key ])
0 commit comments