diff options
author | vvvv <[email protected]> | 2024-11-06 10:40:15 +0300 |
---|---|---|
committer | vvvv <[email protected]> | 2024-11-06 10:50:39 +0300 |
commit | 8cf98f8169af124576399a29eac2bc2a691124e3 (patch) | |
tree | 54260d85e28822402e0d17d1e840ccfb62b33b61 /yql/essentials/ast/yql_expr_check_args_ut.cpp | |
parent | 13cc9ffc62224711fd2923aed53525fc7d1838f8 (diff) |
Moved yql/ast YQL-19206
init
commit_hash:a6a63582073784b9318cc04ffcc1e212f3df703b
Diffstat (limited to 'yql/essentials/ast/yql_expr_check_args_ut.cpp')
-rw-r--r-- | yql/essentials/ast/yql_expr_check_args_ut.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/yql/essentials/ast/yql_expr_check_args_ut.cpp b/yql/essentials/ast/yql_expr_check_args_ut.cpp new file mode 100644 index 00000000000..9f1a8af420a --- /dev/null +++ b/yql/essentials/ast/yql_expr_check_args_ut.cpp @@ -0,0 +1,115 @@ +#include "yql_expr.h" +#include <library/cpp/testing/unittest/registar.h> + +namespace NYql { + +Y_UNIT_TEST_SUITE(TExprCheckArguments) { + Y_UNIT_TEST(TestDuplicateArgument) { + TExprContext ctx; + auto pos = TPositionHandle(); + auto arg0 = ctx.NewArgument(pos, "arg0"); + auto args = ctx.NewArguments(pos, { arg0 }); + auto body = ctx.Builder(pos) + .Callable("+") + .Add(0, arg0) + .Add(1, arg0) + .Seal() + .Build(); + + auto left = ctx.NewLambda(pos, std::move(args), std::move(body)); + + auto arg1 = ctx.NewArgument(pos, "arg0"); + args = ctx.NewArguments(pos, { arg0, arg1 }); + body = ctx.Builder(pos) + .Callable("+") + .Add(0, arg0) + .Add(1, arg1) + .Seal() + .Build(); + + auto right = ctx.NewLambda(pos, std::move(args), std::move(body)); + + auto root = ctx.Builder(pos) + .Callable("SomeTopLevelCallableWithTwoLambdas") + .Add(0, left) + .Add(1, right) + .Seal() + .Build(); + + UNIT_ASSERT_EXCEPTION_CONTAINS(CheckArguments(*root), yexception, "argument is duplicated, #[1]"); + } + + Y_UNIT_TEST(TestUnresolved) { + TExprContext ctx; + auto pos = TPositionHandle(); + + auto arg1 = ctx.NewArgument(pos, "arg1"); + auto arg0 = ctx.NewArgument(pos, "arg0"); + + auto innerLambdaBody = ctx.Builder(pos) + .Callable("+") + .Add(0, arg0) + .Add(1, arg1) + .Seal() + .Build(); + + auto innerLambda = ctx.NewLambda(pos, ctx.NewArguments(pos, { arg1 }), std::move(innerLambdaBody)); + + auto outerLambda = ctx.NewLambda(pos, ctx.NewArguments(pos, { arg0 }), TExprNode::TPtr(innerLambda)); + + auto root = ctx.Builder(pos) + .Callable("SomeTopLevelCallableWithTwoLambdasAndFreeArg") + .Add(0, outerLambda) + .Add(1, innerLambda) + .Seal() + .Build(); + + UNIT_ASSERT_EXCEPTION_CONTAINS(CheckArguments(*root), yexception, "detected unresolved arguments at top level: #[2]"); + + root = ctx.Builder(pos) + .Callable("SomeTopLevelCallableWithTwoLambdasAndFreeArg") + .Add(0, outerLambda) + .Add(1, innerLambda) + .Add(2, ctx.NewArgument(pos, "arg3")) + .Seal() + .Build(); + + UNIT_ASSERT_EXCEPTION_CONTAINS(CheckArguments(*root), yexception, "detected unresolved arguments at top level: #[2, 10]"); + } + + Y_UNIT_TEST(TestUnresolvedFreeArg) { + TExprContext ctx; + auto pos = TPositionHandle(); + auto arg = ctx.NewArgument(pos, "arg"); + UNIT_ASSERT_EXCEPTION_CONTAINS(CheckArguments(*arg), yexception, "detected unresolved arguments at top level: #[1]"); + } + + Y_UNIT_TEST(TestOk) { + TExprContext ctx; + auto pos = TPositionHandle(); + + auto root = ctx.Builder(pos) + .Callable("TopLevelCallableWithTwoLambdas") + .Lambda(0) + .Param("one") + .Lambda() + .Param("two") + .Callable("+") + .Arg(0, "one") + .Arg(1, "two") + .Seal() + .Seal() + .Seal() + .Lambda(1) + .Param("three") + .Callable("Not") + .Arg(0, "three") + .Seal() + .Seal() + .Seal() + .Build(); + UNIT_ASSERT_NO_EXCEPTION(CheckArguments(*root)); + } +} + +} // namespace NYql |