py_cast_ut.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "ut3/py_test_engine.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NPython;
  4. Y_UNIT_TEST_SUITE(TPyCastTest) {
  5. Y_UNIT_TEST(FromPyStrToInt) {
  6. TPythonTestEngine engine;
  7. UNIT_ASSERT_EXCEPTION_CONTAINS(
  8. engine.ToMiniKQL<i32>(
  9. "def Test():\n"
  10. " return '123a'",
  11. [](const NUdf::TUnboxedValuePod& value) {
  12. Y_UNUSED(value);
  13. }),
  14. yexception, "str");
  15. }
  16. Y_UNIT_TEST(FromPyTupleToLong) {
  17. TPythonTestEngine engine;
  18. UNIT_ASSERT_EXCEPTION_CONTAINS(
  19. engine.ToMiniKQL<ui64>(
  20. "def Test():\n"
  21. " return 1, 1",
  22. [](const NUdf::TUnboxedValuePod& value) {
  23. Y_UNUSED(value);
  24. }),
  25. yexception, "tuple");
  26. }
  27. Y_UNIT_TEST(FromPyFuncToString) {
  28. TPythonTestEngine engine;
  29. UNIT_ASSERT_EXCEPTION_CONTAINS(
  30. engine.ToMiniKQL<char*>(
  31. "def f():\n"
  32. " return 42\n"
  33. "def Test():\n"
  34. " return f",
  35. [](const NUdf::TUnboxedValuePod& value) {
  36. Y_UNUSED(value);
  37. }),
  38. yexception, "function");
  39. }
  40. Y_UNIT_TEST(FromPyNoneToString) {
  41. TPythonTestEngine engine;
  42. UNIT_ASSERT_EXCEPTION_CONTAINS(
  43. engine.ToMiniKQL<char*>(
  44. "def Test():\n"
  45. " return None",
  46. [](const NUdf::TUnboxedValuePod& value) {
  47. Y_UNUSED(value);
  48. }),
  49. yexception, "None");
  50. }
  51. Y_UNIT_TEST(BadFromPythonFloat) {
  52. TPythonTestEngine engine;
  53. UNIT_ASSERT_EXCEPTION_CONTAINS(
  54. engine.ToMiniKQL<float>(
  55. "def Test():\n"
  56. " return '3 <dot> 1415926'",
  57. [](const NUdf::TUnboxedValuePod& value) {
  58. Y_UNUSED(value);
  59. Y_UNREACHABLE();
  60. }),
  61. yexception, "Cast error object '3 <dot> 1415926' to Float");
  62. }
  63. #if PY_MAJOR_VERSION >= 3
  64. # define RETVAL "-1"
  65. #else
  66. # define RETVAL "-18446744073709551616L"
  67. #endif
  68. Y_UNIT_TEST(BadFromPythonLong) {
  69. TPythonTestEngine engine;
  70. UNIT_ASSERT_EXCEPTION_CONTAINS(
  71. engine.ToMiniKQL<ui64>(
  72. "def Test():\n"
  73. " return " RETVAL,
  74. [](const NUdf::TUnboxedValuePod& value) {
  75. Y_UNUSED(value);
  76. Y_UNREACHABLE();
  77. }),
  78. yexception, "Cast error object " RETVAL " to Long");
  79. }
  80. }