122c8efcdSZachary Turner //===-- PythonDataObjectsTests.cpp ------------------------------*- C++ -*-===// 222c8efcdSZachary Turner // 322c8efcdSZachary Turner // The LLVM Compiler Infrastructure 422c8efcdSZachary Turner // 522c8efcdSZachary Turner // This file is distributed under the University of Illinois Open Source 622c8efcdSZachary Turner // License. See LICENSE.TXT for details. 722c8efcdSZachary Turner // 822c8efcdSZachary Turner //===----------------------------------------------------------------------===// 922c8efcdSZachary Turner 1022c8efcdSZachary Turner #include "gtest/gtest.h" 1122c8efcdSZachary Turner 1222c8efcdSZachary Turner #include "lldb/Host/HostInfo.h" 1322c8efcdSZachary Turner #include "Plugins/ScriptInterpreter/Python/lldb-python.h" 1422c8efcdSZachary Turner #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h" 1522c8efcdSZachary Turner #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h" 1622c8efcdSZachary Turner 1722c8efcdSZachary Turner using namespace lldb_private; 1822c8efcdSZachary Turner 1922c8efcdSZachary Turner class PythonDataObjectsTest : public testing::Test 2022c8efcdSZachary Turner { 2122c8efcdSZachary Turner public: 2222c8efcdSZachary Turner void 2322c8efcdSZachary Turner SetUp() override 2422c8efcdSZachary Turner { 2522c8efcdSZachary Turner HostInfoBase::Initialize(); 26*f8b22f8fSZachary Turner // ScriptInterpreterPython::Initialize() depends on HostInfo being 27*f8b22f8fSZachary Turner // initializedso it can compute the python directory etc. 2822c8efcdSZachary Turner ScriptInterpreterPython::Initialize(); 29*f8b22f8fSZachary Turner 30*f8b22f8fSZachary Turner // Although we don't care about concurrency for the purposes of running 31*f8b22f8fSZachary Turner // this test suite, Python requires the GIL to be locked even for 32*f8b22f8fSZachary Turner // deallocating memory, which can happen when you call Py_DECREF or 33*f8b22f8fSZachary Turner // Py_INCREF. So acquire the GIL for the entire duration of this 34*f8b22f8fSZachary Turner // test suite. 35*f8b22f8fSZachary Turner m_gil_state = PyGILState_Ensure(); 3622c8efcdSZachary Turner } 3722c8efcdSZachary Turner 3822c8efcdSZachary Turner void 3922c8efcdSZachary Turner TearDown() override 4022c8efcdSZachary Turner { 41*f8b22f8fSZachary Turner PyGILState_Release(m_gil_state); 42*f8b22f8fSZachary Turner 4322c8efcdSZachary Turner ScriptInterpreterPython::Terminate(); 4422c8efcdSZachary Turner } 45*f8b22f8fSZachary Turner 46*f8b22f8fSZachary Turner private: 47*f8b22f8fSZachary Turner PyGILState_STATE m_gil_state; 4822c8efcdSZachary Turner }; 4922c8efcdSZachary Turner 50*f8b22f8fSZachary Turner TEST_F(PythonDataObjectsTest, TestOwnedReferences) 51*f8b22f8fSZachary Turner { 52*f8b22f8fSZachary Turner // After creating a new object, the refcount should be 1 53*f8b22f8fSZachary Turner PyObject *obj = PyLong_FromLong(3); 54*f8b22f8fSZachary Turner EXPECT_EQ(1, obj->ob_refcnt); 55*f8b22f8fSZachary Turner 56*f8b22f8fSZachary Turner // If we take an owned reference, the refcount should still be 1 57*f8b22f8fSZachary Turner PythonObject owned_long(PyRefType::Owned, obj); 58*f8b22f8fSZachary Turner EXPECT_EQ(1, owned_long.get()->ob_refcnt); 59*f8b22f8fSZachary Turner 60*f8b22f8fSZachary Turner // Take another reference and verify that the refcount increases 61*f8b22f8fSZachary Turner PythonObject strong_ref(owned_long); 62*f8b22f8fSZachary Turner EXPECT_EQ(2, strong_ref.get()->ob_refcnt); 63*f8b22f8fSZachary Turner 64*f8b22f8fSZachary Turner // If we reset the first one, the refcount should be 1 again. 65*f8b22f8fSZachary Turner owned_long.Reset(); 66*f8b22f8fSZachary Turner EXPECT_EQ(1, strong_ref.get()->ob_refcnt); 67*f8b22f8fSZachary Turner } 68*f8b22f8fSZachary Turner 69*f8b22f8fSZachary Turner TEST_F(PythonDataObjectsTest, TestResetting) 70*f8b22f8fSZachary Turner { 71*f8b22f8fSZachary Turner PythonDictionary dict(PyInitialValue::Empty); 72*f8b22f8fSZachary Turner 73*f8b22f8fSZachary Turner PyObject *new_dict = PyDict_New(); 74*f8b22f8fSZachary Turner dict.Reset(PyRefType::Owned, new_dict); 75*f8b22f8fSZachary Turner EXPECT_EQ(new_dict, dict.get()); 76*f8b22f8fSZachary Turner 77*f8b22f8fSZachary Turner dict.Reset(PyRefType::Owned, nullptr); 78*f8b22f8fSZachary Turner EXPECT_EQ(nullptr, dict.get()); 79*f8b22f8fSZachary Turner 80*f8b22f8fSZachary Turner dict.Reset(PyRefType::Owned, PyDict_New()); 81*f8b22f8fSZachary Turner EXPECT_NE(nullptr, dict.get()); 82*f8b22f8fSZachary Turner dict.Reset(); 83*f8b22f8fSZachary Turner EXPECT_EQ(nullptr, dict.get()); 84*f8b22f8fSZachary Turner } 85*f8b22f8fSZachary Turner 86*f8b22f8fSZachary Turner TEST_F(PythonDataObjectsTest, TestBorrowedReferences) 87*f8b22f8fSZachary Turner { 88*f8b22f8fSZachary Turner PythonInteger long_value(PyRefType::Owned, PyLong_FromLong(3)); 89*f8b22f8fSZachary Turner EXPECT_EQ(1, long_value.get()->ob_refcnt); 90*f8b22f8fSZachary Turner 91*f8b22f8fSZachary Turner PythonInteger borrowed_long(PyRefType::Borrowed, long_value.get()); 92*f8b22f8fSZachary Turner EXPECT_EQ(2, borrowed_long.get()->ob_refcnt); 93*f8b22f8fSZachary Turner } 94*f8b22f8fSZachary Turner 9522c8efcdSZachary Turner TEST_F(PythonDataObjectsTest, TestPythonInteger) 9622c8efcdSZachary Turner { 9722c8efcdSZachary Turner // Test that integers behave correctly when wrapped by a PythonInteger. 9822c8efcdSZachary Turner 9922c8efcdSZachary Turner #if PY_MAJOR_VERSION < 3 10022c8efcdSZachary Turner // Verify that `PythonInt` works correctly when given a PyInt object. 10122c8efcdSZachary Turner // Note that PyInt doesn't exist in Python 3.x, so this is only for 2.x 10222c8efcdSZachary Turner PyObject *py_int = PyInt_FromLong(12); 10322c8efcdSZachary Turner EXPECT_TRUE(PythonInteger::Check(py_int)); 104*f8b22f8fSZachary Turner PythonInteger python_int(PyRefType::Owned, py_int); 10522c8efcdSZachary Turner 10622c8efcdSZachary Turner EXPECT_EQ(PyObjectType::Integer, python_int.GetObjectType()); 10722c8efcdSZachary Turner EXPECT_EQ(12, python_int.GetInteger()); 10822c8efcdSZachary Turner #endif 10922c8efcdSZachary Turner 11022c8efcdSZachary Turner // Verify that `PythonInt` works correctly when given a PyLong object. 11122c8efcdSZachary Turner PyObject *py_long = PyLong_FromLong(12); 11222c8efcdSZachary Turner EXPECT_TRUE(PythonInteger::Check(py_long)); 113*f8b22f8fSZachary Turner PythonInteger python_long(PyRefType::Owned, py_long); 11422c8efcdSZachary Turner EXPECT_EQ(PyObjectType::Integer, python_long.GetObjectType()); 11522c8efcdSZachary Turner 11622c8efcdSZachary Turner // Verify that you can reset the value and that it is reflected properly. 11722c8efcdSZachary Turner python_long.SetInteger(40); 11822c8efcdSZachary Turner EXPECT_EQ(40, python_long.GetInteger()); 11922c8efcdSZachary Turner } 12022c8efcdSZachary Turner 12122c8efcdSZachary Turner TEST_F(PythonDataObjectsTest, TestPythonString) 12222c8efcdSZachary Turner { 12322c8efcdSZachary Turner // Test that strings behave correctly when wrapped by a PythonString. 12422c8efcdSZachary Turner 12522c8efcdSZachary Turner static const char *test_string = "PythonDataObjectsTest::TestPythonString"; 12622c8efcdSZachary Turner static const char *test_string2 = "PythonDataObjectsTest::TestPythonString"; 12722c8efcdSZachary Turner 12822c8efcdSZachary Turner #if PY_MAJOR_VERSION < 3 12922c8efcdSZachary Turner // Verify that `PythonString` works correctly when given a PyString object. 13022c8efcdSZachary Turner // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x 13122c8efcdSZachary Turner PyObject *py_string = PyString_FromString(test_string); 13222c8efcdSZachary Turner EXPECT_TRUE(PythonString::Check(py_string)); 133*f8b22f8fSZachary Turner PythonString python_string(PyRefType::Owned, py_string); 13422c8efcdSZachary Turner 13522c8efcdSZachary Turner EXPECT_EQ(PyObjectType::String, python_string.GetObjectType()); 13622c8efcdSZachary Turner EXPECT_STREQ(test_string, python_string.GetString().data()); 13722c8efcdSZachary Turner #endif 13822c8efcdSZachary Turner 13922c8efcdSZachary Turner // Verify that `PythonString` works correctly when given a PyUnicode object. 14022c8efcdSZachary Turner PyObject *py_unicode = PyUnicode_FromString(test_string); 14122c8efcdSZachary Turner EXPECT_TRUE(PythonString::Check(py_unicode)); 142*f8b22f8fSZachary Turner PythonString python_unicode(PyRefType::Owned, py_unicode); 14322c8efcdSZachary Turner 14422c8efcdSZachary Turner EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType()); 14522c8efcdSZachary Turner EXPECT_STREQ(test_string, python_unicode.GetString().data()); 14622c8efcdSZachary Turner 14722c8efcdSZachary Turner // Verify that you can reset the value and that it is reflected properly. 14822c8efcdSZachary Turner python_unicode.SetString(test_string2); 14922c8efcdSZachary Turner EXPECT_STREQ(test_string2, python_unicode.GetString().data()); 15022c8efcdSZachary Turner } 15122c8efcdSZachary Turner 15222c8efcdSZachary Turner TEST_F(PythonDataObjectsTest, TestPythonListPrebuilt) 15322c8efcdSZachary Turner { 15422c8efcdSZachary Turner // Test that a list which is built through the native 15522c8efcdSZachary Turner // Python API behaves correctly when wrapped by a PythonList. 15622c8efcdSZachary Turner static const int list_size = 2; 15722c8efcdSZachary Turner static const long long_idx0 = 5; 15822c8efcdSZachary Turner static const char *const string_idx1 = "String Index 1"; 15922c8efcdSZachary Turner 16022c8efcdSZachary Turner PyObject *py_list = PyList_New(2); 161*f8b22f8fSZachary Turner EXPECT_TRUE(PythonList::Check(py_list)); 162*f8b22f8fSZachary Turner PythonList list(PyRefType::Owned, py_list); 163*f8b22f8fSZachary Turner 164*f8b22f8fSZachary Turner PythonObject list_items[list_size]; 165*f8b22f8fSZachary Turner list_items[0].Reset(PyRefType::Owned, PyLong_FromLong(long_idx0)); 166*f8b22f8fSZachary Turner list_items[1].Reset(PyRefType::Owned, PyString_FromString(string_idx1)); 16722c8efcdSZachary Turner 16822c8efcdSZachary Turner for (int i = 0; i < list_size; ++i) 169*f8b22f8fSZachary Turner list.SetItemAtIndex(i, list_items[i]); 17022c8efcdSZachary Turner 17122c8efcdSZachary Turner EXPECT_EQ(list_size, list.GetSize()); 17222c8efcdSZachary Turner EXPECT_EQ(PyObjectType::List, list.GetObjectType()); 17322c8efcdSZachary Turner 17422c8efcdSZachary Turner // PythonList doesn't yet support getting objects by type. 17522c8efcdSZachary Turner // For now, we have to call CreateStructuredArray and use 17622c8efcdSZachary Turner // those objects. That will be in a different test. 17722c8efcdSZachary Turner // TODO: Add the ability for GetItemByIndex() to return a 17822c8efcdSZachary Turner // typed object. 17922c8efcdSZachary Turner } 18022c8efcdSZachary Turner 18122c8efcdSZachary Turner TEST_F(PythonDataObjectsTest, TestPythonDictionaryPrebuilt) 18222c8efcdSZachary Turner { 18322c8efcdSZachary Turner // Test that a dictionary which is built through the native 18422c8efcdSZachary Turner // Python API behaves correctly when wrapped by a PythonDictionary. 18522c8efcdSZachary Turner static const int dict_entries = 2; 18622c8efcdSZachary Turner 187*f8b22f8fSZachary Turner PythonObject keys[dict_entries]; 188*f8b22f8fSZachary Turner PythonObject values[dict_entries]; 18922c8efcdSZachary Turner 190*f8b22f8fSZachary Turner keys[0].Reset(PyRefType::Owned, PyString_FromString("Key 0")); 191*f8b22f8fSZachary Turner keys[1].Reset(PyRefType::Owned, PyLong_FromLong(1)); 192*f8b22f8fSZachary Turner values[0].Reset(PyRefType::Owned, PyLong_FromLong(0)); 193*f8b22f8fSZachary Turner values[1].Reset(PyRefType::Owned, PyString_FromString("Value 1")); 19422c8efcdSZachary Turner 19522c8efcdSZachary Turner PyObject *py_dict = PyDict_New(); 19622c8efcdSZachary Turner EXPECT_TRUE(PythonDictionary::Check(py_dict)); 197*f8b22f8fSZachary Turner PythonDictionary dict(PyRefType::Owned, py_dict); 19822c8efcdSZachary Turner 199*f8b22f8fSZachary Turner for (int i = 0; i < dict_entries; ++i) 200*f8b22f8fSZachary Turner PyDict_SetItem(py_dict, keys[i].get(), values[i].get()); 20122c8efcdSZachary Turner EXPECT_EQ(dict.GetSize(), dict_entries); 20222c8efcdSZachary Turner EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType()); 20322c8efcdSZachary Turner 20422c8efcdSZachary Turner // PythonDictionary doesn't yet support getting objects by type. 20522c8efcdSZachary Turner // For now, we have to call CreateStructuredDictionary and use 20622c8efcdSZachary Turner // those objects. That will be in a different test. 20722c8efcdSZachary Turner // TODO: Add the ability for GetItemByKey() to return a 20822c8efcdSZachary Turner // typed object. 20922c8efcdSZachary Turner } 21022c8efcdSZachary Turner 21122c8efcdSZachary Turner TEST_F(PythonDataObjectsTest, TestPythonListManipulation) 21222c8efcdSZachary Turner { 21322c8efcdSZachary Turner // Test that manipulation of a PythonList behaves correctly when 21422c8efcdSZachary Turner // wrapped by a PythonDictionary. 21522c8efcdSZachary Turner 21622c8efcdSZachary Turner static const long long_idx0 = 5; 21722c8efcdSZachary Turner static const char *const string_idx1 = "String Index 1"; 21822c8efcdSZachary Turner 219*f8b22f8fSZachary Turner PythonList list(PyInitialValue::Empty); 22022c8efcdSZachary Turner PythonInteger integer(long_idx0); 22122c8efcdSZachary Turner PythonString string(string_idx1); 22222c8efcdSZachary Turner 22322c8efcdSZachary Turner list.AppendItem(integer); 22422c8efcdSZachary Turner list.AppendItem(string); 22522c8efcdSZachary Turner EXPECT_EQ(2, list.GetSize()); 22622c8efcdSZachary Turner 22722c8efcdSZachary Turner // PythonList doesn't yet support getting typed objects out, so we 22822c8efcdSZachary Turner // can't easily test that the first item is an integer with the correct 22922c8efcdSZachary Turner // value, etc. 23022c8efcdSZachary Turner // TODO: Add the ability for GetItemByIndex() to return a 23122c8efcdSZachary Turner // typed object. 23222c8efcdSZachary Turner } 23322c8efcdSZachary Turner 23422c8efcdSZachary Turner TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation) 23522c8efcdSZachary Turner { 23622c8efcdSZachary Turner // Test that manipulation of a dictionary behaves correctly when wrapped 23722c8efcdSZachary Turner // by a PythonDictionary. 23822c8efcdSZachary Turner static const int dict_entries = 2; 23922c8efcdSZachary Turner 240*f8b22f8fSZachary Turner PythonString keys[dict_entries]; 241*f8b22f8fSZachary Turner PythonObject values[dict_entries]; 24222c8efcdSZachary Turner 243*f8b22f8fSZachary Turner keys[0].Reset(PyRefType::Owned, PyString_FromString("Key 0")); 244*f8b22f8fSZachary Turner keys[1].Reset(PyRefType::Owned, PyString_FromString("Key 1")); 245*f8b22f8fSZachary Turner values[0].Reset(PyRefType::Owned, PyLong_FromLong(1)); 246*f8b22f8fSZachary Turner values[1].Reset(PyRefType::Owned, PyString_FromString("Value 1")); 24722c8efcdSZachary Turner 248*f8b22f8fSZachary Turner PythonDictionary dict(PyInitialValue::Empty); 24922c8efcdSZachary Turner for (int i = 0; i < 2; ++i) 250*f8b22f8fSZachary Turner dict.SetItemForKey(keys[i], values[i]); 25122c8efcdSZachary Turner 25222c8efcdSZachary Turner EXPECT_EQ(dict_entries, dict.GetSize()); 25322c8efcdSZachary Turner 25422c8efcdSZachary Turner // PythonDictionary doesn't yet support getting objects by type. 25522c8efcdSZachary Turner // For now, we have to call CreateStructuredDictionary and use 25622c8efcdSZachary Turner // those objects. That will be in a different test. 25722c8efcdSZachary Turner // TODO: Add the ability for GetItemByKey() to return a 25822c8efcdSZachary Turner // typed object. 25922c8efcdSZachary Turner } 26022c8efcdSZachary Turner 26122c8efcdSZachary Turner TEST_F(PythonDataObjectsTest, TestPythonListToStructuredObject) 26222c8efcdSZachary Turner { 26322c8efcdSZachary Turner // Test that a PythonList is properly converted to a StructuredArray. 26422c8efcdSZachary Turner // This includes verifying that a list can contain a nested list as 26522c8efcdSZachary Turner // well as a nested dictionary. 26622c8efcdSZachary Turner 26722c8efcdSZachary Turner static const int item_count = 4; 26822c8efcdSZachary Turner static const long long_idx0 = 5; 26922c8efcdSZachary Turner static const char *const string_idx1 = "String Index 1"; 27022c8efcdSZachary Turner 27122c8efcdSZachary Turner static const long nested_list_long_idx0 = 6; 27222c8efcdSZachary Turner static const char *const nested_list_str_idx1 = "Nested String Index 1"; 27322c8efcdSZachary Turner 27422c8efcdSZachary Turner static const char *const nested_dict_key0 = "Nested Key 0"; 27522c8efcdSZachary Turner static const char *const nested_dict_value0 = "Nested Value 0"; 27622c8efcdSZachary Turner static const char *const nested_dict_key1 = "Nested Key 1"; 27722c8efcdSZachary Turner static const long nested_dict_value1 = 2; 27822c8efcdSZachary Turner 279*f8b22f8fSZachary Turner PythonList list(PyInitialValue::Empty); 280*f8b22f8fSZachary Turner PythonList nested_list(PyInitialValue::Empty); 281*f8b22f8fSZachary Turner PythonDictionary nested_dict(PyInitialValue::Empty); 28222c8efcdSZachary Turner 28322c8efcdSZachary Turner nested_list.AppendItem(PythonInteger(nested_list_long_idx0)); 28422c8efcdSZachary Turner nested_list.AppendItem(PythonString(nested_list_str_idx1)); 28522c8efcdSZachary Turner nested_dict.SetItemForKey(PythonString(nested_dict_key0), PythonString(nested_dict_value0)); 28622c8efcdSZachary Turner nested_dict.SetItemForKey(PythonString(nested_dict_key1), PythonInteger(nested_dict_value1)); 28722c8efcdSZachary Turner 28822c8efcdSZachary Turner list.AppendItem(PythonInteger(long_idx0)); 28922c8efcdSZachary Turner list.AppendItem(PythonString(string_idx1)); 29022c8efcdSZachary Turner list.AppendItem(nested_list); 29122c8efcdSZachary Turner list.AppendItem(nested_dict); 29222c8efcdSZachary Turner 29322c8efcdSZachary Turner EXPECT_EQ(item_count, list.GetSize()); 29422c8efcdSZachary Turner 29522c8efcdSZachary Turner StructuredData::ArraySP array_sp = list.CreateStructuredArray(); 29622c8efcdSZachary Turner EXPECT_EQ(list.GetSize(), array_sp->GetSize()); 29722c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeInteger, array_sp->GetItemAtIndex(0)->GetType()); 29822c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeString, array_sp->GetItemAtIndex(1)->GetType()); 29922c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeArray, array_sp->GetItemAtIndex(2)->GetType()); 30022c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeDictionary, array_sp->GetItemAtIndex(3)->GetType()); 30122c8efcdSZachary Turner 30222c8efcdSZachary Turner auto list_int_sp = std::static_pointer_cast<StructuredData::Integer>(array_sp->GetItemAtIndex(0)); 30322c8efcdSZachary Turner auto list_str_sp = std::static_pointer_cast<StructuredData::String>(array_sp->GetItemAtIndex(1)); 30422c8efcdSZachary Turner auto list_list_sp = std::static_pointer_cast<StructuredData::Array>(array_sp->GetItemAtIndex(2)); 30522c8efcdSZachary Turner auto list_dict_sp = std::static_pointer_cast<StructuredData::Dictionary>(array_sp->GetItemAtIndex(3)); 30622c8efcdSZachary Turner 30722c8efcdSZachary Turner // Verify that the first item (long) has the correct value 30822c8efcdSZachary Turner EXPECT_EQ(long_idx0, list_int_sp->GetValue()); 30922c8efcdSZachary Turner 31022c8efcdSZachary Turner // Verify that the second item (string) has the correct value 31122c8efcdSZachary Turner EXPECT_STREQ(string_idx1, list_str_sp->GetValue().c_str()); 31222c8efcdSZachary Turner 31322c8efcdSZachary Turner // Verify that the third item is a list with the correct length and element types 31422c8efcdSZachary Turner EXPECT_EQ(nested_list.GetSize(), list_list_sp->GetSize()); 31522c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeInteger, list_list_sp->GetItemAtIndex(0)->GetType()); 31622c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeString, list_list_sp->GetItemAtIndex(1)->GetType()); 31722c8efcdSZachary Turner // Verify that the values of each element in the list are correct 31822c8efcdSZachary Turner auto nested_list_value_0 = std::static_pointer_cast<StructuredData::Integer>(list_list_sp->GetItemAtIndex(0)); 31922c8efcdSZachary Turner auto nested_list_value_1 = std::static_pointer_cast<StructuredData::String>(list_list_sp->GetItemAtIndex(1)); 32022c8efcdSZachary Turner EXPECT_EQ(nested_list_long_idx0, nested_list_value_0->GetValue()); 32122c8efcdSZachary Turner EXPECT_STREQ(nested_list_str_idx1, nested_list_value_1->GetValue().c_str()); 32222c8efcdSZachary Turner 32322c8efcdSZachary Turner // Verify that the fourth item is a dictionary with the correct length 32422c8efcdSZachary Turner EXPECT_EQ(nested_dict.GetSize(), list_dict_sp->GetSize()); 32522c8efcdSZachary Turner auto dict_keys = std::static_pointer_cast<StructuredData::Array>(list_dict_sp->GetKeys()); 32622c8efcdSZachary Turner 32722c8efcdSZachary Turner // Verify that all of the keys match the values and types of keys we inserted 32822c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys->GetItemAtIndex(0)->GetType()); 32922c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys->GetItemAtIndex(1)->GetType()); 33022c8efcdSZachary Turner auto nested_key_0 = std::static_pointer_cast<StructuredData::String>(dict_keys->GetItemAtIndex(0)); 33122c8efcdSZachary Turner auto nested_key_1 = std::static_pointer_cast<StructuredData::String>(dict_keys->GetItemAtIndex(1)); 33222c8efcdSZachary Turner EXPECT_STREQ(nested_dict_key0, nested_key_0->GetValue().c_str()); 33322c8efcdSZachary Turner EXPECT_STREQ(nested_dict_key1, nested_key_1->GetValue().c_str()); 33422c8efcdSZachary Turner 33522c8efcdSZachary Turner // Verify that for each key, the value has the correct type and value as what we inserted. 33622c8efcdSZachary Turner auto nested_dict_value_0 = list_dict_sp->GetValueForKey(nested_key_0->GetValue()); 33722c8efcdSZachary Turner auto nested_dict_value_1 = list_dict_sp->GetValueForKey(nested_key_1->GetValue()); 33822c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeString, nested_dict_value_0->GetType()); 33922c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeInteger, nested_dict_value_1->GetType()); 34022c8efcdSZachary Turner auto nested_dict_str_value_0 = std::static_pointer_cast<StructuredData::String>(nested_dict_value_0); 34122c8efcdSZachary Turner auto nested_dict_int_value_1 = std::static_pointer_cast<StructuredData::Integer>(nested_dict_value_1); 34222c8efcdSZachary Turner EXPECT_STREQ(nested_dict_value0, nested_dict_str_value_0->GetValue().c_str()); 34322c8efcdSZachary Turner EXPECT_EQ(nested_dict_value1, nested_dict_int_value_1->GetValue()); 34422c8efcdSZachary Turner } 34522c8efcdSZachary Turner 34622c8efcdSZachary Turner TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredObject) 34722c8efcdSZachary Turner { 34822c8efcdSZachary Turner // Test that a PythonDictionary is properly converted to a 34922c8efcdSZachary Turner // StructuredDictionary. This includes verifying that a dictionary 35022c8efcdSZachary Turner // can contain a nested dictionary as well as a nested list. 35122c8efcdSZachary Turner 35222c8efcdSZachary Turner static const int dict_item_count = 4; 35322c8efcdSZachary Turner static const char *const dict_keys[dict_item_count] = {"Key 0 (str)", "Key 1 (long)", "Key 2 (dict)", 35422c8efcdSZachary Turner "Key 3 (list)"}; 35522c8efcdSZachary Turner 35622c8efcdSZachary Turner static const StructuredData::Type dict_value_types[dict_item_count] = { 35722c8efcdSZachary Turner StructuredData::Type::eTypeString, StructuredData::Type::eTypeInteger, StructuredData::Type::eTypeDictionary, 35822c8efcdSZachary Turner StructuredData::Type::eTypeArray}; 35922c8efcdSZachary Turner 36022c8efcdSZachary Turner static const char *const nested_dict_keys[2] = {"Nested Key 0 (str)", "Nested Key 1 (long)"}; 36122c8efcdSZachary Turner 36222c8efcdSZachary Turner static const StructuredData::Type nested_dict_value_types[2] = { 36322c8efcdSZachary Turner StructuredData::Type::eTypeString, StructuredData::Type::eTypeInteger, 36422c8efcdSZachary Turner }; 36522c8efcdSZachary Turner 36622c8efcdSZachary Turner static const StructuredData::Type nested_list_value_types[2] = {StructuredData::Type::eTypeInteger, 36722c8efcdSZachary Turner StructuredData::Type::eTypeString}; 36822c8efcdSZachary Turner 36922c8efcdSZachary Turner static const char *const dict_value0 = "Value 0"; 37022c8efcdSZachary Turner static const long dict_value1 = 2; 37122c8efcdSZachary Turner 37222c8efcdSZachary Turner static const long nested_list_value0 = 5; 37322c8efcdSZachary Turner static const char *const nested_list_value1 = "Nested list string"; 37422c8efcdSZachary Turner 37522c8efcdSZachary Turner static const char *const nested_dict_value0 = "Nested Dict Value 0"; 37622c8efcdSZachary Turner static const long nested_dict_value1 = 7; 37722c8efcdSZachary Turner 378*f8b22f8fSZachary Turner PythonDictionary dict(PyInitialValue::Empty); 379*f8b22f8fSZachary Turner PythonDictionary nested_dict(PyInitialValue::Empty); 380*f8b22f8fSZachary Turner PythonList nested_list(PyInitialValue::Empty); 38122c8efcdSZachary Turner 38222c8efcdSZachary Turner nested_dict.SetItemForKey(PythonString(nested_dict_keys[0]), PythonString(nested_dict_value0)); 38322c8efcdSZachary Turner nested_dict.SetItemForKey(PythonString(nested_dict_keys[1]), PythonInteger(nested_dict_value1)); 38422c8efcdSZachary Turner 38522c8efcdSZachary Turner nested_list.AppendItem(PythonInteger(nested_list_value0)); 38622c8efcdSZachary Turner nested_list.AppendItem(PythonString(nested_list_value1)); 38722c8efcdSZachary Turner 38822c8efcdSZachary Turner dict.SetItemForKey(PythonString(dict_keys[0]), PythonString(dict_value0)); 38922c8efcdSZachary Turner dict.SetItemForKey(PythonString(dict_keys[1]), PythonInteger(dict_value1)); 39022c8efcdSZachary Turner dict.SetItemForKey(PythonString(dict_keys[2]), nested_dict); 39122c8efcdSZachary Turner dict.SetItemForKey(PythonString(dict_keys[3]), nested_list); 39222c8efcdSZachary Turner 39322c8efcdSZachary Turner StructuredData::DictionarySP dict_sp = dict.CreateStructuredDictionary(); 39422c8efcdSZachary Turner EXPECT_EQ(dict_item_count, dict_sp->GetSize()); 39522c8efcdSZachary Turner auto dict_keys_array = std::static_pointer_cast<StructuredData::Array>(dict_sp->GetKeys()); 39622c8efcdSZachary Turner 39722c8efcdSZachary Turner std::vector<StructuredData::StringSP> converted_keys; 39822c8efcdSZachary Turner std::vector<StructuredData::ObjectSP> converted_values; 39922c8efcdSZachary Turner // Verify that all of the keys match the values and types of keys we inserted 40022c8efcdSZachary Turner // (Keys are always strings, so this is easy) 40122c8efcdSZachary Turner for (int i = 0; i < dict_sp->GetSize(); ++i) 40222c8efcdSZachary Turner { 40322c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys_array->GetItemAtIndex(i)->GetType()); 40422c8efcdSZachary Turner auto converted_key = std::static_pointer_cast<StructuredData::String>(dict_keys_array->GetItemAtIndex(i)); 40522c8efcdSZachary Turner converted_keys.push_back(converted_key); 40622c8efcdSZachary Turner converted_values.push_back(dict_sp->GetValueForKey(converted_key->GetValue().c_str())); 40722c8efcdSZachary Turner 40822c8efcdSZachary Turner EXPECT_STREQ(dict_keys[i], converted_key->GetValue().c_str()); 40922c8efcdSZachary Turner EXPECT_EQ(dict_value_types[i], converted_values[i]->GetType()); 41022c8efcdSZachary Turner } 41122c8efcdSZachary Turner 41222c8efcdSZachary Turner auto dict_string_value = std::static_pointer_cast<StructuredData::String>(converted_values[0]); 41322c8efcdSZachary Turner auto dict_int_value = std::static_pointer_cast<StructuredData::Integer>(converted_values[1]); 41422c8efcdSZachary Turner auto dict_dict_value = std::static_pointer_cast<StructuredData::Dictionary>(converted_values[2]); 41522c8efcdSZachary Turner auto dict_list_value = std::static_pointer_cast<StructuredData::Array>(converted_values[3]); 41622c8efcdSZachary Turner 41722c8efcdSZachary Turner // The first two dictionary values are easy to test, because they are just a string and an integer. 41822c8efcdSZachary Turner EXPECT_STREQ(dict_value0, dict_string_value->GetValue().c_str()); 41922c8efcdSZachary Turner EXPECT_EQ(dict_value1, dict_int_value->GetValue()); 42022c8efcdSZachary Turner 42122c8efcdSZachary Turner // For the nested dictionary, repeat the same process as before. 42222c8efcdSZachary Turner EXPECT_EQ(2, dict_dict_value->GetSize()); 42322c8efcdSZachary Turner auto nested_dict_keys_array = std::static_pointer_cast<StructuredData::Array>(dict_dict_value->GetKeys()); 42422c8efcdSZachary Turner 42522c8efcdSZachary Turner std::vector<StructuredData::StringSP> nested_converted_keys; 42622c8efcdSZachary Turner std::vector<StructuredData::ObjectSP> nested_converted_values; 42722c8efcdSZachary Turner // Verify that all of the keys match the values and types of keys we inserted 42822c8efcdSZachary Turner // (Keys are always strings, so this is easy) 42922c8efcdSZachary Turner for (int i = 0; i < dict_dict_value->GetSize(); ++i) 43022c8efcdSZachary Turner { 43122c8efcdSZachary Turner EXPECT_EQ(StructuredData::Type::eTypeString, nested_dict_keys_array->GetItemAtIndex(i)->GetType()); 43222c8efcdSZachary Turner auto converted_key = 43322c8efcdSZachary Turner std::static_pointer_cast<StructuredData::String>(nested_dict_keys_array->GetItemAtIndex(i)); 43422c8efcdSZachary Turner nested_converted_keys.push_back(converted_key); 43522c8efcdSZachary Turner nested_converted_values.push_back(dict_dict_value->GetValueForKey(converted_key->GetValue().c_str())); 43622c8efcdSZachary Turner 43722c8efcdSZachary Turner EXPECT_STREQ(nested_dict_keys[i], converted_key->GetValue().c_str()); 43822c8efcdSZachary Turner EXPECT_EQ(nested_dict_value_types[i], converted_values[i]->GetType()); 43922c8efcdSZachary Turner } 44022c8efcdSZachary Turner 44122c8efcdSZachary Turner auto converted_nested_dict_value_0 = std::static_pointer_cast<StructuredData::String>(nested_converted_values[0]); 44222c8efcdSZachary Turner auto converted_nested_dict_value_1 = std::static_pointer_cast<StructuredData::Integer>(nested_converted_values[1]); 44322c8efcdSZachary Turner 44422c8efcdSZachary Turner // The first two dictionary values are easy to test, because they are just a string and an integer. 44522c8efcdSZachary Turner EXPECT_STREQ(nested_dict_value0, converted_nested_dict_value_0->GetValue().c_str()); 44622c8efcdSZachary Turner EXPECT_EQ(nested_dict_value1, converted_nested_dict_value_1->GetValue()); 44722c8efcdSZachary Turner 44822c8efcdSZachary Turner // For the nested list, just verify the size, type and value of each item 44922c8efcdSZachary Turner nested_converted_values.clear(); 45022c8efcdSZachary Turner EXPECT_EQ(2, dict_list_value->GetSize()); 45122c8efcdSZachary Turner for (int i = 0; i < dict_list_value->GetSize(); ++i) 45222c8efcdSZachary Turner { 45322c8efcdSZachary Turner auto converted_value = dict_list_value->GetItemAtIndex(i); 45422c8efcdSZachary Turner EXPECT_EQ(nested_list_value_types[i], converted_value->GetType()); 45522c8efcdSZachary Turner nested_converted_values.push_back(converted_value); 45622c8efcdSZachary Turner } 45722c8efcdSZachary Turner 45822c8efcdSZachary Turner auto converted_nested_list_value_0 = std::static_pointer_cast<StructuredData::Integer>(nested_converted_values[0]); 45922c8efcdSZachary Turner auto converted_nested_list_value_1 = std::static_pointer_cast<StructuredData::String>(nested_converted_values[1]); 46022c8efcdSZachary Turner EXPECT_EQ(nested_list_value0, converted_nested_list_value_0->GetValue()); 46122c8efcdSZachary Turner EXPECT_STREQ(nested_list_value1, converted_nested_list_value_1->GetValue().c_str()); 46222c8efcdSZachary Turner } 463