/** * Copyright (C) 2020-present MongoDB, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the Server Side Public License, version 1, * as published by MongoDB, Inc. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * Server Side Public License for more details. * * You should have received a copy of the Server Side Public License * along with this program. If not, see * . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the Server Side Public License in all respects for * all of the code used other than as permitted herein. If you modify file(s) * with this exception, you may extend this exception to your version of the * file(s), but you are not obligated to do so. If you do not wish to do so, * delete this exception statement from your version. If you delete this * exception statement from all source files in the program, then also delete * it in the license file. */ #include "mongo/platform/basic.h" #include #include "mongo/bson/json.h" #include "mongo/db/cst/bson_lexer.h" #include "mongo/db/cst/c_node.h" #include "mongo/db/cst/key_fieldname.h" #include "mongo/db/cst/key_value.h" #include "mongo/db/cst/parser_gen.hpp" #include "mongo/unittest/bson_test_util.h" #include "mongo/unittest/unittest.h" namespace mongo { namespace { TEST(CstExpressionTest, ParsesProjectWithAnd) { CNode output; auto input = fromjson( "{pipeline: [{$project: {_id: 9.10, a: {$and: [4, {$and: [7, 8]}]}, b: {$and: [2, " "-3]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : \"\", : { : [ \"\", { " ": [ \"\", \"\" ] } ] }, : { " ": [ \"\", \"\" ] } } }"); } TEST(CstExpressionTest, ParsesProjectWithOr) { CNode output; auto input = fromjson( "{pipeline: [{$project: {_id: 9.10, a: {$or: [4, {$or: [7, 8]}]}, b: {$or: [2, -3]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : \"\", : { : [ \"\", { " ": [ \"\", \"\" ] } ] }, : { " ": [ \"\", \"\" ] } } }"); } TEST(CstExpressionTest, ParsesProjectWithNot) { CNode output; auto input = fromjson( "{pipeline: [{$project: {_id: 9.10, a: {$not: [4]}, b: {$and: [1.0, {$not: " "[true]}]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : \"\", : { : [ \"\" " "] }, : { : [ \"\", { " ": [ \"\" ] } ] } } }"); } TEST(CstExpressionTest, ParsesComparisonExpressions) { auto parseAndTest = [](StringData expr) { CNode output; auto input = fromjson("{pipeline: [{$project: {_id: {$" + expr + ": [1, 2.5]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\", \"\" ] } } }"); }; for (auto&& expr : {"cmp"_sd, "eq"_sd, "gt"_sd, "gte"_sd, "lt"_sd, "lte"_sd, "ne"_sd}) { parseAndTest(expr); } } TEST(CstExpressionTest, FailsToParseInvalidComparisonExpressions) { auto assertFailsToParse = [](StringData expr) { { CNode output; auto input = fromjson("{pipeline: [{$project: {_id: {$" + expr + ": [1]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } { CNode output; auto input = fromjson("{pipeline: [{$project: {_id: {$" + expr + ": [1, 2, 3]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } { CNode output; auto input = fromjson("{pipeline: [{$project: {_id: {$" + expr + ": 1}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } }; for (auto&& expr : {"cmp"_sd, "eq"_sd, "gt"_sd, "gte"_sd, "lt"_sd, "lte"_sd, "ne"_sd}) { assertFailsToParse(expr); } } TEST(CstExpressionTest, FailsToParseInvalidConvertExpressions) { { CNode output; auto input = fromjson("{pipeline: [{$project: {a: {$convert: 'x'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } { CNode output; auto input = fromjson("{pipeline: [{$project: {a: {$convert: {input: 'x'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesConvertExpressions) { CNode output; auto input = fromjson( "{pipeline: [{$project: {a: {$toBool: 1}, b: {$toDate: 1100000000000}, " "c: {$toDecimal: 5}, d: {$toDouble: -2}, e: {$toInt: 1.999999}, " "f: {$toLong: 1.999999}, g: {$toObjectId: '$_id'}, h: {$toString: false}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : " "\"\" }, : { : \"\" }, : { : \"\" " "}, : { : \"\" }, : { : \"\" }, " ": { : \"\" }, : { : \"\" }, : { : " "\"\" } } }"); } TEST(CstExpressionTest, ParsesConvertExpressionsNoOptArgs) { CNode output; auto input = fromjson( "{pipeline: [{$project: {a: {$convert: {input: 1, to: 'string'}}, " "b: {$convert : {input: 'true', to: 'bool'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : \"\", : \"\", : \"\" } }, : { : { : \"\", : " "\"\", : \"\", " ": " "\"\" } } } }"); } TEST(CstExpressionTest, ParsesConvertExpressionsWithOptArgs) { CNode output; auto input = fromjson( "{pipeline: [{$project: {a: {$convert: {input: 1, to: 'string', " "onError: 'Could not convert'}}, b : {$convert : {input: " "true, to : 'double', onNull : 0}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : \"\", : \"\", " ": \"\" } }, : { : { : \"\", : " "\"\", : \"\", " ": " "\"\" } } } }"); } TEST(CstExpressionTest, ParsesIndexOf) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "b: { $indexOfBytes: ['ABC', 'B']}, " "c: { $indexOfCP: [ 'cafeteria', 'e' ] }, " "d: { $indexOfBytes: [ 'foo.bar.fi', '.', 5, 7 ] }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\", \"\" " "] }, : " "{ : [ \"\", \"\" ] }, " ": { " ": [ \"\", \"\", " "\"\", " "\"\" ] } } }"); } TEST(CstExpressionTest, ParsesDateFromString) { CNode output; auto input = fromjson( "{pipeline: [{$project: { m: { $dateFromString: { dateString: '2017-02-08T12:10:40.787', " "timezone: 'America/New_York' } } }}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { : \"\", : \"\", " ": " "\"\", : \"\", : " "\"\" } } } }"); } TEST(CstExpressionTest, ParsesDateToString) { CNode output; auto input = fromjson( "{pipeline: [{$project: { m: { $dateToString: { date: '$date', " "format: '%Y-%m-%d' } } } } ] }"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "\"\", : \"\", " ": " "\"\" } } } }"); } TEST(CstExpressionTest, ParsesReplaceStringExpressions) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "h: { $replaceOne: { input: '$name', find: 'Cafe', replacement: 'CAFE' } }, " "i: { $replaceAll: { input: 'cafeSeattle', find: 'cafe', replacement: 'CAFE' } } }}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "\"\", : \"\" } }, " ": { : " "{ : \"\", : " "\"\", " ": \"\" } } } }"); } TEST(CstExpressionTest, ParsesTrim) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "d: { $ltrim: { input: ' ggggoodbyeeeee' } }, " "e: { $rtrim: { input: 'ggggoodbyeeeee '} }, " "f: { $trim: { input: ' ggggoodbyeeeee', chars: ' ge' } } }}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "\"\" } }, : { : { " ": \"\", : \"\" } }, : { " ": { : \"\", : \"\" } } } }"); } TEST(CstExpressionTest, ParsesToUpperAndLower) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "g: { $toUpper: 'abc' }, " "v: { $toLower: 'ABC' }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : " "\"\" }, : { : \"\" } } }"); } TEST(CstExpressionTest, ParsesRegexExpressions) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "j: { $regexFind: { input: '$details', regex: /^[a-z0-9_.+-]/, options: 'i' } }, " "k: { $regexFindAll: { input: '$fname', regex: /(C(ar)*)ol/ } }, " "l: { $regexMatch: { input: '$description', regex: /lin(e|k)/ } } }}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : " "{ : \"\", : " "\"\", : \"\" } }, " ": { : { " ": \"\", : " "\"\", : " "\"\" } }, : { : { " ": \"\", : " "\"\", : \"\" } } } }"); } TEST(CstExpressionTest, ParsesSubstrExpressions) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "s: { $substr: [ '$quarter', 2, -1 ] }, " "t: { $substrBytes: [ '$name', 0, 3 ] }, " "u: { $substrCP: [ 'Hello World!', 6, 5 ] }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ " "\"\", \"\", " "\"\" ] }, : { : [ " "\"\", \"\", \"\" ] }, : { : [ \"\", \"\", " "\"\" ] } } }"); } TEST(CstExpressionTest, ParsesStringLengthExpressions) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "p: { $strLenBytes: 'cafeteria' }, " "q: { $strLenCP: 'Hello World!' }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : " "\"\" }, : { : " "\"\" } } }"); } TEST(CstExpressionTest, ParsesSplit) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "o: { $split: [ {$toUpper: 'abc'}, '-' ] }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : [ { " ": \"\" }, " "\"\" ] } } }"); } TEST(CstExpressionTest, ParsesStrCaseCmp) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "r: { $strcasecmp: [ '$quarter', '13q4' ] }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\", \"\" ] } } }"); } TEST(CstExpressionTest, ParsesConcat) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $concat: [ 'item', ' - ', '$description' ]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : [ " "\"\", \"\", \"\" ] } } }"); } TEST(CstExpressionTest, ParsesArrayElemAt) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $arrayElemAt: [ [1, 2, 3, 4] , 1 ] }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ [ \"\", " "\"\", \"\", \"\" ], \"\" ] } } }"); } TEST(CstExpressionTest, ParsesArrayToObject) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $arrayToObject: [ [1, 2], [3, 4] ] }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ [ \"\", " "\"\" ], [ \"\", \"\" ] ] } } }"); } TEST(CstExpressionTest, ParsesConcatArrays) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $concatArrays: [ [1, 2], [3, 4] ] }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ [ \"\", " "\"\" ], [ \"\", \"\" ] ] } } }"); } TEST(CstExpressionTest, ParsesFilter) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $filter: {input: [0, 2], as: \"var\", cond: { $gt: [ \"$$var\", 1 ] }}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "{ : [ \"\", \"\" ] }, " ": [ \"\", " "\"\" ] } } } }"); } TEST(CstExpressionTest, ParsesFirst) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $first: [0, 2]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\", \"\" ] } } }"); } TEST(CstExpressionTest, ParsesIn) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $in: [1, [0, 2]]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ " "\"\", [ \"\", \"\" " "] ] } } }"); } TEST(CstExpressionTest, ParsesIndexOfArray) { // Parses with Start and End Arguments { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $indexOfArray: [ [0, 2], \"$searchExpression\", 0, 1 ]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ [ \"\", \"\" ], \"\", \"\", \"\" ] } } }"); } // Parses with just Start argument { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $indexOfArray: [ [0, 2], \"$searchExpression\", 0 ]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ [ \"\", \"\" ], \"\", \"\" ] } } }"); } // Parses without Start and End arguments { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $indexOfArray: [ [0, 2], \"$searchExpression\" ]}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ [ \"\", \"\" ], \"\" ] } } }"); } } TEST(CstExpressionTest, ParsesIsArray) { // Parses with argument wrapped in array { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $isArray: [ [0, 2] ] }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ [ " "\"\", \"\" ] ] } } }"); } // Parses without argument wrapped in array { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $isArray: \"$maybeAnArrayLivesHere\" }}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : " "\"\" } } }"); } } TEST(CstExpressionTest, FailsToParseTripleDollar) { CNode output; auto input = BSON("pipeline" << BSON_ARRAY(BSON("$project" << BSON("a" << "$$$triple")))); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } TEST(CstExpressionTest, FailsToParseLoneDollar) { CNode output; auto input = BSON("pipeline" << BSON_ARRAY(BSON("$project" << BSON("a" << "$")))); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } TEST(CstExpressionTest, FailsToParseInvalidVarName) { CNode output; auto input = BSON("pipeline" << BSON_ARRAY(BSON("$project" << BSON("a" << "$$invalid")))); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } TEST(CstExpressionTest, ParsesDateToParts) { CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $dateToParts: {date: '$date', 'timezone': 'America/New_York', 'iso8601': true}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "\"\", " ": \"\" } } } }"); } TEST(CstExpressionTest, ParsesDateFromParts) { { // Non-iso formatted version: CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $dateFromParts: {year: '$year', month: '$month', day: '$day'," "hour: '$hour', minute: '$minute', second: '$second', millisecond:" " '$millisecs', timezone: 'America/New_York'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "\"\", " ": \"\", : " "\"\", : " "\"\", : \"\", : " "\"\", : \"\" } } } " "}"); } { // Iso formatted version: CNode output; auto input = fromjson( "{pipeline: [{$project: { " "a: { $dateFromParts: {isoWeekYear: '$year', isoWeek: '$isowk', isoDayOfWeek: '$day'," "hour: '$hour', minute: '$minute', second: '$second', millisecond:" " '$millisecs', timezone: 'America/New_York'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : \"\", " ": \"\", : \"\", " ": " "\"\", : \"\", : " "\"\", : \"\" } } } " "}"); } } TEST(CstExpressionTest, ParsesDayOfDateExpressionsWithDocumentArgument) { { // $dayOfMonth CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfMonth: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { : \"\", " ": \"\" } } } }"); } { // $dayOfWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfWeek: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { : \"\", " ": \"\" } } } }"); } { // $dayOfYear CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfYear: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { : \"\", " ": \"\" } } } }"); } } TEST(CstExpressionTest, FailsToParseDayOfDateExpressionsWithInvalidDocumentArgument) { { // $dayOfMonth CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfMonth: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } { // $dayOfWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfWeek: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } { // $dayOfYear CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfYear: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesDayOfDateExpressionsWithExpressionArgument) { { // $dayOfMonth CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfMonth: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } { // $dayOfWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfWeek: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } { // $dayOfYear CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfYear: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } } TEST(CstExpressionTest, ParsesDayOfMonthWithArrayArgument) { // $dayOfMonth { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfMonth: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfMonth: ['$date', '$timeZone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfMonth: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesDayOfWeekWithArrayArgument) { { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfWeek: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfWeek: ['$date', '$timeZone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfWeek: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesDayOfYearWithArrayArgument) { { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfYear: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfYear: ['$date', '$timeZone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $dayOfYear: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesIsoDateExpressionsWithDocumentArgument) { { // $isoDayOfWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoDayOfWeek: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { : \"\", " ": \"\" } } } }"); } { // $isoWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeek: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { : \"\", " ": \"\" } } } }"); } { // $isoWeekYear CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeekYear: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { : \"\", " ": \"\" } } } }"); } } TEST(CstExpressionTest, FailsToParseIsoDateExpressionsWithInvalidDocumentArgument) { { // $isoDayOfWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoDayOfWeek: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } { // $isoWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeek: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } { // $isoWeekYear CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeekYear: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesIsoDateExpressionsWithExpressionArgument) { { // $isoDayOfWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoDayOfWeek: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } { // $isoWeek CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeek: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } { // $isoWeekYear CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeekYear: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } } TEST(CstExpressionTest, ParsesIsoDayOfWeekWithArrayArgument) { { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoDayOfWeek: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoDayOfWeek: ['$date', '$timeZone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoDayOfWeek: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesIsoWeekWithArrayArgument) { { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeek: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeek: ['$date', '$timeZone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeek: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesIsoWeekYearWithArrayArgument) { { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeekYear: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeekYear: ['$date', '$timeZone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $isoWeekYear: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesHourExpressionWithDocumentArgument) { { // $hour CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $hour: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "\"\" } } } }"); } { // Ensure fails to parse with invalid argument-document. CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $hour: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesMillisecondExpressionWithDocumentArgument) { { // $millisecond CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $millisecond: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : { : \"\", " ": \"\" } } } }"); } { // Ensure fails to parse with invalid argument-document. CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $millisecond: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesMinuteExpressionWithDocumentArgument) { { // $minute CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $minute: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", " ": \"\" } } } }"); } { // Ensure fails to parse with invalid argument-document. CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $minute: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesMonthExpressionWithDocumentArgument) { { // $month CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $month: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", " ": \"\" } } } }"); } { // Ensure fails to parse with invalid argument-document. CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $month: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesSecondExpressionWithDocumentArgument) { { // $second CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $second: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", " ": \"\" } } } }"); } { // Ensure fails to parse with invalid argument-document. CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $second: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesWeekExpressionWithDocumentArgument) { { // $week CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $week: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "\"\" } } } }"); } { // Ensure fails to parse with invalid argument-document. CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $week: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesYearExpressionWithDocumentArgument) { { // $year CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $year: {date: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ( stages[0].toBson().toString(), "{ : { : { : { " ": \"\", : " "\"\" } } } }"); } { // Ensure fails to parse with invalid argument-document. CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $year: {notDate: '$date'}}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesHourExpressionWithExpressionArgument) { // Test with argument as-is. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $hour: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : " "\"\" } } }"); } // Test with argument wrapped in array. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $hour: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : " "[ \"\" ] } } }"); } // Ensure fails to parse with a non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $hour: ['$date', '$timezone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $hour: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesMillisecondExpressionWithExpressionArgument) { // Test with argument as-is. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $millisecond: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } // Test with argument wrapped in array. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $millisecond: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with a non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $millisecond: ['$date', '$timezone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $millisecond: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesMinuteExpressionWithExpressionArgument) { // Test with argument as-is. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $minute: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } // Test with argument wrapped in array. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $minute: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with a non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $minute: ['$date', '$timezone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $minute: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesMonthExpressionWithExpressionArgument) { // Test with argument as-is. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $month: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } // Test with argument wrapped in array. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $month: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with a non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $month: ['$date', '$timezone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $month: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesSecondExpressionWithExpressionArgument) { // Test with argument as-is. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $second: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : \"\" } } }"); } // Test with argument wrapped in array. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $second: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : [ \"\" ] } } }"); } // Ensure fails to parse with a non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $second: ['$date', '$timezone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $second: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesWeekExpressionWithExpressionArgument) { // Test with argument as-is. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $week: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : " "\"\" } } }"); } // Test with argument wrapped in array. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $week: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : " "[ \"\" ] } } }"); } // Ensure fails to parse with a non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $week: ['$date', '$timezone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $week: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } TEST(CstExpressionTest, ParsesYearExpressionWithExpressionArgument) { // Test with argument as-is. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $year: '$date'}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : " "\"\" } } }"); } // Test with argument wrapped in array. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $year: ['$date']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_EQ(0, parseTree.parse()); auto stages = stdx::get(output.payload); ASSERT_EQ(1, stages.size()); ASSERT(KeyFieldname::projectInclusion == stages[0].firstKeyFieldname()); ASSERT_EQ(stages[0].toBson().toString(), "{ : { : { : " "[ \"\" ] } } }"); } // Ensure fails to parse with a non-singleton array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $year: ['$date', '$timezone']}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } // Ensure fails to parse with an empty array argument. { CNode output; auto input = fromjson("{pipeline: [{$project: { a: { $year: []}}}]}"); BSONLexer lexer(input["pipeline"].embeddedObject(), ParserGen::token::START_PIPELINE); auto parseTree = ParserGen(lexer, &output); ASSERT_THROWS_CODE(parseTree.parse(), AssertionException, ErrorCodes::FailedToParse); } } } // namespace } // namespace mongo