From ee12cd4b4df78490bb5d2860608bd2b618fcd92b Mon Sep 17 00:00:00 2001 From: ansestepanov Date: Tue, 19 Aug 2025 14:08:42 +0300 Subject: [PATCH] filter function tests commit_hash:1549bd01f40360f820353cecb97414026573b7bb --- .mapping.json | 1 + .../filter_function_array.json | 301 ++++++++++++++++++ .../methods_signatures_array.json | 17 + 3 files changed, 319 insertions(+) create mode 100644 test_data/expression_test_data/filter_function_array.json diff --git a/.mapping.json b/.mapping.json index fc6e576e3..f61a24cc0 100644 --- a/.mapping.json +++ b/.mapping.json @@ -20390,6 +20390,7 @@ "site/server/src/runTs.js":"divkit/public/site/server/src/runTs.js", "site/server/src/runTsWorker.js":"divkit/public/site/server/src/runTsWorker.js", "test_data/expression_test_data/custom_functions.json":"divkit/public/test_data/expression_test_data/custom_functions.json", + "test_data/expression_test_data/filter_function_array.json":"divkit/public/test_data/expression_test_data/filter_function_array.json", "test_data/expression_test_data/function_signatures_array.json":"divkit/public/test_data/expression_test_data/function_signatures_array.json", "test_data/expression_test_data/function_signatures_color.json":"divkit/public/test_data/expression_test_data/function_signatures_color.json", "test_data/expression_test_data/function_signatures_datetime.json":"divkit/public/test_data/expression_test_data/function_signatures_datetime.json", diff --git a/test_data/expression_test_data/filter_function_array.json b/test_data/expression_test_data/filter_function_array.json new file mode 100644 index 000000000..6b9a63700 --- /dev/null +++ b/test_data/expression_test_data/filter_function_array.json @@ -0,0 +1,301 @@ +{ + "cases": [ + { + "expression": "@{source.filter(equalsOneFromDict)}", + "expected": { + "type": "array", + "value": [ + { + "value": "one" + } + ] + }, + "variables": [ + { + "name": "source", + "type": "array", + "value": [ + { + "value": "one" + }, + { + "value": "two" + }, + { + "value": "three" + } + ] + } + ], + "functions": [ + { + "name": "equalsOneFromDict", + "body": "@{it.getString('value') == 'one'}", + "return_type": "boolean", + "arguments": [ + { + "name": "it", + "type": "dict" + } + ] + } + ], + "platforms": [] + }, + { + "expression": "@{source.filter(equalsOne)}", + "expected": { + "type": "array", + "value": [ + "one" + ] + }, + "variables": [ + { + "name": "source", + "type": "array", + "value": [ + "one", + "two", + "three" + ] + } + ], + "functions": [ + { + "name": "equalsOne", + "body": "@{str == 'one'}", + "return_type": "boolean", + "arguments": [ + { + "name": "str", + "type": "string" + } + ] + } + ], + "platforms": [] + }, + { + "expression": "@{source.filter(lessThanThree)}", + "expected": { + "type": "array", + "value": [ + 1, + 2 + ] + }, + "variables": [ + { + "name": "source", + "type": "array", + "value": [ + 1, + 2, + 3 + ] + } + ], + "functions": [ + { + "name": "lessThanThree", + "body": "@{num < 3}", + "return_type": "boolean", + "arguments": [ + { + "name": "num", + "type": "integer" + } + ] + } + ], + "platforms": [] + }, + { + "expression": "@{source.filter(alwaysFalse)}", + "expected": { + "type": "array", + "value": [] + }, + "variables": [ + { + "name": "source", + "type": "array", + "value": [ + "one", + "two", + "three" + ] + } + ], + "functions": [ + { + "name": "alwaysFalse", + "body": "@{false}", + "return_type": "boolean", + "arguments": [ + { + "name": "item", + "type": "string" + } + ] + } + ], + "platforms": [] + }, + { + "expression": "@{boolArray.filter(isTrue)}", + "expected": { + "type": "array", + "value": [ + true, + true + ] + }, + "variables": [ + { + "name": "boolArray", + "type": "array", + "value": [ + true, + false, + true, + false + ] + } + ], + "functions": [ + { + "name": "isTrue", + "body": "@{value == true}", + "return_type": "boolean", + "arguments": [ + { + "name": "value", + "type": "boolean" + } + ] + } + ], + "platforms": [] + }, + { + "expression": "@{source.filter(nonExistentFunction)}", + "expected": { + "type": "error", + "value": "Failed to evaluate [filter(nonExistentFunction)]. Unknown function name: nonExistentFunction." + }, + "variables": [ + { + "name": "source", + "type": "array", + "value": [ + "one", + "two" + ] + } + ], + "functions": [], + "platforms": [] + }, + { + "expression": "@{source.filter(wrongReturnType)}", + "expected": { + "type": "error", + "value": "Failed to evaluate [filter(wrongReturnType)]. Function must return boolean value." + }, + "variables": [ + { + "name": "source", + "type": "array", + "value": [ + "one", + "two" + ] + } + ], + "functions": [ + { + "name": "wrongReturnType", + "body": "@{'not_boolean'}", + "return_type": "string", + "arguments": [ + { + "name": "item", + "type": "string" + } + ] + } + ], + "platforms": [] + }, + { + "expression": "@{source.filter(containsQuery)}", + "expected": { + "type": "error", + "value": "Failed to evaluate [containsQuery]. Exactly 2 argument(s) expected." + }, + "variables": [ + { + "name": "source", + "type": "array", + "value": [ + "one", + "two", + "three" + ] + } + ], + "functions": [ + { + "name": "containsQuery", + "body": "@{contains(it, query)}", + "return_type": "boolean", + "arguments": [ + { + "name": "it", + "type": "string" + }, + { + "name": "query", + "type": "string" + } + ] + } + ], + "platforms": [] + }, + { + "expression": "@{heterogeneousArray.filter(isStringOne)}", + "expected": { + "type": "error", + "value": "Failed to evaluate [filter(isStringOne)]. Incorrect value type: expected String, got Integer." + }, + "variables": [ + { + "name": "heterogeneousArray", + "type": "array", + "value": [ + "one", + 42, + true + ] + } + ], + "functions": [ + { + "name": "isStringOne", + "body": "@{str == 'one'}", + "return_type": "boolean", + "arguments": [ + { + "name": "str", + "type": "string" + } + ] + } + ], + "platforms": [] + } + ] +} diff --git a/test_data/expression_test_data/methods_signatures_array.json b/test_data/expression_test_data/methods_signatures_array.json index e9da4e944..60efdbc0c 100644 --- a/test_data/expression_test_data/methods_signatures_array.json +++ b/test_data/expression_test_data/methods_signatures_array.json @@ -193,6 +193,23 @@ "web", "flutter" ] + }, + { + "function_name": "filter", + "is_method": true, + "doc": "Returns filtered array by calling boolean function to each element of array.", + "arguments": [ + { + "type": "array", + "doc": "Array." + }, + { + "type": "function", + "doc": "Accepts each element of array, returns bool." + } + ], + "result_type": "array", + "platforms": [] } ] }