yql_expr_check_args_ut.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "yql_expr.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. namespace NYql {
  4. Y_UNIT_TEST_SUITE(TExprCheckArguments) {
  5. Y_UNIT_TEST(TestDuplicateArgument) {
  6. TExprContext ctx;
  7. auto pos = TPositionHandle();
  8. auto arg0 = ctx.NewArgument(pos, "arg0");
  9. auto args = ctx.NewArguments(pos, { arg0 });
  10. auto body = ctx.Builder(pos)
  11. .Callable("+")
  12. .Add(0, arg0)
  13. .Add(1, arg0)
  14. .Seal()
  15. .Build();
  16. auto left = ctx.NewLambda(pos, std::move(args), std::move(body));
  17. auto arg1 = ctx.NewArgument(pos, "arg0");
  18. args = ctx.NewArguments(pos, { arg0, arg1 });
  19. body = ctx.Builder(pos)
  20. .Callable("+")
  21. .Add(0, arg0)
  22. .Add(1, arg1)
  23. .Seal()
  24. .Build();
  25. auto right = ctx.NewLambda(pos, std::move(args), std::move(body));
  26. auto root = ctx.Builder(pos)
  27. .Callable("SomeTopLevelCallableWithTwoLambdas")
  28. .Add(0, left)
  29. .Add(1, right)
  30. .Seal()
  31. .Build();
  32. UNIT_ASSERT_EXCEPTION_CONTAINS(CheckArguments(*root), yexception, "argument is duplicated, #[1]");
  33. }
  34. Y_UNIT_TEST(TestUnresolved) {
  35. TExprContext ctx;
  36. auto pos = TPositionHandle();
  37. auto arg1 = ctx.NewArgument(pos, "arg1");
  38. auto arg0 = ctx.NewArgument(pos, "arg0");
  39. auto innerLambdaBody = ctx.Builder(pos)
  40. .Callable("+")
  41. .Add(0, arg0)
  42. .Add(1, arg1)
  43. .Seal()
  44. .Build();
  45. auto innerLambda = ctx.NewLambda(pos, ctx.NewArguments(pos, { arg1 }), std::move(innerLambdaBody));
  46. auto outerLambda = ctx.NewLambda(pos, ctx.NewArguments(pos, { arg0 }), TExprNode::TPtr(innerLambda));
  47. auto root = ctx.Builder(pos)
  48. .Callable("SomeTopLevelCallableWithTwoLambdasAndFreeArg")
  49. .Add(0, outerLambda)
  50. .Add(1, innerLambda)
  51. .Seal()
  52. .Build();
  53. UNIT_ASSERT_EXCEPTION_CONTAINS(CheckArguments(*root), yexception, "detected unresolved arguments at top level: #[2]");
  54. root = ctx.Builder(pos)
  55. .Callable("SomeTopLevelCallableWithTwoLambdasAndFreeArg")
  56. .Add(0, outerLambda)
  57. .Add(1, innerLambda)
  58. .Add(2, ctx.NewArgument(pos, "arg3"))
  59. .Seal()
  60. .Build();
  61. UNIT_ASSERT_EXCEPTION_CONTAINS(CheckArguments(*root), yexception, "detected unresolved arguments at top level: #[2, 10]");
  62. }
  63. Y_UNIT_TEST(TestUnresolvedFreeArg) {
  64. TExprContext ctx;
  65. auto pos = TPositionHandle();
  66. auto arg = ctx.NewArgument(pos, "arg");
  67. UNIT_ASSERT_EXCEPTION_CONTAINS(CheckArguments(*arg), yexception, "detected unresolved arguments at top level: #[1]");
  68. }
  69. Y_UNIT_TEST(TestOk) {
  70. TExprContext ctx;
  71. auto pos = TPositionHandle();
  72. auto root = ctx.Builder(pos)
  73. .Callable("TopLevelCallableWithTwoLambdas")
  74. .Lambda(0)
  75. .Param("one")
  76. .Lambda()
  77. .Param("two")
  78. .Callable("+")
  79. .Arg(0, "one")
  80. .Arg(1, "two")
  81. .Seal()
  82. .Seal()
  83. .Seal()
  84. .Lambda(1)
  85. .Param("three")
  86. .Callable("Not")
  87. .Arg(0, "three")
  88. .Seal()
  89. .Seal()
  90. .Seal()
  91. .Build();
  92. UNIT_ASSERT_NO_EXCEPTION(CheckArguments(*root));
  93. }
  94. }
  95. } // namespace NYql