1## Prerequisites 2- All paths below are relative paths regarding root of arkcompiler_runtime_core repository (<https://gitee.com/openharmony/arkcompiler_runtime_core.git>) 3- The following libraries (Jinja2==3.1.2 MarkupSafe==2.1.1 PyYAML==6.0) are required for test code generator 4- These libraries can be installed by running scripts/install-deps-ubuntu. This script creates the python virtual environment (.venv-panda in the user's home directory) and installs required libraries there. 5 - Go to root of arkcompiler runtime core repository 6 - Run command `sudo ./scripts/install-deps-ubuntu -i=dev -i=test` 7 8## Quick run 9- Go to the folder `$panda_source/tests/tests-u-runner` 10- Run tests `./runner.sh --ets-func-tests --build-dir $panda_build <other options>`. For other options see documentation for runner at `$panda_source/tests/tests-u-runner/readme.md` 11 12## Only generate tests without running 13- Go to the folder `$panda_source/tests/tests-u-runner` 14- Run generator: `./generate.sh -t $panda_source/plugins/ets/tests/stdlib-templates/ -o /tmp/gen`. You can setup any other path for output of the generator if you wish. 15 16## Template structure 17For generating tests against ArkTS stdlib the Python library Jinja2 is used. For detail information about Jinja2 template, see :<https://jinja.palletsprojects.com/en/3.1.x/> 18The common structure of a template is: 19 20- The template itself. It contains a mix of Jinja2 statements and ArkTS code. The content will be rendered into clear ArkTS source code. 21- The configuration file in the yaml format is a set of objects which will be used for rendering Jinja2 template. 22 23There are two levels of templates: 24 25 - common level with code that can be reused in different cases 26 - specific level that usually is constructed from common level templates via **include** command of Jinja2. This level can contain additional code for the task. 27 28 29The specific level template contains the loop over all objects in the configuration file. Then the generator takes an object from the configuration file and replaces corresponded variables in the template. Separated test object will be generated for each object. So number of tests created by generator will be equal number of objects in the configuration file. 30 31 32For example: 33 34#### Specific level template 35``` 36{% for item in std_math %} 37 38/*--- 39desc: {function: {{.item.method_name}}} 40---*/ 41 42{% include 'utils/test_verifier_lib.j2' with context %} 43{% include 'utils/test_function.j2' with context %} 44 45{% endfor %} 46``` 47Where test_`function.j2` is common level template (see example below) and `test_verifier_lib.j2` - the file that contains function for verification test result. 48#### Common level template (`test_function.j2`) 49``` 50{%- for number, param in item.param_list.items() %} 51const TEST_DATA_{{.number|upper}} : {{.item.param_init_data_type}} = {{.item.expected_data}}{{.param}}; 52{%- endfor %} 53const TEST_DATA_EXPECTED : {{.item.expected_data_type}} = {{.item.expected_test_data}}; 54 55function main(): int { 56 let max_test : int = TEST_DATA_EXPECTED.length; 57 let passed_counter : int = 0; 58 59 for (let i = 0; i < max_test; i++) { 60 {%- for number, param in item.param_list.items() %} 61 let {{.number}} = (TEST_DATA_{{.number|upper}}[i]); 62 {%- endfor %} 63 let expected = TEST_DATA_EXPECTED[i] 64 let actual : {{.item.method_return_type}} = {{.item.method_name}}({{.item.param_list.keys()|list|join(', ')}}); 65 {%- for number, param in item.param_list.items() %} 66 console.print("PARAM:" + {{.number}} + ";"); 67 {%- endfor %} 68 console.print("ACTUAL:" + actual + ";"); 69 console.print("EXPECTED:" + expected + ";"); 70 if ({{.item.verify_test_result_function}}(actual, expected)) { 71 console.print("PASSED"); 72 passed_counter++; 73 } else { 74 console.print("FAILED") 75 } 76 console.println(); 77 } 78 79 assertEQ(passed_counter, max_test) 80 return 0; 81} 82 83``` 84#### Fragment of YAML configuration file 85This example contains two YAML objects fragments, each of them will be used for creating a separated test 86``` 87- { 88 method_name: abs, 89 param_list: {"param1":"[ 0.0, PI, -PI, 123.0, -123.0,]"}, 90 param_types: {"param1":double}, 91 method_return_type: double, 92 param_init_data_types: {"param1":"double[]"}, 93 expected_data_type: "double[]", 94 expected_test_data: "[ 0.0, PI, PI, 123.0, 123.0,]", 95 verify_test_result_function: compare_float_point_value 96 } 97- { 98 method_name: scalbn, 99 param_list: {"param1": "[e, e, e, e, e, -e, -e, -e, -e, -e ]", "param2" :"[0, 1, 2, -1, -2, 0, 1, 2, -1, -2 ]"}, 100 method_return_type: double, 101 param_types: {"param1":double, "param2":int}, 102 param_init_data_types: {"param1":"double[]", "param2":"int[]"}, 103 expected_data_type: "double[]", 104 expected_test_data: "[ e, 5.43656366, 10.87312731, 1.35914091, 0.67957046, -e, -5.43656366, -10.87312731, -1.35914091, -0.67957046]", 105 verify_test_result_function: compare_float_point_value 106 } 107 108``` 109### Main keywords that are used in the configuration YAML file 110- object_type - type of object that contains the tested method 111- init_object_type - type of data that will be used for object initialization 112- init_object_data_type - type of container that contains test data, usually this is an array 113- init_object_data - dictionary with data that used for object initialization 114- method_name - define name of the tested method 115- method_return_type - type that returned by the tested method 116- param_list - dictionary that contains test data for each parameter 117- param_types - dictionary that contains type for each parameter 118- param_init_data_types - dictionary that contains data type that used for parameters initialization 119- expected_data_type - type of a container with expected data 120- expected_test_data - actual data that should be used for tests verification 121- verify_test_result_function - name of a function that used for test result verification. Usually it contains two parameters, actual data and expected data. Type of these parameters are described in expected_data_type keyword 122