Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
new.py
00001 """ 00002 Copyright (c) 2018, Arm Limited 00003 SPDX-License-Identifier: Apache-2.0 00004 00005 Licensed under the Apache License, Version 2.0 (the "License"); 00006 you may not use this file except in compliance with the License. 00007 You may obtain a copy of the License at 00008 00009 http://www.apache.org/licenses/LICENSE-2.0 00010 00011 Unless required by applicable law or agreed to in writing, software 00012 distributed under the License is distributed on an "AS IS" BASIS, 00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 See the License for the specific language governing permissions and 00015 limitations under the License. 00016 00017 00018 UNIT TEST GENERATE NEW UNIT TESTS 00019 """ 00020 00021 from __future__ import print_function 00022 import os 00023 import re 00024 import logging 00025 00026 class UnitTestGeneratorTool (object): 00027 """ 00028 Generator tool to create new unit tests from template 00029 """ 00030 00031 def _replace_content(self, template_content, dirname, classname, extension): 00032 if extension == "h": 00033 content = re.sub(r"cppfile", 00034 "", 00035 template_content) 00036 elif extension == "cpp": 00037 content = re.sub(r"cppfile", 00038 "../dirname/template.cpp", 00039 template_content) 00040 00041 content = re.sub(r"headerfile", "../dirname/template.h", content) 00042 content = re.sub(r"dirname", dirname, content) 00043 content = re.sub(r"template", classname, content) 00044 00045 return content 00046 00047 def create (self, mbed_os_root=None, filepath=None): 00048 """ 00049 Generate a unit test for a file to be tested from templates 00050 00051 Keyword arguments: 00052 mbed_os_root - Mbed OS root directory 00053 filepath - Path to the file to be tested 00054 """ 00055 00056 if filepath is None: 00057 logging.error("No filepath given. Cannot create a new unit test.") 00058 return 00059 00060 if mbed_os_root is None: 00061 mbed_os_root = \ 00062 os.path.normpath(os.path.join(os.path.dirname(__file__), 00063 "../..")) 00064 if os.path.isabs(filepath): 00065 filepath = os.path.join( 00066 os.path.relpath(os.path.dirname(filepath), mbed_os_root), 00067 os.path.basename(filepath)) 00068 00069 if not re.match(r"((\w+\/)+)(\w+)\.(c|cpp|h|hpp)$", filepath): 00070 logging.error( 00071 "No proper path to source file given.") 00072 return 00073 00074 if not os.path.isfile(os.path.join(mbed_os_root, filepath)): 00075 logging.warning("Cannot find source file: %s from Mbed OS.", 00076 filepath) 00077 00078 dir_name = re.sub(r"(\\|\/)(\w+)\.(cpp|h)$", "", filepath) 00079 00080 match = re.search(r"(\\|\/)(\w+)\.(cpp|h)$", filepath) 00081 00082 if match: 00083 class_name = match.group(2) 00084 file_extension = match.group(3) 00085 00086 # Create directory tree if not exist. 00087 test_dir = os.path.join(mbed_os_root, "UNITTESTS", dir_name, class_name) 00088 00089 if not os.path.exists(test_dir): 00090 os.makedirs(test_dir) 00091 else: 00092 logging.error("Unit tests exist already for this file.") 00093 return 00094 00095 try: 00096 suite_name = ("%s/%s" % (dir_name, class_name)).replace("/", "_") 00097 00098 # Test source file template 00099 template_source_file = \ 00100 os.path.join(mbed_os_root, 00101 "UNITTESTS/template/test_template.cpp") 00102 00103 with open(template_source_file, "r") as input_file: 00104 template_content = input_file.read() 00105 00106 output_source_file = os.path.join(test_dir, 00107 "test_%s.cpp" % class_name) 00108 00109 with open(output_source_file, "w") as output_file: 00110 content = self._replace_content (template_content, 00111 dir_name, 00112 class_name, 00113 file_extension) 00114 00115 output_file.writelines(content) 00116 00117 # Definition file template 00118 template_source_file = \ 00119 os.path.join(mbed_os_root, 00120 "UNITTESTS/template/unittest.cmake.template") 00121 00122 with open(template_source_file, "r") as input_file: 00123 template_content = input_file.read() 00124 00125 output_source_file = os.path.join(test_dir, "unittest.cmake") 00126 00127 with open(output_source_file, "w") as output_file: 00128 content = self._replace_content (template_content, 00129 dir_name, 00130 class_name, 00131 file_extension) 00132 00133 output_file.writelines(content) 00134 00135 logging.info(""" 00136 Unit test %s created. 00137 00138 Generated files into %s directory. 00139 """, suite_name, test_dir) 00140 00141 except IOError: 00142 logging.error("Cannot create files for a unit test.")
Generated on Tue Aug 9 2022 00:37:16 by
1.7.2