123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- #include "yql_expr.h"
- #include <library/cpp/testing/unittest/registar.h>
- namespace NYql {
- Y_UNIT_TEST_SUITE(TExprBuilder) {
- Y_UNIT_TEST(TestEmpty) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestRootAtom) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle()).Atom("ABC").Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Content(), "ABC");
- }
- Y_UNIT_TEST(TestRootAtomTwice) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Atom("ABC")
- .Atom("ABC")
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestRootEmptyList) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle()).List().Seal().Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::List);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 0);
- }
- Y_UNIT_TEST(TestRootEmptyListTwice) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .List()
- .List()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestListWithAtoms) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .List()
- .Atom(0, "ABC")
- .Atom(1, "XYZ")
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::List);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Content(), "ABC");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "XYZ");
- }
- Y_UNIT_TEST(TestMismatchChildIndex) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .List()
- .Atom(1, "")
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestListWithAdd) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .List()
- .Add(0, ctx.Builder(TPositionHandle()).Atom("ABC").Build())
- .Atom(1, "XYZ")
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::List);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Content(), "ABC");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "XYZ");
- }
- Y_UNIT_TEST(TestNestedListWithAtoms) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .List()
- .List(0)
- .Atom(0, "ABC")
- .Atom(1, "DEF")
- .Seal()
- .Atom(1, "XYZ")
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::List);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::List);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "ABC");
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Content(), "DEF");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "XYZ");
- }
- Y_UNIT_TEST(TestWrongLevelBuild) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .List()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestWrongLevelSeal) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Seal(), yexception);
- }
- Y_UNIT_TEST(TestCallableWithAtoms) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .Callable("Func")
- .Atom(0, "ABC")
- .Atom(1, "XYZ")
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Callable);
- UNIT_ASSERT_VALUES_EQUAL(res->Content(), "Func");
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Content(), "ABC");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "XYZ");
- }
- Y_UNIT_TEST(TestNestedCallableWithAtoms) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .Callable("Func1")
- .Callable(0, "Func2")
- .Atom(0, "ABC")
- .Atom(1, "DEF")
- .Seal()
- .Atom(1, "XYZ")
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Callable);
- UNIT_ASSERT_VALUES_EQUAL(res->Content(), "Func1");
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Callable);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Content(), "Func2");
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "ABC");
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Content(), "DEF");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "XYZ");
- }
- Y_UNIT_TEST(TestRootWorld) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle()).World().Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::World);
- }
- Y_UNIT_TEST(TestCallableWithWorld) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .Callable("Func")
- .World(0)
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Callable);
- UNIT_ASSERT_VALUES_EQUAL(res->Content(), "Func");
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::World);
- }
- Y_UNIT_TEST(TestIncompleteRootLambda) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestIncompleteInnerLambda) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .List()
- .Lambda()
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestRootLambda) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle()).Lambda().Atom("ABC").Seal().Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 0);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "ABC");
- }
- Y_UNIT_TEST(TestRootLambdaWithBodyAsSet) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .Lambda()
- .Set(ctx.Builder(TPositionHandle()).Atom("ABC").Build())
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 0);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "ABC");
- }
- Y_UNIT_TEST(TestInnerLambdaWithParam) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .List()
- .Lambda(0)
- .Param("x")
- .Atom("ABC")
- .Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::List);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Head().Content(), "x");
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Type(), TExprNode::Atom);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Content(), "ABC");
- }
- Y_UNIT_TEST(TestDuplicateLambdaParamNames) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Param("x")
- .Atom("ABC")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestParamAtRoot) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Param("aaa")
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestParamInList) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .List()
- .Param("aaa")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestParamInCallable) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Callable("Func")
- .Param("aaa")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestParamAfterLambdaBody) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("aaa")
- .Atom("ABC")
- .Param("bbb")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestIndexedAtomAtRoot) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Atom(0, "ABC")
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestIndexedListAtRoot) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .List(0)
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestIndexedWorldAtRoot) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .World(0)
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestIndexedCallableAtRoot) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Callable(0, "Func")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestIndexedLambdaAtRoot) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda(0)
- .Atom("ABC")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestWrongIndexAtomAtLambda) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .Atom(1, "ABC")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestWrongIndexListAtLambda) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .List(1)
- .Seal()
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestWrongIndexWorldAtLambda) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .World(1)
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestWrongIndexCallableAtLambda) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .Callable(1, "Func")
- .Seal()
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestWrongIndexLambdaAtLambda) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .Lambda(1)
- .Atom("ABC")
- .Seal()
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestAddAtLambda) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle())
- .Lambda()
- .Add(1, ctx.Builder(TPositionHandle()).Atom("ABC").Build())
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestLambdaWithArgAsBody) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Param("y")
- .Arg("x")
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "x");
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Content(), "y");
- UNIT_ASSERT_EQUAL(res->Child(1), res->Head().Child(0));
- }
- Y_UNIT_TEST(TestIndexedArgAsLambdaBody) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle()).Lambda()
- .Param("x")
- .Arg(1, "x")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestWrongArgName) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle()).Lambda()
- .Param("x")
- .Arg("y")
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestLambdaWithArgInCallables) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Param("y")
- .Callable("+")
- .Arg(0, "y")
- .Arg(1, "x")
- .Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "x");
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Content(), "y");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Callable);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "+");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->ChildrenSize(), 2);
- UNIT_ASSERT_EQUAL(res->Child(1)->Child(0), res->Head().Child(1));
- UNIT_ASSERT_EQUAL(res->Child(1)->Child(1), res->Head().Child(0));
- }
- Y_UNIT_TEST(TestNestedScopeInLambda) {
- TExprContext ctx;
- auto res = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Param("y")
- .Callable("Apply")
- .Lambda(0)
- .Param("x")
- .Callable("+")
- .Arg(0, "x")
- .Arg(1, "y")
- .Seal()
- .Seal()
- .Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "x");
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Child(1)->Content(), "y");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::Callable);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Content(), "Apply");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Head().ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Head().Head().Content(), "x");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Child(1)->Type(), TExprNode::Callable);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Child(1)->Content(), "+");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Child(1)->ChildrenSize(), 2);
- // nested x
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Child(1)->Child(0),
- res->Child(1)->Head().Head().Child(0));
- // outer y
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Head().Child(1)->Child(1),
- res->Head().Child(1));
- }
- Y_UNIT_TEST(TestNonIndexedArg) {
- TExprContext ctx;
- UNIT_ASSERT_EXCEPTION(ctx.Builder(TPositionHandle()).Lambda()
- .Param("x")
- .Callable("f")
- .Arg("x")
- .Seal()
- .Seal()
- .Build(), yexception);
- }
- Y_UNIT_TEST(TestApplyLambdaArgAsRoot) {
- TExprContext ctx;
- auto lambda = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Arg("x")
- .Seal()
- .Build();
- auto res = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("y")
- .Apply(lambda).With(0, "y").Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "y");
- UNIT_ASSERT_EQUAL(res->Child(1), res->Head().Child(0));
- }
- Y_UNIT_TEST(TestApplyLambdaArgInContainer) {
- TExprContext ctx;
- auto lambda = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Arg("x")
- .Seal()
- .Build();
- auto res = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("y")
- .List()
- .Apply(0, lambda).With(0, "y").Seal()
- .Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "y");
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->Type(), TExprNode::List);
- UNIT_ASSERT_VALUES_EQUAL(res->Child(1)->ChildrenSize(), 1);
- UNIT_ASSERT_EQUAL(res->Child(1)->Child(0), res->Head().Child(0));
- }
- Y_UNIT_TEST(TestApplyPartialLambdaArgAsRoot) {
- TExprContext ctx;
- auto lambda = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Callable("Func1")
- .Callable(0, "Func2")
- .Atom(0, "ABC")
- .Arg(1, "x")
- .Seal()
- .Seal()
- .Seal()
- .Build();
- auto res = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("y")
- .ApplyPartial(lambda->HeadPtr(), lambda->Child(1)->HeadPtr()).With(0, "y").Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "y");
- UNIT_ASSERT_EQUAL(res->Child(1)->Child(1), res->Head().Child(0));
- }
- Y_UNIT_TEST(TestApplyPartialLambdaArgInContainer) {
- TExprContext ctx;
- auto lambda = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Callable("Func1")
- .Callable(0, "Func2")
- .Atom(0, "ABC")
- .Arg(1, "x")
- .Seal()
- .Seal()
- .Seal()
- .Build();
- auto res = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("y")
- .Callable("Func3")
- .ApplyPartial(0, lambda->HeadPtr(), lambda->Child(1)->HeadPtr()).With(0, "y").Seal()
- .Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res->Head().Head().Content(), "y");
- UNIT_ASSERT_EQUAL(res->Child(1)->Head().Child(1), res->Head().Child(0));
- }
- Y_UNIT_TEST(TestApplyOuterArg) {
- TExprContext ctx;
- auto ast = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("x")
- .Callable("Func1")
- .Atom(0, "p1")
- .Lambda(1)
- .Callable("Func2")
- .Atom(0, "ABC")
- .Arg(1, "x")
- .Seal()
- .Seal()
- .Seal()
- .Seal()
- .Build();
- auto res1 = ctx.Builder(TPositionHandle())
- .Lambda()
- .Param("y")
- .Callable("Func3")
- .ApplyPartial(0, nullptr, ast->Child(1)->Child(1)->ChildPtr(1))
- .WithNode(*ast->Head().Child(0), "y").Seal()
- .Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res1->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res1->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res1->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res1->Head().ChildrenSize(), 1);
- UNIT_ASSERT_VALUES_EQUAL(res1->Head().Head().Type(), TExprNode::Argument);
- UNIT_ASSERT_VALUES_EQUAL(res1->Head().Head().Content(), "y");
- UNIT_ASSERT_EQUAL(res1->Child(1)->Head().Child(1), res1->Head().Child(0));
- auto atom = ctx.Builder(TPositionHandle())
- .Atom("const")
- .Build();
- auto res2 = ctx.Builder(TPositionHandle())
- .Lambda()
- .Callable("Func3")
- .ApplyPartial(0, nullptr, ast->Child(1)->Child(1)->ChildPtr(1))
- .WithNode(ast->Head().Head(), TExprNode::TPtr(atom)).Seal()
- .Seal()
- .Seal()
- .Build();
- UNIT_ASSERT_VALUES_EQUAL(res2->Type(), TExprNode::Lambda);
- UNIT_ASSERT_VALUES_EQUAL(res2->ChildrenSize(), 2);
- UNIT_ASSERT_VALUES_EQUAL(res2->Head().Type(), TExprNode::Arguments);
- UNIT_ASSERT_VALUES_EQUAL(res2->Head().ChildrenSize(), 0);
- UNIT_ASSERT_EQUAL(res2->Child(1)->Head().ChildPtr(1), atom);
- }
- }
- } // namespace NYql
|