xref: /llvm-project/lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp (revision 22c8efcd3446c59415e2c65ae230668b0563c714)
1 //===-- PythonDataObjectsTests.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "gtest/gtest.h"
11 
12 #include "lldb/Host/HostInfo.h"
13 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
14 #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"
15 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
16 
17 using namespace lldb_private;
18 
19 class PythonDataObjectsTest : public testing::Test
20 {
21   public:
22     void
23     SetUp() override
24     {
25         HostInfoBase::Initialize();
26         // ScriptInterpreterPython::Initialize() depends on things like HostInfo being initialized
27         // so it can compute the python directory etc, so we need to do this after
28         // SystemInitializerCommon::Initialize().
29         ScriptInterpreterPython::Initialize();
30     }
31 
32     void
33     TearDown() override
34     {
35         ScriptInterpreterPython::Terminate();
36     }
37 };
38 
39 TEST_F(PythonDataObjectsTest, TestPythonInteger)
40 {
41 // Test that integers behave correctly when wrapped by a PythonInteger.
42 
43 #if PY_MAJOR_VERSION < 3
44     // Verify that `PythonInt` works correctly when given a PyInt object.
45     // Note that PyInt doesn't exist in Python 3.x, so this is only for 2.x
46     PyObject *py_int = PyInt_FromLong(12);
47     EXPECT_TRUE(PythonInteger::Check(py_int));
48     PythonInteger python_int(py_int);
49 
50     EXPECT_EQ(PyObjectType::Integer, python_int.GetObjectType());
51     EXPECT_EQ(12, python_int.GetInteger());
52 #endif
53 
54     // Verify that `PythonInt` works correctly when given a PyLong object.
55     PyObject *py_long = PyLong_FromLong(12);
56     EXPECT_TRUE(PythonInteger::Check(py_long));
57     PythonInteger python_long(py_long);
58     EXPECT_EQ(PyObjectType::Integer, python_long.GetObjectType());
59 
60     // Verify that you can reset the value and that it is reflected properly.
61     python_long.SetInteger(40);
62     EXPECT_EQ(40, python_long.GetInteger());
63 }
64 
65 TEST_F(PythonDataObjectsTest, TestPythonString)
66 {
67     // Test that strings behave correctly when wrapped by a PythonString.
68 
69     static const char *test_string = "PythonDataObjectsTest::TestPythonString";
70     static const char *test_string2 = "PythonDataObjectsTest::TestPythonString";
71 
72 #if PY_MAJOR_VERSION < 3
73     // Verify that `PythonString` works correctly when given a PyString object.
74     // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x
75     PyObject *py_string = PyString_FromString(test_string);
76     EXPECT_TRUE(PythonString::Check(py_string));
77     PythonString python_string(py_string);
78 
79     EXPECT_EQ(PyObjectType::String, python_string.GetObjectType());
80     EXPECT_STREQ(test_string, python_string.GetString().data());
81 #endif
82 
83     // Verify that `PythonString` works correctly when given a PyUnicode object.
84     PyObject *py_unicode = PyUnicode_FromString(test_string);
85     EXPECT_TRUE(PythonString::Check(py_unicode));
86     PythonString python_unicode(py_unicode);
87 
88     EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType());
89     EXPECT_STREQ(test_string, python_unicode.GetString().data());
90 
91     // Verify that you can reset the value and that it is reflected properly.
92     python_unicode.SetString(test_string2);
93     EXPECT_STREQ(test_string2, python_unicode.GetString().data());
94 }
95 
96 TEST_F(PythonDataObjectsTest, TestPythonListPrebuilt)
97 {
98     // Test that a list which is built through the native
99     // Python API behaves correctly when wrapped by a PythonList.
100     static const int list_size = 2;
101     static const long long_idx0 = 5;
102     static const char *const string_idx1 = "String Index 1";
103 
104     PyObject *list_items[list_size];
105 
106     PyObject *py_list = PyList_New(2);
107     list_items[0] = PyLong_FromLong(long_idx0);
108     list_items[1] = PyString_FromString(string_idx1);
109 
110     for (int i = 0; i < list_size; ++i)
111         PyList_SetItem(py_list, i, list_items[i]);
112 
113     EXPECT_TRUE(PythonList::Check(py_list));
114 
115     PythonList list(py_list);
116     EXPECT_EQ(list_size, list.GetSize());
117     EXPECT_EQ(PyObjectType::List, list.GetObjectType());
118 
119     // PythonList doesn't yet support getting objects by type.
120     // For now, we have to call CreateStructuredArray and use
121     // those objects.  That will be in a different test.
122     // TODO: Add the ability for GetItemByIndex() to return a
123     // typed object.
124 }
125 
126 TEST_F(PythonDataObjectsTest, TestPythonDictionaryPrebuilt)
127 {
128     // Test that a dictionary which is built through the native
129     // Python API behaves correctly when wrapped by a PythonDictionary.
130     static const int dict_entries = 2;
131 
132     PyObject *keys[dict_entries];
133     PyObject *values[dict_entries];
134 
135     keys[0] = PyString_FromString("Key 0");
136     keys[1] = PyLong_FromLong(1);
137     values[0] = PyLong_FromLong(0);
138     values[1] = PyString_FromString("Value 1");
139 
140     PyObject *py_dict = PyDict_New();
141     for (int i = 0; i < dict_entries; ++i)
142         PyDict_SetItem(py_dict, keys[i], values[i]);
143 
144     EXPECT_TRUE(PythonDictionary::Check(py_dict));
145 
146     PythonDictionary dict(py_dict);
147     EXPECT_EQ(dict.GetSize(), dict_entries);
148     EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType());
149 
150     // PythonDictionary doesn't yet support getting objects by type.
151     // For now, we have to call CreateStructuredDictionary and use
152     // those objects.  That will be in a different test.
153     // TODO: Add the ability for GetItemByKey() to return a
154     // typed object.
155 }
156 
157 TEST_F(PythonDataObjectsTest, TestPythonListManipulation)
158 {
159     // Test that manipulation of a PythonList behaves correctly when
160     // wrapped by a PythonDictionary.
161 
162     static const long long_idx0 = 5;
163     static const char *const string_idx1 = "String Index 1";
164 
165     PyObject *py_list = PyList_New(0);
166     PythonList list(py_list);
167     PythonInteger integer(long_idx0);
168     PythonString string(string_idx1);
169 
170     list.AppendItem(integer);
171     list.AppendItem(string);
172     EXPECT_EQ(2, list.GetSize());
173 
174     // PythonList doesn't yet support getting typed objects out, so we
175     // can't easily test that the first item is an integer with the correct
176     // value, etc.
177     // TODO: Add the ability for GetItemByIndex() to return a
178     // typed object.
179 }
180 
181 TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation)
182 {
183     // Test that manipulation of a dictionary behaves correctly when wrapped
184     // by a PythonDictionary.
185     static const int dict_entries = 2;
186 
187     PyObject *keys[dict_entries];
188     PyObject *values[dict_entries];
189 
190     keys[0] = PyString_FromString("Key 0");
191     keys[1] = PyString_FromString("Key 1");
192     values[0] = PyLong_FromLong(1);
193     values[1] = PyString_FromString("Value 1");
194 
195     PyObject *py_dict = PyDict_New();
196 
197     PythonDictionary dict(py_dict);
198     for (int i = 0; i < 2; ++i)
199         dict.SetItemForKey(PythonString(keys[i]), values[i]);
200 
201     EXPECT_EQ(dict_entries, dict.GetSize());
202 
203     // PythonDictionary doesn't yet support getting objects by type.
204     // For now, we have to call CreateStructuredDictionary and use
205     // those objects.  That will be in a different test.
206     // TODO: Add the ability for GetItemByKey() to return a
207     // typed object.
208 }
209 
210 TEST_F(PythonDataObjectsTest, TestPythonListToStructuredObject)
211 {
212     // Test that a PythonList is properly converted to a StructuredArray.
213     // This includes verifying that a list can contain a nested list as
214     // well as a nested dictionary.
215 
216     static const int item_count = 4;
217     static const long long_idx0 = 5;
218     static const char *const string_idx1 = "String Index 1";
219 
220     static const long nested_list_long_idx0 = 6;
221     static const char *const nested_list_str_idx1 = "Nested String Index 1";
222 
223     static const char *const nested_dict_key0 = "Nested Key 0";
224     static const char *const nested_dict_value0 = "Nested Value 0";
225     static const char *const nested_dict_key1 = "Nested Key 1";
226     static const long nested_dict_value1 = 2;
227 
228     PythonList list;
229     PythonList nested_list;
230     PythonDictionary nested_dict;
231 
232     nested_list.AppendItem(PythonInteger(nested_list_long_idx0));
233     nested_list.AppendItem(PythonString(nested_list_str_idx1));
234     nested_dict.SetItemForKey(PythonString(nested_dict_key0), PythonString(nested_dict_value0));
235     nested_dict.SetItemForKey(PythonString(nested_dict_key1), PythonInteger(nested_dict_value1));
236 
237     list.AppendItem(PythonInteger(long_idx0));
238     list.AppendItem(PythonString(string_idx1));
239     list.AppendItem(nested_list);
240     list.AppendItem(nested_dict);
241 
242     EXPECT_EQ(item_count, list.GetSize());
243 
244     StructuredData::ArraySP array_sp = list.CreateStructuredArray();
245     EXPECT_EQ(list.GetSize(), array_sp->GetSize());
246     EXPECT_EQ(StructuredData::Type::eTypeInteger, array_sp->GetItemAtIndex(0)->GetType());
247     EXPECT_EQ(StructuredData::Type::eTypeString, array_sp->GetItemAtIndex(1)->GetType());
248     EXPECT_EQ(StructuredData::Type::eTypeArray, array_sp->GetItemAtIndex(2)->GetType());
249     EXPECT_EQ(StructuredData::Type::eTypeDictionary, array_sp->GetItemAtIndex(3)->GetType());
250 
251     auto list_int_sp = std::static_pointer_cast<StructuredData::Integer>(array_sp->GetItemAtIndex(0));
252     auto list_str_sp = std::static_pointer_cast<StructuredData::String>(array_sp->GetItemAtIndex(1));
253     auto list_list_sp = std::static_pointer_cast<StructuredData::Array>(array_sp->GetItemAtIndex(2));
254     auto list_dict_sp = std::static_pointer_cast<StructuredData::Dictionary>(array_sp->GetItemAtIndex(3));
255 
256     // Verify that the first item (long) has the correct value
257     EXPECT_EQ(long_idx0, list_int_sp->GetValue());
258 
259     // Verify that the second item (string) has the correct value
260     EXPECT_STREQ(string_idx1, list_str_sp->GetValue().c_str());
261 
262     // Verify that the third item is a list with the correct length and element types
263     EXPECT_EQ(nested_list.GetSize(), list_list_sp->GetSize());
264     EXPECT_EQ(StructuredData::Type::eTypeInteger, list_list_sp->GetItemAtIndex(0)->GetType());
265     EXPECT_EQ(StructuredData::Type::eTypeString, list_list_sp->GetItemAtIndex(1)->GetType());
266     // Verify that the values of each element in the list are correct
267     auto nested_list_value_0 = std::static_pointer_cast<StructuredData::Integer>(list_list_sp->GetItemAtIndex(0));
268     auto nested_list_value_1 = std::static_pointer_cast<StructuredData::String>(list_list_sp->GetItemAtIndex(1));
269     EXPECT_EQ(nested_list_long_idx0, nested_list_value_0->GetValue());
270     EXPECT_STREQ(nested_list_str_idx1, nested_list_value_1->GetValue().c_str());
271 
272     // Verify that the fourth item is a dictionary with the correct length
273     EXPECT_EQ(nested_dict.GetSize(), list_dict_sp->GetSize());
274     auto dict_keys = std::static_pointer_cast<StructuredData::Array>(list_dict_sp->GetKeys());
275 
276     // Verify that all of the keys match the values and types of keys we inserted
277     EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys->GetItemAtIndex(0)->GetType());
278     EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys->GetItemAtIndex(1)->GetType());
279     auto nested_key_0 = std::static_pointer_cast<StructuredData::String>(dict_keys->GetItemAtIndex(0));
280     auto nested_key_1 = std::static_pointer_cast<StructuredData::String>(dict_keys->GetItemAtIndex(1));
281     EXPECT_STREQ(nested_dict_key0, nested_key_0->GetValue().c_str());
282     EXPECT_STREQ(nested_dict_key1, nested_key_1->GetValue().c_str());
283 
284     // Verify that for each key, the value has the correct type and value as what we inserted.
285     auto nested_dict_value_0 = list_dict_sp->GetValueForKey(nested_key_0->GetValue());
286     auto nested_dict_value_1 = list_dict_sp->GetValueForKey(nested_key_1->GetValue());
287     EXPECT_EQ(StructuredData::Type::eTypeString, nested_dict_value_0->GetType());
288     EXPECT_EQ(StructuredData::Type::eTypeInteger, nested_dict_value_1->GetType());
289     auto nested_dict_str_value_0 = std::static_pointer_cast<StructuredData::String>(nested_dict_value_0);
290     auto nested_dict_int_value_1 = std::static_pointer_cast<StructuredData::Integer>(nested_dict_value_1);
291     EXPECT_STREQ(nested_dict_value0, nested_dict_str_value_0->GetValue().c_str());
292     EXPECT_EQ(nested_dict_value1, nested_dict_int_value_1->GetValue());
293 }
294 
295 TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredObject)
296 {
297     // Test that a PythonDictionary is properly converted to a
298     // StructuredDictionary.  This includes verifying that a dictionary
299     // can contain a nested dictionary as well as a nested list.
300 
301     static const int dict_item_count = 4;
302     static const char *const dict_keys[dict_item_count] = {"Key 0 (str)", "Key 1 (long)", "Key 2 (dict)",
303                                                            "Key 3 (list)"};
304 
305     static const StructuredData::Type dict_value_types[dict_item_count] = {
306         StructuredData::Type::eTypeString, StructuredData::Type::eTypeInteger, StructuredData::Type::eTypeDictionary,
307         StructuredData::Type::eTypeArray};
308 
309     static const char *const nested_dict_keys[2] = {"Nested Key 0 (str)", "Nested Key 1 (long)"};
310 
311     static const StructuredData::Type nested_dict_value_types[2] = {
312         StructuredData::Type::eTypeString, StructuredData::Type::eTypeInteger,
313     };
314 
315     static const StructuredData::Type nested_list_value_types[2] = {StructuredData::Type::eTypeInteger,
316                                                                     StructuredData::Type::eTypeString};
317 
318     static const char *const dict_value0 = "Value 0";
319     static const long dict_value1 = 2;
320 
321     static const long nested_list_value0 = 5;
322     static const char *const nested_list_value1 = "Nested list string";
323 
324     static const char *const nested_dict_value0 = "Nested Dict Value 0";
325     static const long nested_dict_value1 = 7;
326 
327     PythonDictionary dict;
328     PythonDictionary nested_dict;
329     PythonList nested_list;
330 
331     nested_dict.SetItemForKey(PythonString(nested_dict_keys[0]), PythonString(nested_dict_value0));
332     nested_dict.SetItemForKey(PythonString(nested_dict_keys[1]), PythonInteger(nested_dict_value1));
333 
334     nested_list.AppendItem(PythonInteger(nested_list_value0));
335     nested_list.AppendItem(PythonString(nested_list_value1));
336 
337     dict.SetItemForKey(PythonString(dict_keys[0]), PythonString(dict_value0));
338     dict.SetItemForKey(PythonString(dict_keys[1]), PythonInteger(dict_value1));
339     dict.SetItemForKey(PythonString(dict_keys[2]), nested_dict);
340     dict.SetItemForKey(PythonString(dict_keys[3]), nested_list);
341 
342     StructuredData::DictionarySP dict_sp = dict.CreateStructuredDictionary();
343     EXPECT_EQ(dict_item_count, dict_sp->GetSize());
344     auto dict_keys_array = std::static_pointer_cast<StructuredData::Array>(dict_sp->GetKeys());
345 
346     std::vector<StructuredData::StringSP> converted_keys;
347     std::vector<StructuredData::ObjectSP> converted_values;
348     // Verify that all of the keys match the values and types of keys we inserted
349     // (Keys are always strings, so this is easy)
350     for (int i = 0; i < dict_sp->GetSize(); ++i)
351     {
352         EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys_array->GetItemAtIndex(i)->GetType());
353         auto converted_key = std::static_pointer_cast<StructuredData::String>(dict_keys_array->GetItemAtIndex(i));
354         converted_keys.push_back(converted_key);
355         converted_values.push_back(dict_sp->GetValueForKey(converted_key->GetValue().c_str()));
356 
357         EXPECT_STREQ(dict_keys[i], converted_key->GetValue().c_str());
358         EXPECT_EQ(dict_value_types[i], converted_values[i]->GetType());
359     }
360 
361     auto dict_string_value = std::static_pointer_cast<StructuredData::String>(converted_values[0]);
362     auto dict_int_value = std::static_pointer_cast<StructuredData::Integer>(converted_values[1]);
363     auto dict_dict_value = std::static_pointer_cast<StructuredData::Dictionary>(converted_values[2]);
364     auto dict_list_value = std::static_pointer_cast<StructuredData::Array>(converted_values[3]);
365 
366     // The first two dictionary values are easy to test, because they are just a string and an integer.
367     EXPECT_STREQ(dict_value0, dict_string_value->GetValue().c_str());
368     EXPECT_EQ(dict_value1, dict_int_value->GetValue());
369 
370     // For the nested dictionary, repeat the same process as before.
371     EXPECT_EQ(2, dict_dict_value->GetSize());
372     auto nested_dict_keys_array = std::static_pointer_cast<StructuredData::Array>(dict_dict_value->GetKeys());
373 
374     std::vector<StructuredData::StringSP> nested_converted_keys;
375     std::vector<StructuredData::ObjectSP> nested_converted_values;
376     // Verify that all of the keys match the values and types of keys we inserted
377     // (Keys are always strings, so this is easy)
378     for (int i = 0; i < dict_dict_value->GetSize(); ++i)
379     {
380         EXPECT_EQ(StructuredData::Type::eTypeString, nested_dict_keys_array->GetItemAtIndex(i)->GetType());
381         auto converted_key =
382             std::static_pointer_cast<StructuredData::String>(nested_dict_keys_array->GetItemAtIndex(i));
383         nested_converted_keys.push_back(converted_key);
384         nested_converted_values.push_back(dict_dict_value->GetValueForKey(converted_key->GetValue().c_str()));
385 
386         EXPECT_STREQ(nested_dict_keys[i], converted_key->GetValue().c_str());
387         EXPECT_EQ(nested_dict_value_types[i], converted_values[i]->GetType());
388     }
389 
390     auto converted_nested_dict_value_0 = std::static_pointer_cast<StructuredData::String>(nested_converted_values[0]);
391     auto converted_nested_dict_value_1 = std::static_pointer_cast<StructuredData::Integer>(nested_converted_values[1]);
392 
393     // The first two dictionary values are easy to test, because they are just a string and an integer.
394     EXPECT_STREQ(nested_dict_value0, converted_nested_dict_value_0->GetValue().c_str());
395     EXPECT_EQ(nested_dict_value1, converted_nested_dict_value_1->GetValue());
396 
397     // For the nested list, just verify the size, type and value of each item
398     nested_converted_values.clear();
399     EXPECT_EQ(2, dict_list_value->GetSize());
400     for (int i = 0; i < dict_list_value->GetSize(); ++i)
401     {
402         auto converted_value = dict_list_value->GetItemAtIndex(i);
403         EXPECT_EQ(nested_list_value_types[i], converted_value->GetType());
404         nested_converted_values.push_back(converted_value);
405     }
406 
407     auto converted_nested_list_value_0 = std::static_pointer_cast<StructuredData::Integer>(nested_converted_values[0]);
408     auto converted_nested_list_value_1 = std::static_pointer_cast<StructuredData::String>(nested_converted_values[1]);
409     EXPECT_EQ(nested_list_value0, converted_nested_list_value_0->GetValue());
410     EXPECT_STREQ(nested_list_value1, converted_nested_list_value_1->GetValue().c_str());
411 }
412