Skip to content
0.2.0-dev Download ZIP
104 lines ·5.0 KB Raw
1#!/usr/bin/env python3
2
3import argparse
4import os
5import re
6import sys
7
8
9def main():
10 # Change to the directory where the script is located,
11 # so that the script can be run from any location.
12 os.chdir(os.path.dirname(os.path.realpath(__file__)))
13
14 parser = argparse.ArgumentParser(description="Creates a new unit test file.")
15 parser.add_argument(
16 "name",
17 type=str,
18 help="Specifies the class or component name to be tested, in PascalCase (e.g., MeshInstance3D). The name will be prefixed with 'test_' for the header file and 'Test' for the namespace.",
19 )
20 parser.add_argument(
21 "path",
22 type=str,
23 nargs="?",
24 help="The path to the unit test file relative to the tests folder (e.g. core). This should correspond to the relative path of the class or component being tested. (default: .)",
25 default=".",
26 )
27 args = parser.parse_args()
28
29 snake_case_regex = re.compile(r"(?<!^)(?=[A-Z, 0-9])")
30 # Replace 2D, 3D, and 4D with 2d, 3d, and 4d, respectively. This avoids undesired splits like node_3_d.
31 prefiltered_name = re.sub(r"([234])D", lambda match: match.group(1).lower() + "d", args.name)
32 name_snake_case = snake_case_regex.sub("_", prefiltered_name).lower()
33 file_path = os.path.normpath(os.path.join(args.path, f"test_{name_snake_case}.cpp"))
34
35 # Ensure the directory exists.
36 os.makedirs(os.path.dirname(file_path), exist_ok=True)
37
38 print(file_path)
39 if os.path.isfile(file_path):
40 print(f'ERROR: The file "{file_path}" already exists.')
41 sys.exit(1)
42 with open(file_path, "w", encoding="utf-8", newline="\n") as file:
43 file.write(
44 """/**************************************************************************/
45/* test_{name_snake_case}.cpp {padding} */
46/**************************************************************************/
47/* This file is part of: */
48/* GODOT ENGINE */
49/* https://godotengine.org */
50/**************************************************************************/
51/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
52/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
53/* */
54/* Permission is hereby granted, free of charge, to any person obtaining */
55/* a copy of this software and associated documentation files (the */
56/* "Software"), to deal in the Software without restriction, including */
57/* without limitation the rights to use, copy, modify, merge, publish, */
58/* distribute, sublicense, and/or sell copies of the Software, and to */
59/* permit persons to whom the Software is furnished to do so, subject to */
60/* the following conditions: */
61/* */
62/* The above copyright notice and this permission notice shall be */
63/* included in all copies or substantial portions of the Software. */
64/* */
65/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
66/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
67/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
68/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
69/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
70/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
71/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
72/**************************************************************************/
73
74#include "tests/test_macros.h"
75
76TEST_FORCE_LINK(test_{name_snake_case})
77
78namespace Test{name_pascal_case} {{
79
80TEST_CASE("[{name_pascal_case}] Example test case") {{
81 // TODO: Remove this comment and write your test code here.
82}}
83
84}} // namespace Test{name_pascal_case}
85""".format(
86 name_snake_case=name_snake_case,
87 # Capitalize the first letter but keep capitalization for the rest of the string.
88 # This is done in case the user passes a camelCase string instead of PascalCase.
89 name_pascal_case=args.name[0].upper() + args.name[1:],
90 # The padding length depends on the test name length.
91 padding=" " * (59 - len(name_snake_case)),
92 )
93 )
94
95 # Print an absolute path so it can be Ctrl + clicked in some IDEs and terminal emulators.
96 print("Test file created:")
97 print(os.path.abspath(file_path))
98 print(
99 "Don't forget to add special tags to the test case if needed, see https://docs.godotengine.org/en/latest/contributing/development/core_and_modules/unit_testing.html#special-tags-in-test-case-names for more information."
100 )
101
102
103if __name__ == "__main__":
104 main()